Guest User

Untitled

a guest
Nov 16th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. #include <xc.h>
  2.  
  3. #pragma config FNOSC=PRI,POSCMOD=XT
  4.  
  5. void Delay(long int a)
  6. {
  7. float b=0;
  8. for(; b<a; b++);
  9. }
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17. void I2CStart(void)
  18. {
  19. I2C2CONbits.SEN = 1; //send start bit
  20. while(I2C2CONbits.SEN); //wait for SEN to clear
  21. }
  22.  
  23. void I2CRestart(void)
  24. {
  25. I2C2CONbits.RSEN = 1; //resend start bit
  26. while(I2C2CONbits.RSEN); //wait for RSEN to clear
  27. }
  28.  
  29. void I2CStop(void)
  30. {
  31. I2C2CONbits.PEN; //send stop bit
  32. while(I2C2CONbits.PEN); //wait for PEN to clear
  33. }
  34.  
  35. unsigned char I2CReceive(void)
  36. {
  37. I2C2CONbits.RCEN = 1; //initiate receive
  38. while(I2C2CONbits.RCEN); //wait for recieve to complete
  39. return I2C2RCV; //read I2C2RCV buffer
  40. }
  41.  
  42. void I2CAckPoll(unsigned char addh, unsigned char addl)
  43. {
  44. I2CStart(); //assert start condition
  45. I2C2TRN = 0xA0; //send write control byte
  46. while(I2C2STATbits.TRSTAT); //wait for transmit to complete
  47. if(I2C2STATbits.ACKSTAT)
  48. {
  49. while(I2C2STATbits.ACKSTAT)
  50. {
  51. I2CRestart(); //resend start bit
  52. I2C2TRN = 0xA0; //send write control byte
  53. while(I2C2STATbits.TRSTAT); //wait for transmit to complete
  54. }
  55. }
  56. I2C2TRN = addh; //send write control byte
  57. while(I2C2STATbits.TRSTAT); //wait for transmit to complete
  58. I2C2TRN = addl; //send write control byte
  59. while(I2C2STATbits.TRSTAT); //wait for transmit to complete
  60.  
  61. LATAbits.LATA0 = 1; //turn on LED
  62. while(1); //stop
  63. }
  64.  
  65. int main()
  66. {
  67. PADCFG1 = 0xFF; // Make analog pins digital
  68. OSCCON = 0b0111001010100000; //Turn to external OSC
  69.  
  70. LATA = 0;
  71. TRISA = 0;
  72. LATB = 0;
  73. TRISB = 0;
  74.  
  75.  
  76. //EEPROM
  77.  
  78. I2C2BRG = 37; //57
  79. I2C2CON = 0b1001101000000000;
  80.  
  81. //I2CStart();
  82. //I2CRestart();
  83. //I2CStop();
  84. //I2CAckNack();
  85. //temp = I2CReceive();
  86. I2CAckPoll(0x00,0x00);
  87. }
Add Comment
Please, Sign In to add comment