打印
[Atmel]

用AtmelStudio6.2跑mega16例程(38)T1交通灯

[复制链接]
799|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
ddllxxrr|  楼主 | 2014-10-23 19:46 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
本程序是利用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;                                  
                                                  
        }
}


相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:http://shop34182318.taobao.com/ http://shop562064536.taobao.com

2398

主题

6950

帖子

67

粉丝