当前位置:网站首页>Single chip microcomputer: d/a output

Single chip microcomputer: d/a output

2022-06-13 03:53:00 DC-STDIO

List of articles


Preface

Today, I would like to introduce the basic concepts of single chip microcomputer ,D/A Output


D/A Output

D/A Is and A/D Just in the opposite direction , One 8 Bit D/A, from 0~255, On behalf of 0~2.55 V Words , Then we use MCU to send the third byte 100,D/A The pin will output a 1 V The voltage of , send out 200 Just output one 2 V The voltage of , It's simple , We use a simple program to realize , And through 、 Press the button to increase or decrease the output amplitude value , Increase or decrease each time 0.1 V. If you have a multimeter , You can test it directly on the board AOUT Output voltage at point , Watch it change . because PCF8591 Of DA The maximum output bias error is 50 mv( Provided by the data book ), Therefore, the error between the voltage value measured by the multimeter and the theoretical value should be within 50 mV within .

#include <reg52.h>
unsigned char T0RH = 0; //T0  High byte of overloaded value 
unsigned char T0RL = 0; //T0  The low byte of the overloaded value 
void ConfigTimer0(unsigned int ms);
extern void KeyScan();
extern void KeyDriver();
extern void I2CStart();
extern void I2CStop();
extern bit I2CWrite(unsigned char dat);

void main(){
    
    EA = 1; // General interruption 
    ConfigTimer0(1); // To configure  T0  timing  1ms

    while (1){
    
        KeyDriver(); // Call the key driver 
    }
}
/*  Set up  DAC  Output value ,val- The set value  */
void SetDACOut(unsigned char val){
    
    I2CStart();
    if (!I2CWrite(0x48<<1)){
     // Addressing  PCF8591, If not answered , Stop the operation and return to 
        I2CStop();
        return;
    }
    I2CWrite(0x40); // Write control byte 
    I2CWrite(val); // write in  DA  value 
    I2CStop();
}
/*  Key action function , Perform the corresponding operation according to the key code ,keycode- Key code  */
void KeyAction(unsigned char keycode){
    
    static unsigned char volt = 0; // Output voltage value , Implies a decimal decimal place 
    if (keycode == 0x26){
     // Up key , increase  0.1V  Voltage value 
        if (volt < 25){
    
            volt++;
            SetDACOut(volt*255/25); // Convert to  AD  Output value 
        }
    }else if (keycode == 0x28){
     // Down key , Reduce  0.1V  Voltage value 
        if (volt > 0){
    
            volt--;
            SetDACOut(volt*255/25); // Convert to  AD  Output value 
        }
    }
}
/*  Configure and start  T0,ms-T0  Timing time  */
void ConfigTimer0(unsigned int ms){
    
    unsigned long tmp; // Temporary variable 
    tmp = 11059200 / 12; // The timer counts the frequency 
    tmp = (tmp * ms) / 1000; // Calculate the required count 
    tmp = 65536 - tmp; // Calculate timer overload value 
    tmp = tmp + 28; // Compensate for the error caused by interrupt response delay 
    T0RH = (unsigned char)(tmp>>8); // The timer overload value is split into high and low bytes 
    T0RL = (unsigned char)tmp;
    TMOD &= 0xF0; // Zero clearing  T0  Control bit of 
    TMOD |= 0x01; // To configure  T0  For mode  1
    TH0 = T0RH; // load  T0  Overload value 
    TL0 = T0RL;
    ET0 = 1; // Can make  T0  interrupt 
    TR0 = 1; // start-up  T0
}
/* T0  Interrupt service function , Perform key scan  */
void InterruptTimer0() interrupt 1{
    
    TH0 = T0RH; // Reload overloaded values 
    TL0 = T0RL;
    KeyScan(); // Key scan 
}

原网站

版权声明
本文为[DC-STDIO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206130337493567.html