附上程序:
#include "stm8l15x.h"
#include "stm8_eval.h"
/** @addtogroup STM8L15x_StdPeriph_Examples
* @{
*/
/** @addtogroup ADC_AnalogWatchdog
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define ADC_RATIO ((uint16_t) 806) /*ADC_RATIO = ( 3.3 * 1000 * 1000)/4095 */
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
uint16_t ADCdata = 0;
uint16_t PotVoltage = 0;
/* Private function prototypes -----------------------------------------------*/
static void ADC_Config(void);
static void USART_Config(void);
void SendVoltage(uint16_t Voltage);
/* Private functions ---------------------------------------------------------*/
/**
* @brief Main program.
* @param None
* @retval None
*/
void main(void)
{
uint16_t i;
/* ADC configuration -------------------------------------------*/
ADC_Config();
/* USART configuration -------------------------------------------*/
USART_Config();
while (1)
{
/* Calculate voltage value*/
PotVoltage = (uint16_t)((uint32_t)((uint32_t)ADCdata * (uint32_t)ADC_RATIO) / (uint32_t)1000);
/* Display voltage value on LCD*/
SendVoltage((uint16_t)PotVoltage);
for(i=0;i<30000;i++)
{
;
}
}
}
static void ADC_Config(void)
{
/* Enable ADC1 clock */
CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, ENABLE);
/* Initialise and configure ADC1 */
ADC_Init(ADC1, ADC_ConversionMode_Continuous, ADC_Resolution_12Bit, ADC_Prescaler_2);
ADC_SamplingTimeConfig(ADC1, ADC_Group_SlowChannels, ADC_SamplingTime_384Cycles);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Enable ADC1 Channel 2 */
ADC_ChannelCmd(ADC1, ADC_Channel_2, ENABLE);
/* Enable Analog watchdog ADC1 Interrupt */
ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE);
/* Enable Interrupts */
enableInterrupts();
/* Start ADC1 Conversion using Software trigger*/
ADC_SoftwareStartConv(ADC1);
}
/**
* @brief Configure USART peripheral
* @param None
* @retval None
*/
static void USART_Config(void)
{
/* USART configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- Odd parity
- Receive and transmit enabled
- USART Clock disabled
*/
STM_EVAL_COMInit(COM1, (uint32_t)115200, USART_WordLength_8b, USART_StopBits_1,
USART_Parity_No, (USART_Mode_TypeDef)(USART_Mode_Tx | USART_Mode_Rx));
/* Enable general interrupts */
enableInterrupts();
/* Enable the USART Transmit complete interrupt: this interrupt is generated when the USART
transmit Shift Register is empty */
USART_ITConfig(EVAL_COM1, USART_IT_RXNE, ENABLE);
USART_ITConfig(EVAL_COM1, USART_IT_TC, ENABLE);
/* Enable USART */
USART_Cmd(EVAL_COM1, ENABLE);
}
/**
* @brief Display the current RV voltage on the LCD.
* @param voltage: specifies the voltage to display.
* @retval None
*/
void SendVoltage(uint16_t Voltage)
{
uint8_t i,tmp[3];
if(i>10) return;
for(i=0; i<4; i++)
{
tmp[i] = PotVoltage%10; //tmp[0]为个位, tmp[1]为十位, 依次类推
PotVoltage = PotVoltage/10;
}
//先发高位,再位低位,最后是个位
for(i=0; i<4; i++)
USART_SendData8(EVAL_COM1, (tmp[3-i]+0x30));
USART_SendData8(EVAL_COM1, '\n');
} |