/******************************************************************************* *函数原型: void timer0_ovf_isr(void); *功能:定时器0中断服务程序 *参数: *说明:键盘和led一起构成1/6扫描,每6次中断进行一次键盘处理 *******************************************************************************/ #pragma interrupt_handler timer0_ovf_isr:10 void timer0_ovf_isr(void) { TCNT0 = 0xAA; //set count if (state <= 5) { lflash(); display(state); state++; } else { key(); state = 0; } } void USART0_Init(void) { UCSRB = 0x00; //disable while setting baud rate UCSRA = 0x00; UCSRC = BIT(URSEL) | 0x06; UBRRL = 0x47; //set baud rate lo UBRRH = 0x00; //set baud rate hi UCSRB = 0x18; } //------------------------------------------------------------------------- void USART0_Transmit(unsigned char data) { while (!(UCSRA &(1 << UDRE))) ; UDR = data; } //------------------------------------------------------------------------- void USART0_TransmitString(char *data) { while (*data) USART0_Transmit(*data++); } //------------------------------------------------------------------------- unsigned char USART0_Receive(void) { while (!(UCSRA &(1 << RXC))) ; return UDR; } /******************************************************************************* *函数原型: void port_init(void); *功能:端口初始化 *参数: *说明: *******************************************************************************/ void port_init(void) //端口初始化 { DDRB = 0xff; //led数据口 11111111 DDRC = 0xff; //键盘 00111111 DDRD = 0xfc; //段码选择 11111100 PORTD = 0xff; PORTB = 0xff; PORTC = 0xff; } /******************************************************************************* *函数原型: void init_devices(void); *功能:m8初始化 *参数: *说明: *******************************************************************************/ //call this routine to initialize all peripherals void init_devices(void) { //stop errant interrupts until set up CLI(); //disable all interrupts port_init(); timer0_init(); USART0_Init(); MCUCR = 0x00; GICR = 0x00; TIMSK = 0x01; //timer interrupt sources SEI(); //re-enable interrupts //all peripherals are now initialized } /******************************************************************************* *函数原型: void main() ; *功能:主程序,执行初始化操作,循环等待案件事件 *参数: *说明: *******************************************************************************/ void main() { init_devices(); //标志和常量初始化 task = 0x00; //按键状态 state = 0x00; //显示位置变量 keyprocess = 0; //按键有效标志 keydone = 1; //按键任务完成标志 ledtime = ledshow; //累计闪烁时已点亮或已熄灭时间 ledtask = 0; //当前的闪烁状态 0 代表亮 keymark = 0; //指示当前工作状态为正常工作状态 USART0_TransmitString("OK!\r\n"); while (1) { key_processing(); } } |