1. 用C语言编程,利用定时器产生一个0~99秒变化的秒表,并且显示在数码管上,每过一秒将这个变化写入实验板上AT24C02,当关闭实验板电源,并再次打开实验板电源时,单片机从AT24C02中将原来写入的数据读出来,接着继续变化在数码管上。
#include
#define uchar unsigned char
#define uint unsigned int
//=========全局变量区============================================
bit write=0; //写24C02的标志;
sbit sda=P2^0;
sbit scl=P2^1;
sbit high=P2^4;
sbit mid=P2^3;
sbit low=P2^2;
uint code NumTable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//数字的编码
uchar sec,tcnt;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71
};
//=========全局变量区结束============================================
/**
步骤
接线方式:
1、P0接数码管J12,实现段选
2、译码器和数码管位选输入短接,J15+J16
3、本实验使用的晶振是12.000
**/
//========函数区============================================
void delay()
{;;}
void delay1ms(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
/**
延时
**/
void delay_1ms(uint x){
uint i=x;
uint j;
for(;i>0;--i){
for(j=110;j>0;--j);
}
}
/**
在数码管上显示对应的值
**/
void display(uchar Num)
{
P0=NumTable[Num];
delay_1ms(1);
P0=0; //送完段选信号后,进行消影的处理
}
/**
控制数码管显示后3位,并分解计数值
**/
void DisplayNumByOrder(uint Value){
low=0; mid=0; high=0; display(0);
low=1; mid=0; high=0; display(0);
low=0; mid=1; high=0; display(0);
low=1; mid=1; high=0; display(0);
low=0; mid=0; high=1; display(0);
low=1; mid=0; high=1; display(Value%1000/100);
low=0; mid=1; high=1; display(Value%100/10);
low=1; mid=1; high=1; display(Value%10);
}
void start() //开始信号
{
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
}
void stop() //停止
{
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
}
void respons() //应答
{
uchar i;
scl=1;
delay();
while((sda==1)&&(i<250))i++;
scl=0;
delay();
}
void write_byte(uchar date)
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1;
scl=0;
delay();
sda=CY;
delay();
scl=1;
delay();
}
scl=0;
delay();
sda=1;
delay();
}
uchar read_byte()
{
uchar i,k;
scl=0;
delay();
sda=1;
delay();
for(i=0;i<8;i++)
{
scl=1;
delay();
k=(k<<1)|sda;
scl=0;
delay();
}
return k;
}
void write_address(uchar address,uchar date)
{
start();
write_byte(0xa0);
respons();
write_byte(address);
respons();
write_byte(date);
respons();
stop();
}
uchar read_address(uchar address)
{
uchar date;
start();
write_byte(0xa0);
respons();
write_byte(address);
respons();
start();
write_byte(0xa1);
respons();
date=read_byte();
stop();
return date;
}
void init()
{
sda=1;
delay();
scl=1;
delay();
sec=read_address(2); //读出保存的数据赋于sec
if(sec>100) //防止首次读取出错误数据
sec=0;
TMOD=0x01; //定时器工作在方式1
ET0=1;
EA=1;
TH0=(65536-50000)/256;//对TH0 TL0赋值
TL0=(65536-50000)%256;//使定时器0.05秒中断一次
TR0=1; //开始计时
}
void main()
{
init();//初始化
while(1)
{
DisplayNumByOrder(sec);
if(write==1) //判断计时器是否计时一秒
{
write=0; //清零
write_address(2,sec); //在24c02的地址2中写入数据sec
}
}
}
//========函数区结束============================================
//========中断函数区============================================
void t0() interrupt 1//定时中断服务函数
{
TH0=(65536-50000)/256;//对TH0 TL0赋值
TL0=(65536-50000)%256;//重装计数初值
tcnt++; //每过50ms tcnt加一
if(tcnt==20) //计满20次(1秒)时
{
tcnt=0; //重新再计
sec++;
write=1; //1秒写一次24C02
if(sec==100)//定时100秒,再从零开始计时
sec=0;
}
}
//========中断函数区结束============================================
上一篇:实验3 串口通信
下一篇:实验5 IIC通讯与AD/接DA接口
推荐阅读最新更新时间:2024-03-16 15:29
- 热门资源推荐
- 热门放大器推荐