Simulate IIC mode PIC16F887 XCV1.41
#define SCL_H TRISC0 = 0, RC0 = 1 /* SCL pin is set high IIC two communication ports */
#define SCL_L TRISC0 = 0, RC0 = 0 /* Set SCL pin low */
#define SDA_H TRISC1 = 0, RC1 = 1 /* Set SDA pin high */
#define SDA_L TRISC1 = 0, RC1 = 0 /* Set SDA pin low */
#define SDA_IN TRISC1 = 1 /* SDA input mode */
#define SDA_OUT TRISC1 = 0 /* SDA output mode */
#define SDA_VAL RC1 /* SDA bit value to read */
#define TRUE 1
#define FALSE 0
#define PCF8591Address 0x90 /*Device address of PCF8591*/
#define PCF8591Control 0x40 /* AD conversion start */
void start( void )
{
SCL_H;
SDA_H;
NOP();
SDA_L;
NOP();
SCL_L;
NOP();
}
void stop( void )
{
SDA_L;
NOP();
SCL_H;
NOP();
SDA_H;
NOP();
}
void mack( void )
{
SDA_L;
NOP(); NOP();
SCL_H;
NOP();
SCL_L;
NOP(); NOP();
SDA_H;
NOP();
}
void mnack( void )
{
SDA_H;
NOP(); NOP();
SCL_H;
NOP();
SCL_L;
NOP(); NOP();
SDA_L;
NOP();
}
char check( void )
{
char slaveack;
SDA_H;
NOP(); NOP();
SCL_H;
NOP(); NOP();
SDA_IN;
NOP(); NOP();
slaveack = SDA_VAL; /* Read SDA value*/
SCL_L;
NOP();
SDA_OUT;
if ( slaveack )
return(FALSE);
else return(TRUE); /* Slave 0 represents a response, please note! ! */
}
/********************************************************************
* Function name: write1
* Function: Send a 1 to the I2C bus
* Parameters: None
* Return value: None
********************************************************************/
void write1( void )
{
SDA_H;
NOP();
SCL_H;
NOP();
SCL_L;
NOP();
}
/********************************************************************
* Function name: write0
* Function: Send a 0 to the I2C bus
* Parameters: None
* Return value: None
********************************************************************/
void write0( void )
{
SDA_L;
NOP();
SCL_H;
NOP();
SCL_L;
NOP();
}
/********************************************************************
* Function name: write1byte
* Function: Send one byte of data to the I2C bus
* Parameter: WriteData -- sent data
* Return value: None
********************************************************************/
void write1byte( unsigned char WriteData )
{
unsigned char i;
for ( i = 8; i > 0; i-- )
{
if ( WriteData & 0x80 )
write1();
else write0();
WriteData <<= 1;
}
SDA_H;
NOP();
}
/********************************************************************
* Function name: writeNbyte
* Function: Send N bytes of data to the I2C bus
* Parameter: outbuffer -- pointer to the first address where the sent data is stored
* n -- the number of data
* Return value: 1 - successful sending, 0 - failed
********************************************************************/
unsigned char writeNbyte( unsigned char * outbuffer, unsigned char n )
{
unsigned char i;
for ( i = 0; i < n; i++ )
{
write1byte( *outbuffer );
if ( check() )
{
outbuffer++;
}else {
stop();
return(FALSE);
}
}
stop();
return(TRUE);
}
/********************************************************************
* Function name: read1byte
* Function: Read a byte from the I2C bus
* Parameters: None
* Return value: the data read
********************************************************************/
unsigned char read1byte( void )
{
unsigned char ReadData = 0x00, i;
unsigned char flag;
for ( i = 0; i < 8; i++ )
{
SDA_H;
NOP();
SCL_H;
SDA_IN;
NOP();
flag = SDA_VAL;
ReadData <<= 1;
if ( flag )
ReadData |= 0x01;
SDA_OUT;
SCL_L;
NOP();
}
return(ReadData);
}
/********************************************************************
* Function name: readNbyte
* Function: Read N bytes of data from the I2C bus
* Parameter: inbuffer -- the first address where the data is stored after reading
* n -- the number of data
* Return value: None
********************************************************************/
void readNbyte( unsigned char * inbuffer, unsigned char n )
{
unsigned char i;
for ( i = 0; i < n; i++ )
{
inbuffer[i] = read1byte();
if ( i < (n - 1) )
mack();
else mnack();
}
stop();
}
/********************************************************************
* Function name: PCF_Write_DAC()
* Function: Write the data to be converted into PCF8591 DAC
* Parameter: DACdata -- data to be written
* Return value: 1 - write successful, 0 - fail
********************************************************************/
unsigned char PCF_Write_DAC( unsigned char DACdata )
{
start();
write1byte( PCF8591Address ); /* device address*/
if ( check() )
write1byte( PCF8591Control ); /* write control word*/
else
return(0);
if ( check() )
write1byte( DACdata ); /* DAC data to be converted*/
else
return(0);
if ( check() )
stop();
else return(0);
delay_ms( 10 ); /* Wait for DAC to complete internal writing*/
return(1);
}
Think of DAC in a simpler way:
First initialize the hardware IIC:
TRISC = 0b00011000;
SSPCON = 0b00101000;
SMP = 0;
SSPADD = 4;
If you want a DAC, send a voltage:
SEN = 1;
iic_send_char( 0x90 ); //iic writes iic device address
iic_send_char( 0x40 ); //Write DAC conversion signal
iic_send_char( zhengxian[ccc] ); // write voltage
PEN = 1;
in:
void iic_send_char( char R )
{
SSPBUF = R;
while ( R_nW == 1 )
;
}
Previous article:PIC16F887 MCU PROTEUS simulation C program temperature measurement system DS18B20 TC74
Next article:PIC 18f45k80 MCU EEPROM module code
Recommended ReadingLatest update time:2024-11-16 16:00
- Popular Resources
- Popular amplifiers
- MCU C language programming and Proteus simulation technology (Xu Aijun)
- Teach you to learn 51 single chip microcomputer-C language version (Second Edition) (Song Xuefeng)
- Small Compiler Design Practice (Compiled by Su Mengjin)
- 51 Single Chip Microcomputer Basic Experiment and Comprehensive Practice (Edited by Li Zuojin, Nie Ling, and Zhai Yuan)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Last day! Answer questions to win prizes! Practical sharing | Design a reliable switching power supply from scratch
- How to detect whether the switching power supply transformer is good or bad?
- micropython update: 2021.4
- The battery voltage is pulled down
- Is there any abnormal sound from the common mode inductor?
- There's a problem with the project
- bq27411-g1 (single-cell fuel gauge) bq27541 (single-cell fuel gauge) bq27546-g1 (single-cell fuel gauge)
- Playing with circuits (2) - Killing LM5175
- A post in the Audiophile Academy. I just want to ask why there is no loop phase shift when a resistor is added before the decoupling capacitor.
- TI teaches you how to overcome the three common problems in high-speed amplifier design