ADDA series PCF8591 driver

Publisher:闪耀的星空Latest update time:2016-09-08 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
/*****************************This part is the driver of the I2C bus****************************************/

#include
#include
#include

#define NOP() _nop_() /* define a null instruction*/
#define _Nop() _nop_() /* define a null instruction*/

 
sbit SCL=P2^0; //I2C clock 
sbit SDA=P2^1; //I2C data 
bit ack; /*acknowledgement flag*/
  

/***********************************************************************
                     Start bus functionFunction               
prototype: void Start_I2c();  
Function: Start I2C bus, that is, send I2C start condition.  
********************************************************************/
void Start_I2c()
{
  SDA=1; /*Send data signal of start condition*/
  _Nop();
  SCL=1;
  _Nop(); /*Start condition establishment time is greater than 4.7us, delay*/
  _Nop();
  _Nop();
  _Nop();
  _Nop();    
  SDA=0; /*Send start signal*/
  _Nop(); /*Start condition locking time is greater than 4μs*/
  _Nop();
  _Nop();
  _Nop();
  _Nop();       
  SCL=0; /*Clamp I2C bus, prepare to send or receive data*/
  _Nop();
  _Nop();
}

/***********************************************************************
                      End bus function               
Function prototype: void Stop_I2c();  
Function: End the I2C bus, that is, send the I2C end condition.  
********************************************************************/
void Stop_I2c()
{
  SDA=0; /*Send the data signal of the end condition*/
  _Nop(); /*Send the clock signal of the end condition*/
  SCL=1; /*The establishment time of the end condition is greater than 4μs*/
  _Nop();
  _Nop()
  ;
  _Nop(); _Nop();
  _Nop();
  SDA=1; /*Send the end signal of the I2C bus*/
  _Nop();
  _Nop();
  _Nop();
  _Nop();
}

/*******************************************************************
                 Byte data sending function               
Function prototype: void SendByte(UCHAR c);
Function: Send data c, which can be an address or data. After sending, wait for a response and operate
          on this      status bit. (No response or non-response makes ack=0)
           Sending data is normal, ack=1; ack=0 means that the controlled device has no response or is damaged.
********************************************************************/
void SendByte(unsigned char c)
{
 unsigned char BitCnt;
 
 for(BitCnt=0;BitCnt<8;BitCnt++) /*The length of the data to be transmitted is 8 bits*/
    {
     if((c<        else SDA=0;                
     _Nop();
     SCL=1; /*Set the clock line high to notify the controlled device to start receiving data bits*/
      _Nop(); 
      _Nop(); /*Ensure that the clock high level period is greater than 4μs*/
      _Nop(); _Nop
      ();
      _Nop();         
     SCL=0; 
    }
    
    _Nop();
    _Nop();
    SDA=1; /*Release the data line after 8 bits are sent and prepare to receive the response bit*/
    _Nop();
    _Nop();   
    SCL=1;
    _Nop();
    _Nop();
    _Nop();
    if(SDA==1)ack=0;     
       else ack=1; /*Judge whether the response signal is received*/
    SCL=0;
    _Nop();
    _Nop();
}

/*******************************************************************
                 Byte data receiving function               
Function prototype: UCHAR RcvByte();
Function: Used to receive data from the slave device and judge the bus error (no response signal is sent).
          After sending, please use the response function to respond to the slave device.  
********************************************************************/    
unsigned char RcvByte()
{
  unsigned char retc;
  unsigned char BitCnt;
  
  retc=0; 
  SDA=1; /*Set the data line to input mode*/
  for(BitCnt=0;BitCnt<8;BitCnt++)
      {
        _Nop();           
        SCL=0; /*Set the clock line to low and prepare to receive data bits*/
        _Nop();
        _Nop(); /*Clock low level period is greater than 4.7μs*/
        _Nop();
        _Nop();
        _Nop();
        SCL=1; /*Set the clock line to high to make the data on the data line valid*/
        _Nop();
        _Nop();
        retc=retc<<1;
        if(SDA==1)retc=retc+1; /*Read the data bit, and put the received data bit into retc*/
        _Nop();
        _Nop(); 
      }
  SCL=0;    
  _Nop();
  _Nop();
  return(retc);
}

/************************************************************************
                     Response sub-function
Function prototype: void Ack_I2c(bit a);
Function: The master controller sends a response signal (can be a response or non-response signal, determined by the bit parameter a)
********************************************************************/
void Ack_I2c(bit a)
{
  
  if(a==0)SDA=0; /*Send a response or non-response signal here*/
  else SDA=1;
  _Nop();
  _Nop
  (); _Nop();      
  SCL=1;
  _Nop();
  _Nop(); /*Clock low level period is greater than 4μs*/
  _Nop();
  _Nop();
  _Nop();  
  SCL=0; /*Clear the clock line and clamp the I2C bus to continue receiving*/
  _Nop();
  _Nop();    
}

 

#include
#include

#define PCF8591 0x90 //PCF8591 address


//else IO
sbit LS138A=P2^2;  
sbit LS138B=P2^3;
sbit LS138C=P2^4; 

//This table is the font of LED, common cathode digital tube 0-9 - 
unsigned char code Disp_Tab[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

unsigned char AD_CHANNEL;
unsigned long xdata  LedOut[8];
unsigned int  D[32];
  

/*******************************************************************
DAC conversion, conversion function               
***************************************************************/
bit DACconversion(unsigned char sla,unsigned char c, unsigned char Val)
{
   Start_I2c(); //Start the bus
   SendByte(sla); //Send device address
   if(ack==0)return(0);
   SendByte(c); //Send control byte
   if(ack==0)return(0);
   SendByte(Val); //Send DAC value  
   if(ack==0)return(0);
   Stop_I2c(); //End the bus
   return(1);
}

/*******************************************************************
ADC sends byte [command] data function               
***************************************************************/
bit ISendByte(unsigned char sla,unsigned char c)
{
   Start_I2c(); //Start the bus
   SendByte(sla); //Send device address
   if(ack==0)return(0);
   SendByte(c); //Send data
   if(ack==0)return(0);
   Stop_I2c(); //End the bus
   return(1);
}

/***********************************************************************
ADC read byte data function               
***************************************************************/
unsigned char IRcvByte(unsigned char sla)
{ unsigned char c;

   Start_I2c(); //Start the bus
   SendByte(sla+1); //Send the device address. The reason for adding one to the address is that the last bit of the address word is 0 for writing and 1 for reading
   if(ack==0)return(0);
   c=RcvByte(); //Read data 0

   Ack_I2c(1); //Send non-acknowledge bit
   Stop_I2c(); //End bus
   return(c);
}

//******************************************************************/
main()
{  char i,j;

 while(1)
 {/********The following AD-DA processing****************/  
   switch(AD_CHANNEL)
   {
     case 0: ISendByte(PCF8591,0x41);
             D[0]=IRcvByte(PCF8591)*2; //ADC0 analog-to-digital conversion 1, the reason for multiplying by 2 is to convert the result 00~0xff into 0~510, and divide by 100 to get the actual sampling value
    break;  
 
  case 1: ISendByte(PCF8591,0x42);
             D[1]=IRcvByte(PCF8591)*2; //ADC1 analog-to-digital conversion 2
    break; 

  case 2: ISendByte(PCF8591,0x43);
             D[2]=IRcvByte(PCF8591)*2; //ADC2 analog-to-digital conversion 3
    break; 

  case 3: ISendByte(PCF8591,0x40);
             D[3]=IRcvByte(PCF8591)*2; //ADC3 analog-to-digital conversion 4
    break; 

  case 4: DACconversion(PCF8591,0x40, D[4]/4); //DAC digital-to-analog conversion
          break;
   }

       // D[4]=400; //Digital--->>Analog output
     D[4]=D[3];
   if(++AD_CHANNEL>4) AD_CHANNEL=0;

 /********The following will send the AD value to the LED digital tube for display*************/
         
  LedOut[0]=Disp_Tab[D[1]%10000/1000];
     LedOut[1]=Disp_Tab[D[1]%1000/100];
     LedOut[2]=Disp_Tab[D[1]%100/10]|0x80;
     LedOut[3]=Disp_Tab[D[1]%10];
  
  LedOut[4]=Disp_Tab[D[0]%10000/1000];
     LedOut[5]=Disp_Tab[D[0]%1000/100];
     LedOut[6]=Disp_Tab[D[0]%100/10]|0x80;
     LedOut[7]=Disp_Tab[D[0]%10];  
   
 
  for( i=0; i<8; i++) 
  { P1 = LedOut[i];
   
   switch(i) //Use switch statement to control 138 decoder. You can also use table lookup. Students can try to modify it by themselves      
      {     
   case 0:LS138A=0; LS138B=0; LS138C=0; break;         
         case 1:LS138A=1; LS138B=0; LS138C=0; break;              
         case 2:LS138A=0; LS138B=1; LS138C=0; break; 
         case 3:LS138A=1; LS138B=1; LS138C=0; break; 
   case 4:LS138A=0; LS138B=0; LS138C=1; break;
   case 5:LS138A=1; LS138B=0; LS138C=1; break;
   case 6:LS138A=0; LS138B=1; LS138C=1; break;
   case 7:LS138A=1; LS138B=1; LS138C=1; break;
   
      }
  
      for (j = 0 ; j<90 ;j++) { ;} //Scan interval time
   }

     P1 = 0;

 }  
}

Reference address:ADDA series PCF8591 driver

Previous article:Comparison between 51 MCU interruption process and main program calling subroutine process
Next article:Implementing Fuzzy Control PID Program in Single Chip Microcomputer

Recommended ReadingLatest update time:2024-11-16 15:46

LED Driver Solutions for Lighting
The emergence of a large number of light-emitting diodes (LEDs) as solid-state light sources has made incandescent lamps increasingly obsolete. In the past few years, LED technology has made great progress. Advances in heat dissipation, packaging and process technology have made LEDs brighter, more efficient, longer
[Power Management]
LED Driver Solutions for Lighting
Cost-effective RGB LED driver solution
One of the fastest growing areas of LED application is effect lighting, also known as architectural lighting. Effect lighting can use RGB LEDs, which use three chips, each responsible for generating one color of light (red, green and blue). Another way to generate different colors of light is to use red, green and blue
[Power Management]
Cost-effective RGB LED driver solution
Design and application of IR2110 driver chip in photovoltaic inverter circuit
IR2110 is a bridge driver integrated circuit chip of IR company . It adopts highly integrated level conversion technology, which greatly simplifies the control requirements of logic circuits for power devices and improves the reliability of driving circuits. For the single-phase photovoltaic inverter circuit with ZCS
[Power Management]
Design and application of IR2110 driver chip in photovoltaic inverter circuit
arm driver linux kernel clock
" Linux Kernel Clock" involves four kernel driver functions, one kernel structure, and analyzes one kernel driver function; one related application template or kernel driver template for reference, and one related application template or kernel driver for reference 1. Kernel timer Meaning: Kernel timer is a timer in
[Microcontroller]
Discrete devices – a superior alternative to integrated MOSFET drivers
In Power Tip #42, we discussed emitter trackers used in MOSFET gate drive circuits and saw that drive currents in the 2A range can be achieved with small SOT-23 transistors. In this design tip, we look at self-driven synchronous rectifiers and explore when a discrete driver is needed to protect the synchronous rectifi
[Power Management]
Discrete devices – a superior alternative to integrated MOSFET drivers
G20 Weekly | Hikvision Robotics 5G+AMR drives transmission manufacturing innovation/STEP launches all-terrain vehicle welding solution
1 Hikvision Robotics 5G+AMR drives transmission manufacturing innovation On July 14, Hikvision Robotics and Qingshan Industry jointly explored a new model of DCT full-process intelligent manufacturing. Both parties gave full play
[robot]
Innovative bipolar multi-string LLC topology for high-power LED drive technology
Disadvantages of high-power LED lighting: LED driver power supply has poor reliability and high heat generation. Many LED driver power supplies are burned out, many of which are caused by the failure of inconspicuous peripheral small devices, which leads to the failure of the entire driver circuit. "Because the LED
[Power Management]
Innovative bipolar multi-string LLC topology for high-power LED drive technology
Lower bridge arm IGBT drive circuit diagram - schematic diagram
When using the lower arm driver to drive an IGBT larger than 150 A/1 200 V, an external current buffer can be connected to expand its driving capability. From the working sequence of Figure 2, it can be seen that when the voltage at the DESAT terminal exceeds 7 V, an overcurrent is generated. The driving circuit diagra
[Analog Electronics]
Lower bridge arm IGBT drive circuit diagram - schematic diagram
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号