单片机应用——电压采集器

该应用主要功能是将输出端电压经过电阻分压为目标范围电压,通过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

posted @ 2021-10-27 14:41  steven_lg  阅读(933)  评论(0编辑  收藏  举报