PIC16 PCF8591

Publisher:qazwsx007Latest update time:2020-08-28 Source: eefocusKeywords:PIC16 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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 )

;

}


Keywords:PIC16 Reference address:PIC16 PCF8591

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

A FLASH Bootloader for PIC16 and PIC18 Devices
Purpose of hard translation: I want to see how to write a bootloader. Note: To implement BootLoader in a microcontroller, the microcontroller must first have an IAP function, or the ability to erase and write its own FLASH. Secondly, the size of the FLASH must be considered. Identification:   1. When the chip leaves
[Microcontroller]
A FLASH Bootloader for PIC16 and PIC18 Devices
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号