mcp7940N与PIC18F24K40的接口

问题描述

我正在尝试将PIC18F24k40器件连接到MCP7940 N RTC器件。我已经将配置设备8mhz和RTC的频率设置为100khz。但是我正在使用PIC单片机,带有MCC生成的库。 在下面的代码中,我可以显示min = 20和sec = 10,但是sec参数没有增加。我尝试使用DS1307 / DS3233 / PC8563 RTC设备的类似harware效果很好,我可以使用逻辑分析仪获得SDA / SCL脉冲

我不会收到以下代码的任何响应,但是会显示我正在写入i2c从站地址的内容

有人可以帮我弄清楚应该读什么格式才能获得rtc的分钟和秒

#include "mcc_generated_files/mcc.h"
#include"mcc_generated_files/examples/i2c1_master_example.h"

int sec;
int min;
int hour;
int date;
int month;
int year;
int day;
int temp=0;
int r_data;
#define Seg1 0x01
#define Seg2 0x02
#define Seg3 0x04
#define Seg4 0x08
#define Seg5 0x10
#define Seg6 0x20


unsigned char Flag_Update=0;
void Delay(unsigned int k) {
 unsigned int j;
 for(j=0; j<k; j++);
}


void SetSeg(unsigned short data,unsigned short segno)

{
 switch(data) {
  case 0:
   PORTB = 0x3F;
   break;
  case 1:
   PORTB = 0x06;
   break;
  case 2:
   PORTB = 0x5B;
   break;
  case 3:
   PORTB = 0x4F;
   break;
  case 4:
   PORTB = 0x66;
   break;
  case 5:
   PORTB = 0x6D;
   break;
  case 6:
   PORTB = 0x7D;
   break;
  case 7:
   PORTB = 0x07;
   break;
  case 8:
   PORTB = 0x7F;
   break;
  case 9:
   PORTB = 0x6F;
   break;
  default :
   PORTB = 0X00;
   break;
 }

 if(segno==1) {
  PORTA = Seg4;
 }
 if(segno==2) {
  PORTA = Seg3;
 }
 if(segno==3) {
  PORTA = Seg2;
 }
 if(segno==4) {
  PORTA = Seg1;
 }

 if(segno==5) {
  PORTC=0X00;
  PORTC = 0x40;//DP2 fourth Segment

  // PORTCbits.RC5=1;
 }

 if(segno==6) {
  PORTC=0X00;
  PORTC= 0x20;//DP2 third Segment

  //PORTCbits.RC6=1;
 }


 if(segno==7) {
  PORTA=0X00;
  PORTA = Seg5; //DP2 Second Segment
  // PORTAbits.RA4=1;
 }

 if(segno==8) {
  PORTA=0X00;
  PORTA = Seg6; //DP2 First Segment
  // PORTAbits.RA5=1;
 }

}



unsigned int bcdtodecimal(unsigned int bcd) {
 unsigned int decimal;
 decimal = (((bcd & 0xF0) >> 4) * 10) + (bcd & 0x0F);
 return decimal;
}





void ISR_Routine(void) {
 if(PIR0bits.TMR0IF==1) {
  PIR0bits.TMR0IF = 0;
  count= count+1;
  if(count>=10) {

   Flag_Update=1;
   count=0;
   LED=!LED;

  }
 }



}







void main(void) {
 // Initialize the device
 SYstem_Initialize();
 INTCONbits.GIE=1;
 INTCONbits.PEIE=1;

 I2C1_Initialize();
 I2C1_Write1ByteRegister(0x6F,0x00,0x10);//sec
 I2C1_Write1ByteRegister(0x6F,0x01,0x20);//min
 I2C1_Write1ByteRegister(0x6F,0x02,0x01);//Hour
 I2C1_Write1ByteRegister(0x6F,0x03,0x01);
 I2C1_Write1ByteRegister(0x6F,0x04,0x02);
 I2C1_Write1ByteRegister(0x6F,0x05,0x03);
 I2C1_Write1ByteRegister(0x6F,0x06,0x10);
 I2C1_Write1ByteRegister(0x6F,0x07,0x48);

 while (1)


 {
  sec=I2C1_Read1ByteRegister(0x6F,0x00);

  min=I2C1_Read1ByteRegister(0x6F,0x01);

  if(Flag_Update==1) {
   SetSeg(min >> 4,4);
   __delay_ms(5);
   SetSeg(min & 0x0f,3);
   __delay_ms(5);
   SetSeg(sec >> 4,2);
   __delay_ms(5);
   SetSeg(sec & 0x0f,1);
   __delay_ms(5);
   Flag_Update = 0; //ready for next update
  }


 }
} 

解决方法

  1. I2C1_Initialize();I2C1_Write1ByteRegister(0x6F,0x00,0x10);之间添加一些延迟,例如10毫秒。有时I2C模块在初始化后需要一些延迟。大约需要几微秒,但我个人建议1毫秒或10毫秒。

  2. 将两个读取功能sec=I2C1_Read1ByteRegister(0x6F,0x00);min=I2C1_Read1ByteRegister(0x6F,0x01);放在if(Flag_Update==1)内,因为只有在要显示在七段屏幕上时,才应该读取MCP7940N

  3. 在调试模式(with variable watch中检查是否确实更新了变量minsec。如果两者均已根据实际实时正确更新,请检查您的SetSeg函数。