#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char //声明无符号字符变量,就是可以用uchar来代替unsigned char 例如uchar i;效果就和unsigned char i;一样
#define uint unsigned int //声明无符号字符变量,就是可以用uint来代替unsigned int 例如uint i;效果就和unsigned int i;一样
#define MAIN_Fosc 11059200UL //定义主时钟
#define Timer0_Reload (MAIN_Fosc / 12000)
unsigned char code t_display[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf};//段码//uchar code t_display[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uint code T_COM[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; //位码
sbit P_HC595_DIO = P1^0; //pin 14 SER DIO data input
sbit P_HC595_RCLK = P1^1; //pin 12 RCLk store (latch) clock
sbit P_HC595_SCLK = P1^2; //pin 11 SCLK Shift data clock
uchar LED8[8]; //显示缓冲
uchar display_index; //显示位索引
bit B_1ms; //1ms标志
void main(void)
{
uchar i,k; //用uchar代替unsigned char
uint j; //用uint代替unsigned int
TMOD = 0x01; //定时器0配置16位定时器,12t
TH0 = (65536 - Timer0_Reload) % 256;
TL0 = (65536 - Timer0_Reload) / 256;
ET0 = 1;
TR0 = 1;
EA = 1;
for(i=0; i<8; i++) LED8[i] = 0x10; //上电消隐
j = 0;
k = 0;
while(1)
{
while(!B_1ms) ; //等待1ms到
B_1ms = 0;
if(++j >= 500) //500ms到
{
j = 0;
for(i=0; i<8; i++) LED8[i] = k; //刷新显示
if(++k > 0x10) k = 1; //8个数码管循环显示0,1,2...,A,B..F,消隐.
}
}
}
void Send_595(unsigned char dat) //发送一个字节
{
unsigned char i;
for(i=0; i<8; i++)
{
if(dat & 0x80) P_HC595_DIO = 1;
else P_HC595_DIO = 0;
P_HC595_SCLK = 1;
P_HC595_SCLK = 0;
dat = dat << 1;
}
}
void DisplayScan(void) //显示扫描函数
{
Send_595(T_COM[display_index]); //共阳极输出位码 //Send_595(~T_COM[display_index]); //共阴极输出位码
Send_595(~t_display[LED8[display_index]]); //共阳极输出段码 //Send_595(t_display[LED8[display_index]]); //共阴极输出段码
P_HC595_RCLK = 1;
P_HC595_RCLK = 0; //锁存输出数据
if(++display_index >= 8) display_index = 0; //8位结束回0
}
void timer0 (void) interrupt 1 //Timer0 1ms中断函数
{
TH0 = (65536 - Timer0_Reload) / 256; //重装定时值
TL0 = (65536 - Timer0_Reload) % 256;
DisplayScan(); //1ms扫描显示一位
B_1ms = 1; //1ms标志
}
/*********************************************
uint temp;
//延时子程序
void delay(int ms)
{
uint i,k;
for(i=ms;i>0;i--)
for(k=1;k>0;k--);
}
//将显示数据送入74HC595内部移位寄存器
void P_HC595_in(void)
{
uchar j;
for(j=0;j<16;j++)
{
temp=temp<<1;
P_HC595_DIO=1;//pin 14
P_HC595_SCLK=1;//上升沿发生位移 pin 11
_nop_();
_nop_();
P_HC595_SCLK=0;//pin 11
}
}
//将移位寄存器内的数据锁存到输出寄存器并显示
void P_HC595_OUT(void)
{
P_HC595_RCLK=0;//pin 12
_nop_();
_nop_();
P_HC595_RCLK=1;//上升沿将数据送到输出锁存器 pin 12
_nop_();
_nop_();
_nop_();
P_HC595_RCLK=0;//pin 12
}
//主程序
void main(void)
{
P_HC595_SCLK=0;//pin 11
P_HC595_RCLK=1;//pin 12
while(1)
{
uchar i;
for(i=0;i<8;i++)
{
temp=T_COM[i]
P_HC595_IN();
p_HC595_OUT();
delay(20);
}
}
}
*/
/*
//秒表基时计数
if(++count1==5)
{
count1=0;
if(++ms1==100)
{
ms1=0;
if(++sec1==60)
{
sec1=0;
fi(++min1==60)
{
min1=0;
}
}
}
}
*/
|