马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#ifndef __SEG_H
#define __SEG_H
void Seg_Tran(unsigned char *pucSeg_Buf,
unsigned char *pucSeg_Code) ;
void Seg_Disp(unsigned char *pucSeg_Code,
unsigned char ucSeg_Pos) ;
#endif
sfr P4= 0xC0;
sbit DAT = P4^0;
sbit OE = P4^1;
sbit LCK = P4^2;
sbit SCK = P4^3;
//代码转换: pucSeg_Buf- 显示字符串,pucSeg_ Code-显示字形码
void Seg_Tran(unsigned char *pucSeg_Buf,
unsigned char *pucSeg_Code)
{
unsigned char i, j=0, ucSeg_Code;
for(i=0; i<4; i++, j++)
{
switch(pucSeg_Buf[j])
{//低电平点亮段,段码[MS8...LSB]对应码顺序为[dp g f e d c b a]
case '0': ucSeg_Code =0xc0; break; //11000000
case '1': ucSeg_Code =0xf9; break; //11111001
case '2': ucSeg_Code =0xa4; break; //10100100
case '3': ucSeg_Code = 0xb0; break; //10110000
case '4': ucSeg_Code = 0x99; break; //10011001
case '5': ucSeg_Code = 0x92; break; //10010010
case '6': ucSeg_Code = 0x82; break; //10000010
case '7': ucSeg_Code = 0xf8; break; //11111000
case '8': ucSeg_Code = 0x80; break; //10000000
case '9': ucSeg_Code = 0x90; break; //10010000
case 'A': ucSeg_Code = 0x88; break; //10001000
case 'B': ucSeg_Code = 0x83; break; //10000011
case 'C': ucSeg_Code = 0xc6; break; //11000110
case 'D': ucSeg_Code = 0xA1; break; //10100001
case 'E': ucSeg_Code = 0x86; break; //10000110
case 'F': ucSeg_Code = 0x8E; break; //10001110
case 'H': ucSeg_Code = 0x89; break; //10001001
case 'I': ucSeg_Code =0xC7; break; //11000111
case '-': ucSeg_Code = 0xbf; break; //10111111
default: ucSeg_Code = 0xff; //11111111
}
if(pucSeg_Buf[j+i] == '.')
{
ucSeg_Code &= 0x7f;
j++;
}
pucSeg_Code[i] = ucSeg_Code;
}
}
//数码管显示:pucSeg_Code-显示字形码,ucSeg_Pos-显示位置
void Seg_Disp(unsigned char *pucSeg_Code,
unsigned char ucSeg_Pos)
{
unsigned char i;
unsigned int temp;
temp = (1<<(8+ucSeg_Pos)) + pucSeg_Code[ucSeg_Pos];
LCK = 0;
for(i=0;i<12;i++)
{
SCK = 0;
if(temp & 0x800)
DAT = 1;
else
DAT = 0;
temp <<= 1;
SCK = 1; //移位
}
LCK = 1; //锁存
OE = 0;
}
#include <reg51.h>
#include <STDIO.H>
#include "seg.h"
//sbit P04=P0^4;
//sbit P05=P0^5;
unsigned char ucSec = 0,ucMin =0;
unsigned char pucSeg_Buf[6], pucSeg_Code[4], ucSeg_Pos = 0;
unsigned int uims = 0, uiSeg_Dly = 0;
// T1初始化
void T1_Init (void) // 1毫秒@12.000MHz
{
TL1=0x18;
TH1=0xFC; //设置定时初值高8位
TR1=1; // T1开始计时
}
// T1 处理
void T1_Proc(void)
{
if(!TF1) //定时时间未到
return;
TF1 = 0; //清除时间到标志
if(++uims == 1000) // 1s到
{
uims = 0;
if(++ucSec == 60) // 1min到
ucSec =0;
}
if((uims&3) == 0) //4ma显示1位
{
Seg_Disp(pucSeg_Code, ucSeg_Pos);
if (++ucSeg_Pos == 4) //循环显示
ucSeg_Pos = 0;
}
if(++uiSeg_Dly == 500) //500ms 到
uiSeg_Dly = 0;
}
//数码管处理(500ms刷新-次)
void Seg_Proc (void)
{
if(uiSeg_Dly != 0)
return;
uiSeg_Dly = 1; //500ms 到
sprintf(pucSeg_Buf,"%02u%02u",\
(unsigned int)ucMin,(unsigned int)ucSec);
Seg_Tran (pucSeg_Buf, pucSeg_Code);
}
//主函数
void main (void)
{
T1_Init();
while(1)
{
T1_Proc();
Seg_Proc();
}
}
|