LED流水燈
led.h
#ifndef __LED_H
#define __LED_H
#include "stm32f4xx.h"
void Led_Init(void);
void Led1_Init(void);
void Led2_Init(void);
void Led3_Init(void);
void Led4_Init(void);
#endif
typedef struct
{
uint32_t GPIO_Pin; /* 指定要配置的GPIO引腳 */
GPIOMode_TypeDef GPIO_Mode; /* 指定選定接點的操作模式。*/
GPIOSpeed_TypeDef GPIO_Speed; /* 指定選定接點的速度。*/
GPIOOType_TypeDef GPIO_OType; /* 指定選定接點的操作輸出類型。*/
GPIOPuPd_TypeDef GPIO_PuPd; /* 指定選定接點的操作上拉/下拉。*/
}GPIO_InitTypeDef;
led.c
#include "led.h"
// init 4 pin
void Led_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
//使能GPIOE組時鐘
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
//使能GPIOF組時鐘
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
// GPIOF pin9 pin10
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10; //引腳9/10
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; //輸出模式
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; //推挽輸出
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; //速度
GPIO_Init(GPIOF, &GPIO_InitStruct);
// GPIOE pin9 pin10
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14; //引腳13/14
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; //輸出模式
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; //推挽輸出
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; //速度
GPIO_Init(GPIOE, &GPIO_InitStruct);
}
/*********************************
引腳說明:
LED0 -- PF9
**********************************/
void Led1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
//使能GPIOF組時鐘
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9; //引腳9
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; //輸出模式
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; //推挽輸出
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; //速度
GPIO_Init(GPIOF, &GPIO_InitStruct);
}
/*********************************
引腳說明:
LED2 -- PF10
**********************************/
void Led2_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
//使能GPIOF組時鐘
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10; //引腳10
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; //輸出模式
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; //推挽輸出
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; //速度
GPIO_Init(GPIOF, &GPIO_InitStruct);
}
/*********************************
引腳說明:
LED3 -- PE13
**********************************/
void Led3_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
//使能GPIOE組時鐘
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13; //引腳13
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; //輸出模式
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; //推挽輸出
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; //速度
GPIO_Init(GPIOE, &GPIO_InitStruct);
}
/*********************************
引腳說明:
LED4 -- PE14
**********************************/
void Led4_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
//使能GPIOE組時鐘
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_14; //引腳14
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; //輸出模式
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; //推挽輸出
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; //速度
GPIO_Init(GPIOE, &GPIO_InitStruct);
}
main.c
#include "stm32f4xx.h"
#include "led.h"
// 延時
void delay(int n)
{
int i,j;
for(i=0; i< n; i++)
for(j=0; j< 30000; j++);
}
int main(void)
{
// Led1_Init();
// Led2_Init();
// Led3_Init();
// Led4_Init();
Led_Init();
while(1)
{
//輸出0 LED0燈亮
GPIO_ResetBits(GPIOF, GPIO_Pin_9);
delay(1000);
//輸出1 LED0燈滅
GPIO_SetBits(GPIOF, GPIO_Pin_9);
//輸出0 LED1燈亮
GPIO_ResetBits(GPIOF, GPIO_Pin_10);
delay(1000);
//輸出1 LED1燈滅
GPIO_SetBits(GPIOF, GPIO_Pin_10);
//輸出0 LED2燈亮
GPIO_ResetBits(GPIOE, GPIO_Pin_13);
delay(1000);
//輸出1 LED2燈滅
GPIO_SetBits(GPIOE, GPIO_Pin_13);
//輸出0 LED3燈亮
GPIO_ResetBits(GPIOE, GPIO_Pin_14);
delay(1000);
//輸出1 LED3燈滅
GPIO_SetBits(GPIOE, GPIO_Pin_14);
}
return 0;
}
按鍵控制燈
#ifndef __LED_H
#define __LED_H
#include "stm32f4xx.h"
#define LED0_ON GPIO_ResetBits(GPIOF, GPIO_Pin_9)
#define LED0_OFF GPIO_SetBits(GPIOF, GPIO_Pin_9)
void Led_Init(void);
#endif
/* ================================================ */
#include "led.h"
/*********************************
引腳說明:
LED0 -- PF9
LED1 -- PF10
LED2 -- PE13
LED3 -- PE14
**********************************/
void Led_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
//使能GPIOE組時鐘
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
//使能GPIOF組時鐘
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10; //引腳9 10
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; //輸出模式
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; //推挽輸出
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; //速度
GPIO_Init(GPIOF, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14; //引腳13 14
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; //輸出模式
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; //推挽輸出
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; //速度
GPIO_Init(GPIOE, &GPIO_InitStruct);
GPIO_SetBits(GPIOF, GPIO_Pin_9);
GPIO_SetBits(GPIOF, GPIO_Pin_10);
GPIO_SetBits(GPIOE, GPIO_Pin_13);
GPIO_SetBits(GPIOE, GPIO_Pin_14);
}
#ifndef __KEY_H
#define __KEY_H
#include "stm32f4xx.h"
void Key_Init(void);
#endif
#include "key.h"
/*********************************
引腳說明:
KEY0--PA0
**********************************/
void Key_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
//使能GPIOA組時鐘
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0; //引腳0
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN; //輸入模式
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOA, &GPIO_InitStruct);
}
main.c
#include "stm32f4xx.h"
#include "led.h"
#include "key.h"
void delay(int n)
{
int i,j;
for(i=0; i< n; i++)
for(j=0; j< 30000; j++);
}
int main(void)
{
unsigned char count = 0;
Key_Init();
Led_Init();
while(1)
{
/*
//應用場景1
//判斷按鍵是否按下 按下為低電平
if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == Bit_RESET)
{
//大約延時20ms 起到消抖作用
delay(20);
//判斷按鍵是否按下 按下為低電平
if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == Bit_RESET)
{
//燈狀態變更
GPIO_ToggleBits(GPIOF, GPIO_Pin_9);
count++;
}
}
//根據按鍵時長實現不同的功能
if(count == 50)
{
GPIO_ResetBits(GPIOE, GPIO_Pin_14);
}
*/
//應用場景2 比如輸入銀行密碼
//判斷按鍵是否按下 按下為低電平
if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == Bit_RESET)
{
//大約延時15ms 起到消抖作用
delay(15);
//判斷按鍵是否按下 按下為低電平
if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == Bit_RESET)
{
//等待按鍵松開
while(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == Bit_RESET);
//燈狀態變更
GPIO_ToggleBits(GPIOF, GPIO_Pin_9);
}
}
}
return 0;
}
按鍵中斷
#ifndef __EXTI_H
#define __EXTI_H
#include "stm32f4xx.h"
void Exti_PA0_Init(void);
#endif
#include "exti.h"
/*********************************
引腳說明:
KEY0--PA0(選擇下降沿觸發)
**********************************/
void Exti_PA0_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
EXTI_InitTypeDef EXTI_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
//使能SYSCFG及GPIOA時鐘:
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
//使能GPIOA組時鐘
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
//初始化IO口為輸入。
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0; //引腳0
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN; //輸入模式
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOA, &GPIO_InitStruct);
//設置IO口與中斷線的映射關系。
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);
EXTI_InitStruct.EXTI_Line = EXTI_Line0; //中斷線0
EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt; //中斷模式
EXTI_InitStruct.EXTI_Trigger= EXTI_Trigger_Falling; //下降觸發
EXTI_InitStruct.EXTI_LineCmd= ENABLE; //中斷使能
//初始化線上中斷,設置觸發條件等。
EXTI_Init(&EXTI_InitStruct);
NVIC_InitStruct.NVIC_IRQChannel = EXTI0_IRQn; //中斷通道,可在stm32f4xx.h文件當中查找
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1; //搶占優先級
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1; //響應優先級
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; //通道使能
//配置中斷分組(NVIC),并使能中斷。
NVIC_Init(&NVIC_InitStruct);
}
void delays(int n)
{
int i,j;
for(i=0; i< n; i++)
for(j=0; j< 30000; j++);
}
/***************************************************************
1、中斷服務函數是滿足條件后,CPU自行執行的函數不需要主動調用
2、中斷服務函數是不能傳遞值與返回值
3、STM32的中斷服務函數名可在startup_stm32f40_41xxx.s中查找
****************************************************************/
//編寫中斷服務函數
void EXTI0_IRQHandler(void)
{
//判斷標志位是否1
if(EXTI_GetITStatus(EXTI_Line0) == SET)
{
if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == Bit_RESET)
{
//延時一段時間
delays(15);
if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == Bit_RESET)
{
GPIO_ToggleBits(GPIOF, GPIO_Pin_9);
}
}
}
//清空中斷線0
EXTI_ClearITPendingBit(EXTI_Line0);
}
main.c
#include "stm32f4xx.h"
#include "led.h"
#include "key.h"
void delay(int n)
{
int i,j;
for(i=0; i< n; i++)
for(j=0; j< 30000; j++);
}
int main(void)
{
unsigned char count = 0;
Key_Init();
Led_Init();
while(1)
{
//判斷按鍵1是否按下 按下為低電平
if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == Bit_RESET)
{
//大約延時15ms 起到消抖作用
delay(15);
//判斷按鍵1是否按下 按下為低電平
if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == Bit_RESET)
{
//等待按鍵1松開
while(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == Bit_RESET);
//燈1狀態變更
GPIO_ToggleBits(GPIOF, GPIO_Pin_9);
}
}
//判斷按鍵2是否按下 按下為低電平
if(GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_2) == Bit_RESET)
{
//大約延時15ms 起到消抖作用
delay(15);
//判斷按鍵2是否按下 按下為低電平
if(GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_2) == Bit_RESET)
{
//等待按鍵2松開
while(GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_2) == Bit_RESET);
//燈2狀態變更
GPIO_ToggleBits(GPIOF, GPIO_Pin_10);
}
}
//判斷按鍵3是否按下 按下為低電平
if(GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_3) == Bit_RESET)
{
//大約延時15ms 起到消抖作用
delay(15);
//判斷按鍵3是否按下 按下為低電平
if(GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_3) == Bit_RESET)
{
//等待按鍵3松開
while(GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_3) == Bit_RESET);
//燈3狀態變更
GPIO_ToggleBits(GPIOE, GPIO_Pin_13);
}
}
//判斷按鍵4是否按下 按下為低電平
if(GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_4) == Bit_RESET)
{
//大約延時15ms 起到消抖作用
delay(15);
//判斷按鍵4是否按下 按下為低電平
if(GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_4) == Bit_RESET)
{
//等待按鍵4松開
while(GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_4) == Bit_RESET);
//燈4狀態變更
GPIO_ToggleBits(GPIOE, GPIO_Pin_14);
}
}
}
return 0;
}
作業
#ifndef __BEEP_H
#define __BEEP_H
#include "stm32f4xx.h"
void Beep_Init(void);
#endif
#include "beep.h"
/************************************
引腳說明
Beep -- PF8
************************************/
void Beep_Init()
{
GPIO_InitTypeDef GPIO_InitStruct;
//使能GPIOF組時鐘
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8; //引腳8
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; //輸出模式
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; //推挽輸出
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_DOWN; //xia la
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz; //速度
GPIO_Init(GPIOF, &GPIO_InitStruct);
}
#include "stm32f4xx.h"
#include "beep.h"
void delay(int n)
{
int i,j;
for(i=0; i< n; i++)
for(j=0; j< 30000; j++);
}
int main(void)
{
Beep_Init();
while(1)
{
GPIO_ResetBits(GPIOF, GPIO_Pin_8);
delay(1000);
GPIO_SetBits(GPIOF, GPIO_Pin_8);
delay(1000);
}
return 0;
}
聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。
舉報投訴
-
STM32
+關注
關注
2287文章
10988瀏覽量
361636 -
RCC
+關注
關注
0文章
94瀏覽量
27351 -
GPIO
+關注
關注
16文章
1246瀏覽量
53386 -
按鍵控制
+關注
關注
1文章
44瀏覽量
8933 -
LED流水燈
+關注
關注
0文章
10瀏覽量
8474
發布評論請先 登錄
相關推薦
熱點推薦
基于標準庫函數與基于HAL庫函數的stm32編程方式對比
一、基于標準庫函數的stm32編程方式二、基于HAL庫函數的stm32編程方式差異上面也提到了,STM32有非常多的寄存器,而導致了
發表于 12-28 19:09
?30次下載

STM32庫函數開發-GPIO
2021-01-11 學習日志STM32f1庫函數開發學習實戰一 · I/O口1. 文件夾結構2. 配置細節 · 從寄存器到庫函數3. 跑馬燈4.
發表于 01-13 16:17
?14次下載

評論