我用msp430f5529控制ds3231时钟芯片,采用IO口模拟I2C,程序正确,时间可以正常工作,
当我用msp430g2553控制ds3231时,也是用IO口模拟I2C,程序基本一样,与5529的程序的唯一区别是时钟不一样,但是G2553却不行,显示为00;请问是什么原因?
5529的时钟程序如下:- #define CPU_F ((double)4000000)
- #define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))
- #define delayus(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))
- #define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))
- #define DelayMS(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))
- void clk_init()
- {
- P5SEL |= BIT2+BIT3; // Port select XT2
- UCSCTL6 &= ~XT2OFF; // Enable XT2
- UCSCTL3 |= SELREF_2; // FLLref = REFO
- // Since LFXT1 is not used,
- // sourcing FLL with LFXT1 can cause
- // XT1OFFG flag to set
- UCSCTL4 |= SELA_2; // ACLK=REFO,SMCLK=DCO,MCLK=DCO
- // Loop until XT1,XT2 & DCO stabilizes - in this case loop until XT2 settles
- do
- {
- UCSCTL7 &= ~(XT2OFFG + XT1LFOFFG + DCOFFG);
- // Clear XT2,XT1,DCO fault flags
- SFRIFG1 &= ~OFIFG; // Clear fault flags
- }while (SFRIFG1&OFIFG); // Test oscillator fault flag
- UCSCTL6 &= ~XT2DRIVE0; // Decrease XT2 Drive according to
- // expected frequency
- UCSCTL4 |= SELS_5 + SELM_5; // SMCLK=MCLK=XT2
- UCSCTL5 |= DIVS_2;
- }
复制代码2553的时钟程序如下:- #define CPU_F ((double)16000000)
- #define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))
- #define delayus(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))
- #define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))
- #define DelayMS(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))
复制代码- BCSCTL1 = CALBC1_16MHZ; // Set DCO to 1, 8, 12 or 16MHz
- DCOCTL = CALDCO_16MHZ;
复制代码5529现象如下(工作正常):
2553现象如下(一直显示00,i2c工作不正常):
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
IIC的时序延时都是用delayus来控制的,比如
- void Start_I2C()
- {
- SDA_OUT;
- SDA_H; //发送起始条件的数据信号
- delayus(1);
- SCL_H;
- delayus(5); //起始条件建立时间大于4.7us,延时
- SDA_L; //发送起始信号
- delayus(5); // 起始条件锁定时间大于4μs
- SCL_L; //钳住I2C总线,准备发送或接收数据
- delayus(2);
- }
- void Stop_I2C()
- {
- SDA_OUT;
- SDA_L; //发送结束条件的数据信号
- delayus(1); //发送结束条件的时钟信号
- SCL_H; //结束条件建立时间大于4us
- delayus(5);
- SDA_H; //发送I2C总线结束信号
- delayus(4);
- }
复制代码一周热门 更多>