|
51單片機多路AD轉(zhuǎn)換,串口發(fā)送數(shù)據(jù)- #include "config.h"
- #include "adc.h"
- #include "delay.h"
- #include "soft_uart.h"
- /************* 功能說明 **************
- 本程序演示多路ADC查詢采樣,通過模擬串口發(fā)送給上位機,波特率9600,8,n,1。
- 用戶可以修改為1~8路的ADC轉(zhuǎn)換。
- ******************************************/
- /************* 本地常量聲明 **************/
- /************* 本地變量聲明 **************/
- /************* 本地函數(shù)聲明 **************/
- /************* 外部函數(shù)和變量聲明 *****************/
- void ADC_config(void)
- {
- ADC_InitTypeDef ADC_InitStructure; //結(jié)構(gòu)定義
- ADC_InitStructure.ADC_Px = ADC_P10 | ADC_P11 | ADC_P12; //設(shè)置要做ADC的IO, ADC_P10 ~ ADC_P17(或操作),ADC_P1_All
- ADC_InitStructure.ADC_Speed = ADC_360T; //ADC速度 ADC_90T,ADC_180T,ADC_360T,ADC_540T
- ADC_InitStructure.ADC_Power = ENABLE; //ADC功率允許/關(guān)閉 ENABLE,DISABLE
- ADC_InitStructure.ADC_AdjResult = ADC_RES_H8L2; //ADC結(jié)果調(diào)整, ADC_RES_H2L8,ADC_RES_H8L2
- ADC_InitStructure.ADC_Polity = PolityLow; //優(yōu)先級設(shè)置 PolityHigh,PolityLow
- ADC_InitStructure.ADC_Interrupt = DISABLE; //中斷允許 ENABLE,DISABLE
- ADC_Inilize(&ADC_InitStructure); //初始化
- ADC_PowerControl(ENABLE); //單獨的ADC電源操作函數(shù), ENABLE或DISABLE
- }
- /**********************************************/
- void main(void)
- {
- u8 i;
- u16 j;
- ADC_config();
- while (1)
- {
- for(i=0; i<3; i++)
- {
- delay_ms(250);
- // Get_ADC10bitResult(i); //參數(shù)0~7,查詢方式做一次ADC, 丟棄一次
- j = Get_ADC10bitResult(i); //參數(shù)0~7,查詢方式做一次ADC, 返回值就是結(jié)果, == 1024 為錯誤
- TxSend('A');
- TxSend('D');
- TxSend(i+'0');
- TxSend('=');
- TxSend(j/1000 + '0');
- TxSend(j%1000/100 + '0');
- TxSend(j%100/10 + '0');
- TxSend(j%10 + '0');
- TxSend(' ');
- TxSend(' ');
- }
- PrintString("\r\n");
- }
- }
復制代碼 |
|