首页资源分类嵌入式处理器51内核 > 单片机数码管控制

单片机数码管控制

已有 445117个资源

下载专区

上传者其他资源

文档信息举报收藏

标    签:数码管码段

分    享:

文档简介

通过对段码的组合来组成数码管的数字

文档预览

#include "reg51.h" // 包含头文件,该文件中有51单片机的寄存器定义以及一些位定义 #include "absacc.h" // 该头文件中包含外部端口定义的方法 #define LEDBit_Port XBYTE[0xE400] // 数码管 位 驱动端口地址定义 #define LEDSeg_Port XBYTE[0xE000] // 数码管 段 驱动端口地址定义 /*=================================================================================== 数码管相关预定义及变量声明区 ===================================================================================*/ // D0----D7分别对应的IO口位 #define BIT0 (0x01) #define BIT1 (0x02) #define BIT2 (0x04) #define BIT3 (0x08) #define BIT4 (0x10) #define BIT5 (0x20) #define BIT6 (0x40) #define BIT7 (0x80) // 数码管各段与单片机IO口的连接关系,硬件改动之后相应得变更这里即可 #define con_Sa BIT0 #define con_Sb BIT1 #define con_Sc BIT2 #define con_Sd BIT3 #define con_Se BIT4 #define con_Sf BIT5 #define con_Sg BIT6 #define con_Sdp BIT7 // 数码管可以显示的各种字符,编成各笔段相加的形式 #define con_SegCode0 ( con_Sa + con_Sb + con_Sc + con_Sd + con_Se + con_Sf ) #define con_SegCode1 ( con_Sb + con_Sc ) #define con_SegCode2 ( con_Sa + con_Sb + con_Sd + con_Se + con_Sg ) #define con_SegCode3 ( con_Sa + con_Sb + con_Sc + con_Sd + con_Sg ) #define con_SegCode4 ( con_Sb + con_Sc + con_Sf + con_Sg ) #define con_SegCode5 ( con_Sa + con_Sc + con_Sd + con_Sf + con_Sg ) #define con_SegCode6 ( con_Sa + con_Sc + con_Sd + con_Se + con_Sf + con_Sg ) #define con_SegCode7 ( con_Sa + con_Sb + con_Sc ) #define con_SegCode8 ( con_Sa + con_Sb + con_Sc + con_Sd + con_Se + con_Sf + con_Sg ) #define con_SegCode9 ( con_Sa + con_Sb + con_Sc + con_Sd + con_Sf + con_Sg ) #define con_SegCodeA ( con_Sa + con_Sb + con_Sc + con_Se + con_Sf + con_Sg ) #define con_SegCodeb ( con_Sc + con_Sd + con_Se + con_Sf + con_Sg ) #define con_SegCodeC ( con_Sa + con_Sd + con_Se + con_Sf ) #define con_SegCoded ( con_Sb + con_Sc + con_Sd + con_Se + con_Sg ) #define con_SegCodeE ( con_Sa + con_Sd + con_Se + con_Sf + con_Sg ) #define con_SegCodeF ( con_Sa + con_Se + con_Sf + con_Sg ) #define con_SegCodeOn ( con_Sa + con_Sb + con_Sc + con_Sd + con_Se + con_Sf + con_Sg + con_Sdp ) // 全亮 #define con_SegCodeOFF ( 0x00 ) // 全灭 #define con_SegCodeH ( con_Sb + con_Sc + con_Se + con_Sf + con_Sg ) #define con_SegCodeI ( con_Se + con_Sf ) #define con_SegCodei ( con_Se ) #define con_SegCodeJ ( con_Sb + con_Sc + con_Sd ) #define con_SegCodeL ( con_Sd + con_Se + con_Sf ) #define con_SegCodeN ( con_Sa + con_Sb + con_Sc + con_Se + con_Sf ) #define con_SegCoden ( con_Sc + con_Se + con_Sg ) #define con_SegCodeo ( con_Sc + con_Sd + con_Se + con_Sg ) #define con_SegCodeP ( con_Sa + con_Sb + con_Se + con_Sf + con_Sg ) #define con_SegCodeU ( con_Sb + con_Sc + con_Sd + con_Se + con_Sf ) #define con_SegCodeEqu ( con_Sd + con_Sg ) #define con_SegCodeBar ( con_Sg ) //定义各种显示字符 unsigned char Disp_Buf[6] = { 1, 2, 3, 4, 5, 6 }; // 显示缓冲区 初始化值为:123456,则上电时显示123456 unsigned char Point_Buf[6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // 小数点缓冲区 0x08对应小数点亮 0x00则不亮 unsigned char Shine_Buf[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; // 闪烁缓冲区 0x00则闪烁 0xFF则不闪烁 unsigned char Disp_State = 0; // LED的显示状态,6位数码管 从0-5变化 每1ms扫描1位,代表当前扫描第几位数码管 unsigned int Shine_Counter = 0; // 闪烁计数器 1ms的计时时基 unsigned int const con_ShineSpan = 500; // 闪烁时间间隔 0.5秒 unsigned int const con_ShineT = 1000; // 闪烁周期 1秒 unsigned char flag_1ms =0; unsigned char const LEDSeg_Tab[ ] = { con_SegCode0, con_SegCode1, con_SegCode2, con_SegCode3, con_SegCode4, con_SegCode5, con_SegCode6, con_SegCode7, con_SegCode8, con_SegCode9, con_SegCodeA, con_SegCodeb, con_SegCodeC, con_SegCoded, con_SegCodeE, con_SegCodeF, con_SegCodeOn, con_SegCodeOFF, con_SegCodeH, con_SegCodeI, con_SegCodei, con_SegCodeJ, con_SegCodeL, con_SegCodeN, con_SegCoden, con_SegCodeo, con_SegCodeP, con_SegCodeU, con_SegCodeEqu, con_SegCodeBar }; // 数码管的段码数组 unsigned char const LEDBit_Tab[] = { 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };// 数码管的位选数组共6位 void timer_init( void ){ //定时器0初始化 TMOD = 0x01; //定时器0,工作方式1,16位定时模式 TH0 = 0xFC; //定时器0高位初值 TL0 = 0x18; //定时器1高位初值,使用12MHz晶振,实现1ms溢出一次产生定时器中断 EA = 1; //开总中断 ET0 = 1; //开定时器0中断 TR0 = 1; //启动定时器0 } void LED_Display( void ){ // 底层显示驱动程序 unsigned char Temp_Data; LEDSeg_Port = 0x00; // 行间消隐动作 LEDBit_Port = LEDBit_Tab[Disp_State]; // 送位选信号 Temp_Data = Disp_Buf[Disp_State]; // 取出当前数码管要显示的数据 Temp_Data = LEDSeg_Tab[Disp_State]; // 查段码 Temp_Data |= Point_Buf[Disp_State]; // 该位小数点处理 if( Shine_Counter > con_ShineSpan ){ // 闪烁处理 后0.5秒与闪烁缓冲区进行 与 操作 Temp_Data &= Shine_Buf[Disp_State]; } LEDSeg_Port = Temp_Data; // 包含小数点和闪烁处理后的段码送到 段驱动端口 // 闪烁计时处理 Shine_Counter++; // 闪烁时间处理 if(Shine_Counter >= con_ShineT ){ Shine_Counter = 0; } // 数码管显示状态处理 Disp_State++; if( Disp_State >= 6 ){ Disp_State = 0; } // 6位数码管 } void main( void ){ timer_init(); // 调用定时器初始化函数,前已定义 while(1){ // 主循环通过适时调用显示函数 实现1ms刷一位数码管动作 //1毫秒时间到后 if( Flag_1ms ){ Flag_1ms = 0; // 时间单位标志清除为下次判断调用做准备 LED_Display(); // 调用显示函数将需要显示的数据刷到数码管上 } } } void timer0( ) interrupt 1 { TH0 = 0xFC; // 溢出之后重新赋初值 TL0 = 0x18; Flag_1ms = 1; //定时器溢出时间到后建立一个1毫秒的时间单位标志 }

Top_arrow
回到顶部
EEWORLD下载中心所有资源均来自网友分享,如有侵权,请发送举报邮件到客服邮箱bbs_service@eeworld.com.cn 或通过站内短信息或QQ:273568022联系管理员 高进,我们会尽快处理。