完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
程序如下: void TIM1_Init(void) { NVIC_InitTypeDef NVIC_InitStructure; TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; TIM_BDTRInitTypeDef TIM_BDTRInitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 |RCC_APB2Periph_GPIOA |RCC_APB2Periph_GPIOB |RCC_APB2Periph_AFIO, ENABLE ); //--TIM1更新中断配置------------------------------------- //NVIC_InitStructure.NVIC_IRQChannel= TIM1_UP_IRQn; //NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 1; //NVIC_InitStructure.NVIC_IRQChannelSubPriority= 0; //NVIC_InitStructure.NVIC_IRQChannelCmd= ENABLE; //NVIC_Init(&NVIC_InitStructure); TIM_DeInit(TIM1); //--TIM1基本配置----------------------------------------- TIM_TimeBaseStructure.TIM_Period = 11; //设置周期 ,频率=72MHz/11=6.5MHz TIM_TimeBaseStructure.TIM_Prescaler = 0; //预分频(时钟分频) 不分频 TIM_TimeBaseStructure.TIM_ClockDivision = 0x00; //设置时钟分割 TIM_TimeBaseStructure.TIM_CounterMode= TIM_CounterMode_Up; //设置向上计数模式 TIM_TimeBaseStructure.TIM_RepetitionCounter= 0x0A; //重复计数器计数值 TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure); //初始化函数 TIM_SelectOnePulseMode(TIM1, TIM_OPMode_Single); //设置单脉冲模式,在发生下一次更新事件时计数器停止 TIM_ARRPreloadConfig(TIM1,DISABLE); //失能在ARR上的预装载寄存器,没有缓冲 TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Disable); //失能在CCR1上的预装载寄存器 //--TIM1 PWM输出OC1配置---------------------------------------------- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式1 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //使能输出比较状态 TIM_OCInitStructure.TIM_OutputNState= TIM_OutputNState_Enable; //使能互补输出状态 TIM_OCInitStructure.TIM_Pulse = 3; //设置待装入捕获比较寄存器的脉冲值 占空时间 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性低 TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //互补 输出极性高 TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; //MOE=0 设置 TIM1输出比较空闲状态 TIM_OCInitStructure.TIM_OCNIdleState= TIM_OCIdleState_Set; //MOE=0 重置 TIM1输出比较空闲状态 TIM_OC1Init(TIM1,&TIM_OCInitStructure); //函数初始化 //--TIM1 PWM输出BDTR配置---------------------------------------------- TIM_BDTRInitStructure.TIM_OSSRState= TIM_OSSRState_Enable; //OSSR TIM_BDTRInitStructure.TIM_OSSIState= TIM_OSSIState_Enable; //OSSI TIM_BDTRInitStructure.TIM_LOCKLevel= TIM_LOCKLevel_1; //锁定级别1 TIM_BDTRInitStructure.TIM_DeadTime = 0; //死区时间0 TIM_BDTRInitStructure.TIM_Break= TIM_Break_Disable; //空闲模式不允许 TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High; //空闲电平极性高 TIM_BDTRInitStructure.TIM_AutomaticOutput= TIM_AutomaticOutput_Enable; //自动输出使能 TIM_BDTRConfig(TIM1,&TIM_BDTRInitStructure); //TIM_ITConfig(TIM1,TIM_IT_Update , ENABLE); //使能更新中断 TIM_ITConfig(TIM1,TIM_IT_Update , DISABLE); //失能中断 TIM_UpdateRequestConfig(TIM1,TIM_UpdateSource_Global); //设置更新请求源 0 TIM_GenerateEvent(TIM1,TIM_EventSource_Update); //设置由软件产生一个TIM1更新事件 __nop(); TIM_ClearFlag(TIM1,TIM_IT_Update); //清除待处理标志位 TIM_UpdateRequestConfig(TIM1,TIM_UpdateSource_Regular); //设置更新请求源 1,只有溢出才能触发DMA // TIM_CtrlPWMOutputs(TIM1, DISABLE); //主输出失能 TIM_Cmd(TIM1, DISABLE); //使能TIM外设 } |
|
相关推荐
6个回答
|
|
|
|
|
|
请问楼主这个代码是可以用的吗
|
|
|
|
重复计数器的使用可否说明一下
|
|
|
|
学习中,感觉没怎么明白楼主的意思
|
|
|
|
非常感谢分享,谢谢
|
|
|
|
问题解决了吗?
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
316 浏览 0 评论
AD7686芯片不传输数据给STM32,但是手按住就会有数据。
834 浏览 2 评论
1996 浏览 0 评论
如何解决MPU-9250与STM32通讯时,出现HAL_ERROR = 0x01U
1099 浏览 1 评论
hal库中i2c卡死在HAL_I2C_Master_Transmit
1525 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-20 06:35 , Processed in 0.759521 second(s), Total 83, Slave 66 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号