📄 adc.c
字号:
/******************************************************************************
* *
* ********** *
* ************ *
* *** *** *
* *** ++ *** *
* *** + + *** CHIPCON *
* *** + *
* *** + + *** *
* *** ++ *** *
* *** *** *
* ************ *
* ********** *
* *
*******************************************************************************
Filename: adc.c
Target: cc2430
Author: KJA
Revised: 16/12-2005
Revision: 1.0
Description:
This application shows how to use the AD-converter (single sample mode).
When the potmeter at the Evaluation Board is turned, the output voltage is
sampled, converted and written to LCD. The blinking speed of the green and
yellow LEDs is adjusted according to the voltage.
The direction of the joystick is shown on the LCD.
******************************************************************************/
#include "stdio.h"
#include "lcd.h"
// Prototypes
void initAdc(void);
void adc_main(void);
void updateVoltageLCD(int8 potVoltage, int8 adc_value);
void updateJoystickDirectionLCD(JOYSTICK_DIRECTION direction);
void updateCounter(int8 delay);
int8 scaleValue(int8 adc_value);
/******************************************************************************
* @fn initAdc
*
* @brief
* Initializes components for use with the ADC application example (e.g.
* LEDs, PotMeter, Joystick).
*
* Parameters:
*
* @param void
*
* @return void
*
******************************************************************************/
void initAdc(void)
{
initLcd();
SET_MAIN_CLOCK_SOURCE(CRYSTAL);
//init LEDs
INIT_GLED();
INIT_YLED();
INIT_POT();
INIT_JOYSTICK();
}
/******************************************************************************
* @fn adc_main
*
* @brief
* Main function of the ADC application example.
*
* Parameters:
*
* @param void
*
* @return void
*
******************************************************************************/
void main(void){
int8 adc_value;
uint8 potVoltage = 0;
bool updateLCD = TRUE;
initAdc();
while(1){
ADC_ENABLE_CHANNEL(7);
ADC_SINGLE_CONVERSION(ADC_REF_AVDD | ADC_8_BIT | ADC_AIN7);
ADC_SAMPLE_SINGLE();
while(!ADC_SAMPLE_READY());
ADC_DISABLE_CHANNEL(7);
adc_value = ADCH;
if (potVoltage != scaleValue(adc_value)){
//potVoltage changed since last sample
potVoltage = scaleValue(adc_value);
updateLCD = TRUE;
}
if (updateLCD){
updateVoltageLCD(potVoltage, adc_value);
updateLCD = FALSE;
}
updateCounter(adc_value);
}
}
/******************************************************************************
* @fn updateCounter
*
* @brief
* Function for updating counting speed for binary counter
*
* Parameters:
*
* @param int8 delay
* New counting delay
*
* @return void
*
******************************************************************************/
void updateCounter(int8 delay)
{
static uint8 counter = 0;
uint16 i = 0;
i = ((delay > 0) ? 0x7F - delay : 0x7F);
halWait( i );
counter++;
SET_LED_MASK( (byte)counter );
}
/******************************************************************************
* @fn scaleValue
*
* @brief
* Function for scaling ADC value according to current VDD
*
* Parameters:
*
* @param int8 adc_value
* Sampled ADC value
*
* @return int8
* Scaled value (output voltage)
*
******************************************************************************/
#define VDD 33
// return 10 times sampled voltage
int8 scaleValue(int8 adc_value)
{
//0x0000 = 0V and 0x0F = 3.3V
float v;
adc_value = (adc_value > 0 ? adc_value : 0);
v = ((float)adc_value / (float)0x7F);
v *= VDD;
return (int8)v;
}
/******************************************************************************
* @fn updateVoltageLCD
*
* @brief
* Function for printing PotVoltage and sampled ADC value to LCD.
*
* Parameters:
*
* @param int8 potVoltage
* Potmeter voltage
*
* int8 adc_value
* Sampled ADC value
*
* @return void
*
******************************************************************************/
void updateVoltageLCD(int8 potVoltage, int8 adc_value)
{
char s[16];
// potVoltage is 10 times correct voltage
// print dc value on LCD
sprintf(s, (char*)"%d.%d Volt (%d)", ((int16)(potVoltage / 10)), ((int16)(potVoltage % 10)), adc_value);
lcdUpdate((char*)"Voltage is:", s);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -