2.LED流水灯
main.c
#include "bord.h"
/*********************************************************************
@Function : 程序入口
@Parameter : N/A
@Return : N/A
**********************************************************************/
int main(void)
{
uint8_t temp=0;
uint8_t status=1;
delay_init(); //启动滴答定时器
usart1_init(9600); //USART1初始化
SystemTinerInit(1000-1,3600-1); //系统时间初始化 定时100ms
LED_Init(); //板载LED初始化
ExpLEDInit(); //开发板LED初始化
ExpLEDSet(0XFF); //关闭开发板所有灯
printf("初始化成功!\r\n");
while(1)
{
if (WaitTimerOut(3)) // 如果定时器3超时,取反板载LED
LED=!LED;
// 如果定时器1超时
if (WaitTimerOut(1))
{
if (status) // 如果当前状态为点亮
{
temp = (temp << 1) & 0xFE; // 顺序点亮
if (temp == 0x00) status = 0; // 如果所有LED都被点亮,开始熄灭
}
// 如果当前状态为熄灭
else
{
temp = (temp << 1) | 0x01; // 顺序熄灭
if (temp == 0xFF) // 如果所有LED都被熄灭,开始点亮
status = 1;
}
// 设置开发板LED的状态
ExpLEDSet(temp);
}
}
}
led.c
#include "led.h"
/*********************************************************************
@Function : LED IO初始化
@Parameter : N/A
@Return : N/A
**********************************************************************/
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure; // 定义 GPIO 初始化结构体变量
/* 时钟使能 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // 使能 GPIOA 时钟
/* 引脚配置 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; // 将板载 LED 连接的引脚设置为 PA8
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // 设置引脚为推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // 设置引脚的输出速度为 50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); // 应用以上配置到 GPIOA 上
GPIO_SetBits(GPIOA, GPIO_Pin_8); // 将 GPIOA 的引脚 PA8 设置为高电平,使得板载 LED 灭
}
/*********************************************************************
@Function : 开发板LED IO初始化
@Parameter : N/A
@Return : N/A
**********************************************************************/
void ExpLEDInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure; // 定义 GPIO 初始化结构体变量
/* 时钟使能 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // 使能 GPIOA 时钟
/* 引脚配置 */
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; // D1-D8->PA0-PA7
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // 设置引脚为推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // 设置引脚的输出速度为 50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); // 应用以上配置到 GPIOA 上
GPIO_SetBits(GPIOA, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 |
GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7); // 将 GPIOA 的引脚 PA0-PA7 设置为高电平,使得开发板 LED 灭
}
/*********************************************************************
@Function : 开发板LED控制
@Parameter : ledbit :LED状态
@Return : N/A
**********************************************************************/
void ExpLEDSet(uint8_t ledbit)
{
uint8_t i;
for(i=0;i<8;i++)
{
if(ledbit&0x01)
PAout(i) = 1;
else
PAout(i) = 0;
ledbit >>=1;
}
}
led.h
#ifndef _LED_H
#define _LED_H
// An highlighted block
#include <stdint.h> // 包含用于标准整数类型的头文件
#include "stm32f10x_gpio.h" // 包含 GPIO 头文件
#include "stm32f10x_rcc.h" // 包含 RCC 头文件
#include "misc.h" // 包含其他外设库的头文件
#include ".\sys\sys.h" // 包含系统配置头文件
// 核心板板载LED宏定义
#define LED PAout(8) // 定义用于控制核心板板载LED的宏
// 开发板LED宏定义
#define LED1 PAout(0) // L4->PA0
#define LED2 PAout(1) // L4->PA1
#define LED3 PAout(2) // L4->PA2
#define LED4 PAout(3) // L4->PA3
#define LED5 PAout(4) // L4->PA4
#define LED6 PAout(5) // L4->PA5
#define LED7 PAout(6) // L4->PA6
#define LED8 PAout(7) // L4->PA7
// 函数声明
void LED_Init(void); // LED初始化函数声明
void ExpLEDInit(void); // 外部LED初始化函数声明
void ExpLEDSet(uint8_t ledbit); // 设置外部LED状态函数声明
#endif
bord.h
#ifndef __BORD_H_
#define __BORD_H_
#include "system_config.h"
//头文件包含
/*************SYSTEM*****************/
/*#include ".\sys\sys.h"*/
#include ".\delay\delay.h"
#include ".\usart\usart.h"
#include ".\timer\timer.h"
/***********Hardweare***************/
#include "led.h"
/***********Funlibrary***************/
#endif
◆接线:
PA0 <----------> D1
PA1 <----------> D2
PA2 <----------> D3
PA3 <----------> D4
PA4 <----------> D5
PA5 <----------> D6
PA6 <----------> D7
PA7 <----------> D8
本实验下载后,开发板上的LED D1-D8先依次被点亮,直到8颗LED全部被点亮后,开始由D1至D8依次熄灭。