标题:
STM32控制LED代码
[打印本页]
作者:
2.31
时间:
2018-5-8 20:32
标题:
STM32控制LED代码
1设计要求
控制实验平台的发光二极管LED1、LED2、LED3、LED4,使它们有规律的点亮,具体顺序如下:
LED1亮->LED2亮->LED3亮->LED4亮,如此反复,当按下开发板上的B3按钮时,灯全部熄灭,
2秒钟后发光二极管全部点亮,再过一秒钟后返回被中断前的状态,有规律的依次点亮。
2 硬件电路设计
在评估板上,LED1、LED2、LED3、LED4分别与PC6、PC7、PC8、PC9相连,按键Key与PB9相连。
3软件程序设计
根据任务要求,程序内容主要包括:
(1) 配置PB口第9个引脚作为外部中断,下降延触发;
(2) 读取端口数据输出寄存器GPIOC_ODR[15:0]的值,因为C口[9:6]位和四个LED灯连通。
(3) 初始化时,LED依次点亮,当按下Key键时产生中断;
(4) 中断服务子程序内容:灯全部熄灭,2秒钟后发光二极管全部点亮,再过1秒钟后中断返回。
整个工程包含3个源文件:STM32F10x.s、stm32f10x_it.c和main.c,其中STM32F10x.s为启动代码,
所有中断服务子程序均在stm32f10x_it.c中,其它函数则在main.c中。下面分别介绍相关的函数,
具体程序清单见参考程序。
GPIO_Configuration函数负责配置GPIO端口,其中GPIO_InitStructure数据结构包含所有GPIO端口
配置所需各项,函数GPIO_Init则实现某个端口的配置。在STM32F10xR.LIB和STM3210xD.LIB中均已
定义,读者可通过查阅5.3节所述的ST公司发布的《ARM?-based 32-bit MCU STM32F101xx and
STM32F103xx Firmware Library》手册来了解该数据结构以及相关驱动函数。
NVIC_Configuration函数用于配置嵌入式中断控制器,相关内容将在后面章节介绍,此处需要将PB9
引脚配置为外部中断。
EXTI9_5_IRQHandler函数是PB9的中断服务子程序,当按下Key按钮之后触发EXTI9_5中断,在中断服
务子程序中先将灯全部熄灭,延迟2秒后全部点亮,再延迟1秒之后退出中断服务子程序。
SysTick_Configuration函数用于配置和允许系统时钟中断,系统时钟中断服务子程序SysTickHandler
函数则用于产生1毫秒的延时,这样Delay函数就可以通过开关系统时钟计数器来实现精确延时了。
4 运行过程
(1) 使用Keil uVision3 通过ULINK 2仿真器连接实验板,打开实验例程目录GPIO_TEST子目录下的
GPIO.Uv2例程,编译链接工程;
(2) 选择软件调试模式,点击MDK 的Debug菜单,选择Start/Stop Debug Session项或Ctrl+F5键,
在逻辑分析仪中添加GPIOC_ODR.6、GPIOC_ODR.7、GPIOC_ODR.8、GPIOC_ODR.9,点击Run按钮即可在
逻辑分析仪中看到如图7-11;
(3) 选择硬件调试模式,选择Start/Stop Debug Session项或Ctrl+F5键,下载程序并运行,观察LED
灯的变化情况;当程序运行在while循环体内时,按Key键,程序进入中断服务子程序EXTI9_5_IRQHandler(),
单步运行,观察LED的变化情况。
(4) 退出Debug模式,打开Flash菜单>Download,将程序下载到开发板的Flash中,按RESET键复位,
观察LED灯的情况,正常情况应如表7-6所列。
LED灯状态 说 明
LED1 LED2 LED3 LED4
亮 灭 灭 灭 程序正常运行,发光二极管依次点亮
灭 亮 灭 灭
灭 灭 亮 灭
灭 灭 灭 亮
灭 灭 灭 灭 外部信号输入,发生中断,执行中断处理程序
亮 亮 亮 亮
单片机源程序:
/*******************************************************************************
* File Name : main.c
* Author : Wuhan R&D Center, Embest
* Date First Issued : 08/08/2008
* Description : Main program body
********************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_lib.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
#define ADC1_DR_Address ((u32)0x4001244C)
unsigned short int ADC_ConvertedValue;
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
ErrorStatus HSEStartUpStatus;
extern vu32 TimingDelay;
uc8 table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d, //数码管编码
0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void NVIC_Configuration(void);
void GPIO_Configuration(void);
void Delay(vu32 nTime);
void SysTick_Configuration(void);
void SetupLED (void) ;
extern void SetupADC (void);
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name : main
* Description : Main program.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
int main(void)
{
u8 i;
#ifdef DEBUG
debug();
#endif
/* Configure the system clocks */
RCC_Configuration();
SysTick_Configuration();
/* NVIC Configuration */
NVIC_Configuration();
/* Configure the GPIO ports */
GPIO_Configuration();
for(i=0;i<8;i++)
{
GPIOE->ODR = 0xffffff00 | (1<<i);
GPIOD->ODR = 0xffffffff ;
Delay(100);
}
for(;;)
{
for(i=0;i<8;i++)
{
GPIOE->ODR = 0xffffffff & ~(1<<i);
GPIOD->ODR = 0xffffff00 | table[i];
Delay(800);
//Delay(1);
}
}
}
/*******************************************************************************
* Function Name : SysTick_Configuration
* Description : Configures the SysTick to generate an interrupt each 1 millisecond.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SysTick_Configuration(void)
{
/* Select AHB clock(HCLK) as SysTick clock source */
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
/* Set SysTick Priority to 3 */
NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 3, 0);
/* SysTick interrupt each 1ms with HCLK equal to 72MHz */
SysTick_SetReload(72000);
/* Enable the SysTick Interrupt */
SysTick_ITConfig(ENABLE);
}
/*******************************************************************************
* Function Name : RCC_Configuration
* Description : Configures the different system clocks.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RCC_Configuration(void)
{
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL */
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}
/* Enable GPIOB, GPIOC and AFIO clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC |
RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE , ENABLE);
}
/*******************************************************************************
* Function Name : NVIC_Configuration
* Description : Configures Vector Table base location.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
/* Configure one bit for preemption priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/* Enable the EXTI9_5 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/*******************************************************************************
* Function Name : GPIO_Configuration
* Description : Configures the different GPIO ports.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure PD. as Output push-pull */
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(GPIOD, &GPIO_InitStructure);
/* Configure PE. as Output push-pull */
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(GPIOE, &GPIO_InitStructure);
}
/*******************************************************************************
* Function Name : Delay
* Description : Inserts a delay time.
* Input : nTime: specifies the delay time length, in milliseconds.
* Output : None
* Return : None
*******************************************************************************/
void Delay(u32 nTime)
{
/* Enable the SysTick Counter */
SysTick_CounterCmd(SysTick_Counter_Enable);
TimingDelay = nTime;
while(TimingDelay != 0);
/* Disable the SysTick Counter */
SysTick_CounterCmd(SysTick_Counter_Disable);
/* Clear the SysTick Counter */
SysTick_CounterCmd(SysTick_Counter_Clear);
}
/*******************************************************************************
* Function Name : Delay
* Description : Inserts a delay time.
* Input : nCount: specifies the delay time length.
* Output : None
* Return : None
*******************************************************************************/
/*
void Delay(vu32 nCount)
{
for(; nCount != 0; nCount--);
……………………
…………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
2.数码管.rar
(52.23 KB, 下载次数: 19)
2018-5-8 20:31 上传
点击文件名下载附件
数码管代码
下载积分: 黑币 -5
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1