蓝桥杯开发版LED测试程序
led.h
#ifndef SRC_LED_H__
#define SRC_LED_H__
void LED_init(void);
void LED_enable(int index);
void LED_disable(int index);
#endif //SRC_LED_H__
led.c
#include "led.h"
#include <stm32f10x.h>
/*
确定引脚连接
LE -> PD2
D0~D7 => PC8~PC15
使用ST标准库函数
Project\STM32F10x_StdPeriph_Examples\GPIO\IOToggle
*/
static uint16_t led_data ; //0~7Bits -> D0~D7
static void update_led_data(){
//led_date[0:7] => PC8~PC15
uint16_t m = GPIO_ReadOutputData( GPIOC ); //PC8~PC15 => m
m &=0x00FF ;
m |=( ~led_data << 8) ;
GPIO_Write( GPIOC, m);
GPIO_SetBits( GPIOD, GPIO_Pin_2 ); //锁存数据
GPIO_ResetBits( GPIOD, GPIO_Pin_2 );
}
void LED_init(void){
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); //打开GPIOD的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); //打开GPIOC的时钟
//配置PD2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_ResetBits( GPIOD, GPIO_Pin_2 );
//配置PC8~PC15
GPIO_InitStructure.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;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
led_data = 0;
update_led_data();
}
void LED_enable(int index){
led_data |= ( 1 << index );
update_led_data();
}
void LED_disable(int index){
led_data &= ~( 1<<index );
update_led_data();
}
stm32f10x_it.c
/**
******************************************************************************
* @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>© COPYRIGHT 2011 STMicroelectronics</center></h2>
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_it.h"
/** @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)
{
}
/******************************************************************************/
/* 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****/
main.c
#include "led.h"
void mydelay_ms(int ms){
volatile int i;
int k = 2333 * ms * 3;
for(i=0;i<k;i++){
;
}
}
int main(void)
{
int count;
LED_init();
count = 0 ;
while(1){
if( count & 0x0001 ) LED_enable(0); else LED_disable(0);
if( count & 0x0002 ) LED_enable(1); else LED_disable(1);
if( count & 0x0004 ) LED_enable(2); else LED_disable(2);
if( count & 0x0008 ) LED_enable(3); else LED_disable(3);
if( count & 0x0010 ) LED_enable(4); else LED_disable(4);
if( count & 0x0020 ) LED_enable(5); else LED_disable(5);
if( count & 0x0040 ) LED_enable(6); else LED_disable(6);
if( count & 0x0080 ) LED_enable(7); else LED_disable(7);
mydelay_ms(500);
count++;
}
}