📄 bl37.c
字号:
#include "reg52.h"
#include "INTRINS.H"
sbit R372A_SCL = P2^4;
sbit R372A_SDA = P2^5;
sbit CS = 0xa0;
sbit Clk = 0xa1;
sbit DO1 = 0xa2;
sbit DI = 0xa3;
unsigned char time12[] = { 0x12,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x11,
0x32,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x30,0x31};
unsigned char time24[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x11,
0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x20,0x21,0x22,0x23};
void Show_XW_LCD(unsigned char xPos,unsigned char yPos,unsigned char sData);
void LCD_Init( void );
void Print_LCD ( unsigned char xPos, unsigned char yPos,int nSentenceID );
void Clear_H_LCD(unsigned char H);
void Delay_xMs(unsigned int xMs);
unsigned char T2S(unsigned char dat);
unsigned char A_D(bit CH);
/*
{
unsigned int i,j;
for(i=0;i < xMs;i++)
{
for(j=0;j<500;j++);
}
}*/
/*----------------------------------------------------------------------------*/
void Start_Cond()
{
R372A_SCL = 0;
_nop_();_nop_();_nop_();_nop_();_nop_();
R372A_SDA = 1;
_nop_();_nop_();_nop_();_nop_();_nop_();
R372A_SCL = 1;
_nop_();_nop_();_nop_();_nop_();_nop_();
R372A_SDA = 0;
_nop_();_nop_();_nop_();_nop_();_nop_();
}
/*----------------------------------------------------------------------------*/
void Stop_Cond()
{
R372A_SCL = 0;
_nop_();_nop_();_nop_();_nop_();_nop_();
R372A_SDA = 0;
_nop_();_nop_();_nop_();_nop_();_nop_();
R372A_SCL = 1;
_nop_();_nop_();_nop_();_nop_();_nop_();
R372A_SDA = 1;
_nop_();_nop_();_nop_();_nop_();_nop_();
}
/*----------------------------------------------------------------------------*/
void Ack()
{
R372A_SCL = 0;
_nop_();_nop_();_nop_();_nop_();_nop_();
R372A_SDA = 0;
_nop_();_nop_();_nop_();_nop_();_nop_();
R372A_SCL = 1;
_nop_();_nop_();_nop_();_nop_();_nop_();
R372A_SCL = 0;
_nop_();_nop_();_nop_();_nop_();_nop_();
R372A_SDA = 1;
_nop_();_nop_();_nop_();_nop_();_nop_();
}
/*----------------------------------------------------------------------------*/
void NoAck()
{
R372A_SCL = 0;
_nop_();_nop_();_nop_();_nop_();_nop_();
R372A_SDA = 1;
_nop_();_nop_();_nop_();_nop_();_nop_();
R372A_SCL = 1;
_nop_();_nop_();_nop_();_nop_();_nop_();
R372A_SCL = 0;
_nop_();_nop_();_nop_();_nop_();_nop_();
}
/*----------------------------------------------------------------------------*/
bit Write8Bit(unsigned char input) {
unsigned char i;
R372A_SCL = 0;
for (i=0;i<8;i++)
{
input <<= 1;
R372A_SDA = CY;
R372A_SCL = 1;
_nop_();_nop_();_nop_();_nop_();_nop_();
R372A_SCL = 0;
_nop_();_nop_();_nop_();_nop_();_nop_();
}
R372A_SCL = 0;
_nop_();_nop_();_nop_();_nop_();_nop_();
R372A_SDA = 1;
_nop_();_nop_();_nop_();_nop_();_nop_();
R372A_SCL = 1;
_nop_();_nop_();_nop_();_nop_();_nop_();
CY = R372A_SDA;
return(CY) ;
}
/*----------------------------------------------------------------------------*/
unsigned char Read8Bit()
{
unsigned char temp,rbyte=0;
for (temp = 8;temp != 0;temp--)
{
R372A_SCL = 0;
_nop_();_nop_();
rbyte = (rbyte << 1) | R372A_SDA;
R372A_SCL = 1;
_nop_();_nop_();
}
return rbyte;
}
/*----------------------------------------------------------------------------*/
bit Write_Byte_R372A(unsigned char nRegister,unsigned char nCommand)
{
Start_Cond();
Write8Bit(0x64);
Write8Bit(nRegister << 4);
Write8Bit(nCommand);
Stop_Cond();
_nop_();
Stop_Cond();
return(CY);
}
/*----------------------------------------------------------------------------*/
bit Init_R372A (void)
{
unsigned char i,Temp;
bit Error_Flag = 0;
for (i = 0;i < 255;i++)
{
Start_Cond();
if (Write8Bit(0x64)) /*写Slave address 写模式 MSB 0 1 1 0 0 1 0 R/W LSB*/
{
Error_Flag = 1 ;
Delay_xMs(10);
continue;
}
else
{
Write8Bit(0xf4);
Temp = Read8Bit();
Ack();//NoAck(); //BL372A Ack(); RICOH372A NoAck();
if (Temp == 0xFF)
{
Error_Flag = 1;
continue;
}
break;
}
}
Delay_xMs(25);
Write_Byte_R372A(0x0F,0x0C); /*24 Hours System,ADJ=0,CLEN=1,CTFG=0*/
Write_Byte_R372A(0x07,0x00); /*XSL=0 Use 32.768KHz,no time trimming*/
Write_Byte_R372A(0x0E,0x00); /*Control Register 1 ALE=BLE=0 SL2=SL1=0 CT2=CT1=CT0=0*/
Stop_Cond();
return(Error_Flag);
}
/*----------------------------------------------------------------------------*/
bit Read_R372A (unsigned char *nContent)
{
unsigned char i;
Start_Cond();
_nop_();
Write8Bit(0x64); /*写Slave address 写模式 MSB 0 1 1 0 0 1 0 R/W LSB*/
Write8Bit(0x04); //04 /*从0寄存器开始读,采用方式第2种*/
for( i = 0;i < 6;i++ )
{
nContent[i] = Read8Bit();
Ack();
}
nContent[6] = Read8Bit();
NoAck();
Stop_Cond();
nContent[3] = nContent[4];
nContent[4] = nContent[5];
nContent[5] = nContent[6];
for(i=0;i<24;i++)
{
if ( nContent[2] == time12[i] )
break;
}
nContent[2] = time24[i];
if ( (nContent[5] & 0x0f ) > 0x09 || ( nContent[5] & 0xf0 ) > 0x90
|| ( nContent[4] > 0x12 )
|| ( nContent[3] > 0x31 ) )
return(1);
if (nContent[3] == 0xff || nContent[4] == 0xff || nContent[4] == 0 || nContent[3] == 0 )
return(1);
return(0);
}
/*----------------------------------------------------------------------------*/
bit Write_R372A (unsigned char *nContent)
{
unsigned char i;
bit Error_Flag = 0;
// nContent[2] = time[nContent[2]];
for(i=0;i<24;i++)
{
if ( nContent[2] == time24[i] )
break;
}
nContent[2] = time12[i];
Start_Cond();
Write8Bit(0x64); /*写Slave address 写模式 MSB 0 1 1 0 0 1 0 R/W LSB*/
Write8Bit(0x00); /*从0寄存器开始写,采用方式第1种*/
for (i = 0; i < 7; i++)
{
if (i == 3)
{
Error_Flag = Write8Bit(1); /*将Week寄存器写入1 不用*/
}
else
{
Error_Flag = Write8Bit(*nContent);
nContent++;
}
}
Stop_Cond();
return(Error_Flag); /*返回0正确,返回1错误*/
}
void main()
{
unsigned char CH0_Data,Show_Data;
unsigned char y = 0,Temp[7],Data1[] = {0x00,0x00,0x12,0x16,0x05,0x03};
unsigned char hour,min,sec;
Init_R372A();
Write_R372A(Data1);
LCD_Init(); //液晶初始化?
Clear_H_LCD(0);
Clear_H_LCD(1);
Print_LCD(5,0,0); //显示★电子工作室★
Print_LCD(0,2,1); //显示现在是: : :
while(1)
{
Read_R372A(Temp);
hour = Temp[2];
min = Temp[1];
sec = Temp[0];
Show_XW_LCD(56,2,(hour>>4));
Show_XW_LCD(64,2,(hour&0x0f));
Show_XW_LCD(80,2,(min>>4));
Show_XW_LCD(88,2,(min&0x0f));
Show_XW_LCD(104,2,(sec>>4));
Show_XW_LCD(112,2,(sec&0x0f));
//Delay_xMs(300);
CH0_Data = A_D(0); //AD采样
Show_Data = T2S(CH0_Data); //温度转换成显示码
Show_XW_LCD(84,0,(Show_Data>>4)); //显示温度十位
Show_XW_LCD(92,0,(Show_Data&0x0f)); //显示温度个位
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -