今天搞了一下AD采集热敏电阻值。
原理图:
代码:
#include "main.h"
/** @addtogroup Examples
@{
*/
/** @addtogroup ADC_ContinuousConversion
@{
*/
/** @defgroup ADC_ContinuousConversion_Macros Macros
@{
*/
/* printf function configs to USART1 */
#define DEBUG_USART USART1
/**@} end of group ADC_ContinuousConversion_Macros*/
/** @defgroup ADC_ContinuousConversion_Functions Functions
@{
*/
void ADC_Init(void);
void Delay(uint32_t count);
/*!
* [url=home.php?mod=space&uid=247401]@brief[/url] Main program
*
* @param None
*
* @retval None
*
*/
int main(void)
{
USART_Config_T USART_ConfigStruct;
/* USART config */
USART_ConfigStruct.baudRate = 115200;
USART_ConfigStruct.hardwareFlow = USART_HARDWARE_FLOW_NONE;
USART_ConfigStruct.mode = USART_MODE_TX;
USART_ConfigStruct.parity = USART_PARITY_NONE;
USART_ConfigStruct.stopBits = USART_STOP_BIT_1;
USART_ConfigStruct.wordLength = USART_WORD_LEN_8B;
APM_MINI_COMInit(COM1, &USART_ConfigStruct);
/* ADC1 initialization */
ADC_Init();
while(1)
{
printf("\r\n voltage : mV\r\n");
}
}
/*!
* @brief ADC Init
*
* @param None
*
* @retval None
*/
void ADC_Init(void)
{
GPIO_Config_T gpioConfig;
ADC_Config_T adcConfig;
/* Enable GPIOA clock */
RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOA);
/* ADC channel 0 configuration */
GPIO_ConfigStructInit(&gpioConfig);
gpioConfig.mode = GPIO_MODE_ANALOG;
gpioConfig.pin = GPIO_PIN_0;
GPIO_Config(GPIOA, &gpioConfig);
/* ADCCLK = PCLK2/4 */
RCM_ConfigADCCLK(RCM_PCLK2_DIV_4);
/* Enable ADC clock */
RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_ADC1);
/* ADC configuration */
ADC_Reset(ADC1);
ADC_ConfigStructInit(&adcConfig);
adcConfig.mode = ADC_MODE_INDEPENDENT;
adcConfig.scanConvMode = DISABLE;
adcConfig.continuosConvMode = ENABLE;
adcConfig.externalTrigConv = ADC_EXT_TRIG_CONV_None;
adcConfig.dataAlign = ADC_DATA_ALIGN_RIGHT;
/* channel number */
adcConfig.nbrOfChannel = 1;
ADC_Config(ADC1, &adcConfig);
/* ADC channel Convert configuration */
ADC_ConfigRegularChannel(ADC1, ADC_CHANNEL_0, 1, ADC_SAMPLETIME_13CYCLES5);
/* Enable complete conversion interupt */
ADC_EnableInterrupt(ADC1, ADC_INT_EOC);
/* NVIC configuration */
NVIC_EnableIRQRequest(ADC1_2_IRQn, 1, 1);
/* Enable ADC */
ADC_Enable(ADC1);
/* Enable ADC1 reset calibration register */
ADC_ResetCalibration(ADC1);
/* Check the end of ADC1 reset calibration register */
while(ADC_ReadResetCalibrationStatus(ADC1));
/* Start ADC1 calibration */
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
while(ADC_ReadCalibrationStartFlag(ADC1));
/* Start ADC1 Software Conversion */
ADC_EnableSoftwareStartConv(ADC1);
}
/*!
* @brief ADC1 Isr
*
* @param None
*
* @retval None
*
* [url=home.php?mod=space&uid=536309]@NOTE[/url] This function need to put into ADC1_2_IRQHandler in apm32f10x_int.c
*/
void ADC1_Isr(void)
{
uint16_t adcData = 0;
uint16_t voltage = 0;
if (ADC_ReadIntFlag(ADC1, ADC_INT_EOC) == SET)
{
ADC_ClearIntFlag(ADC1, ADC_INT_EOC);
/* Read ADC Conversion value */
adcData = ADC_ReadConversionValue(ADC1);
/* voltage(mV) = adcData * (3300mV / 4095) */
voltage = (adcData * 3300) / 4095;
printf("\r\n voltage : %d mV\r\n", voltage);
}
}
/*!
* @brief Delay
*
* @param count: delay count
*
* @retval None
*/
void Delay(uint32_t count)
{
uint16_t i = 0;
while (count--)
{
i = 7995;
while (i--);
}
}
#if defined (__CC_ARM) || defined (__ICCARM__) || (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))
/*!
* @brief Redirect C Library function printf to serial port.
* After Redirection, you can use printf function.
*
* @param ch: The characters that need to be send.
*
* @param *f: pointer to a FILE that can recording all information
* needed to control a stream
*
* @retval The characters that need to be send.
*
* @note
*/
int fputc(int ch, FILE* f)
{
/* send a byte of data to the serial port */
USART_TxData(DEBUG_USART, (uint8_t)ch);
/* wait for the data to be send */
while (USART_ReadStatusFlag(DEBUG_USART, USART_FLAG_TXBE) == RESET);
return (ch);
}
#elif defined (__GNUC__)
/*!
* @brief Redirect C Library function printf to serial port.
* After Redirection, you can use printf function.
*
* @param ch: The characters that need to be send.
*
* @retval The characters that need to be send.
*
* @note
*/
int __io_putchar(int ch)
{
/* send a byte of data to the serial port */
USART_TxData(DEBUG_USART, ch);
/* wait for the data to be send */
while (USART_ReadStatusFlag(DEBUG_USART, USART_FLAG_TXBE) == RESET);
return ch;
}
/*!
* @brief Redirect C Library function printf to serial port.
* After Redirection, you can use printf function.
*
* @param file: Meaningless in this function.
*
* @param *ptr: Buffer pointer for data to be sent.
*
* @param len: Length of data to be sent.
*
* @retval The characters that need to be send.
*
* @note
*/
int _write(int file, char* ptr, int len)
{
int i;
for (i = 0; i < len; i++)
{
__io_putchar(*ptr++);
}
return len;
}
#else
#warning Not supported compiler type
#endif
/**@} end of group ADC_ContinuousConversion_Functions */
/**@} end of group ADC_ContinuousConversion */
/**@} end of group Examples */
效果图:
不摸热敏电阻
用手摸热敏电阻
|