STC实验箱4 IAP15W4K58S4 Keil uVision V5.29.0.0 PK51 Prof.Developers Kit Version:9.60.0.0
硬知识
摘自《STC库函数使用参考》
ADC初始化函数ADC_Inilize ADC_InitTypeDef的定义见于文件"ADC.H"。
typedef struct {u8 ADC_Px; //设置要做ADC的IO, ADC_P10~ADC_P17, ADC_P1_Allu8 ADC_Speed; //ADC速度ADC_90T, ADC_180T, ADC_360T, ADC_540T u8 ADC_Power; //ADC功率允许/关闭 ENABLE, DISABLE u8 ADCAdjResult;//ADC结果调整, ADC_RES_H2L8, ADC_RES_H8L2u8 ADC_Polity; //优先级设置 PolityHigh, PolityLow u8 ADC_Interrupt; //中断允许 ENABLE, DISABLE}ADC_InitTypeDef;ADC_Px:设置要做ADC的IO: 以上参数可以使用或运算,比如:
ADC_InitStructure.ADC_Px = ADC_P10 | ADC_P11 | ADC_P12;ADC_Speed:设置ADC的速度: ADC_Power:ADC电源控制: ADC_AdjResult:ADC结果调整: ADC_Polity:中断的优先级: ADC_Interrupt:中断允许或禁止:
ADC电源控制函数ADC_PowerControl
ADC查询转换函数GetADC_10bitResult
测试main.c #include "./Drivers/config.h"#include "./Drivers/delay.h"#include "./Drivers/adc.h"#include "./Drivers/soft_uart.h"#include <stdio.h>char putchar(char Char){TxSend(Char);return Char;}/*------------------------------------------------------------------*//* --- STC MCU International Limited -------------------------------*//* --- STC 1T Series MCU RC Demo -----------------------------------*//* --- Mobile: (86)13922805190 -------------------------------------*//* --- Fax: 86-0513-55012956,55012947,55012969 ---------------------*//* --- Tel: 86-0513-55012928,55012929,55012966 ---------------------*//* --- Web: www.GXWMCU.com -----------------------------------------*//* --- QQ: 800003751 ----------------------------------------------*//* If you want to use the program or the program referenced in the *//* article, please specify in which data and procedures from STC *//*------------------------------------------------------------------*//*************功能说明**************本程序演示多路ADC查询采样,通过模拟串口发送给上位机,波特率9600,8,n,1。用户可以修改为1~8路的ADC转换。******************************************//************* 外部函数和变量声明 *****************/void ADC_config(void){ADC_InitTypeDef ADC_InitStructure;//结构定义ADC_InitStructure.ADC_Px = ADC_P12 | ADC_P13 | ADC_P14;//设置要做ADC的IO,ADC_P10 ~ ADC_P17(或操作),ADC_P1_AllADC_InitStructure.ADC_Speed = ADC_540T;//ADC速度ADC_90T,ADC_180T,ADC_360T,ADC_540TADC_InitStructure.ADC_Power = ENABLE;//ADC功率允许/关闭ENABLE,DISABLEADC_InitStructure.ADC_AdjResult = ADC_RES_H8L2;//ADC结果调整,ADC_RES_H2L8,ADC_RES_H8L2ADC_InitStructure.ADC_Polity = PolityLow;//优先级设置PolityHigh,PolityLowADC_InitStructure.ADC_Interrupt = DISABLE;//中断允许ENABLE,DISABLEADC_Inilize(&ADC_InitStructure);//初始化ADC_PowerControl(ENABLE);//单独的ADC电源操作函数, ENABLE或DISABLE}/******************** 主函数**************************/void main(void){u8 i;u16 tempValue;ADC_config();printf("OK\r\n");while (1){for(i = 2; i < 5; i++){tempValue = Get_ADC10bitResult(i);printf("%f, ", (float)tempValue * 5 / 1024); //参数0~7,查询方式做一次ADC, 返回值就是结果, == 1024 为错误}printf("%d\r\n", (u16)(tempValue / 64. + 0.5));//发送键值}} 实验现象
按下按键后,键值随之改变 我们发现ADC2的波形非常混乱,实测如下
可以看到本应为2.5V的基准电压上竟有幅值达280mV的纹波
查看431的手册,其原因是431的带容性负载能力不是很好,当负载电容CL=0.01uFC_L=0.01uFCL=0.01uF~5uF5uF5uF范围之内时就可能引发自激振荡。 可能是画板子的老哥由于习惯顺手画上去的,结果事与愿违,产生了自激振荡。 尝试将其摘除 可见得到的2.5V基准电压已非常平稳