Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <xc.h>
- #include <stdint.h>
- void i2c_init(void)
- {
- SSPSTATbits.SMP = 1; // FIXME: needed?
- SSPCON1bits.SSPM = 0b1000;
- SSPCON1bits.SSPEN = 1;
- //SSPADD = 0x4; // ~400 kHz, see table 17-3 of the DS39631E manual.
- SSPADD = 0x13; // ~100 kHz, see table 17-3 of the DS39631E manual.
- }
- inline void i2c_busy_wait(void)
- {
- // SSPCON has various flags that are cleared by hardware when the operation
- // has completed. SSPSTATbots.R_NOT_W tells whether a transmit is in progress.
- while(SSPCON2 & 0b00011111 || SSPSTATbits.R_NOT_W);
- }
- inline void i2c_start(void)
- {
- i2c_busy_wait();
- SSPCON2bits.SEN = 1;
- }
- inline void i2c_stop(void)
- {
- i2c_busy_wait();
- SSPCON2bits.PEN = 1;
- }
- inline void i2c_send(uint8_t byte)
- {
- i2c_busy_wait();
- SSPBUF = byte;
- }
- void i2c_send_byte(uint8_t addr, uint8_t data)
- {
- i2c_start();
- i2c_send(addr);
- i2c_send(data);
- i2c_stop();
- }
- int main(int argc, char** argv) {
- i2c_init();
- while(1)
- {
- i2c_send_byte(0x80, 0x80);
- }
- return (EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement