问题如上述:
#include "led.h"
#include "delay.h"
#include "sys.h"
#include "key.h"
#include "usart.h"
#include "exti.h"
#include "timer.h"
int main(void)
{
u32 i=0;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init(); //Ñóê±oˉêy3õê¼»ˉ
LED_Init(); //3õê¼»ˉóëLEDᬽóμÄó2¼t½ó¿ú
KEY_Init();
key_exti_init();
uart2_init(115200);
TIMx_Int_Init(5000-1,7199);
TIMx_PWM_Init(800-1,0);
while(1)
{
i++;
delay_ms(5);
TIM_SetCompare1(TIM1,i);
if(i>300)
}}
#include "timer.h"
#include "led.h"
#include "usart.h"
//í¨óö¨ê±Æ÷ÖD¶Ï3õê¼»ˉ
//Õaàïê±ÖóÑ¡ÔñÎaAPB1μÄ2±¶£¬¶øAPB1Îa36M
//arr£o×Ô¶ˉÖØ×°Öμ¡£
//psc£oê±ÖóÔ¤·ÖÆμêy
//Õaàïê1óÃμÄêǶ¨ê±Æ÷3!
void TIMx_Int_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //TIM3ê±Öóê1Äü
//¶Ôó|IO¿úμÄ3õê¼»ˉ
TIM_TimeBaseStructure.TIM_Period = arr; //éèÖÃÔúÏÂò»¸ö¸üDÂê¼t×°èë»î¶ˉμÄ×Ô¶ˉÖØ×°ÔؼÄ′æÆ÷ÖüÆúμÄÖμ
TIM_TimeBaseStructure.TIM_Prescaler =psc; //éèÖÃóÃà′×÷ÎaTIMxê±ÖóÆμÂê3yêyμÄÔ¤·ÖÆμÖμ
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòéϼÆêyÄ£ê½
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //¸ù¾YTIM_TimeBaseInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
TIM_ITConfig( //ê1Äü»òÕßê§ÄüÖ¸¶¨μÄTIMÖD¶Ï
TIM3, //TIMx
TIM_IT_Update | //TIM ÖD¶ÏÔ′,¸üDÂÖD¶Ï
TIM_IT_Trigger, //TIM ′¥·¢ÖD¶ÏÔ′ £¬′¥·¢ÖD¶Ï
ENABLE //ê1Äü
);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIMxÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //ÏèÕ¼óÅÏ輶0¼¶
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //′óóÅÏ輶3¼¶
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μà±»ê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YNVIC_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèNVIC¼Ä′æÆ÷
TIM_Cmd(TIM3, ENABLE); //ê1ÄüTIMxíaéè
}
void TIM3_IRQHandler(void) //TIMxÖD¶Ï
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //¼ì2éÖ¸¶¨μÄTIMÖD¶Ï·¢éúóë·ñ:TIM ÖD¶ÏÔ′
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update); //Çå3yTIMxμÄÖD¶Ï′y′|àíλ:TIM ÖD¶ÏÔ′
//ìí¼óÖD¶ÏoóÖ′DDμÄê¼t
LED1=~LED1;
}
}
//PWMêä3ö3õê¼»ˉ
//arr£o×Ô¶ˉÖØ×°Öμ
//psc£oê±ÖóÔ¤·ÖÆμêy
void TIMx_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //¶¨ê±Æ÷ê1Äü
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //ê1ÄüGPIOíaéèoíAFIO¸′óÃ1|ÄüÄ£¿éê±Öóê1Äü
// GPIO_PinRemapConfig(GPIO_PartialRemap_TIMx, ENABLE); //Timerx2¿·ÖÖØó3éä TIM3_CH2->
B5 //óÃóúTIM3μÄCH2êä3öμÄ
WMí¨1y¸ÃLEDÏÔê¾
//éèÖøÃòy½ÅÎa¸′óÃêä3ö1|Äü,êä3öTIMx CHxμÄ
WMÂö3å2¨DÎ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //TIM_CHx
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//GPIO_WriteBit(GPIOx, GPIO_Pin_x,Bit_SET); // Px.xéÏà-
TIM_TimeBaseStructure.TIM_Period = arr; //éèÖÃÔúÏÂò»¸ö¸üDÂê¼t×°èë»î¶ˉμÄ×Ô¶ˉÖØ×°ÔؼÄ′æÆ÷ÖüÆúμÄÖμ
TIM_TimeBaseStructure.TIM_Prescaler =psc; //éèÖÃóÃà′×÷ÎaTIMxê±ÖóÆμÂê3yêyμÄÔ¤·ÖÆμÖμ
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòéϼÆêyÄ£ê½
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //¸ù¾YTIM_TimeBaseInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //Ñ¡Ôñ¶¨ê±Æ÷Ä£ê½:TIMÂö3å¿í¶èμ÷ÖÆÄ£ê½(Ä£ê½1£ocnt<ccrê±£¬óDD§μçƽ£»Ä£ê½2£ocnt>ccrê±£¬óDD§μçƽ£»oí¼«DÔ¾ö¶¨)
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±è½Ïêä3öê1Äü
TIM_OCInitStructure.TIM_Pulse = 0; //éèÖÃ′y×°èë2¶»ñ±è½Ï¼Ä′æÆ÷μÄÂö3åÖμ
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //êä3ö¼«DÔ:TIMêä3ö±è½Ï¼«DÔ¸ß
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //¸ù¾YTIM_OCInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèTIMx,OCx¶Ôó|chx
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); //ê1ÄüTIMxÔúCCR2éÏμÄԤװÔؼÄ′æÆ÷
TIM_CtrlPWMOutputs(TIM1,ENABLE);
TIM_ARRPreloadConfig(TIM1, ENABLE); //ê1ÄüTIMxÔúARRéÏμÄԤװÔؼÄ′æÆ÷
TIM_Cmd(TIM1, ENABLE); //ê1ÄüTIMxíaéè
}
一周热门 更多>