完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
#include "STM32f10x_lib.h"
#include "stdio.h" #include"delay.h" #include"sys.h" void RCC_Configuration(void); void GPIO_Configuration(void); void TIM_Configuration(void); void PWM_Enable(void); //启动PWM void PWM_DISABLE(void); //失能PWM unsigned int Light_level;//调脉宽 vu16 PWM_level[5]={500,25000,35000,45000,55000};//PWM脉宽 int main(void) { //初始化 RCC_Configuration(); GPIO_Configuration(); TIM_Configuration(); Light_level=PWM_level[4]; PWM_DISABLE(); PWM_Enable(); while(1) { } } void RCC_Configuration(void) { ErrorStatus HSEStartUpStatus; RCC_DeInit(); RCC_HSEConfig(RCC_HSE_ON); HSEStartUpStatus=RCC_WaitForHSEStartUp(); if(HSEStartUpStatus==SUCCESS) { RCC_HCLKConfig(RCC_SYSCLK_Div1); RCC_HCLKConfig(RCC_HCLK_Div1); RCC_HCLKConfig(RCC_HCLK_Div2); FLASH_SetLatency(FLASH_Latency_2); FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9); RCC_PLLCmd(ENABLE); while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET); RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); while(RCC_GetSYSCLKSource()!=0x08); } RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE) ; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOD,ENABLE); } void TIM_Configuration(void)//PWM { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; TIM_TimeBaseStructure.TIM_Period=60000; TIM_TimeBaseStructure.TIM_Prescaler=0; TIM_TimeBaseStructure.TIM_ClockDivision=0; TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure); TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High; TIM_OCInitStructure.TIM_Pulse=Light_level;//设置脉宽 TIM_OC3Init(TIM3,&TIM_OCInitStructure); } void PWM_Enable(void) { TIM_OCInitTypeDef TIM_OCInitStructure; TIM_OCInitStructure.TIM_Pulse=Light_level;//设置脉宽 TIM_OC3Init(TIM3,&TIM_OCInitStructure); TIM_OC3PreloadConfig(TIM3,TIM_OCPreload_Enable); TIM_ARRPreloadConfig(TIM3,ENABLE); TIM_Cmd(TIM3,ENABLE); } void PWM_DISABLE(void) { TIM_OC3PreloadConfig(TIM3,TIM_OCPreload_Disable); TIM_ARRPreloadConfig(TIM3,DISABLE); TIM_Cmd(TIM3,DISABLE); } void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; GPIO_Init(GPIOB,&GPIO_InitStructure); } 我想用STM32的TIM3CH3(PB0)产生PWM,但是PB0所连接的LED一直没亮,不管怎么调TIM_OCInitStructure.TIM_Pulse和TIM_TimeBaseStructure.TIM_Period,求解答!! |
|
相关推荐
4 个讨论
|
|
你正在撰写讨论
如果你是对讨论或其他讨论精选点评或询问,请使用“评论”功能。
1020 浏览 0 评论
AD7686芯片不传输数据给STM32,但是手按住就会有数据。
996 浏览 2 评论
2102 浏览 0 评论
如何解决MPU-9250与STM32通讯时,出现HAL_ERROR = 0x01U
1202 浏览 1 评论
hal库中i2c卡死在HAL_I2C_Master_Transmit
1621 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-23 16:54 , Processed in 0.746199 second(s), Total 49, Slave 40 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号