【蓝桥杯嵌入式】1_LED

1 原理图:

在这里插入图片描述
1、要操作LED需先使能573锁存器即将N LE端口即PD2置1。

2、由下图可知LED对应端口为PC8~15,所以只要将这些端口都置0LED就可以全亮了。
在这里插入图片描述
3、由于液晶使用了LD0~7和HD0~7PC0~15所以使用LED势必和液晶产生冲突所以LED和液晶的使用要分开不可同时操作。


2 部分源码

main.c

#include "Headfile.h"

int main(void)
{
	u8 i = 0;
	SysTick_Config(SystemCoreClock/1000);

	Delay_Ms(200);
	
	//STM3210B_LCD_Init();
	//LED和液晶不可同时操作
	LEDInit();
	LEDEnable(Bit_SET);
	for(i = 0; i < 9; i++){
		LEDSingleRun(i);
		Delay_Ms(100);
	} 
	LEDSingleRun(0);//关闭LED
	for(i = 8; i > 0; i--){
		LEDSingleRun(i);
		Delay_Ms(100);
	} 
	LEDSingleRun(0);
	for(i = 0; i < 9; i++){
		LEDRun(i);
		Delay_Ms(100);
	} 
	LEDSingleRun(0);
	for(i = 8; i > 0; i--){
		LEDRun(i);
		Delay_Ms(100);
	} 
	LEDSingleRun(0);
	LEDEnable(Bit_RESET);
	
	while(1);
}


LED.c
这里列些了多种LED操作方式,其中LEDRun是带锁存方式,LEDSingleRun是不带锁存方式。

PS:MDK的ARM开发环境中竟然没有intrins文件我只能自己写一个crol函数╮(╯▽╰)╭。(注释应该挺详细的了吧…)

#include "Headfile.h"

u16 _crol_(u16 val,u8 n)//16位数循环左移
{
  n = n % 16;
  u16 buf = val >> ( 16 - n );
  val = val << n;
  val |= buf;
  return val;
	//val --->	0xFEFF
	//n   --->	3
	//0xFEFF->	1111 1110 1111 1111
	//buf ---> 	0000 0000 0000 0111
	//val --->	1111 0111 1111 1000
	//val --->  1111 0111 1111 1111
}

void LEDInit(){
	GPIO_InitTypeDef GPIOInitStruct;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD,ENABLE);
	GPIOInitStruct.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
	GPIOInitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIOInitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_Init(GPIOC,&GPIOInitStruct);
	GPIOInitStruct.GPIO_Pin = GPIO_Pin_2;
	GPIOInitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIOInitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_Init(GPIOD,&GPIOInitStruct);
		
}

void LEDEnable(BitAction BitVal){
		////Bit_RESET = 0,Bit_SET
		GPIO_WriteBit(GPIOD,GPIO_Pin_2,BitVal);
}

void LEDSingleRun(u8 index){
	if(index != 0){
	GPIO_Write(GPIOC,_crol_(0xFEFF,index - 1) );
	}else{
		GPIO_Write(GPIOC,0xFFFF);
	}
}

void LEDRun(u8 index){
	if(index != 0){
	GPIO_Write(GPIOC,(_crol_(0xFEFF,index - 1) 
		& GPIO_ReadOutputData(GPIOC)) );
	}else{
		GPIO_Write(GPIOC,0xFFFF);
	}
}


Delay.c

u32 TimingDelay = 0;

void Delay_Ms(u32 nTime)
{
	TimingDelay = nTime;
	while(TimingDelay != 0);	
}

stm32f10x_it.c
设定滴答定时器中断使达到1ms延时的功能。

/**
  ******************************************************************************
  * @file    I2S/SPI_I2S_Switch/stm32f10x_it.c 
  * @author  MCD Application Team
  * @version V3.5.0
  * @date    08-April-2011
  * @brief   Main Interrupt Service Routines.
  *          This file provides template for all exceptions handler and peripherals
  *          interrupt service routine.
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_it.h"

extern u32 TimingDelay;

/** @addtogroup STM32F10x_StdPeriph_Examples
  * @{
  */

/** @addtogroup I2S_SPI_I2S_Switch
  * @{
  */ 

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/******************************************************************************/
/*            Cortex-M3 Processor Exceptions Handlers                         */
/******************************************************************************/

/**
  * @brief  This function handles NMI exception.
  * @param  None
  * @retval None
  */
void NMI_Handler(void)
{
}

/**
  * @brief  This function handles Hard Fault exception.
  * @param  None
  * @retval None
  */
void HardFault_Handler(void)
{
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1)
  {}
}

/**
  * @brief  This function handles Memory Manage exception.
  * @param  None
  * @retval None
  */
void MemManage_Handler(void)
{
  /* Go to infinite loop when Memory Manage exception occurs */
  while (1)
  {}
}

/**
  * @brief  This function handles Bus Fault exception.
  * @param  None
  * @retval None
  */
void BusFault_Handler(void)
{
  /* Go to infinite loop when Bus Fault exception occurs */
  while (1)
  {}
}

/**
  * @brief  This function handles Usage Fault exception.
  * @param  None
  * @retval None
  */
void UsageFault_Handler(void)
{
  /* Go to infinite loop when Usage Fault exception occurs */
  while (1)
  {}
}

/**
  * @brief  This function handles Debug Monitor exception.
  * @param  None
  * @retval None
  */
void DebugMon_Handler(void)
{
}

/**
  * @brief  This function handles SVCall exception.
  * @param  None
  * @retval None
  */
void SVC_Handler(void)
{
}

/**
  * @brief  This function handles PendSV_Handler exception.
  * @param  None
  * @retval None
  */
void PendSV_Handler(void)
{
}

/**
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval None
  */
void SysTick_Handler(void)
{
	TimingDelay--;
}

/******************************************************************************/
/*                 STM32F10x Peripherals Interrupt Handlers                   */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_stm32f10x_xx.s).                                            */
/******************************************************************************/

/**
  * @brief  This function handles PPP interrupt request.
  * @param  None
  * @retval None
  */
/*void PPP_Switch_IRQHandler(void)
{
}*/

/**
  * @}
  */ 

/**
  * @}
  */ 

/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/

发布了88 篇原创文章 · 获赞 39 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_43444989/article/details/103442576