|
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
typedef unsigned char uint8;
typedef unsigned int uint16;
/****************************************PWM初始化**************************************************/
void PWMInit(void)
{
PWMPOL = 0x20; /*脉冲先高后低*/
PWMCLK = 0x20; /*使用SA时钟源*/
PWMPRCLK = 0x07; /*时钟A为总线8分频*/
PWMCAE_CAE3 = 0; /*通道3为左对齐输出方式*/
PWMCTL_CON23 = 0; /*使用通道3*/
PWMCAE_CAE5 = 0; /*通道5为左对齐输出方式*/
PWMCTL_CON45 = 0; /*使用通道5*/
PWMSCLA = 0x05; /*ClockSA = ClockA/(2*PWMSCLA)为3KHz*/
PWMPER5 = 0xC8; /*设定周期*/
}
/****************************************延时程序模块**************************************************/
void delay(void)
{
uint16 i, j = 0;
for(i=0; i<65535; i++)
{
j++;
}
}
void delay_3s(void)
{
uint16 i, j = 0, m = 10;
do {
for(i=0; i<65535; i++)
{
j++;
}
m--;
}while(m!=0);
}
void delay_60s(void)
{
uint16 i, j = 0, m = 200;
do {
for(i=0; i<65535; i++)
{
j++;
}
m--;
}while(m!=0);
}
/****************************************交通灯模块**************************************************/
void Traffic_Light(void)
{
uint8 n = 3;
/*******Traffic Lights 方向寄存器********/
DDRA_BIT1 = 1;DDRA_BIT2 = 1;DDRA_BIT3 = 1;DDRA_BIT4 = 1;DDRA_BIT5 = 1;DDRA_BIT6 = 1;DDRA_BIT7 = 1; /*A口方向寄存器*/
DDRB_BIT0 = 1;DDRB_BIT2 = 1;DDRB_BIT7 = 1;/*B口方向寄存器*/
DDRE_BIT2 = 1;DDRE_BIT3 = 1;/*E口方向寄存器*/
DDRK_BIT7 = 1;/*K口方向寄存器*/
DDRM_DDRM4 = 1;DDRM_DDRM5 = 1;DDRM_DDRM6 = 1;DDRM_DDRM7 = 1;/*M口方向寄存器*/
/*******关闭所有交通灯********/
PORTA_BIT1 = 1;PORTA_BIT2 = 1;PORTA_BIT3 = 1;PORTA_BIT4 = 1;PORTA_BIT5 = 1;PORTA_BIT6 = 1;PORTA_BIT7 = 1;/*关A口灯*/
PORTB_BIT0 = 1;PORTB_BIT2 = 1;PORTB_BIT7 = 1;/*关B口灯*/
PORTE_BIT2 = 1;PORTE_BIT3 = 1;/*关E口灯*/
PORTK_BIT7 = 1;/*关K口灯*/
PTM_PTM4 = 1;PTM_PTM5 = 1;PTM_PTM6 = 1;PTM_PTM7 = 1;/*关M口灯*/
/*******Traffic Lights PORT(A/B/E/K/M)********/
/*******南北通行********/
PORTA_BIT4 = 0;PORTA_BIT5 = 0;PTM_PTM5 = 0;PORTB_BIT0 = 0;PTM_PTM6 = 0;PORTK_BIT7 = 0;PORTB_BIT7 = 0;
delay_60s();
/*******变灯提示********/
do{
PORTA_BIT6 = 0;PORTB_BIT2 = 0;
delay_3s();
PORTA_BIT6 = 1;PORTB_BIT2 = 1;
delay_3s();
n--;
}while(n!=0);
/*******南北停止通行********/
PORTA_BIT4 = 1;PORTA_BIT5 = 1;PTM_PTM5 = 1;PORTB_BIT0 = 1;PTM_PTM6 = 1;PORTK_BIT7 = 1;PORTB_BIT7 = 1;
/*******东西通行********/
PORTA_BIT7 = 0;PORTA_BIT2 = 0;PORTA_BIT1 = 0;PTM_PTM7 = 0;PORTE_BIT3 = 0;PTM_PTM4 = 0;PORTB_BIT7 = 0;
delay_60s();
n =3; /*重新给n赋值*/
/*******变灯提示********/
do{
PORTA_BIT3 = 0;PORTE_BIT2 = 0;
delay_3s();
PORTA_BIT3 = 1;PORTE_BIT2 = 1;
delay_3s();
n--;
}while(n!=0);
/*******东西停止通行********/
PORTA_BIT7 = 1;PORTA_BIT2 = 1;PORTA_BIT1 = 1;PTM_PTM7 = 1;PORTE_BIT3 = 1;PTM_PTM4 = 1;PORTB_BIT7 = 1;
}
/****************************************继电器模块**************************************************/
void Electrical_Relay(void)
{
DDRK_BIT4 = 1; DDRK_BIT5 = 1; /*初始化*/
PORTK_BIT4 = 1; /*继电器开断*/
delay();
PORTK_BIT4 = 0;
delay();
PORTK_BIT5 = 1;
delay();
PORTK_BIT5 = 0;
delay();
}
/****************************************蜂鸣器模块**************************************************/
void Buzzer(void)
{
PWMInit(); /*初始化PWM*/
PWME_PWME3 = 1; /*使能PWM3*/
PWMDTY3 = 0x0A; /*设置蜂鸣器占空比5%*/
delay();
PWMDTY3 = 0x14; /*设置蜂鸣器占空比10%*/
delay();
PWMDTY3 = 0x28; /*设置蜂鸣器占空比20%*/
delay();
PWMDTY3 = 0x50; /*设置蜂鸣器占空比40%*/
delay();
PWMDTY3 = 0x64; /*设置蜂鸣器占空比50%*/
delay();
PWMDTY3 = 0x78; /*设置蜂鸣器占空比60%*/
delay();
PWMDTY3 = 0x8C; /*设置蜂鸣器占空比70%*/
delay();
PWMDTY3 = 0xA0; /*设置蜂鸣器占空比80%*/
delay();
PWMDTY3 = 0xB4; /*设置蜂鸣器占空比90%*/
delay();
PWMDTY3 = 0xC8; /*设置蜂鸣器占空比100%*/
delay();
}
/****************************************Main 函数**************************************************/
void main(void)
{
uint8 i;
EnableInterrupts;
PWMInit(); /*初始化PWM*/
for(; ;)
{
for(i=0; i<=0xC8; i++)
{
delay();
PWMDTY5 = i; /*设置风扇占空比*/
PWME_PWME5 = 1; /*使能PWM5*/
while (i==0xC8)
{
PWME_PWME5 = 0;; /*关闭使能PWM5*/
Traffic_Light();/*交通灯模块*/
delay();
PWME_PWME5 = 1;; /*使能PWM5*/
delay_3s();
Electrical_Relay();/*继电器模块*/
delay();
Buzzer();
}
}
}
}
|
|