12M时钟,不过每次读出来的数据都不对 ,希望有人指导下小弟
#define _MAIN_C
#include "all.h"
#include "Lcd1602.h"
#include "DS3232.h"
#include "main.h"
bit flag1s = 0;
unsigned char T0RH = 0;
unsigned char T0RL = 0;
void main()
{
InitLcd1602();
ConfigTimer0(1);
while(!flag1s);
flag1s = 0;
DS3232_Write_Reg(0x00,0x00);
while(1)
{
if(flag1s)
{
flag1s = 0;
Refresh_Time_Show();
}
}
}
void ConfigTimer0(unsigned int ms)
{
unsigned long temp = 0;
temp = 11059200 / 12;
temp = (temp * ms) / 1000;
temp = 65536 - temp;
T0RH = (unsigned char)(temp >> 8);
T0RL = temp;
TMOD = (TMOD & 0xF0) | 0x01;
ET0 = 1;
TR0 = 1;
EA = 1;
}
void Refresh_Time_Show()
{
uint8 time;
uint8 str[5];
time = DS3231_Read_Reg(0x00);
str[0] = (time >> 4) + '0';
str[1] = (time & 0x0F) + '0';
str[2] = '\0';
LcdShowStr(0,0,str);
}
void Timer0Interrupt() interrupt 1
{
static unsigned int timer1s = 0;
TH0 = T0RH;
TL0 = T0RL;
timer1s++;
if(timer1s >= 1000)
{
timer1s = 0;
flag1s = 1;
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------
#define _LCD1602_C
#include "all.h"
#include "Lcd1602.h"
void LcdReady()
{
unsigned char sta;
LCD1602_DB = 0xFF;
LCD1602_RS = 0;
LCD1602_RW = 1;
do
{
LCD1602_E = 1;
sta = LCD1602_DB;
LCD1602_E = 0;
} while(sta & 0x80);
}
/* 写命令 */
void LcdWriteCmd(unsigned char cmd)
{
LcdReady();
LCD1602_RS = 0;
LCD1602_RW = 0;
LCD1602_DB = cmd;
LCD1602_E = 1;
LCD1602_E = 0;
}
/* 写数据 */
void LcdWriteData(unsigned char dat)
{
LcdReady();
LCD1602_RS = 1;
LCD1602_RW = 0;
LCD1602_DB = dat;
LCD1602_E = 1;
LCD1602_E = 0;
}
/* 设置RAM起始地址,光标位置 */
void LcdSetAddr(unsigned char x,unsigned char y)
{
unsigned char addr;
if(y == 0)
addr = 0x00 + x;
else
addr = 0x40 + x;
LcdWriteCmd(addr | 0x80);
}
/* 在液晶显示字符串,(x,y)起始坐标,str字符串指针 */
void LcdShowStr(unsigned char x,unsigned y,unsigned char * str)
{
LcdSetAddr(x,y);
while(*str != '\0')
{
LcdWriteData(*str++);
}
}
/* 初始化液晶 */
void InitLcd1602()
{
LcdWriteCmd(0x38); // 16*2显示,5*7点阵,8位数据接口
LcdWriteCmd(0x0C); // 显示器开,光标关闭
LcdWriteCmd(0x06); // 文字不动,地址自动加1
LcdWriteCmd(0x01); // 清屏
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#define _IIC_C
#include "all.h"
#include "IIC.h"
void IIC_Delay()
{
_nop_();_nop_();_nop_();_nop_();_nop_();
}
void IIC_Start()
{
IIC_SCL = 1;
IIC_SDA = 1;
IIC_Delay();
IIC_SDA = 0;
IIC_Delay();
IIC_SCL = 0;
}
void IIC_Stop()
{
IIC_SCL = 0;
IIC_SDA = 0;
IIC_Delay();
IIC_SCL = 1;
IIC_Delay();
IIC_SDA = 1;
IIC_Delay();
}
void IIC_Write_Byte(uint8 dat)
{
uint8 mask;
for(mask = 0x80;mask != 0;mask >>= 1)
{
if((mask & dat) != 0)
IIC_SDA = 1;
else
IIC_SDA = 0;
IIC_Delay();
IIC_SCL = 1;
IIC_Delay();
IIC_SCL = 0;
}
IIC_SDA = 1;
IIC_Delay();
}
bit IIC_Read_Ack()
{
bit ack = 1;
uint8 i = 0;
IIC_SCL = 1;
IIC_Delay();
while((ack) && (i < 4))
{
ack = IIC_SDA;
i++;
}
IIC_SCL = 0;
return ack;
}
uint8 IIC_Read_Byte()
{
uint8 mask;
uint8 dat;
IIC_SDA = 1;
for(mask != 0x80;mask != 0;mask >>= 1)
{
IIC_Delay();
IIC_SCL = 1;
if(IIC_SDA != 0)
dat |= mask;
else
dat &= ~mask;
IIC_Delay();
IIC_SCL = 0;
}
return dat;
}
/* 单片机发送应答信号 ACK = 0; NACK = 1 */
void IIC_Write_Ack(bit ack)
{
IIC_SDA = ack;
IIC_Delay();
IIC_SCL = 1;
IIC_Delay();
IIC_SCL = 0;
IIC_Delay();
IIC_SDA = 1; // 释放IIC_SDA
IIC_Delay();
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------
#define _DS3232_C
#include "all.h"
#include "IIC.h"
#include "DS3232.h"
void DS3232_Write_Reg(uint8 addr,uint8 dat)
{
IIC_Start();
IIC_Write_Byte(0x68 << 1);
IIC_Read_Ack();
IIC_Write_Byte(addr);
IIC_Read_Ack();
IIC_Write_Byte(dat);
IIC_Read_Ack();
IIC_Stop();
}
uint8 DS3231_Read_Reg(uint8 addr)
{
uint8 dat;
IIC_Start();
IIC_Write_Byte(0x68 << 1);
IIC_Read_Ack();
IIC_Write_Byte(addr);
IIC_Read_Ack();
IIC_Start();
IIC_Write_Byte((0x68 << 1) | 0x01);
IIC_Read_Ack();
dat = IIC_Read_Byte();
IIC_Write_Ack(1);
IIC_Stop();
return dat;
}
|