本文共 3625 字,大约阅读时间需要 12 分钟。
设计背景
模拟汽车转向的功能,对相应的车况信息进行反馈显示设计基础目的
利用按键操作,点阵屏显示,汽车状况模块使用
LED模块、8*8led点阵、独立键盘、蜂鸣器辅助工具
Keil C51编程软件、烧写软件(STC-ISP-15xx) 总体个人设计意图功能演示
stc89c52实现汽车转向灯功能
代码实现
#include "reg51.h" //此文件中定义了单片机的一些特殊功能寄存器#includetypedef unsigned int u16; //对数据类型进行声明定义typedef unsigned char u8;sbit led=P2^0; //定义P20口是ledsbit SRCLK=P3^6;sbit RCLK=P3^5;sbit SER=P3^4;sbit k1=P3^1;sbit k2=P3^2; sbit k4=P3^3; //定义按键K4sbit k3=P3^4; //定义按键K4sbit beep=P1^5;u8 flag=0;u8 car=0;u8 turnright[]={ 0x18,0x18,0x18,0x18,0x81,0x42,0x3C,0x18,};u8 turnleft[]={ 0x18,0x3C,0x42,0x81,0x18,0x18,0x18,0x18,}; u8 gostraight[]={ 0x10,0x20,0x40,0x8F,0x8F,0x40,0x20,0x10,};u8 back[]={ 0x08,0x04,0x02,0xF1,0xF1,0x02,0x04,0x08,};u8 donotenter[]={ 0x06,0x10,0x42,0x98,0x98,0x42,0x10,0x06,};u8 doubleflash[]={ 0x10,0x20,0x40,0x8F,0x8F,0x40,0x20,0x10,};u8 start[]={ 0x18,0x24,0x5A,0xBD,0xBD,0x5A,0x24,0x18,} ;u8 stop[]={ 0x81,0x42,0x24,0x18,0x18,0x24,0x42,0x81,};u8 ledwei[]={ 0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};void delay(u16 i){ while(i--); }void Hc595SendByte(u8 dat){ u8 a; SRCLK=0; RCLK=0; for(a=0;a<8;a++) { SER=dat>>7; dat<<=1; SRCLK=1; _nop_(); _nop_(); SRCLK=0; } RCLK=1; _nop_(); _nop_(); RCLK=0;}/******************************************************************************** 函 数 名 : Timer0Init* 函数功能 : 定时器0初始化* 输 入 : 无* 输 出 : 无*******************************************************************************/void Timer0Init(){ TMOD|=0X01;//选择为定时器0模式,工作方式1,仅用TR0打开启动。 TH0=0XFC; //给定时器赋初值,定时1ms TL0=0X18; ET0=1;//打开定时器0中断允许 EA=1;//打开总中断 TR0=1;//打开定时器 }/******************************************************************************** 函 数 名 : main* 函数功能 : 主函数* 输 入 : 无* 输 出 : 无*******************************************************************************/void main(){ Timer0Init(); //定时器0初始化 while(1); }void test(){ u8 t; if(car==2) { if(k4==0&&k1!=0) { P2=0xc7; for(t=0;t<8;t++) { P0=ledwei[t]; //位选 Hc595SendByte(turnright[t]); //发送段选数据 delay(100); //延时 Hc595SendByte(0x00); //消隐 } car=2; beep=~beep; delay(100); //延时大约1ms 通过修改此延时时间达到不同的发声效果 } if(k4!=0&&k1==0) { P2=0xe3; for(t=0;t<8;t++) { P0=ledwei[t]; //位选 Hc595SendByte(turnleft[t]); //发送段选数据 delay(100); //延时 Hc595SendByte(0x00); //消隐 } beep=~beep; delay(100); //延时大约1ms 通过修改此延时时间达到不同的发声效果 car=2; } if(k4==0&&k1==0) { P2=0x00; for(t=0;t<8;t++) { P0=ledwei[t]; //位选 Hc595SendByte(donotenter[t]); //发送段选数据 delay(100); //延时 Hc595SendByte(0x00); //消隐 } car=2; } if(k4!=0&&k1!=0) { P2=0xff; for(t=0;t<8;t++) { P0=ledwei[t]; //位选 Hc595SendByte(gostraight[t]); //发送段选数据 delay(100); //延时 Hc595SendByte(0x00); //消隐 } car=2; } } if(k2==0&&k4==0) { if(car==0) { for(t=0;t<8;t++) { P0=ledwei[t]; //位选 Hc595SendByte(start[t]); //发送段选数据 delay(100); //延时 Hc595SendByte(0x00); //消隐 } car=2; } } if(k2==0&&k4!=0) { car=0; } if(car==0) { for(t=0;t<8;t++) { P0=ledwei[t]; //位选 Hc595SendByte(stop[t]); //发送段选数据 delay(100); //延时 Hc595SendByte(0x00); //消隐 } } P2=0xe7;}void Timer0() interrupt 1{ static u16 i; static u16 j; TH0=0XFC; //给定时器赋初值,定时1ms TL0=0X18; i++; if(i==500) { j=0; for(i=0;i<8;i++) { test(); if(i==7&&j!=10) { j++; i=0; } } } }
小白两名,如有错误,请联系我指正。
转载地址:http://vorwi.baihongyu.com/