// 名称: LED模拟交通灯
//-----------------------------------------------------------------
// 说明: 东西向绿灯亮若干秒后,黄灯闪烁,闪烁5次后亮红灯,
//红灯亮后,南北向由红灯变为绿灯,2秒后南北向黄灯闪烁,
//闪烁5次后亮红灯,东西向绿灯亮,如此往复.
//本例将切换时间设得较短,以便快速观察运银轿行效果.
//
//-----------------------------------------------------------------
#include <pic.h>
#define INT8U unsigned char
#define _XTAL_FREQ 1000000UL
#define RED_EW_ON() RC0 = 0 //东西向指示灯开
#define YELLOW_EW_ON() RC1 = 0
#define GREEN_EW_ON() RC2 = 0
#define RED_EW_OFF() RC0 = 1 //东西向指示灯关
#define YELLOW_EW_OFF() RC1 = 1
#define GREEN_EW_OFF() RC2 = 1
#define RED_SN_ON() RC3 = 0 //南北向指示灯开
#define YELLOW_SN_ON() RC4 = 0
#define GREEN_SN_ON() RC5 = 0
#define RED_SN_OFF() RC3 = 1 //南北向指示灯关
#define YELLOW_SN_OFF() RC4 = 1
#define GREEN_SN_OFF() RC5 = 1
#define YELLOW_EW_BLINK() RC1 = ~RC1 //东西向黄灯闪烁
#define YELLOW_SN_BLINK() RC4 = ~RC4 //南北向黄灯闪烁
//闪烁次数, *** 作类型变量
INT8U Flash_Count = 0, Operation_Type = 1
//-----------------------------------------------------------------
// 延时函数卜册
//-----------------------------------------------------------------
void DelayX100ms(INT8U n)
{
while (n--) __delay_ms(100)
}
//-----------------------------------------------------------------
// 交通灯型搏宏切换子程序
//-----------------------------------------------------------------
void Traffic_Light()
{int i
switch (1 )
{
case 1: //东西向绿灯与南北向红灯亮
RC2 = 0
GREEN_SN_OFF()
RED_SN_ON()
RED_EW_OFF()
DelayX100ms(20)
case 2: //东西向黄灯开始闪烁,绿灯关闭
YELLOW_EW_OFF()
GREEN_EW_OFF()
for(i=0i<5i++)
{
YELLOW_EW_BLINK()
DelayX100ms(10)
}
YELLOW_EW_OFF()
case 3: //东西向红灯与南北向绿灯亮
RED_EW_ON()
RED_SN_OFF()
GREEN_SN_ON()
DelayX100ms(20)
case 4: //南北向黄灯开始闪烁
GREEN_SN_OFF()
YELLOW_SN_OFF()
for(i=0i<5i++)
{
YELLOW_SN_BLINK()
DelayX100ms(10)
}
YELLOW_SN_OFF()
}
}
//-----------------------------------------------------------------
// 主程序
//-----------------------------------------------------------------
void main()
{TRISC=0
YELLOW_EW_OFF()
YELLOW_SN_OFF()
RED_EW_OFF()
GREEN_EW_OFF()
YELLOW_SN_OFF()
GREEN_SN_OFF()
while(1)
//Traffic_Light()
}
#include "pic.h"unsigned char dispbitcnt //数码管位码扫描变量
unsigned char second //秒变量
unsigned char minite //分团歼告变量
unsigned char hour //时变量
unsigned char tcnt //秒信号产生变量
unsigned char mstcnt //扫描时间变量
const unsigned char dispcode[10]={0xc0,0xf9,0xa4,0xb0,0x99,
0x92,0x82,0xf8,0x80,0x90}
//0123456789段码
const unsigned char dispbitcode[6]={0xfe,0xfd,0xfb,0xf7,
0xef,0xdf}
//123456位码
unsigned char dispbuf[6]={0,0,0,0,0,0}
//缓冲数组
//**************TMR0中断数码管扫描程序塌明*********************
void interrupt tmr0(void)
{
if(T0IE &&T0IF)
{
T0IF=0 //清除中断标志位
{
PORTC=dispcode[dispbuf[dispbitcnt]]
PORTA=dispbitcode[dispbitcnt]
dispbitcnt++
if(dispbitcnt==6)
{dispbitcnt=0
}
}
tcnt++ //中断产生秒信号
if(tcnt==244)
{tcnt=0
second++
if(second==60)
{second=0
minite++
if(minite==60)
{minite=0
hour++
if(hour==24)
{hour=0
}
}
}
dispbuf[4]=second%10
dispbuf[5]=second/10
dispbuf[2]=minite%10
dispbuf[3]=minite/10
dispbuf[0]=hour%10
dispbuf[1]=hour/10
}
}
}
///****************1ms延改衡时程序******************
void delay_1ms(unsigned int i)
{
unsigned char j
for(i!=0i--)
for(j=141j!=0j--)
}
///****************按键检测*********************
void key_scan(void)
{
while(1)
{
if(RB0==0)
{
delay_1ms(20)
if(RB0==0)
{second++
if(second==60)
{second=0
}
dispbuf[4]=second%10
dispbuf[5]=second/10
while(RB0==0)
}
}
if(RB1==0)
{
delay_1ms(20)
if(RB1==0)
{
minite++
if(minite==60)
{minite=0
}
dispbuf[2]=minite%10
dispbuf[3]=minite/10
while(RB1==0)
}
}
if(RB2==0)
{
delay_1ms(20)
if(RB2==0)
{
hour++
if(hour==24)
{
hour=0
}
}
dispbuf[0]=hour%10
dispbuf[1]=hour/10
while(RB2==0)
}
}
}
//***************端口初始化***********************
void PORT_INIT(void)
{
TRISA=0 //A口输出
ADCON1=0x06//数字I/O
TRISC=0 //C口输出
TRISB=0XFF //B口输入
}
//***************TMR0初始**************************
void TMR0_INIT(void)
{
GIE=1 //开启全局中断
OPTION=0X03//开启内部上拉,预分频比1/16
T0IF=0 //清中断标志
T0IE=1 //允许TMR0中断
}
//*************主程序********************
void main(void)
{
while(1)
{
dispbitcnt=0
PORT_INIT()
PORTC=0x00 //开段码
PORTA=0x00 //开位码
delay_1ms(200) //延时
delay_1ms(200) //延时
delay_1ms(200) //延时
delay_1ms(200) //延时
PORTA=0xff //关位码
TMR0_INIT()//调初始子程序
TMR0=0 //开启TMR0中断
key_scan() //按键检测子程序
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)