Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <xc.h>
- #pragma config FNOSC=PRI,POSCMOD=XT
- void Delay(long int a)
- {
- float b=0;
- for(; b<a; b++);
- }
- void I2CStart(void)
- {
- I2C2CONbits.SEN = 1; //send start bit
- while(I2C2CONbits.SEN); //wait for SEN to clear
- }
- void I2CRestart(void)
- {
- I2C2CONbits.RSEN = 1; //resend start bit
- while(I2C2CONbits.RSEN); //wait for RSEN to clear
- }
- void I2CStop(void)
- {
- I2C2CONbits.PEN; //send stop bit
- while(I2C2CONbits.PEN); //wait for PEN to clear
- }
- unsigned char I2CReceive(void)
- {
- I2C2CONbits.RCEN = 1; //initiate receive
- while(I2C2CONbits.RCEN); //wait for recieve to complete
- return I2C2RCV; //read I2C2RCV buffer
- }
- void I2CAckPoll(unsigned char addh, unsigned char addl)
- {
- I2CStart(); //assert start condition
- I2C2TRN = 0xA0; //send write control byte
- while(I2C2STATbits.TRSTAT); //wait for transmit to complete
- if(I2C2STATbits.ACKSTAT)
- {
- while(I2C2STATbits.ACKSTAT)
- {
- I2CRestart(); //resend start bit
- I2C2TRN = 0xA0; //send write control byte
- while(I2C2STATbits.TRSTAT); //wait for transmit to complete
- }
- }
- I2C2TRN = addh; //send write control byte
- while(I2C2STATbits.TRSTAT); //wait for transmit to complete
- I2C2TRN = addl; //send write control byte
- while(I2C2STATbits.TRSTAT); //wait for transmit to complete
- LATAbits.LATA0 = 1; //turn on LED
- while(1); //stop
- }
- int main()
- {
- PADCFG1 = 0xFF; // Make analog pins digital
- OSCCON = 0b0111001010100000; //Turn to external OSC
- LATA = 0;
- TRISA = 0;
- LATB = 0;
- TRISB = 0;
- //EEPROM
- I2C2BRG = 37; //57
- I2C2CON = 0b1001101000000000;
- //I2CStart();
- //I2CRestart();
- //I2CStop();
- //I2CAckNack();
- //temp = I2CReceive();
- I2CAckPoll(0x00,0x00);
- }
Add Comment
Please, Sign In to add comment