Advertisement
Guest User

Untitled

a guest
Jan 28th, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <xc.h>
  4. #include <stdint.h>
  5.  
  6. void i2c_init(void)
  7. {
  8.     SSPSTATbits.SMP = 1; // FIXME: needed?
  9.     SSPCON1bits.SSPM = 0b1000;
  10.     SSPCON1bits.SSPEN = 1;
  11.     //SSPADD = 0x4; // ~400 kHz, see table 17-3 of the DS39631E manual.
  12.     SSPADD = 0x13; // ~100 kHz, see table 17-3 of the DS39631E manual.
  13. }
  14.  
  15.  
  16. inline void i2c_busy_wait(void)
  17. {
  18.     // SSPCON has various flags that are cleared by hardware when the operation
  19.     // has completed. SSPSTATbots.R_NOT_W tells whether a transmit is in progress.
  20.     while(SSPCON2 & 0b00011111  || SSPSTATbits.R_NOT_W);
  21. }
  22.  
  23. inline void i2c_start(void)
  24. {
  25.     i2c_busy_wait();
  26.     SSPCON2bits.SEN = 1;
  27. }
  28.  
  29. inline void i2c_stop(void)
  30. {
  31.     i2c_busy_wait();
  32.     SSPCON2bits.PEN = 1;
  33. }
  34.  
  35. inline void i2c_send(uint8_t byte)
  36. {
  37.     i2c_busy_wait();
  38.     SSPBUF = byte;
  39. }
  40.  
  41. void i2c_send_byte(uint8_t addr, uint8_t data)
  42. {
  43.     i2c_start();
  44.     i2c_send(addr);
  45.     i2c_send(data);
  46.     i2c_stop();
  47. }
  48.  
  49. int main(int argc, char** argv) {
  50.     i2c_init();
  51.     while(1)
  52.     {
  53.         i2c_send_byte(0x80, 0x80);
  54.     }
  55.  
  56.    
  57.     return (EXIT_SUCCESS);
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement