List 4: #include <stdio.h> #include "system.h" #include "altera_avalon_pio_regs.h" #include "sys/alt_alarm.h" #include "alt_types.h" long Ticks, Num; long HEXtable[10] = {0x40, // 0:1000000 0x79, // 1:1111001 0x24, // 2:0100100 0x30, // 3:0110000 0x19, // 4:0011001 0x12, // 5:0010010 0x02, // 6:0000010 0x78, // 7:1111000 0x00, // 8:0000000 0x10 // 9:0010000 }; #define KEY_START 0x01 #define KEY_HOLD 0x02 #define KEY_STOP 0x04 #define KEY_RESET 0x08 void LEDR_out(int num) { if((num < 0)||(num > 9)) return; IOWR_ALTERA_AVALON_PIO_DATA(LEDR_BASE, 1 << num); } void HEX_out(int digit, int num) { if((num < 0)||(num > 9)) return; if((digit < 0)||(digit > 2)) return; if(digit) IOWR_ALTERA_AVALON_PIO_DATA(HEX1_BASE, HEXtable[num]); else IOWR_ALTERA_AVALON_PIO_DATA(HEX0_BASE, HEXtable[num]); } alt_u32 Alarm_Int(void* context) { Ticks++; if (Ticks >= 100) { Num++; if (Num > 999) Num = 0; LEDR_out(Num%10); HEX_out(0, (Num/10)%10); HEX_out(1, Num/100); Ticks=0; } return 1; } int main() { static alt_alarm Alarm; alt_u8 KeyStatus, FLAG=0; // 0:Stop 1:Running // Initial Display LEDR_out(0); HEX_out(0, 0); HEX_out(1, 0); while(1) { KeyStatus = IORD_ALTERA_AVALON_PIO_DATA(KEY_BASE); if(( KeyStatus & KEY_START ) == 0 ) { if ( FLAG != 1 ) { alt_alarm_start(&Alarm, 1, Alarm_Int, NULL); } FLAG = 1; } else if (( KeyStatus & KEY_HOLD ) == 0 ) { if( FLAG ) { alt_alarm_stop(&Alarm); FLAG = 0; } else { alt_alarm_start(&Alarm, 1, Alarm_Int, NULL); FLAG = 1; } } else if (( KeyStatus & KEY_STOP ) == 0 ) { alt_alarm_stop(&Alarm); FLAG = 0; } else if (( KeyStatus & KEY_RESET ) == 0 ) { Ticks = 0; Num = 0; LEDR_out(0); HEX_out(0, 0); HEX_out(1, 0); } } return 0; }
Copyright © ITmedia, Inc. All Rights Reserved.