我仿照原子的输入捕获实验写了一个类似的程序,结果在调试的时候发现设置比较值的函数TIM_SetCompare2(TIM2,150);没用了,完全由TIM_OCInitStructure.TIM_Pulse=180;这句里面的TIM_Pulse=180来决定了,如果我把TIM_Pulse的值设置为0,就连PWM波都不能输出了,求助!!!![mw_shl_code=applescript,true]#include "timer.h"
#include "stm32f10x.h"
#include "led.h"
#include "stm32f10x_tim.h"
//PWM输出实验函数
void TIM2_PWM_Init(u16 arr,u16 psc)
{
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
GPIO_InitTypeDef GPIO_InitStructrue;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
GPIO_InitStructrue.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructrue.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStructrue.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructrue);
GPIO_PinRemapConfig(GPIO_FullRemap_TIM2,ENABLE);
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period=arr;
TIM_TimeBaseInitStructure.TIM_Prescaler=psc;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
TIM_OCInitStructure.TIM_Pulse=180;
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OC3Init(TIM2,&TIM_OCInitStructure);
// TIM_OCInitStructure.TIM_Pulse=150;
// TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
// TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
// TIM_OC4Init(TIM2,&TIM_OCInitStructure);
// TIM_OC4PreloadConfig(TIM2,TIM_OCPreload_Enable);
TIM_OC3PreloadConfig(TIM2,TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM2, ENABLE);
TIM_Cmd(TIM2,ENABLE);
}
//输入捕获实验函数
void TIM3_Cap_Init(u16 arr,u16 psc)
{
NVIC_InitTypeDef NVIC_InitStructrue;
TIM_ICInitTypeDef TIM_ICInitStructrue;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructrue;
GPIO_InitTypeDef GPIO_InitStructrue;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//①使能GPIOB时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);//②使能定时器3时钟
GPIO_InitStructrue.GPIO_Mode=GPIO_Mode_IPD;
GPIO_InitStructrue.GPIO_Pin=GPIO_Pin_1;
GPIO_InitStructrue.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructrue);//③GPIO初始化
TIM_TimeBaseInitStructrue.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInitStructrue.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStructrue.TIM_Period=arr;
TIM_TimeBaseInitStructrue.TIM_Prescaler=psc;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructrue);//④时基初始化
TIM_ICInitStructrue.TIM_Channel=TIM_Channel_4;
TIM_ICInitStructrue.TIM_ICPolarity=TIM_ICPolarity_Rising;
TIM_ICInitStructrue.TIM_ICPrescaler=TIM_ICPSC_DIV1;
TIM_ICInitStructrue.TIM_ICSelection=TIM_ICSelection_DirectTI;
TIM_ICInitStructrue.TIM_ICFilter=0x00;
TIM_ICInit(TIM3,&TIM_ICInitStructrue);//⑤定时器输入捕获初始化
NVIC_InitStructrue.NVIC_IRQChannel=TIM3_IRQn;
NVIC_InitStructrue.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStructrue.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructrue.NVIC_IRQChannelSubPriority=1;
NVIC_Init(&NVIC_InitStructrue);//⑥初始化定时器中断优先级
TIM_ITConfig(TIM3,TIM_IT_Update|TIM_IT_CC4,ENABLE);//⑦使能定时器中断
TIM_Cmd(TIM3,ENABLE);//⑧使能定时器
}
u8 TIM3CH4_CAPTURE_STA=0;
u16 TIM3CH4_CAPTURE_VAL;
//定时器5中断服务函数
void TIM3_IRQHandler(void)
{
if((TIM3CH4_CAPTURE_STA&0x80)==0)//还未成功捕获
{
if(TIM_GetITStatus(TIM3,TIM_IT_CC4)!=0)//捕获事件发生
{
if((TIM3CH4_CAPTURE_STA&0x40)==0)//刚发生捕获
{
TIM3CH4_CAPTURE_STA=0;
TIM3CH4_CAPTURE_VAL=0;
TIM_SetCounter(TIM3,0);
TIM3CH4_CAPTURE_STA|=0x40;//标记为捕获到上升沿
TIM_OC4PolarityConfig(TIM3,TIM_ICPolarity_Falling);//设置为输入下降沿捕获
}
else if(TIM3CH4_CAPTURE_STA&0x40)//捕获到下降沿
{
TIM3CH4_CAPTURE_STA|=0x80;//标记捕获完成
TIM3CH4_CAPTURE_VAL=TIM_GetCapture4(TIM3);//将cnt寄存器里面的值加载到VAL里
TIM_OC4PolarityConfig(TIM3,TIM_ICPolarity_Rising);//设置为上升沿捕获
}
}
if(TIM_GetITStatus(TIM3,TIM_IT_Update)!=0)//如果上升沿期间定时器有溢出
{
if(TIM3CH4_CAPTURE_STA&0x40)//捕获到高电平
{
if((TIM3CH4_CAPTURE_STA&0x3f)==0x3f)//判断TIM3CH1_CAPTURE_STA的值是否达到最大计数
{
TIM3CH4_CAPTURE_STA|=0x80;//强制标记为捕获完成
TIM3CH4_CAPTURE_VAL=0xffff;//将VAL的值标记为最大
}
else TIM3CH4_CAPTURE_STA++;
}
}
}
TIM_ClearITPendingBit(TIM3,TIM_IT_Update|TIM_IT_CC4);//清除中断标志位!!!!!
}
/*****上面是timer.c里面的程序****/
/*****下面的是main.c里面的****/
#include "stm32f10x_tim.h"
#include "delay.h"
#include "timer.h"
#include "math.h"
#include "sys.h"
#include "1602.h"
u32 high,cycle;
extern u8 TIM3CH4_CAPTURE_STA; //输入捕获状态
extern u16 TIM3CH4_CAPTURE_VAL; //输入捕获值
u8 DispBuf[3];
u8 table1[]="duty cycle=";
u8 table2[]={'0','1','2','3','4','5','6','7','8','9'};
/**********功率因数计算函数**************/
double POWER_Init(u32 t,u32 T)
{
double A,B,PI,C;
A=T/t;
PI=3.1415926;
B=PI/A;
C=cos(B);
return(C);
}
/*************向LCD里面写入功率因数值**********/
void LCD_Write_Val()
{
u8 i;
double Y;
Y=POWER_Init(high,20000);
DispBuf[0]=(int)Y;
DispBuf[1]=(int)(Y*10);
DispBuf[2]=(int)(Y*100)%10;
i=DispBuf[0];
DispBuf[0]=table2;
i=DispBuf[1];
DispBuf[1]=table2;
i=DispBuf[2];
DispBuf[2]=table2;
LCD_Write_Command(0x80+0x0b);
LCD_Write_Date(DispBuf[0]);
delay_ms(5);
LCD_Write_Date('.');
delay_ms(5);
LCD_Write_Date(DispBuf[1]);
delay_ms(5);
LCD_Write_Date(DispBuf[2]);
delay_ms(5);
}
/***********主函数***********/
int main(void)
{
u32 temp=0;
u8 i;
delay_init();//延时函数初始化
LCD1602_Init();//LCD1602初始化函数
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
// LED_Init(); //LED端口初始化
TIM2_PWM_Init(199,7199); //PWM频率=10000/(199+1)=50hz
TIM3_Cap_Init(0XFFFF,72-1); //以1Mhz的频率计数
while(1)
{
delay_ms(10);
TIM_SetCompare2(TIM2,150);
if(TIM3CH4_CAPTURE_STA&0X80)//成功捕获到了一次上升沿
{
temp=TIM3CH4_CAPTURE_STA&0X3F;
temp*=65536;//溢出时间总和
temp+=TIM3CH4_CAPTURE_VAL;//得到总的高电平时间
high=temp;
TIM3CH4_CAPTURE_STA=0;//开启下一次捕获
}
LCD_Write_Command(0x80);//第一行的首地址
for(i=0;i<11;i++)
{
LCD_Write_Date(table1);
delay_ms(5);
}
LCD_Write_Val();
}
}
[/mw_shl_code]
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>