0% found this document useful (0 votes)
23 views

Report Lab 4

The document describes two reports on building Nios II systems with timers and writing C programs to control LEDs using alarms. The first report details a program to turn on each LED from 1 to 10 for 500ms sequentially in a loop. The second report details a program to blink different LEDs at varying frequencies when a switch is engaged, with all LEDs off when not. Pseudocode and data flow diagrams are provided to explain the program logic.

Uploaded by

Minh Nguyễn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Report Lab 4

The document describes two reports on building Nios II systems with timers and writing C programs to control LEDs using alarms. The first report details a program to turn on each LED from 1 to 10 for 500ms sequentially in a loop. The second report details a program to blink different LEDs at varying frequencies when a switch is engaged, with all LEDs off when not. Pseudocode and data flow diagrams are provided to explain the program logic.

Uploaded by

Minh Nguyễn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

REPORT LAB 4 :

I. Build a Nios II system with timer. Write a C program to do this task by using
alarm .Each led from LED1 to 10 turns on in 500ms and then turn off.

1.1 Build Nios II system :

2 Quartus code
library ieee;
use ieee.std_logic_1164.all;

entity Lab4 is
port
(
------------ CLOCK ------------
CLOCK2_50 :in std_logic;
CLOCK3_50 :in std_logic;
CLOCK4_50 :in std_logic;
CLOCK_50 :in std_logic;
------------ KEY ------------
KEY :in std_logic_vector(3 downto 0);
------------ SW ------------
SW :in std_logic_vector(9 downto 0);
------------ LED ------------
LEDR :out std_logic_vector(9 downto 0);
------------ Seg7 ------------
HEX0 :out std_logic_vector(6 downto 0);
HEX1 :out std_logic_vector(6 downto 0);
HEX2 :out std_logic_vector(6 downto 0);
HEX3 :out std_logic_vector(6 downto 0);
HEX4 :out std_logic_vector(6 downto 0);
HEX5 :out std_logic_vector(6 downto 0);

------------ SDRAM ------------


DRAM_ADDR :out std_logic_vector(12 downto 0);
DRAM_BA :out std_logic_vector(1 downto 0);
DRAM_CAS_N :out std_logic;
DRAM_CKE :out std_logic;
DRAM_CLK :out std_logic;
DRAM_CS_N :out std_logic;
DRAM_DQ :inout std_logic_vector(15 downto 0);
DRAM_LDQM :out std_logic;
DRAM_RAS_N :out std_logic;
DRAM_UDQM :out std_logic;
DRAM_WE_N :out std_logic
);

end entity;
---------------------------------------------------------
-- Structural coding
---------------------------------------------------------
architecture rtl of Lab4 is

component qsys is
port (
clk_clk : in std_logic := 'X'; -- clk
ledr_export : out std_logic_vector(9 downto 0); --
export
reset_reset : in std_logic := 'X' -- reset
);
end component qsys;
begin
stage:qsys port map ( clock_50 , ledr , '0' ) ;
end rtl;

1.3 / C program to do task

Idea for code :


- Call my_alarm_callback fucntion each 500ms , when call function , sub
function will shift one-bit on the left in LED_BASE address , to turn on next
LED and turn off the current LED . Until the led 10 is turn on , the data in
LED_BASE set to 1 to turn on LED-1 again
Data flow :

Code in elipse :
#include <stdio.h>
#include <alt_types.h>
#include <sys/alt_alarm.h>
#include <unistd.h>
#include <system.h>
#define led (*(volatile int*)LEDR_BASE)
int a = 0 ;

alt_u32 my_alarm_callback(void* context){


if ( a < 10){
led = (1<<a) ; // turn on led at a position
a += 1 ; // shift to next led

}
else {
led = 0 ; // when a = 10 ( led 10 on ) , reset to led 0
a=0;
}
// This function is called once per second
return alt_ticks_per_second()/2; // return 0.5s for call
}

int main()
{
static alt_alarm alarm;
if (alt_alarm_start(&alarm, alt_ticks_per_second()/2,my_alarm_callback,
NULL) < 0)
{
printf("No system clock available\n");
}

return 0;
}

II. Build a Nios II system with timers. Write a C program to do these tasks:
- When reset, all LEDs off.
- When switch 1 is set, LED1 blink with frequency 0.25Hz, LED3 blink with
frequency
0.5Hz, LED5 blink with frequency 1Hz, LED7 blink with frequency 2Hz,
LED9 blink
with frequency 4Hz.
- When switch 1 is not set, all LEDs off.

2.1 Build Nios II system :


2.2 Quartus code

library ieee;
use ieee.std_logic_1164.all;

entity lab4_2 is
port
(

------------ CLOCK ------------


CLOCK2_50 :in std_logic;
CLOCK3_50 :in std_logic;
CLOCK4_50 :in std_logic;
CLOCK_50 :in std_logic;

------------ KEY ------------


KEY :in std_logic_vector(3 downto 0);

------------ SW ------------
SW :in std_logic_vector(9 downto 0);

------------ LED ------------


LEDR :out std_logic_vector(9 downto 0);

------------ Seg7 ------------


HEX0 :out std_logic_vector(6 downto 0);
HEX1 :out std_logic_vector(6 downto 0);
HEX2 :out std_logic_vector(6 downto 0);
HEX3 :out std_logic_vector(6 downto 0);
HEX4 :out std_logic_vector(6 downto 0);
HEX5 :out std_logic_vector(6 downto 0);
------------ SDRAM ------------
DRAM_ADDR :out std_logic_vector(12 downto 0);
DRAM_BA :out std_logic_vector(1 downto 0);
DRAM_CAS_N :out std_logic;
DRAM_CKE :out std_logic;
DRAM_CLK :out std_logic;
DRAM_CS_N :out std_logic;
DRAM_DQ :inout std_logic_vector(15 downto 0);
DRAM_LDQM :out std_logic;
DRAM_RAS_N :out std_logic;
DRAM_UDQM :out std_logic;
DRAM_WE_N :out std_logic
);

end entity;

---------------------------------------------------------
-- Structural coding
---------------------------------------------------------

architecture rtl of lab4_2 is


component qsys is
port (
clk_clk : in std_logic := 'X'; -- clk
ledr_export : out std_logic_vector(9 downto 0);
-- export
reset_reset : in std_logic := 'X'; --
reset
sw_wire_export : in std_logic_vector(9 downto 0) :=
(others => 'X') -- export
);
end component qsys;

begin
stage0: qsys PORT MAP ( clock_50 , ledr , not(key(0)x) , sw ) ;
end rtl;

2.3 C Program to do task

Idea of code :
- LED1 blink with frequency 0.25Hz => Turn on 2s , off 2s ( state 1)
- LED3 blink with frequency 0.5Hz => Turn on 1s , off 1s ( state 2)
- LED5 blink with frequency 1Hz => Turn on 0.5s , off 0.5s ( state 3)
- LED7 blink with frequency 2Hz => Turn on 0.25s , off 0.25s ( state 4)
- LED9 blink with frequency 4Hz => Turn on 0.125s , off 0.125s ( state 5)

- Base on time to turn on/off each led , we set time to call my_alarm_callback
function is 0.125s. The idea is count the clock for each state of led , when call
the function , the value count ( call 'a' ) count down one , and set to call
my_alarm_callback function a times need to turn on LED . For example ,
LED1 need to turn on 2s => a set to 16 , so the function calls 16 times , the led
turns on 0.25s*8 = 2s . After the count goes down zero , it reset count depend
on what state it is and count again .
- Set if swicth turn off , it reset to state 1 and set count to 16 ( 2 seconds )

Data flow :

Data description for data flow

state 1 :
- if led on , LED = 1<<0
- countset = 16 , newset = 8
state 2 :
- if led on , LED = 1<<2
- countset = 8 , newset = 4
state 3 :
- if led on , LED = 1<<4
- countset = 4 , newset = 2
state 4:
- if led on , LED = 1<<6
- countset = 2 , newset = 1
state 5 :
- if led on , LED = 1<<8
- countset = 1 , newset = 16

Code in elipse :
#include <stdio.h>
#include <unistd.h>
#include <alt_types.h>
#include <sys/alt_alarm.h>
#include <system.h>
#define sw (*(volatile int*)SWITCH_BASE)
#define led (*(volatile int*)LEDR_BASE)
// global varible
int a = 16;
int state = 1 ;
int ledon = 1 ;

alt_u32 my_alarm_callback(void* context){


if (sw == 1 ){
switch (state) {
case 1 :
if ( a > 0 && ledon == 1 ){
led = 1 ;
a -= 1 ;
}
else if ( a > 0 && ledon == 0 ) {
led = 0 ;
a -= 1 ;
}
else if ( a== 0 && ledon == 1 ) {
ledon = 0 ;
a = 16 ;
}
else {
a=8;
ledon = 1 ;
state = 2 ;
}
break ;

case 2 :
if ( a > 0 && ledon == 1 ){
led = 1<<2;
a -= 1 ;
}
else if ( a > 0 && ledon == 0 ) {
led = 0 ;
a -= 1 ;
}
else if ( a== 0 && ledon == 1 ) {
ledon = 0 ;
a=8;
}
else {
a=4;
ledon = 1 ;
state = 3 ;
}
break ;

case 3 :
if ( a > 0 && ledon == 1 ){
led = 1<<4;
a -= 1 ;
}
else if ( a > 0 && ledon == 0 ) {
led = 0 ;
a -= 1 ;
}
else if ( a== 0 && ledon == 1 ) {
ledon = 0 ;
a=4;
}
else {
a=2;
ledon = 1 ;
state = 4;
}

break ;
case 4 :
if ( a > 0 && ledon == 1 ){
led = 1<<6 ;
a -= 1 ;
}
else if ( a > 0 && ledon == 0 ) {
led = 0 ;
a -= 1 ;
}
else if ( a== 0 && ledon == 1 ) {
ledon = 0 ;
a=2;
}
else {
a=1;
ledon = 1 ;
state = 5 ;
}
break ;
case 5 :
if ( a > 0 && ledon == 1 ){
led = 1<<8 ;
a -= 1 ;
}
else if ( a > 0 && ledon == 0 ) {
led = 0 ;
a -= 1 ;
}
else if ( a== 0 && ledon == 1 ) {
ledon = 0 ;
a=1;
}
else {
a = 16 ;
ledon = 1 ;
state = 1 ;
}
break ;

default :
printf (" error state !!!! ") ;

}
printf ("a = %d \n" , a ) ;
}

else {
led = 0 ;
state = 1;
ledon = 1;
a = 16 ;
}

return( alt_ticks_per_second()/8);
}

int main()
{
static alt_alarm alarm;

if (alt_alarm_start(&alarm, alt_ticks_per_second()/8,
my_alarm_callback, NULL) < 0)
{
printf("No system clock available\n");
}
while (1){
}
return 0;
}

III. Build a Nios II system with timers. Write a C program to do these tasks by
using
alarm:
- When switch 1 is set, each led at even position from LED1 to 10 turns on in
500ms
and then turn off while each led at odd position from LED1 to 10 turns on in 1s
and
then turn off.
- When switch 2 is set, each led from LED1 to 5 turns on in 500ms and then
turn off.
Each led from led 6 to 10 turn on in 1s and turn off.
- When two switch are set, turn on all leds.
- When two switch are not set, turn off all leds.

3.1 .Build Nios II system


3.2 Quartus Code

---------------------------------------------------------
-- This code is generated by Terasic System Builder
---------------------------------------------------------

library ieee;
use ieee.std_logic_1164.all;

entity lab4_2 is
port
(

------------ CLOCK ------------


CLOCK2_50 :in std_logic;
CLOCK3_50 :in std_logic;
CLOCK4_50 :in std_logic;
CLOCK_50 :in std_logic;

------------ KEY ------------


KEY :in std_logic_vector(3 downto 0);

------------ SW ------------
SW :in std_logic_vector(9 downto 0);

------------ LED ------------


LEDR :out std_logic_vector(9 downto 0);

------------ Seg7 ------------


HEX0 :out std_logic_vector(6 downto 0);
HEX1 :out std_logic_vector(6 downto 0);
HEX2 :out std_logic_vector(6 downto 0);
HEX3 :out std_logic_vector(6 downto 0);
HEX4 :out std_logic_vector(6 downto 0);
HEX5 :out std_logic_vector(6 downto 0);

------------ SDRAM ------------


DRAM_ADDR :out std_logic_vector(12 downto 0);
DRAM_BA :out std_logic_vector(1 downto 0);
DRAM_CAS_N :out std_logic;
DRAM_CKE :out std_logic;
DRAM_CLK :out std_logic;
DRAM_CS_N :out std_logic;
DRAM_DQ :inout std_logic_vector(15 downto 0);
DRAM_LDQM :out std_logic;
DRAM_RAS_N :out std_logic;
DRAM_UDQM :out std_logic;
DRAM_WE_N :out std_logic
);

end entity;

---------------------------------------------------------
-- Structural coding
---------------------------------------------------------

architecture rtl of lab4_2 is


component qsys is
port (
clk_clk : in std_logic := 'X'; -- clk
ledr_export : out std_logic_vector(9 downto 0);
-- export
reset_reset : in std_logic := 'X'; --
reset
sw_wire_export : in std_logic_vector(9 downto 0) :=
(others => 'X') -- export
);
end component qsys;

begin
stage0: qsys PORT MAP ( clock_50 , ledr , not(key(0)x) , sw ) ;
end rtl;

3.3 C Program
Idea :
- first need to make "check" to check if the switch is change after call
my_alarm_callback fucntion . If change it reset value and state for system to
do the task corresponding to switch to avoid error when change value of led .
- Set value time to call back my_alarm_callback function affter 500 ms .
Create two counting components 'count1s' and 'count05s' to count the time to
do task , count1s is count down from 2 ( 1 second ) , count05s is count down
form 1 ( 0.5s ) .
- When call the function , the counting component is countdown , if count goes
to 0 , depend on switcth state , it will do the task .
- In detail :
At switch 1 on , switch 2 off :
+ count1s : odd position led ( 1,3,5,7,9) is turn on and of alternately
+ count05s : even position led ( 2,4,6,8,10) is turn on and of alternately
At switch 2 on , switch 1 off :
+ count1s : led 6 to 10 turn on and off alternately
+ count05s :led 1 to 5 turn on and off alternately
At switch 1,2 on/off :
+ all led on/off .

Data flow :

Code in elipse
#include <stdio.h>
#include <alt_types.h>
#include <sys/alt_alarm.h>
#include <system.h>
#define sw (*(volatile int*)SWITCH_BASE)
#define led (*(volatile int*)LEDR_BASE)

// global varible
int check = 0 ;
int count1s = 2 ;
int count05s = 1 ;

alt_u32 my_alarm_callback(void* context){


// check if switch change
if ( check != sw ){
printf ("testing ");
led = 0 ;
count1s =2 ;
count05s = 1 ;
}
check = sw;

// switch = 1
if ((sw&0b11) == 1){
// odd led process
if (count05s > 0 && count1s > 0 ){
count05s -= 1 ;
count1s -= 1;
}
else if ( count05s == 0 && count1s > 0 )
{
count05s = 1 ;
count1s -= 1 ;

}
else if ( count05s > 0 && count1s == 0 )
{
count1s = 2 ;
count05s -= 1 ;
}
else{
count1s = 2 ;
count05s =1 ;
}
if ( count1s == 0 ){
for (int i = 0 ; i < 5 ; i++) {
led = led^(1<<(2*i));
}
}
if ( count05s == 0 ){
for (int i = 0 ; i < 5 ; i++) {
led = led^(1<<(2*i+1));
}
}
printf ("05s = %d , 1s = %d , binary = %d \n" , count05s ,
count1s , led ) ;
}

else if ( (sw&0b11) == 0b10 ){


if (count05s > 0 && count1s > 0 ){
count05s -= 1 ;
count1s -= 1;
}
else if ( count05s == 0 && count1s > 0 )
{
count05s = 1 ;
count1s -= 1 ;
}
else if ( count05s > 0 && count1s == 0 )
{
count1s = 2 ;
count05s -= 1 ;
}
else{
count1s = 2 ;
count05s =1 ;
}
if ( count1s == 0 ){
for (int i = 0 ; i < 5 ; i++) {
led = led^(1<<(i+5));
}
}
if ( count05s == 0 ){
for (int i = 0 ; i < 5 ; i++) {
led = led^(1<<(i));
}
}
}

else if ( (sw&0b11) == 0b11 ){


led = 0b11111111111 ;
}

else if ( (sw&0b11) == 0){


led = 0 ;
}
else {
led = 0 ;
printf (" Wrong button ") ;
}
return( alt_ticks_per_second()/2);
}

int main()
{
static alt_alarm alarm;

if (alt_alarm_start(&alarm, alt_ticks_per_second()/2,
my_alarm_callback, NULL) < 0)
{
printf("No system clock available\n");
}
while (1){

}
return 0;
}

You might also like