Stm32共有11个定时器:
1两个高级定时器:TIM1、TIM8-------------------------APB2
2四个通用定时器:TIM2~TIM5-------------------------APB1
3两个基本定时器:TIM6、TIM7-------------------------APB1
4两个看门狗
5一个系统嘀嗒定时器(SysTick)
主程序mainc
/说明:
PA0:KEY1;PA1:KEY2;
PA2:LED1;PA3:LED2;
PA9:USART1_TX;PA10:USART1_RX
/
#include "stm32f10xh"
#include "stm32f10x_rcch"
#include "stm32f10x_gpioh"
#include "stm32f10x_timh"
#include "stm32f10x_extih"
#include "system_stm32f10xh"
#include "misch"
void RCC_Configuration(void);
void GPIO_Configuration(void);
void TIM3_Configuration(void);
void NVIC_Configuration(void);
int main()
{
SystemInit();
RCC_Configuration();
GPIO_Configuration();
TIM3_Configuration();
NVIC_Configuration();
while(1);
}
void RCC_Configuration(void)
{
////USART2和USART3都在在APB1上而USART1是在APB2上的
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructureGPIO_Pin = GPIO_Pin_2|GPIO_Pin_3;
GPIO_InitStructureGPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructureGPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void TIM3_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
TIM_TimeBaseStructureTIM_Period = 2000;//频率:72MHz 72000000/36000=2000
TIM_TimeBaseStructureTIM_Prescaler = 35999;//36000-1=35999
TIM_TimeBaseStructureTIM_ClockDivision = 0x0;
TIM_TimeBaseStructureTIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE );
TIM_Cmd(TIM3,ENABLE);
}
void NVIC_Configuration(void)//配置中断优先级
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructureNVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructureNVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructureNVIC_IRQChannelSubPriority = 1;
NVIC_InitStructureNVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
中断函数stm32f10x_itc
#include "\include\stm32f10xh"
#include "\include\stm32f10x_ith"
#include "\include\stm32f10x_gpioh"
#include "\include\stm32f10x_timh"
void TIM3_IRQHandler(void)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_2)==Bit_RESET)
{
GPIO_SetBits(GPIOA,GPIO_Pin_2);
}else{
GPIO_ResetBits(GPIOA,GPIO_Pin_2);
}
}
中断函数声明头文件stm32f10x_ith
#ifndef __STM32F10x_IT_H
#define __STM32F10x_IT_H
/ Includes ------------------------------------------------------------------/
/ Exported types ------------------------------------------------------------/
/ Exported constants --------------------------------------------------------/
/ Exported macro ------------------------------------------------------------/
/ Exported functions ------------------------------------------------------- /
void NMIException(void);
void HardFaultException(void);
void MemManageException(void);
void BusFaultException(void);
void UsageFaultException(void);
void DebugMonitor(void);
void SVCHandler(void);
void PendSVC(void);
void SysTickHandler(void);
void WWDG_IRQHandler(void);
void PVD_IRQHandler(void);
void TAMPER_IRQHandler(void);
void RTC_IRQHandler(void);
void FLASH_IRQHandler(void);
void RCC_IRQHandler(void);
void EXTI0_IRQHandler(void);
void EXTI1_IRQHandler(void);
void EXTI2_IRQHandler(void);
void EXTI3_IRQHandler(void);
void EXTI4_IRQHandler(void);
void DMA1_Channel1_IRQHandler(void);
void DMA1_Channel2_IRQHandler(void);
void DMA1_Channel3_IRQHandler(void);
void DMA1_Channel4_IRQHandler(void);
void DMA1_Channel5_IRQHandler(void);
void DMA1_Channel6_IRQHandler(void);
void DMA1_Channel7_IRQHandler(void);
void ADC1_2_IRQHandler(void);
void USB_HP_CAN_TX_IRQHandler(void);
void USB_LP_CAN_RX0_IRQHandler(void);
void CAN_RX1_IRQHandler(void);
void CAN_SCE_IRQHandler(void);
void EXTI9_5_IRQHandler(void);
void TIM1_BRK_IRQHandler(void);
void TIM1_UP_IRQHandler(void);
void TIM1_TRG_COM_IRQHandler(void);
void TIM1_CC_IRQHandler(void);
void TIM2_IRQHandler(void);
void TIM3_IRQHandler(void);
void TIM4_IRQHandler(void);
void I2C1_EV_IRQHandler(void);
void I2C1_ER_IRQHandler(void);
void I2C2_EV_IRQHandler(void);
void I2C2_ER_IRQHandler(void);
void SPI1_IRQHandler(void);
void SPI2_IRQHandler(void);
void USART1_IRQHandler(void);
void USART2_IRQHandler(void);
void USART3_IRQHandler(void);
void EXTI15_10_IRQHandler(void);
void RTCAlarm_IRQHandler(void);
void USBWakeUp_IRQHandler(void);
void TIM8_BRK_IRQHandler(void);
void TIM8_UP_IRQHandler(void);
void TIM8_TRG_COM_IRQHandler(void);
void TIM8_CC_IRQHandler(void);
void ADC3_IRQHandler(void);
void FSMC_IRQHandler(void);
void SDIO_IRQHandler(void);
void TIM5_IRQHandler(void);
void SPI3_IRQHandler(void);
void UART4_IRQHandler(void);
void UART5_IRQHandler(void);
void TIM6_IRQHandler(void);
void TIM7_IRQHandler(void);
void DMA2_Channel1_IRQHandler(void);
void DMA2_Channel2_IRQHandler(void);
void DMA2_Channel3_IRQHandler(void);
void DMA2_Channel4_5_IRQHandler(void);
#endif / __STM32F10x_IT_H /
/ (C) COPYRIGHT 2008 STMicroelectronics END OF FILE/
void TIM1_Config(void)
{
TIM1_DeInit();
TIM1_TimeBaseInit(15, TIM1_COUNTERMODE_UP, 65535, 0);//600 Second
TIM1_ITConfig(TIM1_IT_UPDATE, DISABLE);
TIM1_ARRPreloadConfig(ENABLE);
TIM1_Cmd(DISABLE);
}
void TIM2_Config(void)
{
TIM2_DeInit();
TIM2_TimeBaseInit(TIM2_PRESCALER_1, 999);
TIM2_TimeBaseInit(TIM2_PRESCALER_16,2700);//TIM2_PRESCALER_16
TIM2_ITConfig(TIM2_IT_UPDATE, ENABLE);
TIM2_ARRPreloadConfig(ENABLE);
TIM2_Cmd(ENABLE);
}
main()
{
TIM1_Config();
TIM2_Config();
enableInterrupts();
}
void TIM1_CC_IRQHandler (void)
{
if(TIM_GetITStatus(TIM1, TIM_IT_CC1) != RESET)
{
TIM_ClearITPendingBit(TIM1, TIM_IT_CC1);
TIM1_ARR_Cnt++;
TIM_SetAutoreload(TIM1, TIM1_ARR_Cnt); // 改变ARR, 改变频率
}
}
在定时器中断中Autoreload新的ARR值。
以上就是关于新人求助,无法控制LED灯全部的内容,包括:新人求助,无法控制LED灯、stm8s用库函数 怎么配置定时器、在STM32中则么实现ARR的改变等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)