#include "adc.h"
/*
** CC2541 ADC引脚分别是P0.0~P07
*/
unsigned int Read_Vddvalue(unsigned char channel)
{
unsigned int value;
ADCCFG |= BV(channel);
ADCCON3 = (channel|HAL_ADC_DEC_064|HAL_ADC_REF_AVDD);
while (!(ADCCON1 & HAL_ADC_EOC));
ADCCFG &= (BV(channel) ^ 0xFF);
value = ADCL;
value |= ((unsigned int)ADCH)<<8;
return (value>>8);
}
//头文件===================================================
#ifndef __ADC_H__
#define __ADC_H__
#include "all.h"
/* Resolution */
#define HAL_ADC_RESOLUTION_8 0x01
#define HAL_ADC_RESOLUTION_10 0x02
#define HAL_ADC_RESOLUTION_12 0x03
#define HAL_ADC_RESOLUTION_14 0x04
/* Channels */
#define HAL_ADC_CHANNEL_0 0x00
#define HAL_ADC_CHANNEL_1 0x01
#define HAL_ADC_CHANNEL_2 0x02
#define HAL_ADC_CHANNEL_3 0x03
#define HAL_ADC_CHANNEL_4 0x04
#define HAL_ADC_CHANNEL_5 0x05
#define HAL_ADC_CHANNEL_6 0x06
#define HAL_ADC_CHANNEL_7 0x07
#define HAL_ADC_CHN_AIN0 0x00 /* AIN0 */
#define HAL_ADC_CHN_AIN1 0x01 /* AIN1 */
#define HAL_ADC_CHN_AIN2 0x02 /* AIN2 */
#define HAL_ADC_CHN_AIN3 0x03 /* AIN3 */
#define HAL_ADC_CHN_AIN4 0x04 /* AIN4 */
#define HAL_ADC_CHN_AIN5 0x05 /* AIN5 */
#define HAL_ADC_CHN_AIN6 0x06 /* AIN6 */
#define HAL_ADC_CHN_AIN7 0x07 /* AIN7 */
#define HAL_ADC_CHN_A0A1 0x08 /* AIN0,AIN1 */
#define HAL_ADC_CHN_A2A3 0x09 /* AIN2,AIN3 */
#define HAL_ADC_CHN_A4A5 0x0a /* AIN4,AIN5 */
#define HAL_ADC_CHN_A6A7 0x0b /* AIN6,AIN7 */
#define HAL_ADC_CHN_GND 0x0c /* GND */
#define HAL_ADC_CHN_VREF 0x0d /* Positive voltage reference */
#define HAL_ADC_CHN_TEMP 0x0e /* Temperature sensor */
#define HAL_ADC_CHN_VDD3 0x0f /* VDD/3 */
#define HAL_ADC_CHN_BITS 0x0f /* Bits [3:0] */
#define HAL_ADC_CHANNEL_TEMP HAL_ADC_CHN_TEMP
#define HAL_ADC_CHANNEL_VDD HAL_ADC_CHN_VDD3 /* channel VDD divided by 3 */
/* Vdd Limits */
#define HAL_ADC_VDD_LIMIT_0 0x00
#define HAL_ADC_VDD_LIMIT_1 0x01
#define HAL_ADC_VDD_LIMIT_2 0x02
#define HAL_ADC_VDD_LIMIT_3 0x03
#define HAL_ADC_VDD_LIMIT_4 0x04
#define HAL_ADC_VDD_LIMIT_5 0x05
#define HAL_ADC_VDD_LIMIT_6 0x06
#define HAL_ADC_VDD_LIMIT_7 0x07
/* Reference Voltages */
#define HAL_ADC_REF_125V 0x00 /* Internal Reference (1.25V-CC2430)(1.15V-CC2540) */
#define HAL_ADC_REF_AIN7 0x40 /* AIN7 Reference */
#define HAL_ADC_REF_AVDD 0x80 /* AVDD_SOC Pin Reference */
#define HAL_ADC_REF_DIFF 0xc0 /* AIN7,AIN6 Differential Reference */
#define HAL_ADC_REF_BITS 0xc0 /* Bits [7:6] */
#define HAL_ADC_EOC 0x80 /* End of Conversion bit */
#define HAL_ADC_START 0x40 /* Starts Conversion */
#define HAL_ADC_STSEL_EXT 0x00 /* External Trigger */
#define HAL_ADC_STSEL_FULL 0x10 /* Full Speed, No Trigger */
#define HAL_ADC_STSEL_T1C0 0x20 /* Timer1, Channel 0 Compare Event Trigger */
#define HAL_ADC_STSEL_ST 0x30 /* ADCCON1.ST =1 Trigger */
#define HAL_ADC_RAND_NORM 0x00 /* Normal Operation */
#define HAL_ADC_RAND_LFSR 0x04 /* Clock LFSR */
#define HAL_ADC_RAND_SEED 0x08 /* Seed Modulator */
#define HAL_ADC_RAND_STOP 0x0c /* Stop Random Generator */
#define HAL_ADC_RAND_BITS 0x0c /* Bits [3:2] */
#define HAL_ADC_DEC_064 0x00 /* Decimate by 64 : 8-bit resolution */
#define HAL_ADC_DEC_128 0x10 /* Decimate by 128 : 10-bit resolution */
#define HAL_ADC_DEC_256 0x20 /* Decimate by 256 : 12-bit resolution */
#define HAL_ADC_DEC_512 0x30 /* Decimate by 512 : 14-bit resolution */
#define HAL_ADC_DEC_BITS 0x30 /* Bits [5:4] */
#define HAL_ADC_STSEL HAL_ADC_STSEL_ST
#define HAL_ADC_RAND_GEN HAL_ADC_RAND_STOP
#define HAL_ADC_REF_VOLT HAL_ADC_REF_AVDD
#define HAL_ADC_DEC_RATE HAL_ADC_DEC_064
#define HAL_ADC_SCHN HAL_ADC_CHN_VDD3
#define HAL_ADC_ECHN HAL_ADC_CHN_GND
unsigned int Read_Vddvalue(unsigned char channel);
#endif
//例子==========================================
void DelayMS(uint16 msec)
{
uint16 i,j;
for (i=0; i<msec; i++)
for (j=0; j<536; j++);
}
void main(void)
{
char p_buf[]="hello world\r\n";
char buf[30];
unsigned int data;
CLKCONCMD &= ~0x40; //设置系统时钟源为32MHZ晶振
while(CLKCONSTA & 0x40); //等待晶振稳定
CLKCONCMD &= ~0x47; //设置系统主时钟频率为32MHZ
InitGPIO();
Uart_Init(UART_BAUDRATE_115200);
Uart_String(p_buf);
while(1) //死循环
{
data = Read_Vddvalue(HAL_ADC_CHANNEL_7);
sprintf(buf,"v=%d\r\n",data);
Uart_String(buf);
DelayMS(1000);
}
}
QQ图片20180830115252.png
网友评论