#include "stc8h.h" #include "eeprom.h" #include "stdio.h" #include "intrins.h"
typedef unsigned char u8; typedef unsigned int u16; typedef unsigned long u32;
/************* ±¾µØ³£Á¿ÉùÃ÷ **************/ //1~16,使用的ADC转换通道数量,必须和[ADC_DMA通道使能寄存器(DMA_ADC_CHSWx)]中启用的ADC通道数量一致 #define ADC_CH_NUM 1 //ADC转换次数,必须和[ADC_DMA配置寄存器2(DMA_ADC_CFG2)]设置的一致 #define ADC_SAMPLES_NUM 8 //每个通道ADC转换数据总字节数=2*转换次数+4 #define ADC_DATA_SIZE (ADC_SAMPLES_NUM*2 + 4)
//存储ADC DMA转换结果,即ADC DMA的目的地址 u8 xdata adc_samples_buff[ADC_CH_NUM][ADC_DATA_SIZE]; //ADC DMA转换完成标志 bit DmaFlag;
void Uart1_Init(void); //4800bps@11.0592MHz void sendByte(char dat); void sendString(unsigned char *dat); char putchar(char c); void adc_dma_config(void); void Delay500ms(void); //@11.0592MHz void ADC_Init(void);
void main() { u8 i,j; u16 adc_value; float voltage; Uart1_Init(); ADC_Init();
EA = 1; printf("Hello");
while(1) { if(DmaFlag) { DmaFlag = 0;
adc_value=(adc_samples_buff[0][16]<<8)+adc_samples_buff[0][17]; voltage = (5*adc_value)/4096; printf("voltage: %.2fV\r\n",voltage);
DMA_ADC_CR = 0xC0; } Delay500ms(); }
}
void adc_dma_config(void) { P_SW2 = 0x80; DMA_ADC_STA = 0x00; //清零ADC DMA状态寄存器 DMA_ADC_CFG = 0x80; //开启ADC DMA中断 DMA_ADC_RXAH = (u8)((u16)(&adc_samples_buff) >> 8); //ADC转换数据存储地址,即ADC DMA目的地址 DMA_ADC_RXAL = (u8)((u16)(&adc_samples_buff)); DMA_ADC_CFG2 = 0x0A; //每个通道ADC转换次数:8 DMA_ADC_CHSW0 = 0x01; //使能ADC通道1 DMA_ADC_CHSW1 = 0x00; DMA_ADC_CR = 0xC0; //启动ADC DMA转换 }
void ADC_Init(void) { P1M0=0x00; //设置P10为高阻输入 P1M1=0x01;
ADCTIM = 0x3F; //设置ADC内部时许 ADCCFG = 0x2F; //设置ADC时钟为系统时钟,数据右对齐 ADC_CONTR = 0x80; //使能ADC模块
}
void Uart1_Init(void) //4800bps@11.0592MHz { PCON &= 0x7F; //波特率不倍速 SCON = 0x50; //8位数据位,可变波特率 AUXR &= 0xBF; //定时器时钟12T模式 AUXR &= 0xFE; //串口1选择定时器1为波特率发生器 TMOD &= 0x0F; TMOD |= 0x20; TL1 = 0xFA; TH1 = 0xFA; ET1 = 0; TR1 = 1; }
void sendByte(char dat) { SBUF = dat; while(!TI); TI = 0; }
void sendString(unsigned char *dat) { while(*dat != '\0') { sendByte(*dat++); } }
char putchar(char c) { sendByte(c); return c; }
void ADC_DMA_Interrupt(void) interrupt 13 { DMA_ADC_STA = 0; DmaFlag = 1; }
void Delay500ms(void) //@11.0592MHz { unsigned char data i, j, k;
_nop_(); _nop_(); i = 22; j = 3; k = 227; do { do { while (--k); } while (--j); } while (--i); }
|