單片機應用——電壓采集器


該應用主要功能是將輸出端電壓經過電阻分壓為目標范圍電壓,通過MCU的ADC轉換成數值,通過串口發送給433模塊,使用此方式輸出電壓值,在電腦端連接433接口模塊可以讀取電壓值。原理圖見

P1

電阻分壓范圍必須固定在ADC有效采樣范圍之內,20V電壓需要做1/4分壓到5V以內。

九齊MCU擁有10位ADC,可有效的采集電壓0.0048v.

詳細內容見源代碼

/* =========================================================================
 * Project:       GPIO_Setting
 * File:          main.c
 * Description:   Set GPIO of PORTA/PORTB
 *                  1. PORTB I/O state
 *                      - PB3 ~ PB2 set input mode and enable pull-low resistor
 *                      - PB1 ~ PB0 set open-drain output mode                               
 *                              
 *                  2. PORTA I/O state
 *                      - PA3 ~ PA2 set output mode 
 *                      - PA1 ~ PA0 set input mode and enable pull-low resistor                                
 * Author:        JasonLee
 * Version:       V1.1                              
 * Date:          2018/09/12
 =========================================================================*/
#include <ny8.h>
#include "ny8_constant.h"
#include "stdint.h"
#define UPDATE_REG(x)    __asm__("MOVR _" #x ",F")

//variable
unsigned int  R_AIN0_DATA;    
unsigned char R_AIN0_DATA_LB;            
unsigned int  R_Quarter_VDD_DATA;    
unsigned char R_Quarter_VDD_DATA_LB;    
unsigned int  advalue = 0;
unsigned int  advalue_tr = 0;
unsigned char adv_tr = 0;

void F_AIN0_Convert(char);
//void F_Quarter_VDD_Convert(char);
void F_wait_eoc(void);
void delay(int);
void uarttrx(unsigned char data);

void main(void)
{
    //;Initial GPIO  
    //   ; PORTB I/O state
    //     ; PB3 ~ PB2 set input mode and enable pull-low resistor
    //     ; PB1 ~ PB0 set open-drain output mode   
    IOSTB = 0x00;
    PORTB = 0x00;                    // PB3 為低輸出
    //   ; PORTA I/O state
    //     ; PA3 ~ PA2 set output mode and outputs high
    //     ; PA1 ~ PA0 set input mode and enable pull-high resistor    
    IOSTA = C_PA4_Input | C_PA5_Input | C_PA0_Input;        // PA4 & PA5 set output mode ; PA1 & PA0 set input mode
    APHCON = (unsigned char)~( C_PA2_PHB ); // Enable PA1、PA0 Pull-High Resistor,others disable
    PORTA = 0x04;                            // PA3 & PA2 output high

    //  initial ADC
    //----- Initial ADC-----      
    ADMD  =  C_ADC_CH_Dis | C_ADC_PA0 ;    // Enable ADC power, Disable global ADC input channel, Select PB3 pad as ADC input (SFR "ADMD")
     //----- ADC high reference voltage source select-----
     ADVREFH = C_Vrefh_VDD;                    // ADC reference high voltage is supplied by VDD
    //----- ADC clock frequency select----------------------------     
    ADR      = C_Ckl_Div8;                        // ADC clock=Fcpu/8, Clear ADIF, disable ADC interrupt    
    //----- ADC Sampling pulse width select-------------
     //ADCR  = C_Sample_1clk | C_12BIT | C_PB3_AIN8 ;        // Sample pulse width=1 adc clock, ADC select 12-bit conversion,PB ADC input ( Note: ADC clock freq. must be equal or less than 500KHz)
    ADCR = C_Sample_1clk | C_12BIT ;
    //--------------------------------------------------
    PACON = C_PA0_AIN0;    
    ADMDbits.GCHS = 1;                        // Enable global ADC channel    (SFR "ADMD")
    ADMDbits.ADEN = 1;
    delay(50);                                // Delay 0.56ms(Instru
    //initial wrl_md
    
    //
    while(1)
    {
        //CLRWDT();
        R_AIN0_DATA=R_AIN0_DATA_LB=R_Quarter_VDD_DATA=R_Quarter_VDD_DATA_LB=0x00;            
        //DISI();
        F_AIN0_Convert(8);                    // execute AIN0 ADC converting 8 times
        //ENI();
        R_AIN0_DATA <<= 4;                    // R_AIN0_DATA shift left 4 bit
        R_AIN0_DATA_LB &= 0xF0;                // Only get Bit7~4
        R_AIN0_DATA += R_AIN0_DATA_LB;        // R_AIN0_DATA + R_AIN0_DATA_LB
        R_AIN0_DATA >>=3;                    // R_AIN0_DATA divided 8  
        advalue = R_AIN0_DATA;
        //tr 0_500
        if(advalue > 4096)
        {
            advalue = 0;
        }
        advalue_tr = (unsigned int)(((unsigned long)(advalue) * 125) / 1024) ;
        //uart_trx
        adv_tr = (unsigned char)(advalue_tr);
        uarttrx(adv_tr);
        adv_tr = (unsigned char)(advalue_tr >> 8);
        uarttrx(adv_tr);
         
    }
}

void uarttrx(unsigned char data)
{
    unsigned char i,trdata;
    trdata = data;
    //TR_START    
    PORTAbits.PA2 = 0;
    //延時110us
    delay(20);
    for( i = 0;i < 8;i++ )
    {
        if(trdata & 0x01)
        {
            PORTAbits.PA2 = 1;
        }
        else
        {
            PORTAbits.PA2 = 0;
        }
        trdata >>= 1;
        //延時110us
        delay(18);    
    }
    
    //TR_STOP
    PORTAbits.PA2 = 1;
    //延時150us
    delay(30);
}


//----- Sub-Routine ----- 
void F_AIN0_Convert(char count)
{
      char i;
      ADMD  = 0x90 | C_ADC_PA0;                // Select AIN0(PA0) pad as ADC input
      CLRWDT();    
      for(i=1;i<=count;i++)
      {                  
       ADMDbits.START = 1;                    // Start a ADC conversion session
       F_wait_eoc();                            // Wait for ADC conversion complete
       R_AIN0_DATA_LB += ( 0x0F & ADR); 
       R_AIN0_DATA    += ADD; 
      }
}

void F_wait_eoc(void)
{
   while(ADMDbits.EOC==0);
}

//delay(20);  _107US
//delay(30);  _157US
void delay(int count)
{
    int i;
    for(i=1;i<=count;i++);
}

8位機(單片機)項目合作聯系我:18665321219


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM