📄 khts52.c
字号:
if ( I2C_SDA == 0) return 0;
if ( I2C_SCK == 0) return 0;
I2C_SDA = 0;
Delay_10_uS();
I2C_SCK = 0;
Delay_10_uS();
return 1;
}
void I2C_Stop(void)
{
Delay_10_uS();
I2C_SDA = 0;
Delay_10_uS();
I2C_SCK = 1;
Delay_10_uS();
I2C_SDA = 1;
Delay_10_uS();
}
void I2C_Nack(void)
{
Delay_10_uS();
I2C_SDA=1;
Delay_10_uS();
I2C_SCK=1;
Delay_10_uS();
I2C_SCK=0;
Delay_10_uS();
}
/********************************************************************
函 数 名:I2C_Send_Byte()
功 能:EEPROM写入一字节
说 明:往AT24C64写入1Byte数据 (内部函数)
入口参数:d 写入的数据
返 回 值:0:写入成功,1:写入失败。
设 计:周志江 日 期: 07-10-10
修 改: 日 期:
***********************************************************************/
bit I2C_Send_Byte( unsigned char d)
{
unsigned char i = 8;
bit bit_ack;
while( i-- ){
Delay_10_uS();
if ( d & 0x80 )I2C_SDA =1; else I2C_SDA =0;
Delay_10_uS();
I2C_SCK = 1;
Delay_10_uS();
I2C_SCK = 0;
d = d << 1;
}
Delay_10_uS();
I2C_SDA = 1;
Delay_10_uS();
I2C_SCK = 1;
Delay_10_uS();
bit_ack = I2C_SDA;
I2C_SCK =0;
Delay_10_uS();
return bit_ack;
}
/********************************************************************
函 数 名:I2C_Receive_Byte()
功 能:EEPROM读出一字节
说 明:从AT24C64读出1Byte数据 (内部函数)
入口参数:无
返 回 值:读出1Byte数据
设 计:周志江 日 期: 07-10-10
修 改: 日 期:
***********************************************************************/
unsigned char I2C_Receive_Byte(void)
{
unsigned char i = 8, d;
Delay_10_uS();
I2C_SDA = 1;
while ( i--){
d = d << 1;
Delay_10_uS();
I2C_SCK =1;
if( I2C_SDA )d++;
Delay_10_uS();
I2C_SCK =0;
}
return d;
}
/********************************************************************
函 数 名:AT24C64_W()
功 能:EEPROM写入count个字节
说 明:往AT24C64写入count Byte数据
入口参数:数据的地址*mcu_address
写入的地址 AT24C64_address
数据的个数count
返 回 值:无。
设 计:周志江 日 期: 07-10-10
修 改: 日 期:
***********************************************************************/
void AT24C64_W(void *mcu_address,unsigned int AT24C64_address,unsigned int count)
{
I2C_WP = 0;
while(count--){
I2C_Start();
/*I2C_Send_Byte( 0xa0 + AT24C64_address /256 *2);*/ /* 24C16 USE */
I2C_Send_Byte( 0xa0 );
I2C_Send_Byte( AT24C64_address/256 );
I2C_Send_Byte( AT24C64_address %256 );
I2C_Send_Byte( *(unsigned char*)mcu_address );
I2C_Stop();
Delay_N_mS(10); /* waiting for write cycle to be completed */
((unsigned char*)mcu_address)++;
AT24C64_address++;
}
I2C_WP = 1;
}
/********************************************************************
函 数 名:AT24C64_W_Byte()
功 能:EEPROM写入1个字节
说 明:往AT24C64写入1 Byte数据
入口参数:数据 Data
写入的地址 AT24C64_address
返 回 值:无。
设 计:周志江 日 期: 07-10-10
修 改: 日 期:
***********************************************************************/
void AT24C64_W_Byte(unsigned int AT24C64_address,unsigned char Data)
{
I2C_WP = 0;
I2C_Start();
I2C_Send_Byte( 0xa0 );
I2C_Send_Byte( AT24C64_address/256 );
I2C_Send_Byte( AT24C64_address %256 );
while(I2C_Send_Byte(Data)){}
I2C_Stop();
Delay_N_mS(20); /* waiting for write cycle to be completed */
I2C_WP = 1;
}
/********************************************************************
函 数 名:AT24C64_R()
功 能:EEPROM读出count个字节
说 明:往AT24C64读出count Byte数据
入口参数:数据的地址*mcu_address
读出的地址 AT24C64_address
数据的个数count
返 回 值:无。
设 计:周志江 日 期: 07-10-10
修 改: 日 期:
***********************************************************************/
void AT24C64_R(void *mcu_address,unsigned int AT24C64_address,unsigned int count)
{
//DOG_WDI=!DOG_WDI;
//DOGTIME=0;
while(count--){
I2C_Start();
/*I2C_Send_Byte( 0xa0 + AT24C64_address / 256 *2 );*/ /* 24C16 USE */
I2C_Send_Byte( 0xa0 );
I2C_Send_Byte( AT24C64_address/256 );
I2C_Send_Byte( AT24C64_address % 256 );
I2C_Start();
/*I2C_Send_Byte( 0xa1 + AT24C64_address /256 *2 );*/
I2C_Send_Byte( 0xa1 );
*(unsigned char*)mcu_address = I2C_Receive_Byte();
I2C_Nack();
I2C_Stop();
((unsigned char*)mcu_address)++;
AT24C64_address++;
}
}
/********************************************************************
函 数 名:AT24C64_R_Byte()
功 能:EEPROM读出1个字节
说 明:AT24C64读出1 Byte数据
入口参数:读出的地址 AT24C64_address
返 回 值:1 Byte数据 。
设 计:周志江 日 期: 07-10-10
修 改: 日 期:
***********************************************************************/
unsigned char AT24C64_R_Byte(unsigned int AT24C64_address)
{
unsigned char temp;
I2C_Start();
I2C_Send_Byte( 0xa0 );
I2C_Send_Byte( AT24C64_address/256 );
I2C_Send_Byte( AT24C64_address % 256 );
I2C_Start();
I2C_Send_Byte( 0xa1 );
temp = I2C_Receive_Byte();
I2C_Nack();
I2C_Stop();
return (temp);
}
/*------------------------------------------------------------------*-
---- END OF AT24C64 Mode -------------------------------------------------
-*------------------------------------------------------------------*/
/****************************************************************
模块名称:DS1302.c
功 能:实时时钟模块 时钟芯片型号:DS1302
*********************************************************************/
/********************************************************************
函 数 名:RTInputByte()
功 能:实时时钟写入一字节
说 明:往DS1302写入1Byte数据 (内部函数)
入口参数:d 写入的数据
返 回 值:无
设 计:周志江 日 期: 07-10-10
修 改: 日 期:
***********************************************************************/
void RTInputByte(unsigned char d)
{
unsigned char i;
ACC = d;
for(i=8; i>0; i--)
{
I2C_SDT = ACC0; /*相当于汇编中的 RRC */
I2C_SCK = 1;
I2C_SCK = 0;
ACC = ACC >> 1;
}
}
/********************************************************************
函 数 名:RTOutputByte()
功 能:实时时钟读取一字节
说 明:从DS1302读取1Byte数据 (内部函数)
入口参数:无
返 回 值:ACC
设 计:周志江 日 期: 07-10-10
修 改: 日 期:
*********************************************************************/
unsigned char RTOutputByte(void)
{
unsigned char i;
for(i=8; i>0; i--){
ACC = ACC >>1; /*相当于汇编中的 RRC */
ACC7 = I2C_SDT;
I2C_SCK = 1;
I2C_SCK = 0;
}
return(ACC);
}
/********************************************************************
函 数 名:W1302()
功 能:往DS1302写入数据
说 明:先写地址,后写命令/数据 (内部函数)
调 用:RTInputByte()
入口参数:ucAddr: DS1302地址, ucData: 要写的数据
返 回 值:无
设 计:周志江 日 期: 07-10-10
修 改: 日 期:
*********************************************************************/
void W1302(unsigned char ucAddr, unsigned char ucDa)
{
I2C_WP = 0;
I2C_SCK = 0;
I2C_WP = 1;
RTInputByte(ucAddr); /* 地址,命令 */
RTInputByte(ucDa); /* 写1Byte数据*/
I2C_SCK = 1;
I2C_WP = 0;
}
/********************************************************************
函 数 名:R1302()
功 能:读取DS1302某地址的数据
说 明:先写地址,后读命令/数据 (内部函数)
调 用:RTInputByte() , RTOutputByte()
入口参数:ucAddr: DS1302地址
返 回 值:ucData :读取的数据
设 计:周志江 日 期: 07-10-10
修 改: 日 期:
*********************************************************************/
unsigned char R1302(unsigned char ucAddr)
{
unsigned char ucData;
I2C_WP = 0;
I2C_SCK = 0;
I2C_WP = 1;
RTInputByte(ucAddr); /* 地址,命令 */
ucData = RTOutputByte(); /* 读1Byte数据 */
I2C_SCK = 1;
I2C_WP = 0;
return(ucData);
}
/********************************************************************
函 数 名:Set1302()
功 能:设置初始时间
说 明:先写地址,后读命令/数据(寄存器多字节方式)
调 用:W1302()
入口参数:pClock: 设置时钟数据地址 格式为: 秒 分 时 日 月 星期 年
7Byte (BCD码)1B 1B 1B 1B 1B 1B 1B
返 回 值:无
设 计:周志江 日 期: 07-10-10
修 改: 日 期:
*********************************************************************/
void Set1302(unsigned char *pClock)
{
unsigned char ucAddr = 0x80;
W1302(0x8e,0x00); /* 控制命令,WP=0,写操作?*/
W1302(ucAddr,*pClock); /* 秒 分 时 日 月 年 */
pClock++;
ucAddr +=2;
W1302(ucAddr,*pClock);
pClock++;
ucAddr +=2;
W1302(ucAddr,*pClock);
pClock++;
ucAddr +=2;
W1302(ucAddr,*pClock);
pClock++;
ucAddr +=2;
W1302(ucAddr,*pClock);
pClock++;
ucAddr +=4;
W1302(ucAddr,*pClock);
W1302(0x8e,0x80); /* 控制命令,WP=1,写保护?*/
}
/********************************************************************
函 数 名:Get1302()
功 能:读取DS1302当前时间
说 明:
调 用:R1302()
入口参数:ucCurtime: 保存当前时间地址。当前时间格式为: 秒 分 时 日 月 星期 年
7Byte (BCD码) 1B 1B 1B 1B 1B 1B 1B
返 回 值:无
设 计:周志江 日 期: 07-10-10
修 改: 日 期:
*********************************************************************/
void Get1302(unsigned char ucCurtime[])
{
unsigned char ucAddr = 0x81;
ucCurtime[0] = R1302(ucAddr);/*格式为: 秒 分 时 日 月 年 */
ucCurtime[1] = R1302(ucAddr+2);
ucCurtime[2] = R1302(ucAddr+4);
ucCurtime[3] = R1302(ucAddr+6);
ucCurtime[4] = R1302(ucAddr+8);
ucCurtime[5] = R1302(ucAddr+12);
}
/*------------------------------------------------------------------*-
---- END OF DS1302 Mode-------------------------------------------------
-*------------------------------------------------------------------*/
void KeyMenu(void)
{
unsigned int i;
unsigned char t,DisCnt;
unsigned char SetCode,SetByte,SetData;
SetCode = 0;
SetByte = 0;
SetData = DisBuf[0];
F0=1;
while(F0){
if(SetCode==0) DisCnt = 1;
else if(++DisCnt > 20) DisCnt = 1;
P2 &= 0x07; P2 |= 18<<3;
k1_pf = k1_f;
k2_pf = k2_f;
k3_pf = k3_f;
k4_pf = k4_f;
if(IK1){ if(k1_f == 0)if(++k1c > 2)k1_f = 1;}
else { if(k1_f != 0)if(++k1c > 2)k1_f = 0;}
if(IK2){ if(k2_f == 0)if(++k2c > 2)k2_f = 1;}
else { if(k2_f != 0)if(++k2c > 2)k2_f = 0;}
if(IK3){ if(k3_f == 0)if(++k3c > 2)k3_f = 1;}
else { if(k3_f != 0)if(++k3c > 2)k3_f = 0;}
if(IK4){ if(k4_f == 0)if(++k4c > 2)k4_f = 1;}
else { if(k4_f != 0)if(++k4c > 2)k4_f = 0;}
P2 &=0x07;
if(~k1_f && k1_pf){// KEY1 UP
if(++SetCode > 8)SetCode = 0;
switch(SetCode){
case 0:
DT[3] = DisBuf[0]|DisBuf[1]<<4;
DT[4] = DisBuf[2]|DisBuf[3]<<4;
DT[5] = DisBuf[4]|DisBuf[5]<<4;
Set1302(DT);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -