Report Lab 4
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.
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);
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;
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 ;
}
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.
library ieee;
use ieee.std_logic_1164.all;
entity lab4_2 is
port
(
------------ SW ------------
SW :in std_logic_vector(9 downto 0);
end entity;
---------------------------------------------------------
-- Structural coding
---------------------------------------------------------
begin
stage0: qsys PORT MAP ( clock_50 , ledr , not(key(0)x) , sw ) ;
end rtl;
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 :
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 ;
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.
---------------------------------------------------------
-- This code is generated by Terasic System Builder
---------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
entity lab4_2 is
port
(
------------ SW ------------
SW :in std_logic_vector(9 downto 0);
end entity;
---------------------------------------------------------
-- Structural coding
---------------------------------------------------------
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 ;
// 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 ) ;
}
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;
}