5269 views|4 replies

23

Posts

0

Resources
The OP
 

A program about PCF8563T, not tuned out [Copy link]

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 /****************************** *******************/
This post is from Microcontroller MCU

Latest reply

Timing problem, use an oscilloscope to view the waveform  Details Published on 2018-8-3 09:02
 

23

Posts

0

Resources
2
 
This post was last edited by q891031520 on 2018-8-2 18:43 This is the source file

149源码.zip

144.13 KB, downloads: 4

This post is from Microcontroller MCU
 
 

23

Posts

0

Resources
3
 
The analog IIC is used because the IIC pin is already occupied, and the actual data read out is FFFFFFFFFF.
This post is from Microcontroller MCU
 
 
 

3181

Posts

0

Resources
4
 
Timing problem, use an oscilloscope to view the waveform
This post is from Microcontroller MCU
 
Personal signature为江山踏坏了乌骓马,为社稷拉断了宝雕弓。
 
 

23

Posts

0

Resources
5
 
This is an old post. It is better to write your own code than to trust other people's code. Thanks to all the brothers!
This post is from Microcontroller MCU
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list