您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > stm32课程设计报告
利用TIM定时器的中断和定时功能实现跑马灯一、原理及目的1、学习stm32库开发2、理解和熟悉I/O的使用;3、进一步掌握定时器、中断处理程序的编程方法4、利用库函数开发实现跑马灯二、电路原理图三、流程图四、软件算法(代码)1、Main.c#includestm32f10x.h#includebsp_led.h#includebsp_TiMbase.hvolatileu32time=0;inti=0;intmain(void){LED_GPIO_Config();TIM2_Configuration();TIM2_NVIC_Configuration();RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);while(1){if(time==1000){time=0;i++;if(i=3)i=0;switch(i){case0:LED1(OFF);LED2(OFF);LED3(ON);break;case1:LED1(ON);LED2(OFF);LED3(OFF);break;case2:LED1(OFF);LED2(ON);LED3(OFF);break;}}}}2、led.c#includebsp_led.hvoidLED_GPIO_Config(void){GPIO_InitTypeDefGPIO_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC,ENABLE);GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIOB,&GPIO_InitStructure);GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_3;GPIO_Init(GPIOC,&GPIO_InitStructure);GPIO_SetBits(GPIOB,GPIO_Pin_0);GPIO_SetBits(GPIOC,GPIO_Pin_4|GPIO_Pin_3);}3、led.h#ifndef__LED_H#define__LED_H#includestm32f10x.h#defineON0#defineOFF1#defineLED1(a)if(a)\GPIO_SetBits(GPIOB,GPIO_Pin_0);\else\GPIO_ResetBits(GPIOB,GPIO_Pin_0)#defineLED2(a)if(a)\GPIO_SetBits(GPIOC,GPIO_Pin_4);\else\GPIO_ResetBits(GPIOC,GPIO_Pin_4)#defineLED3(a)if(a)\GPIO_SetBits(GPIOC,GPIO_Pin_3);\else\GPIO_ResetBits(GPIOC,GPIO_Pin_3)#definedigitalHi(p,i){p-BSRR=i;}#definedigitalLo(p,i){p-BRR=i;}#definedigitalToggle(p,i){p-ODR^=i;}#defineLED1_TOGGLEdigitalToggle(GPIOB,GPIO_Pin_0)#defineLED1_OFFdigitalHi(GPIOB,GPIO_Pin_0)#defineLED1_ONdigitalLo(GPIOB,GPIO_Pin_0)#defineLED2_TOGGLEdigitalToggle(GPIOC,GPIO_Pin_4)#defineLED2_OFFdigitalHi(GPIOC,GPIO_Pin_4)#defineLED2_ONdigitalLo(GPIOC,GPIO_Pin_4)#defineLED3_TOGGLEdigitalToggle(GPIOC,GPIO_Pin_3)#defineLED3_OFFdigitalHi(GPIOC,GPIO_Pin_3)#defineLED3_ONdigitalLo(GPIOC,GPIO_Pin_3)voidLED_GPIO_Config(void);#endif/*__LED_H*/bsp_TiMbase.c#includebsp_TiMbase.hvoidTIM2_NVIC_Configuration(void){NVIC_InitTypeDefNVIC_InitStructure;NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;NVIC_InitStructure.NVIC_IRQChannelSubPriority=3;NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;NVIC_Init(&NVIC_InitStructure);}voidTIM2_Configuration(void){TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);TIM_TimeBaseStructure.TIM_Period=1000;TIM_TimeBaseStructure.TIM_Prescaler=71;TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);TIM_ClearFlag(TIM2,TIM_FLAG_Update);TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);TIM_Cmd(TIM2,ENABLE);RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,DISABLE);}4、bsp_TiMbase.h#ifndefTIME_TEST_H#defineTIME_TEST_H#includestm32f10x.hvoidTIM2_NVIC_Configuration(void);voidTIM2_Configuration(void);#endif五、效果图
本文标题:stm32课程设计报告
链接地址:https://www.777doc.com/doc-4358364 .html