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);
}
Previous article:pic 16 f1947 two-way usart
Next article:PIC operates LCD to simulate SPI 9 non-standard SPI 9 data bits
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Let’s talk about the “Three Musketeers” of radar in autonomous driving
- Why software-defined vehicles transform cars from tools into living spaces
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- AD20 Select PCB Components cannot be used?
- Newly purchased MicroPython development board
- STM32F 7508DK I2C BH1570 driver development and I2C usage
- C2000 MCU, Vienna Rectifier-Based Three-Phase Power Factor Correction Reference Design
- The 5G era has arrived. This is a rare opportunity. What ideas do you have to share?
- General method of displaying pictures and texts on TFT color screen
- Motor inductance problem
- I need a simulation diagram of a car reversing radar system (paid)
- Microcontroller touch dimming desk lamp program
- An interview question