STM32+按键调控PWM输出+串口输出占空比.docx

上传人:大张伟 文档编号:11658833 上传时间:2021-08-28 格式:DOCX 页数:6 大小:68.61KB
返回 下载 相关 举报
STM32+按键调控PWM输出+串口输出占空比.docx_第1页
第1页 / 共6页
STM32+按键调控PWM输出+串口输出占空比.docx_第2页
第2页 / 共6页
STM32+按键调控PWM输出+串口输出占空比.docx_第3页
第3页 / 共6页
STM32+按键调控PWM输出+串口输出占空比.docx_第4页
第4页 / 共6页
STM32+按键调控PWM输出+串口输出占空比.docx_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《STM32+按键调控PWM输出+串口输出占空比.docx》由会员分享,可在线阅读,更多相关《STM32+按键调控PWM输出+串口输出占空比.docx(6页珍藏版)》请在三一文库上搜索。

1、#include STM32Libstm32f10x.h#include hal.h/* Function Name : GPIO_Configuration*设置PD3,PD4,PD5,PD6为键盘输入* 设置 PB0,5,8,9; PC5,7; PD7 ;PA8 为输出 LED灯*/void GPIO_Configuration(void)GPIO_InitTypeDef GPIO_InitStructure;/*允许总线CLOCK在使用GPIO之前必须允许相应端的时钟.从STM32a勺设计角度上说,没被允许的端将不接入时钟,也就不会耗能这是STM32节能的一种技巧,*/RCC_APB2P

2、eriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);/* PC8按键输入*/GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; / 上拉输入 GPIO_Init(GPIOC, &GPIO_InitStructure);/* PC9

3、按键输入*/GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; / 上拉输入 GPIO_Init(GPIOC, &GPIO_InitStructure);hal.h#ifndef HAL_H#define HAL_H/硬件初始化extern void ChipHalInit(void);extern voidChipOutHalInit(void);/输入宏定义#define GET_LEFT()(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_8

4、)#define GET_RIGHT()(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_9)extern void USART1_Putc(u8 c);extern void USART_Configuration(void);extern void USART1_Puts(char * str);#endifTIM.c#include STM32Libstm32f10x.hvoid Tim1_Configuration(void)TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;TIM_OCInitTypeDef TIM_

5、OCInitStructure;GPIO_InitTypeDef GPIO_InitStructure;/* PA8设置为功能脚(PWM) */GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOA, &GPIO_InitStructure);RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);TI

6、M_DeInit(TIM1);/预分频(时钟分频)72M/72=1000K/*TIM1时钟配置*/TIM_TimeBaseStructure.TIM_Prescaler = 72;/向上计数TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;TIM_TimeBaseStructure.TIM_Period = 2000;/装载值1000k/2000=500hzTIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;TIM_TimeBaseStructure.TIM_Repet

7、itionCounter = 0x0;TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);/* Channel 1 Configuration in PWM mode */TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;/PWM模式2TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;/正向通道有效TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;反向通道无效

8、TIM_OCInitStructure.TIM_Pulse = 300;/占空时间TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;/ 输出极性TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;/ 互补端的极性TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;TIM_OC1Init

9、(TIM1,&TIM_OCInitStructure);/ 通道 1/* TIM1 counter enable */TIM_Cmd(TIM1,ENABLE);/* TIM1 Main Output Enable */TIM_CtrlPWMOutputs(TIM1,ENABLE);/设置捕获寄存器1void SetT1Pwm1(u16 pulse)TIM1-CCR1=pulse;main.c/*实验名称:PWM*功能:是PA8产生PWM出,按键调节占空比*/#include STM32Libstm32f10x.h#include hal.h#define SIZE 0u8 table11=“0

10、123456789 ;char buffer10=0000000000;void Delay(u16 n);void d_2_char(u32 x)bufferSIZE+0=tablex%10000000000/100000;bufferSIZE+1=tablex%1000000000/100000;bufferSIZE+2=tablex%100000000/100000;bufferSIZE+3=tablex%10000000/10000;bufferSIZE+4=tablex%1000000/10000;bufferSIZE+5=tablex%100000/10000;bufferSIZE

11、+6=tablex%10000/1000;bufferSIZE+7=tablex%1000/100;bufferSIZE+8=tablex%100/10;bufferSIZE+9=tablex%10;/延迟函数void Delay(u16 speed)u16 i;while(speed!=0)speed-;for(i=0;i400;i+);extern void SetT1Pwm1(u16 pulse);int main(void)u16 pulse=300;ChipHalInit();/片内硬件初始化ChipOutHalInit();/片外硬件初始化for(;)if(GET_LEFT()=0)while(GET_LEFT()=0);if(pulse=2000)pulse+=30;/ SetTIPwml(pulse);elsepulse=300;d_2_char(pulse);USART1_Puts(buffer);USART1_Puts(rn);if(GET_RIGHT()=0)while(GET_RIGHT()=0);if(pulse=2000)pulse-=60;else pulse=1800;d_2_char(pulse); USART1_Puts(buffer); USART1_Puts(rn); SetTIPwml(pulse);

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1