先编写代码
#include "stdio.h"//ÉèÖÃtarget£¬use microlib,include"stdio.h"£¬Öض¨Òå·¢ËͽÓÊÕº¯Êý²Å¿ÉÓÃprintf.
#include "delay.h"
#include "rcc.h"
#include "usart.h"
#include "timer.h"
#include "adc.h"
#include "stm32f10x_adc.h"
int volt;
#define VREF 3.3
int fputc(int ch, FILE *f);
unsigned int temp0,temp1,temp2;
void PWM_TEST(void);
int main(void)
{
float Volt=0.00;
int ADValue = 0;
RCC_Configuration();
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
USART_int(9600);
ADC_CONFIG();
TIM2_PWM_Init(2400,0); //²»·ÖƵ¡£PWMƵÂÊ=72000000/900=80Khz
while(1)
{
ADValue = Get_Adc_Average(ADC_Channel_8,10); //PB0,¶ÔӦͨµÀ8
Volt = VREF*ADValue/4095;
/*voltµÄÖµÓÉVoltÖ±½Ó¾ö¶¨ voltÊÇVolt³ËÒÔ1000µÄÕûÐÍÖµ*/
volt=Volt*1000;
printf("²É¼¯µ½µÄµçѹÊÇ:%f V \r\n",Volt);
PWM_TEST();
delay_ms(400);
}
}
int fputc(int ch,FILE *f)
{
USART_SendData(USART1, (uint8_t) ch);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{}
return ch;
}
void PWM_TEST(void)
{
TIM_SetCompare2(TIM2,volt); //ͨµÀ2
}
void TIM2_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //ʹÄܶ¨Ê±Æ÷3ʱÖÓ
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); //ʹÄÜGPIOÍâÉèºÍAFIO¸´Óù¦ÄÜÄ£¿éʱÖÓ
GPIO_PinRemapConfig(GPIO_PartialRemap2_TIM2, ENABLE); //Timer
//ÉèÖøÃÒý½ÅΪ¸´ÓÃÊä³ö¹¦ÄÜ,Êä³öTIM3 CH2µÄPWMÂö³å²¨ÐÎ GPIOB.5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //TIM_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIO
//³õʼ»¯TIM3
TIM_TimeBaseStructure.TIM_Period = arr; //ÉèÖÃÔÚÏÂÒ»¸ö¸üÐÂʼþ×°Èë»î¶¯µÄ×Ô¶¯ÖØ×°ÔؼĴæÆ÷ÖÜÆÚµÄÖµ
TIM_TimeBaseStructure.TIM_Prescaler =psc; //ÉèÖÃÓÃÀ´×÷ΪTIMxʱÖÓƵÂʳýÊýµÄÔ¤·ÖƵֵ
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //ÉèÖÃʱÖÓ·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòÉϼÆÊýģʽ
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //¸ù¾ÝTIM_TimeBaseInitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯TIMxµÄʱ¼ä»ùÊýµ¥Î»
//³õʼ»¯TIM3 Channel2 PWMģʽ
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //Ñ¡Ôñ¶¨Ê±Æ÷ģʽ:TIMÂö³å¿í¶Èµ÷ÖÆģʽ2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±È½ÏÊä³öʹÄÜ
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //Êä³ö¼«ÐÔ:TIMÊä³ö±È½Ï¼«ÐÔ¸ß
TIM_OC2Init(TIM2, &TIM_OCInitStructure); //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM3 OC2
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable); //ʹÄÜTIM3ÔÚCCR2ÉϵÄԤװÔؼĴæÆ÷
TIM_Cmd(TIM2, ENABLE); //ʹÄÜTIM3
}
void ADC_CONFIG(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
ADC_DeInit(ADC1);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOB, ENABLE);
RCC_ADCCLKConfig(RCC_PCLK2_Div6); //ADC×î´óʱ¼ä²»Äܳ¬¹ý14M
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;//ADCËùÔڶ˿ÚPB0
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;//Ä£ÄâÊäÈëģʽ
GPIO_Init(GPIOB, &GPIO_InitStructure);
ADC_DeInit(ADC1); //¸´Î»ADC1,½«ÍâÉè ADC1 µÄÈ«²¿¼Ä´æÆ÷ÖØÉèΪȱʡֵ
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC¶ÀÁ¢¹¤×÷ģʽ
ADC_InitStructure.ADC_ScanConvMode = DISABLE; //µ¥Í¨µÀ»ò¶àͨµÀɨÃè
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //Á¬Ðø»òµ¥´Î
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //ÊÇ·ñÍⲿ´¥·¢À´Æô¶¯
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//ADCÊý¾ÝÓÒ¶ÔÆë
ADC_InitStructure.ADC_NbrOfChannel = 1;//ADCͨµÀÊýΪ1
ADC_Init(ADC1, &ADC_InitStructure);//³õʼ»¯ADC1
ADC_Cmd(ADC1, ENABLE); //ʹÄÜÖ¸¶¨µÄADC1
ADC_ResetCalibration(ADC1); //ʹÄܸ´Î»Ð£×¼
while(ADC_GetResetCalibrationStatus(ADC1)); //µÈ´ý¸´Î»Ð£×¼½áÊø
ADC_StartCalibration(ADC1); //¿ªÆôADУ׼
while(ADC_GetCalibrationStatus(ADC1)); //µÈ´ýУ׼½áÊø
}
int Get_ADC(u8 ch)
{
ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_55Cycles5);//ADC1¹æÔò×éͨµÀ£º8 £¬×ª»»Ë³Ðò£¬²ÉÑùʱ¼ä
ADC_SoftwareStartConvCmd(ADC1, ENABLE);//ʹÄÜADC1Èí¼þת»»¹¦ÄÜ
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));//µÈ´ýת»»½áÊø
return ADC_GetConversionValue(ADC1); //·µ»Ø×î½üÒ»´ÎµÄADC1¹æÔò×éת»»½á¹û
}
u16 Get_Adc_Average(u8 ch,u8 times)
{
u32 temp_val=0;
u8 t;
for(t=0;t