Single-chip microcomputer - breathing light based on DA conversion

1. Basic knowledge
1.1. The resolution of the AD converter
is 12 bits. The resolution is 12 bits, or 1/(2^12) of the full scale.
1.2. The quantization error is the error caused by the quantization of the analog quantity by the limited number of bits of the ADC.
1.3, the type of AD converter
Double integral type, successive approximation type, parallel comparison type.
1.4. PCF8591
PCF8591 is an 8-bit general-purpose analog input and output converter integrating four 12-bit analog-to-digital converters and one 8-bit analog-to-digital converter. It can communicate with other devices through the I2C bus, and can convert 4 different analog input signals into digital signal output. In addition, it provides a voltage output with programmable gain amplifier and programmable output voltage range.

PCF8591 is often used in analog signal acquisition and processing applications, such as signal acquisition of temperature, pressure, humidity and other sensors, and analog circuit control in control systems. It can also be used in areas such as audio processing, image processing, motion control and optical measurement. In embedded system, PCF8591 is a very useful tool, because it can realize the conversion of analog signal and digital signal, and integrate different signals into one chip, thus saving system space and design cost.

PCF8591 also uses the I2C communication protocol. The command sequence of PCF8591 is: the host computer sends address command to it first, then the control word command, and finally the data command.
insert image description here

It has two conversion modes, ADC and DAC.
ADC function configuration, send 0x90, 0x40+channel (values ​​0, 1, 2, 3, a total of four channels), 0x91;
DAC function configuration, send 0x90, 0x40, user_data (8bit);
this time we use DAC.
2. Program part
2.1. The program is generally delayed, I2C protocol, sending data, and digital tube latching. It will not be introduced here. Refer to my previous blog respectively.
Link 1: Explanation of the program to control the on and off of a light by single-chip microcomputer
Link 2: Single-chip microcomputer - digital tube dynamic display
Link 3: Single-chip microcomputer - IIC protocol and 24C02
2.2, new part - the use of PCF8591

 DAC(uchar dat)
{
    
    
start();
 send(0x90);
 ack();
 
 send(0x40);
 ack();

 send(dat);
 ack();

 stop();

}

The basic principle
PCF8591 command sequence is: the host computer sends address command to it first, then the control word command, and finally the data command. DAC function configuration, just send 0x90, 0x40, user_data (8bit) successively; so this part of the program is relatively easy to understand, that is, open, send address command, reply, send control command, reply, send data, reply. stop.
2.3. Main function

void main()
{
    
    
uchar num;
delay(5);
cmg();
   while(1)
   {
    
    
   DAC(num);
   num++;
   delay(30);
   }
}

The specific flow of the program is as follows:
In the main function, the delay function and the cmg function are called first, which are used to delay and initialize the I2C bus respectively.

After entering the while loop, the DAC function is called first, and the value of num is output through the DAC of the PCF8591 chip. The function of the DAC function is to write the value of num to the DAC input terminal of the PCF8591 chip, so as to generate a corresponding voltage output at the output terminal of the PCF8591 chip.

Add 1 to the value of num for the next output.

Call the delay function to delay 30 milliseconds. Easy to display.

Repeat steps 2 to 4 to output increasing numbers in a loop.
3. Complete code

 #include<reg52.h>
 #include<intrins.h>
 #define uchar unsigned char
 #define uint unsigned int

 sbit duan=P2^6;
 sbit wei =P2^7;
 sbit RST =P2^4;
 sbit SCL=P2^1;
 sbit SDA=P2^0;

 void delay(uchar k)
{
    
    
  uchar i,j;
  for (i=0;i<k;i++)
  {
    
    
    for (j=0;j<113;j++)
    {
    
    
     ;
    }
  }
}


//iic部分
//start
void start()
{
    
    
SDA=1;
_nop_();
SCL=1;
_nop_(); 
SDA=0;
_nop_();
}

//Stop
void stop()
{
    
    
SDA=0;
_nop_();
SCL=1;
_nop_(); 
SDA=1;
_nop_();
}

//应答
void ack()
{
    
    
SDA=0;
_nop_();
SCL=1;
_nop_(); 
SCL=0;
_nop_();
}

//非应答
void noack()
{
    
    
SDA=1;
_nop_();
SCL=1;
_nop_(); 
SCL=0;
_nop_();
}

//发数据
void send(uchar dat)
{
    
      uchar i;
   for(i=0;i<8;i++)
   {
    
    
   SCL=0;
   _nop_();
   SDA=dat>>7;
   _nop_();
   SCL=1;
   _nop_();
  dat= dat<<1;
  }
  SCL=0;
}

//数码管锁存
void cmg()
{
    
    
duan=1;
P0=0x00;
duan=0;
wei=1;
P0=0x00;
wei=0;
RST=0;
}


//PCF先发地址,再发控制命令,最后发数据
 DAC(uchar dat)
{
    
    
start();
 send(0x90);
 ack();
 
 send(0x40);
 ack();

 send(dat);
 ack();

 stop();

}


void main()
{
    
    
uchar num;
delay(5);
cmg();
   while(1)
   {
    
    
   DAC(num);
   num++;//256溢出,8bit,2的八次方等于256
   delay(30);
   }
}

4. Experimental phenomenon
insert image description here
insert image description here

insert image description here
It is the effect of a dynamic breathing light.

Guess you like

Origin blog.csdn.net/m0_46155417/article/details/129538886