写了通用定时器TIM2和TIM3的定时
TIM2定时为每0.5秒闪烁一次
TIM3定时为每1秒闪烁一次
频率、定时时间的具体计算方法写在了注解上
计算方法刚开始有点迷糊不记得了
是从http://wj86843248.blog.163.com/blog/static/6864301220132974130478/这个网站上看的
目前没有办法写上高级定时器TIM8
目测是库的问题,求解决中!
还有,发现了一个很有用的STM32库函数查询的PDF
可以查询各种库函数,也有使用范例
STM32固件库说明(中文版).pdf
视频地址:http://player.youku.com/player.php/sid/XODM1MDg0NTYw/v.swf
#include "stm32f10x.h" #include "misc.h" void RCC_Configuration(void); void NVIC_Configuration(void); void GPIO_Configuration(void); void TIM3_Configuration(void); void TIM2_Configuration(void); void RCC_Configuration(void) { SystemInit();//24兆 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);//TIM3外设时钟打开,TIM3是挂在APB1上的,一 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//TIM2外设时钟打开,TIM2是挂在APB1上的,一 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);//GPIOC外设时钟打开 } void NVIC_Configuration(void)//设置中断,二 { NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//抢占优先级 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;//响应式优先级 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//抢占优先级 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;//响应式优先级 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } void GPIO_Configuration(void)//三,LED配置 { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_5|GPIO_Pin_6;;//使用到PC1.2.5.6 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); } void TIM2_Configuration(void)//四,定时器配置 { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_TimeBaseStructure.TIM_Period = 4999;//计4999个数就产生一次更新事件,4999+1=5000,5000*0.1ms=0.5s TIM_TimeBaseStructure.TIM_Prescaler = 2399;//预分频系数为2399,则计数器的时钟为24MHZ/2399+1=10kHZ,1/10000=0,0001.即0.1ms //预分频数取值范围为0~65535 TIM_TimeBaseStructure.TIM_ClockDivision = 0;// TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数模式,从0开始向上 TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);//将配置应用到TIM3中 } void TIM3_Configuration(void)//四,定时器配置 { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_TimeBaseStructure.TIM_Period = 9999;//计9999个数就产生一次更新事件,9999+1=10000,10000*0.1ms=1s TIM_TimeBaseStructure.TIM_Prescaler = 2399;//预分频系数为2399,则计数器的时钟为24MHZ/2399+1=10kHZ,1/10000=0,0001.即0.1ms TIM_TimeBaseStructure.TIM_ClockDivision = 0;// TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数模式,从0开始向上 TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);//将配置应用到TIM3中 } int main(void) { RCC_Configuration(); NVIC_Configuration(); GPIO_Configuration(); TIM2_Configuration(); TIM3_Configuration(); TIM_ClearFlag(TIM2, TIM_FLAG_Update); TIM_ClearFlag(TIM3, TIM_FLAG_Update); TIM_ARRPreloadConfig(TIM2, DISABLE); TIM_ARRPreloadConfig(TIM3, DISABLE); TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); TIM_Cmd(TIM2, ENABLE);//开启定时器TIM3 TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); TIM_Cmd(TIM3, ENABLE);//开启定时器TIM3 while (1) { } } #include "stm32f10x_it.h" void TIM2_IRQHandler(void) { if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) //检测是否发生溢出更新事件 { TIM_ClearITPendingBit(TIM2, TIM_IT_Update); GPIO_WriteBit(GPIOC, GPIO_Pin_2|GPIO_Pin_6, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_2|GPIO_Pin_6)));//取反库函数 } } void TIM3_IRQHandler(void) { if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //检测是否发生溢出更新事件 { TIM_ClearITPendingBit(TIM3, TIM_IT_Update); GPIO_WriteBit(GPIOC, GPIO_Pin_1|GPIO_Pin_5, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_1|GPIO_Pin_5)));//取反库函数 } }
用PWM调节LED亮度大小
由于RGB的3个颜色的LED亮度不同
根据不同的组合可以得到不同的颜色
PA1-通道2-Blue
PA2-通道3-Red
PA3-通道4-Green
之前由于TIM_SetCompare4(TIM2,temp2);之后的延时过小看不到明显变化
后来在
TIM_SetCompare2(TIM2,temp0);delay_ms(400); //
TIM_SetCompare3(TIM2,temp1);delay_ms(400);//
TIM_SetCompare4(TIM2,temp2);delay_ms(400);
之后都加上了相对比较大的延时
所以在每一个通道的占空比发生变化后组合都不同
产生了更多种的颜色组合
话说, TIM_SetCompare2(TIM2,temp0);里temp的取值范围到底是多少啊
之前设置得太小变化不明显
所以就设到了5000,会有什么后遗症么?
视频地址:http://player.youku.com/player.php/sid/XODM5MDk0NjEy/v.swf
#include "stm32f10x.h"
#include "stm32_eval.h"
#include <stdio.h>
#define VREF 3.3
int temp0,temp1,temp2;
/*延时函数 微秒*/
void delay_us(u32 n)
{
u8 j;
while(n--)
for(j=0;j<10;j++);
}
/*延时函数 毫秒*/
void delay_ms(u32 n)
{
while(n--)
delay_us(1000);
}
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
void RCC_Configuration(void)
{
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_ResetBits(GPIOD,GPIO_Pin_2);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_SetBits(GPIOC,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
}
void USART_int(long BaudRate)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* PA10 USART1_Rx */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = BaudRate;//??????
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//???????8bit
USART_InitStructure.USART_StopBits = USART_StopBits_1;//????1
USART_InitStructure.USART_Parity = USART_Parity_No;//????
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//??????none
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//??????????
USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
USART_ClockInit(USART1, &USART_ClockInitStructure);
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
}
void PWM_Config()
{uint16_t PrescalerValue = 0;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;//定义结构体
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//打开TIM2,TIM2挂在AP1上
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO , ENABLE);//一、开启功能复用
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//所需的3个管脚,复用输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_Cmd(TIM2, ENABLE);
PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;//二、配置TIM2
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 0x0FFE;
TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//三、脉冲宽度调制模式1
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
/* PWM1 Mode configuration: Channel2 *///四、
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//选择输出比较状态
TIM_OCInitStructure.TIM_Pulse = 0xFFFF;//IM_Pulse-设置了待装入捕获比较寄存器的脉冲值,取值在0x0000-0xFFFF之间
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
/* PWM1 Mode configuration: Channel3 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0xFFFF;//0x0000最亮,0xFFFF熄灭
TIM_OC3Init(TIM2, &TIM_OCInitStructure);
/* PWM1 Mode configuration: Channel4 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0xFFFF;
TIM_OC4Init(TIM2, &TIM_OCInitStructure);
TIM_ARRPreloadConfig(TIM2, ENABLE);//reload-重新装载初始值
}
void colour(int z)
{
switch(z)//分别点亮相应的颜色
{
case 0 : temp0=5000,temp1=2000,temp2=3000;/*橙色*/ break;
case 1 : temp0=5000,temp1=0,temp2=0; /*黄色*/ break;
case 2 : temp0=4000,temp1=5000,temp2=100; /*绿色*/ break;
case 3 : temp0=100,temp1=150,temp2=5000; /*紫色*/ break;
case 4 : temp0=1,temp1=5000,temp2=1; /*水蓝*/ break;
default: printf(" error\n");
}
}
void PWM_TEST()
{
int i=0;
for(i=0;i<6;i++)
{
colour(i); //temp1-Red,temp2-Green,temp0-Blue
TIM_SetCompare2(TIM2,temp0);delay_ms(400); //设置TIM2捕获比较器2寄存器的值
TIM_SetCompare3(TIM2,temp1);delay_ms(400);//TIM——通道数(定时器,数值)
TIM_SetCompare4(TIM2,temp2);delay_ms(400);
}
}
int main(void)
{
RCC_Configuration();
USART_int(115200);
printf(" config done...\r\n");
PWM_Config();
delay_ms(1000);
while(1)
{
PWM_TEST();
}
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t* file, uint32_t line)
{
while (1)
{
}
}
#endif
#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
PUTCHAR_PROTOTYPE
{
USART_SendData(EVAL_COM1, (uint8_t) ch);
while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)
{}
return ch;
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t* file, uint32_t line)
{
while (1)
{
}
}
#endif
回复
有奖活动 | |
---|---|
【有奖活动】分享技术经验,兑换京东卡 | |
话不多说,快进群! | |
请大声喊出:我要开发板! | |
【有奖活动】EEPW网站征稿正在进行时,欢迎踊跃投稿啦 | |
奖!发布技术笔记,技术评测贴换取您心仪的礼品 | |
打赏了!打赏了!打赏了! |
打赏帖 | |
---|---|
vscode+cmake搭建雅特力AT32L021开发环境被打赏30分 | |
【换取逻辑分析仪】自制底板并驱动ArduinoNanoRP2040ConnectLCD扩展板被打赏47分 | |
【分享评测,赢取加热台】RISC-V GCC 内嵌汇编使用被打赏38分 | |
【换取逻辑分析仪】-基于ADI单片机MAX78000的简易MP3音乐播放器被打赏48分 | |
我想要一部加热台+树莓派PICO驱动AHT10被打赏38分 | |
【换取逻辑分析仪】-硬件SPI驱动OLED屏幕被打赏36分 | |
换逻辑分析仪+上下拉与多路选择器被打赏29分 | |
Let'sdo第3期任务合集被打赏50分 | |
换逻辑分析仪+Verilog三态门被打赏27分 | |
换逻辑分析仪+Verilog多输出门被打赏24分 |