用I/O模拟还是用硬件
#ifndef __SWI2C16_H#define __SWI2C16_H
/* PIC18 software I2C interface header */
/***** COMMON FUNCTION PROTOTYPES *****/
void SWStopI2C( void ); // Generate bus stop condition
void SWStartI2C( void ); // Generate bus start condition
void SWRestartI2C( void ); // Generate bus restart condition
void SWStopI2C( void ); // Generate bus stop condition
// USER NEEDS TO DEFINE DATA AND CLOCK PINS. RESISTORS ARE REQUIRED ON
// DATA AND CLOCK PINS.
#if defined(__18F1220) || defined(__18F1320)
#defineDATA_LOW TRISBbits.TRISB4 = 0; // define macro for data pin output
#defineDATA_HI TRISBbits.TRISB4 = 1; // define macro for data pin input
#defineDATA_LAT LATBbits.LATB4 // define macro for data pin latch
#defineDATA_PIN PORTBbits.RB4 // define macro for data pin
#defineCLOCK_LOWTRISBbits.TRISB3 = 0; // define macro for clock pin output
#defineCLOCK_HI TRISBbits.TRISB3 = 1; // define macro for clock pin input
#defineSCLK_LAT LATBbits.LATB3 // define macro for clock pin latch
#defineSCLK_PIN PORTBbits.RB3 // define macro for clock pin
#elif defined(__18F2455) || defined(__18F2550) || \
defined(__18F4455) || defined(__18F4550)
#defineDATA_LOW TRISBbits.TRISB0 = 0; // define macro for data pin output
#defineDATA_HI TRISBbits.TRISB0 = 1; // define macro for data pin input
#defineDATA_LAT LATBbits.LATB0 // define macro for data pin latch
#defineDATA_PIN PORTBbits.RB0 // define macro for data pin
#defineCLOCK_LOWTRISBbits.TRISB1 = 0; // define macro for clock pin output
#defineCLOCK_HI TRISBbits.TRISB1 = 1; // define macro for clock pin input
#defineSCLK_LAT LATBbits.LATB1 // define macro for clock pin latch
#defineSCLK_PIN PORTBbits.RB1 // define macro for clock pin
#else
#defineDATA_LOW TRISCbits.TRISC4 = 0; // define macro for data pin output
#defineDATA_HI TRISCbits.TRISC4 = 1; // define macro for data pin input
#defineDATA_LAT LATCbits.LATC4 // define macro for data pin latch
#defineDATA_PIN PORTCbits.RC4 // define macro for data pin
#defineCLOCK_LOWTRISCbits.TRISC3 = 0; // define macro for clock pin output
#defineCLOCK_HI TRISCbits.TRISC3 = 1; // define macro for clock pin input
#defineSCLK_LAT LATCbits.LATC3 // define macro for clock pin latch
#defineSCLK_PIN PORTCbits.RC3 // define macro for clock pin
#endif
/***** FUNCTION PROTOTYPES FOR PIC18CXXX *****/
signed char SWAckI2C( void ); // Read bus ACK condition
signed char Clock_test( void );
unsigned intSWReadI2C( void ); // Read in single byte
signed char SWWriteI2C( auto unsigned char data_out ); // Write out single byte
signed char SWGetsI2C( auto unsigned char *rdptr, auto unsigned char length ); // Read in string from I2C module
signed char SWPutsI2C( auto unsigned char *wrptr ); // Write string to I2C module
#defineSWPutcI2C SWWriteI2C
#defineSWGetcI2C SWReadI2C
#defineSWNotAckI2CSWAckI2C
#endif
页:
[1]