#include "stm32f10x.h"
#include "stdio.h"
void NVIC_Config(void);
void TIM2_GPIO_Config(void);
void TIM2_Mode_Config(void);
void TIM2_IRQHandler(void);
void TIM3_GPIO_Configuration(void);
void TIM3_Mode_Configuration(void);
void USART1_Configuration(void);
int fputc(int ch, FILE *f);
void USART1_Putc(u8 c);
void USART1_Puts(char * str);
void Delay(void);
__IO uint32_t IC2ReadValue1 = 0, IC2ReadValue2 = 0;
__IO uint32_t CaptureNumber = 0;
__IO uint32_t Capture = 0;
__IO uint32_t TIM2Freq = 0;
int main(void)
{
NVIC_Config();
TIM2_GPIO_Config();
TIM2_Mode_Config();
TIM3_GPIO_Configuration();
TIM3_Mode_Configuration();
USART1_Configuration();
TIM_Cmd(TIM2, ENABLE);
TIM_Cmd(TIM3, ENABLE);
TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE);
while(1)
{
printf("%d
", TIM2Freq);
}
}
void TIM2_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void TIM2_Mode_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_TimeBaseStructure.TIM_Period = 60000;
/* 时钟预分频数 72M/1+1 = 36M 所采集的频率的最小值应该为 36M/60000=600HZ*/
TIM_TimeBaseStructure.TIM_Prescaler =1 ;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/*输入捕获设置*/
/*设置频道1,即PA0*/
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
/*设置捕获模式为上升沿捕获*/
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
/*设置输入1与IC1相连*/
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
/*设置TIM捕获在捕获输入上每探测到一个边沿执行一次*/
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
/*设置输入比较滤波器*/
TIM_ICInitStructure.TIM_ICFilter = 0x0f;
/*初始化输入捕获设置*/
TIM_ICInit(TIM2,&TIM_ICInitStructure);
}
void TIM3_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);;
}
void TIM3_Mode_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseStructure.TIM_Period = 500-1;
TIM_TimeBaseStructure.TIM_Prescaler = 720-1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_Pulse = 200;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_Pulse = 200;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
}
void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM2_IRQHandler(void) //中断函数
{
if(TIM_GetITStatus(TIM2, TIM_IT_CC1) == SET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);
/*第一次采集到上升沿的值IC2ReadValue1*/
if(CaptureNumber == 0)
{
IC2ReadValue1 = TIM_GetCapture1(TIM2);
CaptureNumber = 1;
}
/*第二次采集到上升沿的值IC2ReadValue2*/
else if(CaptureNumber == 1)
{
IC2ReadValue2 = TIM_GetCapture1(TIM2);
/*计算两次采集的时间差的绝对值|IC2ReadValue2-IC2ReadValue1|=Capture*/
if (IC2ReadValue2 > IC2ReadValue1)
{
Capture = (IC2ReadValue2 - IC2ReadValue1);
}
else
{
Capture = ((60000 - IC2ReadValue1) + IC2ReadValue2);
}
/*计算频率计时器时钟时钟36000000/Capture=频率*/
TIM2Freq = (uint32_t) 36000000 / Capture;
CaptureNumber = 0;
}
}
}
void USART1_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_InitStructure.USART_BaudRate = 9600; //波特率9600
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长8bit
USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位1位
USART_InitStructure.USART_Parity = USART_Parity_No; //无奇偶校验
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//收发使能
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //无硬件流控制
USART_Init(USART1, &USART_InitStructure); //初始化外设 USART1
USART_Cmd(USART1, ENABLE); //使能USART外设
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//使能接收中断
}
int fputc(int ch, FILE *f)
{
/* 将Printf内容发往串口 */
USART_SendData(USART1, (unsigned char) ch);
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
return (ch);
}
void USART1_Putc(unsigned char c)
{
USART_SendData(USART1, c);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET );
}
void USART1_Puts(char * str)
{
while(*str)
{
USART_SendData(USART1, *str++);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
}
我想用PA6产生一个占空比为40%的PWM波,然后通过TIM2采样器频率,然后通过串口输出,实现不了啊,求助~~~~ 我把PA6用导线连接到PA0端口上的~~~
此帖出自
小平头技术问答
看看有没有中断来,应该是没有吧
一周热门 更多>