代码:
#include "ac78xx_adc.h"
#include "ac78xx_gpio.h"
#include "ac78xx.h"
#include "ac78xx_debugout.h"
#include "oled.h"
#include "bmp.h"
#define DISABLE 0
#define ENABLE 1
#define VOLTAGE_BG (1.2) ///<Bandgap Voltage
uint32_t g_adcValue = 0;
uint32_t g_displayValue = 0;
float g_vdd = 0; ///<Power Voltage
#define ADC_KEY_NUM 3 ///<define keypad number
static float s_ADC_DatMap[ADC_KEY_NUM] = {0.9, 0.6, 0.3}; ///<define KEY voltage
/**
* OSD_KEY
*
* @param[in] no
* @return no
*
* @brief Display select KEY pad
*/
void OSD_KEY()
{
ADC_SoftwareStartRegularConvCmd(ADC, ADC_ENABLE);
while (!ADC_GetIntFlag(ADC, ADC_FLAG_EOC));
udelay(1);
g_adcValue = ADC_GetRegularConversionValue(ADC);
if(g_adcValue < (4096 * s_ADC_DatMap[2])) ///<select KEY pad
{
OLED_ShowString(8, 4, "K5", 8);
}
else if(g_adcValue < (4096 * s_ADC_DatMap[1]))
{
OLED_ShowString(8, 4, "K4", 8);
}
else if(g_adcValue < (4096 * s_ADC_DatMap[0]))
{
OLED_ShowString(8, 4, "K3", 8);
}
else
{
OLED_ShowString(8, 4, "NO", 8);
}
}
/**
* OSD_VDD
*
* @param[in] no
* @return no
*
* @brief Display Power Voltage
*/
void OSD_VDD()
{
OLED_ShowString(8, 2, "VDD:", 8);
ADC_SoftwareStartRegularConvCmd(ADC, ADC_ENABLE);
while (!ADC_GetIntFlag(ADC, ADC_FLAG_EOC));
udelay(1);
g_adcValue = ADC_GetRegularConversionValue(ADC); ///<Float Voltage display
g_vdd = (VOLTAGE_BG / g_adcValue) * 4095;
g_displayValue = (uint32_t)g_vdd;
OLED_ShowNum(48, 2, g_displayValue, 1, 8);
OLED_ShowString(56, 2, ".", 8);
g_displayValue = (uint32_t)(g_vdd * 10) % 10;
OLED_ShowNum(64, 2, g_displayValue , 1, 8);
g_displayValue = (uint32_t)(g_vdd * 100) % 10; ///<Float Voltage display
OLED_ShowNum(72, 2, g_displayValue , 1, 8);
OLED_ShowString(80, 2, "V", 8);
}
/**
* main
*
* @param[in] none
* @return 0: success, other: error value
*
* @brief use ADC detect pressed KEYss
*/
int main(void)
{
ADC_InitType tempAdcConfig = {0, 0, 0, 0, 0, 0, 1};
ADC_TrigSourceType tempAdcTrigSource = {0, 0};
ADC_InitType* adcConfig;
ADC_TrigSourceType* adcTrigSource;
adcConfig = &tempAdcConfig;
adcTrigSource = &tempAdcTrigSource;
adcConfig->scanMode = 0;///ADC mode 0
adcConfig->continousMode = 0;
adcConfig->disContinousModeOnRegularGroup = 0;
adcConfig->disContinousModeOnInjectGroup = 0;
adcConfig->injectAutoMode = 0;
GPIO_SetFunc(8, 1); ///ADC_IN1 Analog function enable
ADC_Init(ADC, adcConfig); ///ADC works Mode Config
ADC_SetClockPrescaler(ADC, 4); ///Set ADC Prescaler
ADC_TrigSourceInit(ADC, adcTrigSource);
ADC_SetRegularGroupLength(ADC, 1);
ADC_SetRegularGroupSequence(ADC, 1, ADC_CHANNEL_AD16_BANDGAP); ///set ADC_CHANNEL_AD16_BANDGAP for AD Sample
ADC_ChannelSampleTimeSel(ADC, ADC_CHANNEL_AD1, ADC_SampleTime_14Cycle);///Set ADC_CHANNEL1 Sample Cycles
ADC_ChannelSampleTimeSel(ADC, ADC_CHANNEL_AD16_BANDGAP, ADC_SampleTime_14Cycle);///Set ADC_CHANNEL_AD16_BANDGAP Sample Cycles
ADC_Cmd(ADC, ENABLE);
InitDelay();
InitDebug(); /// Initialize debug UART 1
OLED_Init();
OLED_Clear();
printf("\r\nAC7811: ADC_Demo\r\n");
OLED_ShowString(0, 0, "ADC Demo", 8);
OLED_DrawBMP(64, 4, 128, 8, g_atc**BMP);
OSD_VDD();
OLED_ShowString(8, 3, "Pressed Keypad:", 8);
OLED_ShowString(8, 4, "NO Key", 8);
ADC_SetRegularGroupSequence(ADC, 1, ADC_CHANNEL_AD1); //set ADC_CHANNEL1 for AD Sample
mdelay(100);
while(1)
{
OSD_KEY();
mdelay(100);
}
}
效果图:
|