STM32F103ZET6之跑馬燈實驗
文章目錄
- STM32F103ZET6之跑馬燈實驗
- 前言
- 一、原理圖
- 二、程式原始碼
- 1.led.h
- 2.led.c
- 3.main.c
- 三、實驗結果
- 總結
前言
對于STM32的學習可分為3個版本,
1.暫存器版本
2.庫函式版本
3.HAL庫版本
由于個人原因,選擇庫函式版本來進行STM32的學習,
提示:軟體安裝等問題,不進行講解!!!
一、原理圖
原理圖可知,PB5對應LED0,PE5對應LED1,


二、程式原始碼
1.led.h
代碼如下:
#ifndef _LED_H
#define _LED_H
void LED_Init(void);
#endif
2.led.c
代碼如下:
#include "led.h"
#include "stm32f10x.h"
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_Initstr;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE); //時鐘使能
GPIO_Initstr.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Initstr.GPIO_Pin=GPIO_Pin_5 ;
GPIO_Initstr.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_Initstr); //配置IO
GPIO_SetBits(GPIOB, GPIO_Pin_5); //輸出配置
GPIO_Initstr.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Initstr.GPIO_Pin=GPIO_Pin_5 ;
GPIO_Initstr.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_Initstr);
GPIO_SetBits(GPIOE, GPIO_Pin_5);
}
3.main.c
代碼如下:
#include "stm32f10x.h"
#include "led.h"
#include "delay.h"
int main(void)
{
delay_init();
LED_Init();
while(1)
{
//方法一 LED0,LED1同時點亮,同時熄滅,間隔為500ms(庫函式)
GPIO_SetBits(GPIOB, GPIO_Pin_5);
GPIO_SetBits(GPIOE, GPIO_Pin_5);
delay_ms(500);
GPIO_ResetBits(GPIOB, GPIO_Pin_5);
GPIO_ResetBits(GPIOE, GPIO_Pin_5);
delay_ms(500);
//方法二 跑馬燈,間隔為500ms(位操作)
// PBout(5)=0;
// PEout(5)=1;
// delay_ms(500);
// PBout(5)=1;
// PEout(5)=0;
// delay_ms(500);
}
}
三、實驗結果
如圖所示,兩個LED燈同時點亮,同時熄滅,間隔為500ms,

總結
學習的程序中,希望大家觀看一遍視頻,然后自己撰寫程式,遇到不會時再觀看視頻,最后方能靈活掌握!!!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/247717.html
標籤:其他
