《AVR定时器控制交通灯(共3页).doc》由会员分享,可在线阅读,更多相关《AVR定时器控制交通灯(共3页).doc(3页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精选优质文档-倾情为你奉上/-/定时器Timer1练习,用定时器1模拟控制交通灯,定时时间为0.5s/外部定时器,内部时钟1MHz,即1秒时间内有1M次计数次数/-|/ 1、东西向绿灯与南北向红灯亮5s; |/ 2、东西向绿灯灭,黄灯闪烁5次; |/ 3、东西向红灯与南北向绿灯亮5s; |/ 4、南北向绿灯灭,黄灯闪烁5次 |/ 编译环境:IAR/-|-#include#define uint_8 unsigned char#define uint_16 unsigned intuint_8 operation_type=1,Time_Count=0;#define GREEN_EW_ON()
2、 PORTC&=(12) /东西向指示灯打开#define YELLOW_EW_ON() PORTC&=(11)#define RED_EW_ON() PORTC&=(10)#define GREEN_EW_OFF() PORTC|=12 /东西向指示灯关闭#define YELLOW_EW_OFF() PORTC|=11#define RED_EW_OFF() PORTC|=10#define GREEN_SN_ON() PORTC&=(15) /南北向指示灯打开#define YELLOW_SN_ON() PORTC&=(14)#define RED_SN_ON() PORTC&=(13)
3、#define GREEN_SN_OFF() PORTC|=15 /南北向指示灯关闭#define YELLOW_SN_OFF() PORTC|=14#define RED_SN_OFF() PORTC|=13#define YELLOW_EW_BLINK() PORTC=(11)#define YELLOW_SN_BLINK() PORTC=(14)void timer_init() TCCR1B=0x04;/定时器1设置为256分频 TCNT1=65536-(/2)/256; TIMSK|=(12);/打开定时器0和定时器1的中断使能位 SREG=0x80;/打开全局中断 int main
4、() DDRC=0xff; PORTC=0xff; timer_init(); while (1); #pragma vector = TIMER1_OVF_vect_interrupt void TIMER1_server(void) TCNT1=65536-(/2)/256; /YELLOW_EW_BLINK(); switch(operation_type) case 1: GREEN_EW_ON();YELLOW_EW_OFF();RED_EW_OFF(); RED_SN_ON();GREEN_SN_OFF();YELLOW_SN_OFF(); if(+Time_Count!=10)
5、return; Time_Count=0; operation_type=2; break; case 2: GREEN_EW_OFF(); YELLOW_EW_BLINK(); if(+Time_Count!=10) return; Time_Count=0; operation_type=3; break; case 3: RED_EW_ON();GREEN_EW_OFF();YELLOW_EW_OFF(); GREEN_SN_ON();YELLOW_SN_OFF();RED_SN_OFF(); if(+Time_Count!=10) return; Time_Count=0; operation_type=4; break; case 4: GREEN_SN_OFF(); YELLOW_SN_BLINK(); if(+Time_Count!=10) return; Time_Count=0; operation_type=1; break; 专心-专注-专业
限制150内