TA的每日心情 | 奋斗 2012-10-15 15:19 |
---|
签到天数: 1 天 连续签到: 1 天 [LV.1]初来乍到
|
本帖最后由 xinxincaijq 于 2012-9-12 18:32 编辑
#include<avr/io.h>
#include<stdio.h>
//#define sbi(x,y) (x|=(1<<y)) //set bit y in byte x
//#define cbi(x,y) (x&=(~(1<<y))) //clear bit y in byte x
#define uint8_t unsigned int
#define uchar unsigned char
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
///////////////////////////////////////////
//////////////////////////////////////////////////////
void delay_nms(uint8_t a)
{
while(a--);
}
////////////////////////////////////////////////////////////////////
void TWI_WRITE_8563(uint8_t address_package,uint8_t data_package)
{
TWBR=0x20;
sbi(TWSR,TWPS0) ;
cbi(TWSR,TWPS1) ;
begin:
TWCR&=(1<<TWINT);
TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
while(!(TWCR&(1<<TWINT)));
if(((TWSR&0XF8)!=0x08)&((TWSR&0XF8)!=0x10))
goto begin;
TWDR=0xa2;
TWCR=(1<<TWINT)|(1<<TWEN);
while(!(TWCR&(1<<TWINT)))
{
;
}
if((TWSR&0XF8)!=0x18)
goto begin;
TWDR=address_package;
TWCR=TWCR=(1<<TWINT)|(1<<TWEN);
while(!(TWCR&(1<<TWINT)))
if((TWSR&0XF8)!=0x28)
goto begin;
TWDR=data_package;
TWCR=(1<<TWINT)|(1<<TWEN);
while(!(TWCR&(1<<TWINT)))
{
;
}
if((TWSR&0XF8)!=0x28)
goto begin;
TWCR=(1<<TWINT)|(1<<TWSTO)|(1<<TWEN);
}
////////////////////////////////////////////////////
uint8_t TWI_READ_8563(uint8_t address_package)
{
uint8_t result=0;
TWBR=0X20;
sbi(TWSR,TWPS0) ;
cbi(TWSR,TWPS1) ;
begin:
delay_nms(2);
TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
while(!(TWCR&(1<<TWINT)));
if(((TWSR&0XF8)!=0x08)&((TWSR&0XF8)!=0x10))
goto begin;
TWDR=0xa2;
TWCR=(1<<TWINT)|(1<<TWEN);
while(!(TWCR&(1<<TWINT)));
if((TWSR&0XF8)!=0x18)
goto begin;
TWDR=address_package;
TWCR=(1<<TWINT)|(1<<TWEN);
while(!(TWCR&(1<<TWINT)));
if((TWSR&0XF8)!=0x28)
goto begin;
TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
while(!(TWCR&(1<<TWINT)));
if(((TWSR&0XF8)!=0x08)&((TWSR&0XF8)!=0x10))
goto begin;
TWDR=0xa3;
TWCR=(1<<TWINT)|(1<<TWEN);
while(!(TWCR&(1<<TWINT)));
if((TWSR&0XF8)!=0x40)
goto begin;
TWCR=(1<<TWINT)|(1<<TWEN);
while(!(TWCR&(1<<TWINT)));
if((TWSR&0XF8)!=0x58)
goto begin;
TWCR=(1<<TWSTO)|(1<<TWEN);
result=TWDR;
return result;
}
////////////////////////////////////////////////////////
uchar _BCD(uchar x)
{
return (x/10)*16+(x%10);
}
void CALENDAR_WRITE(uint8_t second,uint8_t minute,uint8_t hour,uint8_t data,uint8_t month,uint8_t year)
{
uint8_t temp=0;
TWI_WRITE_8563(0x00,0x20);
temp=_BCD(second)&0x07;
TWI_WRITE_8563(0x02,temp);
TWI_WRITE_8563(0x03,_BCD(minute));
TWI_WRITE_8563(0x04,_BCD(hour));
TWI_WRITE_8563(0x05,_BCD(data));
temp=_BCD(month)&0x07;
TWI_WRITE_8563(0x07,temp);
TWI_WRITE_8563(0x03,_BCD(year));
TWI_WRITE_8563(0x00,0x00);
}
/////////////////////////////////////////////////////////////////
uint8_t* CALENDAR_READ(void)
{
uint8_t result[6];
uint8_t temp;
TWI_WRITE_8563(0x00,0x20);
temp=TWI_READ_8563(0X08);
result[0]=temp;
temp=TWI_READ_8563(0X07);
temp&=0X1F;
result[1]=temp;
temp=TWI_READ_8563(0X05);
temp&=0X3F;
result[2]=temp;
temp=TWI_READ_8563(0X04);
temp&=0X3F;
result[3]=temp;
temp=TWI_READ_8563(0X03);
temp&=0X7F;
result[4]=temp;
temp=TWI_READ_8563(0X02);
temp&=0X7F;
result[5]=temp;
TWI_WRITE_8563(0x00,0x20);
return result;
}
int main()
{
while(1)
{
CALENDAR_WRITE(0x02,0x02,0x02,0x02,0x02,0x02);
}
}
|
|