给你把文件合并在一起,能通过编译,文件中有两处错误。
错误u8 shi,fen,miao,a[]; //全局变量
正确u8 shi,fen,miao,a[8]; //全局变量
错误if(SDA = 1)
正确if(SDA == 1)
- #include "stc15.h"
- #include <intrins.h>
- typedef unsigned char u8;
- typedef unsigned int u16;
- #define SCK_CLR SCK = 0;
- #define SCK_SET SCK = 1;
- #define RST_CLR RST = 0;
- #define RST_SET RST = 1;
- #define SDA_CLR SDA = 0;
- #define SDA_SET SDA = 1;
- void Timer0Init(void);
- void num_display();
- void Refresh_time();
- void Delay2ms();
- void DS1302_Write_Byte(u8 dat);
- void DS1302_Write(u8 addr,u8 dat);
- unsigned char DS1302_Read_Byte(void);
- unsigned char DS1302_Read(u8 addr);
- void DS1302_Init(u8 shi,u8 fen,u8 miao);
- sbit SCK = P1^7; //时钟芯片引脚
- sbit RST = P1^3;
- sbit SDA = P2^3;
- u8 shi,fen,miao,a[8]; //全局变量
- bit time_flag;
- u8 code table[] =
- { //共阳极编码表
- 0xc0,0xf9,0xa4,0xb0,
- //0, 1, 2, 3
- 0x99,0x92,0x82,0xf8,
- //4, 5, 6, 7
- 0x80,0x90,0xff,0xbf
- //8, 9, 空,-,
- };
- u8 code order[] =
- { //数码管位码
- 0x01,0x02,0x04,0x08,
- 0x10,0x20,0x40,0x80
- };
- void Delay2ms() //@11.0592MHz 2ms延时
- {
- unsigned char i, j;
- _nop_();
- _nop_();
- i = 22;
- j = 128;
- do
- {
- while (--j);
- } while (--i);
- }
- void Refresh_time() //数码管显示时间
- {
- if(time_flag == 1)
- {
- time_flag = 0;
- shi = DS1302_Read(0x85);
- fen = DS1302_Read(0x83);
- miao = DS1302_Read(0x81);
- }
- a[0] = shi/16;
- a[1] = shi%16;
- a[2] = 11;
- a[3] = fen/16;
- a[4] = fen%16;
- a[5] = 11;
- a[6] = miao/16;
- a[7] = miao%16;
- }
- void num_display() //数码管显示
- {
- static u8 i=0;
- P0 = 0x00;
- P2 = (P2 & 0x1f) | 0xc0;
- P2 = (P2 & 0x1f);
- P0 = table[a[i]];
- P2 = (P2 & 0x1f) | 0xe0;
- P2 = (P2 & 0x1f);
- P0 = order[i];
- P2 = (P2 & 0x1f) | 0xc0;
- P2 = (P2 & 0x1f);
- i++;
- i &= 0x07;
- Delay2ms();
- }
- void Timer0Init(void) //2毫秒@11.0592MHz
- {
- AUXR |= 0x80; //定时器时钟1T模式
- TMOD &= 0xF0; //设置定时器模式
- TL0 = 0x9A; //设置定时初值
- TH0 = 0xA9; //设置定时初值
- TF0 = 0; //清除TF0标志
- TR0 = 1;
- ET0 = 1;
- EA = 1; //定时器0开始计时
- }
- void main()
- {
- Timer0Init();
- DS1302_Init(12,00,00);
- while(1)
- {
- Refresh_time();
- num_display();
- }
- }
- void T0_time() interrupt 1
- {
- static u8 time_count = 0;
- num_display();
- if(++time_count >= 200)
- {
- time_count = 0;
- time_flag = 1;
- }
- }
- void DS1302_Write_Byte(u8 dat) //单字节写入一字节数据
- {
- u8 i;
- SCK_CLR; //SCLK管脚置低,初始化DS1302
- for(i = 0;i < 8;i++)
- {
- if(dat & 0x01)
- {
- SCK_SET; //I/O口置高电平
- }
- else
- {
- SCK_CLR; //I/O口置低电平
- }
- SCK_SET; //SCLK上升沿,读取数据
- SCK_CLR; //拉低SCLK,传输下一位数据
- dat = dat >> 1;
- }
- }
- void DS1302_Write(u8 addr,u8 dat) //向 DS1302 单字节写入一字节数据
- {
- RST_CLR; //RST管脚置低,初始化DS1302
- SCK_CLR; //SCLK管脚置低,初始化DS1302
- RST_SET; //启动DS1302总线,RST置高电平
- addr &= 0xfe; //最低位置零,保证稳定环境
- DS1302_Write_Byte(addr); //写入目标地址
- DS1302_Write_Byte(dat); //写入数据
- RST_CLR; //停止DS1302总线
- SCK_CLR;
- }
- unsigned char DS1302_Read_Byte(void)//单字节读一字节数据
- {
- u8 i,dat = 0;
- for(i = 0;i < 8;i++)
- {
- dat = dat >> 1;
- if(SDA == 1)
- {
- dat |= 0x80;
- }
- else
- {
- dat &= 0x7f;
- }
- SCK_SET; //SCLK下降沿,锁存数据
- SCK_CLR;
- }
- return dat;
- }
- unsigned char DS1302_Read(u8 addr) //从 DS1302 单字节读出一字节数据
- {
- u8 temp;
- RST_CLR;
- SCK_CLR;
- RST_SET;
- addr |= 0x01; //最低位置高,保证是读操作
- DS1302_Write_Byte(addr);
- temp = DS1302_Read_Byte(); //从 DS1302 单字节读出一字节数据
- RST_CLR;
- SDA_CLR;
- return temp;
- }
- void DS1302_Init(u8 shi,u8 fen,u8 miao) //时钟初始化
- {
- DS1302_Write(0x8e,0x00); //关闭写保护
- DS1302_Write(0x80,((miao/10)<<4)|(miao%10)); //分别写秒分时地址
- DS1302_Write(0x82,((fen/10)<<4)|(fen%10));
- DS1302_Write(0x84,((shi/10)<<4)|(shi%10));
- DS1302_Write(0x8e,0x80); //开启写保护
- }
复制代码 |