Files

  • 404 File Not Found
copied
Last update 6 years 8 months by Mengjiao Hong
FilesHW5HW5.X
..
nbproject
Makefile
i2c_master_noint.c
i2c_master_noint.h
main.c
newfile.c
i2c_master_noint.c
#include <xc.h> #include "i2c_master_noint.h" // I2C Master utilities, 100 kHz, using polling rather than interrupts // The functions must be callled in the correct order as per the I2C protocol // I2C pins use 10k pull-up resistors void i2c_master_setup(void) { I2C2BRG = 233; // I2CBRG = [1/(2*Fsck) - PGD]*Pblck - 2 // for PIC32MX250F128B, Pblck = 48MHz I2C2CONbits.ON = 1; // turn on the I2C2 module } // Start a transmission on the I2C bus void i2c_master_start(void) { I2C2CONbits.SEN = 1; // send the start bit while(I2C2CONbits.SEN) { ; } // wait for the start bit to be sent } void i2c_master_restart(void) { I2C2CONbits.RSEN = 1; // send a restart while(I2C2CONbits.RSEN) { ; } // wait for the restart to clear } void i2c_master_send(unsigned char byte) { // send a byte to slave I2C2TRN = byte; // if an address, bit 0 = 0 for write, 1 for read while(I2C2STATbits.TRSTAT) { ; } // wait for the transmission to finish if(I2C2STATbits.ACKSTAT) { // if this is high, slave has not acknowledged ; // ("I2C2 Master: failed to receive ACK\r\n"); } } unsigned char i2c_master_recv(void) { // receive a byte from the slave I2C2CONbits.RCEN = 1; // start receiving data while(!I2C2STATbits.RBF) { ; } // wait to receive the data return I2C2RCV; // read and return the data } void i2c_master_ack(int val) { // sends ACK = 0 (slave should send another byte) // or NACK = 1 (no more bytes requested from slave) I2C2CONbits.ACKDT = val; // store ACK/NACK in ACKDT I2C2CONbits.ACKEN = 1; // send ACKDT while(I2C2CONbits.ACKEN) { ; } // wait for ACK/NACK to be sent } void i2c_master_stop(void) { // send a STOP: I2C2CONbits.PEN = 1; // comm is complete and master relinquishes bus while(I2C2CONbits.PEN) { ; } // wait for STOP to complete }
Report a bug