亲测代码可用 代码下载地址:
#include "stm32f10x.h"
void delay_ms(u16 nms);
void LEDXToggle(uint8_t ledx);
#include
int main()
{
uint16_t Temp_Value;
float ADC_Value=0.00;
while (SysTick_Config(SystemCoreClock/1000000));
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure; //??????????
RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA,ENABLE);
/*USART1_TX ->PA9*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //??????????
//????????
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //????????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//????9???
GPIO_Init(GPIOA , &GPIO_InitStructure); //????,?????????????
/*USART1_RX ->PA10*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
//??????
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA , &GPIO_InitStructure);
/*????????*/
USART_InitStructure.USART_BaudRate = 9600; //???
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //???8?
USART_InitStructure.USART_StopBits = USART_StopBits_1; //???1?
USART_InitStructure.USART_Parity = USART_Parity_No; //??? ?
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//????
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //?????????
USART_Init(USART1, &USART_InitStructure);
USART_ClearFlag(USART1,USART_FLAG_TC);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
USART_Cmd(USART1, ENABLE);
printf("*************************************************************\r\n");
printf("* *\r\n");
printf("* Thank you for using The Development Board Of YuanDi ! ^_^ *\r\n");
printf("* *\r\n");
printf("*************************************************************\r\n");
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* ADC1 configuration ------------------------------------------------------*/
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; /* ¶ÀÁ¢Ä£Ê½ */
ADC_InitStructure.ADC_ScanConvMode = ENABLE; /* Á¬Ðø¶àͨµÀģʽ */
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; /* Á¬Ðøת»» */
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; /* ת»»²»ÊÜÍâ½ç¾ö¶¨ */
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; /* ÓÒ¶ÔÆë */
ADC_InitStructure.ADC_NbrOfChannel = 1; /* ɨÃèͨµÀÊý */
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channel0 configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_239Cycles5);
ADC_Cmd(ADC1, ENABLE); /* Enable ADC1 */
ADC_SoftwareStartConvCmd(ADC1,ENABLE); /* ʹÄÜת»»¿ªÊ¼ */
while (1)
{
Temp_Value = ADC_GetConversionValue(ADC1);
//关于为什么要用Temp_Value*(3.3/4096)这个问题
//因为SDC输出的是一个没有单位的12位ADC值 最大的数字量是4096
//把数字量转换为有单位的值的时候就要用到参考电压来计算。得到多少伏的量
ADC_Value = (3.3/4096)*Temp_Value; /*½«ADֵת»»Îªµçѹֵ*/
printf("\r\n The Temp_Value is %d \r\n",Temp_Value);
printf("\r\n The Value is %f \r\n",ADC_Value);
// OLED_DisDecimal(10, 15, ADC_Value,4, &tFont12);
// LEDXToggle(LED1);
delay_ms(500);
}
}
void LEDXToggle(uint8_t ledx)
{
if(ledx==1)
{
GPIOC->ODR^=GPIO_Pin_13;
}
}
u16 nTime;
void SysTick_Handler(void)
{
if(nTime)
nTime--;
}
void delay_us(u16 nus)
{
nTime=nus;
while(nTime);
}
void delay_ms(u16 nms)
{
while(nms--)
delay_us(1000);
}
int fputc(int ch, FILE *f)
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(USART1, (uint8_t) ch);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{}
return ch;
}