本程序是利用T1定时器,来控制交通灯的闪烁。
Proteus的截图:
Studio6.2的截图:
程序:
/*
* GccApplication1.c
*
* Created: 2014-10-23 18:57:04
* Author: Administrator
*/
#define F_CPU 4000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdint.h>
#include <util/delay.h>
#define RED_EW_ON() PORTC |= (1<<0)
#define YELLOW_EW_ON() PORTC |= (1<<1)
#define GREEN_EW_ON() PORTC |= (1<<2)
#define RED_EW_OFF() PORTC &= ~(1<<0)
#define YELLOW_EW_OFF() PORTC &= ~(1<<1)
#define GREEN_EW_OFF() PORTC &= ~(1<<2)
#define RED_SN_ON() PORTC |= (1<<3)
#define YELLOE_SN_ON() PORTC |= (1<<4)
#define GREEN_SN_ON() PORTC |= (1<<5)
#define RED_SN_OFF() PORTC &=~(1<<3)
#define YELLOW_SN_OFF() PORTC |=~(1<<4)
#define GREEN_SN_OFF() PORTC &= ~(1<<5)
#define YELLOW_EW_BLINK() PORTC^=0x02
#define YELLOW_SN_BLINK() PORTC^=0x10
#define BEEP() (PORTB ^=0x01)
uint8_t Time_Count = 0,Flash_Count = 0,Operation_Type = 1;
int main(void)
{
DDRB = 0xFF; PORTC = 0xFF;
DDRC = 0xFF; PORTC = 0x00;
TCCR1B = 0x03; //T1 分频 64
TCNT1 = 65536 - F_CPU/64.0 *0.5;
sei();
while(1)
{
//TODO:: Please write your application code
}
}
void Yellow_Light_Alarm()
{
uint8_t i;
for(i=0;i<100;i++)
{
BEEP();_delay_us(380);
}
}
ISR(TIMER1_OVF_vect)
{
TCNT1 = 65536 - F_CPU/64.0*0.5; //重装定时值
switch(Operation_Type)
{
case 1:
RED_EW_OFF(); YELLOW_EW_OFF(); GREEN_EW_ON();
RED_SN_ON(); YELLOW_SN_OFF(); GREEN_SN_OFF();
if(++Time_Count != 10) return;
Time_Count = 0;
Operation_Type = 2;
break;
case 2:
Yellow_Light_Alarm();
GREEN_EW_OFF();
YELLOW_EW_BLINK();
if(++Flash_Count != 10) return;
Flash_Count = 0;
Operation_Type = 3;
break;
case 3:
RED_EW_ON(); YELLOW_EW_OFF();GREEN_EW_OFF();
RED_SN_OFF(); YELLOW_SN_OFF();GREEN_SN_ON();
if(++Time_Count != 10)return;
Time_Count = 0;
Operation_Type = 4;
break;
case 4:
Yellow_Light_Alarm();
GREEN_SN_OFF();
YELLOW_SN_BLINK();
if(++Flash_Count != 10)return;
Flash_Count = 0;
Operation_Type = 1;
}
}
|