基于51单片机的航标灯硬件部分 实验原理图 示波器模拟波形 ①第一种波形 Key1置’0’ Key0置’0’ ②第二种波形 Key1置’0’ Key0置’1’ ③第三种波形 Key1置’1’ Key0置’0’ ④第四种波形 Key1置’1’ Key0置’1’
单片机源码:
- #include "reg51.h"
- #define COUNT_50MS 500 //focs=12MHz T=1us 50ms gcd:50ms的倍数
- #define D_OPEN 1 //开灯
- #define D_CLOSE !D_OPEN //关灯
- sbit D=P0^0; //灯输出控制,高电平有效
- sbit OPCON=P1^2;//光照检测 ,低电平有效(晚上)
- sbit Key0=P1^0;
- sbit Key1=P1^1;
- //结构体类型声明1
- struct IALA
- {
- unsigned char gcd; //各时间间隔最大公约数
- unsigned char state[6]; //无符号字符型时间常数
- };
- unsigned int State[6];
- //结构体全局变量定义,code:常数
- struct IALA code IALA4[4]={
-
- { 0,0,0,0,0,0,0},
- {10,1,3,0,0,0,0},
- {10,1,1,1,7,0,0},
- {10,1,2,1,2,1,5}
- };
- //T0_初始化函数
- void T0_Init(void)
- {
- TMOD=(TMOD&0xf0)|0x01;
- TH0=(65536-COUNT_50MS)/256;
- TL0=(65536-COUNT_50MS)%256;
- ET0=1; //中断使能
- EA=1;
- TR0=1; //T0启动
- }
- //T0_ISR程序
- unsigned int it=0;
- void T0_ISR(void) interrupt 1
- {
- unsigned char j;
- TH0=(65536-COUNT_50MS)/256;
- TL0=(65536-COUNT_50MS)%256;
- if(OPCON==0)//黑夜、暗照度
- {
- it++;
- for(j=0;j<6;j++)
- { if(it==State[j])
- D=(j%2)?D_OPEN:D_CLOSE; //判断是开灯还是关灯状态
- }
- if(it>=State[5])
- {
- it=0;
- }
- }
- else //白天、亮照度
- {
- D=D_CLOSE;
- it = 0;
- }
- }
- //按键判断
- unsigned char GetKey()
- {
- unsigned char key;
- if(Key1==0&&Key0==0)
- {
- key=0;
- }
- else if(Key1==0&&Key0==1)
- {
- key=1;
- }
- else if(Key1==1&&Key0==0)
- {
- key=2;
- }
- else if(Key1==1&&Key0==1)
- {
- key=3;
- }
- return(key);
- }
- void main(void)
- {
- unsigned char key,pre_key=0,i;
- unsigned int sum;
- T0_Init();
- D=0;
- while(1)
- {
- key=GetKey(); //获取key1、Key0特征码
- if(key!=pre_key) //若有新的按键,载入控灯时间
- {
- sum=0;
- for(i=0;i<6;i++)
- {
- sum=sum+IALA4[key].gcd*IALA4[key].state[i];
- State[i]=sum;
- }
- }
- pre_key=key;
- }
- }
复制代码
完整的Word格式文档51黑下载地址:
实验三 航标灯.doc
(178.5 KB, 下载次数: 19)
|