pic 16 using 8563 rtc iic communication internal iic

Publisher:zhihuaLatest update time:2020-09-04 Source: eefocusKeywords:pic  rtc  iic  communication Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

void init_iic()
{
    
    time.second=57;
    time.minute=2;
    time.hour=12;
    time.day=1;
    time.week=3;
    time.month=3;
    time.year=17;//Default time
    TRISCbits.TRISC3=1; //iic master mode is configured as input
    TRISCbits.TRISC4=1;
    SSP1STAT=0x00;  
    SSP1CON1=0x38; //Enable iic master mode
    SSP1CON2=0x00;  
    SSP1ADD=0x19;//Baud rate setting 400K HZ
}

void IIC_write_n(unsigned char addr,unsigned char length,unsigned char *pbuf)//Write

    unsigned char send_da;
unsigned char i=0; 
    SSP1IF=0; //MSSP interrupt flag bit 0 means no waiting for your corresponding interrupt
 // SEN=1;//Start bit The start enable bit is only in IIC master mode //Establish and send a start signal timing on the SDA and SCL lines
  SSP1CON2bits.SEN=1;
while(!SSP1IF); 
SSP1IF=0;  
SSP1BUF=0xA2;//Write control bit //Send peripheral IIC device address and write signalwhile
(!SSP1IF); 
SSP1IF=0; //Software clearing will not automatically clear
SSP1BUF=addr;//Start addresswhile
(!SSP1IF); 
SSP1IF=0; 

for(i=0;i{
send_da=*(pbuf++);
SSP1BUF=send_da;
while(!SSP1IF); 
SSP1IF=0;
}
SSP1CON2bits.PEN=1;
//PEN=1;//Stop bit //Establish and send a stop signal timing on the SDA and SCL lineswhile
(!SSP1IF); //If a stop signal is received
, SSP1IF=0  will also be set ;
  
 } 

void IIC_write(unsigned char addr,unsigned char data)//write

    SSP1IF=0; //MSSP interrupt flag bit 0 means no corresponding interrupt is waiting for you
    SSP1CON2bits.SEN=1;
 // SEN=1;//Start bit The start enable bit is only in IIC master mode //Establish and send a start signal timing on the SDA and SCL lines
while(!SSP1IF); 
SSP1IF=0;  
SSP1BUF=0xA2;//Write control bit//Send the peripheral IIC device address and write signal
while(!SSP1IF); 
SSP1IF=0; //Software clearing will not automatically clear
SSP1BUF=addr;//Write address//Send the address of the peripheral IIC device data to be read
while(!SSP1IF); 
SSP1IF=0;  
SSP1BUF=data;//write data//send the data or command to be stored in the peripheral IIC device to be readwhile
(!SSP1IF); 
SSP1IF=0;  
SSP1CON2bits.PEN=1;
//PEN=1;//stop bit //Establish and send a stop signal timing on the SDA and SCL lineswhile (!SSP1IF); //If a stop signal is received , SSP1IF=0 
will also be set ;  }  unsigned char IIC_read_n(unsigned char addr,unsigned char length,unsigned char *pbuf)//read  { //unsigned char data;   //unsigned int receive_da; unsigned char i=0; SSP1IF=0;   SSP1CON2bits.SEN=1; //SEN=1;//start  bitwhile(!SSP1IF);  SSP1IF=0;  SSP1BUF=0xA2;//control bit writewhile  (!SSP1IF); SSP1IF=0;   SSP1BUF=addr;//write addresswhile   (!SSP1IF);  SSP1IF=0;  SSP1CON2bits.RSEN=1;  // RSEN=1;//restart      bitwhile(!SSP1IF);       SSP1IF=0;       SSP1BUF=0xA3;//control bit readwhile       (!SSP1IF);       SSP1IF=0; 

     
























// RCEN=1;//Enable receiving FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
// while(!SSPIF); 
     for(i=0;i{
         SSP1CON2bits.RCEN=1;//Enable receivingwhile 
     (!SSP1IF); 
*pbuf =SSP1BUF;
//SSPBUF=send_da;
while(!SSP1IF); 
SSP1IF=0;
SSP1CON2bits.ACKDT=0;
 // ACKDT=0;
SSP1CON2bits.ACKEN=1;
    // ACKEN=1;//Enable acknowledge 
      bitwhile(!SSP1IF); 
     SSP1IF=0;
     pbuf++;
}
     SSP1CON2bits.RCEN=0;
     // RCEN=0;
     SSP1CON2bits.PEN=1;
    // PEN=1;//Stop 
     bitwhile(!SSP1IF); 
     SSP1IF=0; 
     return *pbuf;   
     
}

//unsigned char IIC_read(unsigned char addr)//Read 
//{ unsigned char data;  
//
//SSP1IF=0;  
//SSP1CON2bits.SEN=1;
SEN=1;//Start bit 
//while(!SSP1IF); 
//SSP1IF=0; 
//SSP1BUF=0xA2;//Control bit write 
//while(!SSP1IF);
//SSP1IF=0;  
//SSP1BUF=addr;//Write address  
//while(!SSP1IF); 
//SSP1IF=0;
//SSP1CON2bits.RSEN=1;
// // RSEN=1;//Restart bit
// while(!SSP1IF); 
// SSP1IF=0; 
// SSP1BUF=0xA3;//Control bit read 
// while(!SSP1IF); 
// SSP1IF=0; 
// SSP1CON2bits.RCEN=1;
// // RCEN=1;//Enable reception 
// while(!SSP1IF); 
// data=SSP1BUF;//Read data 
// while(!SSP1IF);
// SSP1IF=0;
// SSP1CON2bits.ACKDT=0;
// // ACKDT=0;
// //ACKEN=1;//Enable acknowledge bit 
// SSP1CON2bits.ACKEN=1;
// while(!SSP1IF); 
// SSP1IF=0;
// SSP1CON2bits.RCEN=0;
// // RCEN=0;
// SSP1CON2bits.PEN=1;
// // PEN=1;//Stop bit 
// while(!SSP1IF); 
// SSP1IF=0; 
// return(data);  
//}

unsigned char BCD2Val(unsigned char x) //BCD to decimal
{
    unsigned char tmp = 0;

tmp = ((unsigned char)(x & (unsigned char)0xF0) >> (unsigned char)0x04) * 10;
return (tmp + (x & (unsigned char)0x0F));


}

unsigned char Val2BCD(unsigned char x) //Decimal to BCD code subroutine
{
    
    
    unsigned char bcdhigh = 0;

while (x >= 10)
{
bcdhigh++;
x -= 10;
}

return ((unsigned char)(bcdhigh << 4) | x);
 
}
unsigned char P8563_Read_Time() //PCF8563 read time subroutine
{
    
    unsigned char temp[7];
IIC_read_n(0x02,0x07,temp);

time.second=BCD2Val(temp[0]&0x7f);
time.minute=BCD2Val(temp[1]&0x7f);
time.hour=BCD2Val(temp[2]&0x3f);
time.day=BCD2Val(temp[3]&0x3f);
time.week=BCD2Val(temp[4]&0x07);
time.month=BCD2Val(temp[5]&0x1f);
time.year=BCD2Val(temp[6]);
return 0;
 
}

void P8563_Set_Time()   //PCF8563设置时间子程序
{
unsigned char temp[7];
unsigned char i;
for(i=0;i<7;i++)
{
temp[i]=Val2BCD(((unsigned char *)(&time))[i]);
}
 IIC_write_n(0x02,7,temp);
}

Keywords:pic  rtc  iic  communication Reference address:pic 16 using 8563 rtc iic communication internal iic

Previous article:pic 16 f1947 two-way usart
Next article:PIC operates LCD to simulate SPI 9 non-standard SPI 9 data bits

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号