5269 views|4 replies
- Last login
- 2019-7-14
- Online Time
- 17 hours
- Prestige
- 14 points
- Points
- 0 points
|
This post was last edited by q891031520 on 2018-8-2 18:42 I recently tried to use the PCF8563T chip and communicate with IIC, but after several attempts, I could not write data in or read data out. But the same logic 51 can read and write. I found a brother's code on the Internet. After modifying it, it still doesn't work. I posted the program for everyone to help me. I have not used IIC much. /* PCF8563 low-level driver2006-04-18 */ #include
//#include "msp430_m.h" #include "BaseConfig.h" #define ADDR_RD 0xa3 #define ADDR_WR 0xa2 #define REG_CTL1 0x00 #define REG_CTL2 0x01 #define REG_SEC 0x02 #define REG_MIN 0x03 #define REG_HOUR 0x04 #define REG_DAY 0x05 #define REG_WEEK 0x06 #define REG_MON 0x07 #define REG_YEAR 0x08 #define REG_ALARM_MIN 0x09 #define REG_ALARM_HOUR 0x0a #define REG_ALARM_DAY 0x0b #define REG_ALARM_WEEK 0x0c #define REG_FREQ 0x0d #define REG_TMRCTL 0x0e #define REG_TMR 0x0f #define uchar unsigned char #define uint unsigned int //************ ****************************************** #define set_sda() Set_Bit(P3OUT ,BIT2)//bitclr(P4DIR,BIT0); //Set to input mode, because there is a pull-up resistor, the output is high level #define clr_sda() Clr_Bit(P3OUT,BIT2) //bitset(P4DIR,BIT0) / /Set to output mode, port POUT must be set to 0 when initialized #define sdain() Clr_Bit(P3DIR,BIT2) #define sdaout() Set_Bit(P3DIR,BIT2) [ i]#define set_sclk() Set_Bit(P3OUT,BIT3)//bitset(P4OUT,BIT3) #define clr_sclk() Clr_Bit(P3OUT,BIT3)//bitclr(P4OUT,BIT3) #define sclkout() Set_Bit(P3OUT,BIT3) #define bittest_sda_1 () (P3IN&BIT2)==BIT2//bittest(P4IN,BIT0) //Test whether the input signal of SDA is 1 //********************** *********************************************** uchar time_init_data[7]={ 0,0,12,5,5,9,14}; uchar time_data[7]={0,0,0,0,0,0,0}; uint sec; void test_pcf8563(void); void InitOSC(void ) { unsigned char i; BCSCTL1 &= ~XT2OFF; //Turn on XT oscillator BCSCTL2 |= SELM_2+ SELS + DIVS_3; do { IFG1 &= ~OFIFG; //Clear oscillation error flag for(i = 0; i < 100; i++) _NOP(); //Delay waiting} while ((IFG1 & OFIFG) != 0); //If the flag is 1, continue to loop and wait} static void Start(void) //ok,Start the IIC bus { sdaout(); sclkout(); set_sda(); //1 _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); set_sclk(); _NOP(); _NOP() ; _NOP(); _NOP(); _NOP(); clr_sda(); _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); clr_sclk(); _NOP(); _NOP(); _NOP (); _NOP(); _NOP(); } static void Stop(void) { clr_sda(); _NOP(); _NOP( ); _NOP(); _NOP(); _NOP(); set_sclk(); _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); set_sda(); _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); clr_sclk(); _NOP(); _NOP( ); _NOP(); _NOP(); _NOP(); } static void Ack(void) { clr_sda(); _NOP() ; _NOP(); _NOP(); _NOP(); _NOP(); set_sclk(); _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); clr_sclk(); _NOP(); _NOP (); _NOP(); _NOP(); _NOP(); } static void NoAck(void) { set_sda(); _NOP(); _NOP(); _NOP(); _NOP(); _NOP (); set_sclk(); _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); clr_sclk(); _NOP(); _NOP(); _NOP(); _NOP(); _NOP() ; } static void Write8Bit(unsigned char input) { unsigned char i; for(i=0;i<8;i--) { if(input&0x80) { set_sda(); _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); set_sclk(); _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); clr_sclk(); _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); } else { clr_sda(); _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); set_sclk(); _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); clr_sclk (); _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); } input<<=1; } } /*************************************/[/ i] static unsigned char Read8Bit(void) { unsigned char i,rbyte; rbyte=0; for(i=0;i<8;i++) { sdain(); _NOP(); _NOP( ); _NOP(); _NOP(); _NOP(); set_sclk(); _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); rbyte<<=1; if(bittest_sda_1()) rbyte|=0x01; clr_sclk(); _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); } return(rbyte); } [i ]/*************************************/ void pcf8563_wr(uchar addr,uchar *ptr_data1,uchar length) { Start(); Write8Bit(ADDR_WR); _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); Write8Bit(addr); _NOP(); _NOP(); _NOP(); for(;length;--length) { Write8Bit(*ptr_data1++); _NOP();_NOP();_NOP();_NOP();_NOP(); } Stop(); } /*************************************/[/ i] void pcf8563_rd(uchar addr,uchar *ptr_data1,uchar length) { Start(); Write8Bit(ADDR_WR); Write8Bit(addr); Start(); Write8Bit(ADDR_RD); for(;length;length--) { Ack(); *ptr_data1++=Read8Bit(); } NoAck(); Stop(); } void rtc_init() { Start(); Write8Bit(ADDR_WR); Write8Bit(0x00); Write8Bit(0x00); Stop(); } void main (void) { WDTCTL = WDTPW + WDTHOLD; rtc_init(); while(1) pcf8563_rd(0x02,time_data,7); } /****************************** ****/ #if 0 uint sec; void test_pcf8563(void) { uchar i,arr[16]; uchar const arr_init_rtc[]= { 0x00, 0x00, 0x00, 0x00, 0x08, 0x01, 0x01, 0x05, 0x04, 0x80, 0x80, 0x80, 0x80, 0x83, 0x00 }; for(i=0;i="arr_init_rtc;" for(;;)="" for(i="0;i<20;++i)" pcf8563_rd(0,arr,8);="" pcf8563_wr(0,arr,sizeof(arr_init_rtc));="" sec="(arr[2]" {="">>4)*10; sec+=arr[2]&0x0f; i=sec; _NOP(); } } #endif /****************************** *******************/
|
|