STM32-----LED
來(lái)源:程序員人生 發(fā)布時(shí)間:2014-10-13 00:36:31 閱讀次數(shù):2510次
#include "stm32f10x.h"
/* 為了使用編程書(shū)寫(xiě)方便,我們定義幾個(gè)控制LED開(kāi)關(guān)的宏 */
#define LED1_ON() {GPIO_ResetBits(GPIOF, GPIO_Pin_6);} /* PF6 = 0 點(diǎn)亮LED1 */
#define LED1_OFF() {GPIO_SetBits(GPIOF, GPIO_Pin_6);} /* PF6 = 1 熄滅LED1 */
#define LED2_ON() {GPIO_ResetBits(GPIOF, GPIO_Pin_7);} /* PF7 = 0 點(diǎn)亮LED2 */
#define LED2_OFF() {GPIO_SetBits(GPIOF, GPIO_Pin_7);} /* PF7 = 1 點(diǎn)亮LED2 */
#define LED3_ON() {GPIO_ResetBits(GPIOF, GPIO_Pin_8);} /* PF8 = 0 點(diǎn)亮LED3 */
#define LED3_OFF() {GPIO_SetBits(GPIOF, GPIO_Pin_8);} /* PF8 = 1 點(diǎn)亮LED3 */
#define LED4_ON() {GPIO_ResetBits(GPIOF, GPIO_Pin_9);} /* PF9 = 0 點(diǎn)亮LED4 */
#define LED4_OFF() {GPIO_SetBits(GPIOF, GPIO_Pin_9);} /* PF9 = 1 點(diǎn)亮LED4 */
void Delay(__IO uint32_t nCount)
{
for(; nCount != 0; nCount--);
}
int main(void)
{
{
GPIO_InitTypeDef GPIO_InitStructure;
/* 打開(kāi)GPIOF 時(shí)鐘 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE);
/* 在將LED設(shè)置為輸出前先設(shè)置輸出1,避免一開(kāi)始就點(diǎn)亮LED */
GPIO_SetBits(GPIOF, GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9);
/* 可以對(duì)PF6 - PF9 一起初始化 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7
| GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; /* 推挽輸出模式 */
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOF, &GPIO_InitStructure); /* 調(diào)用庫(kù)函數(shù)初始化GPIO */
}
/* 下面這個(gè) while循環(huán)實(shí)現(xiàn)簡(jiǎn)單的跑馬燈功能 */
while (1)
{
LED1_ON(); /* 點(diǎn)亮LED1 */
Delay(0xAFFFF); /* 插入延時(shí) */
LED2_ON(); /* 點(diǎn)亮LED2 */
LED3_ON(); /* 點(diǎn)亮LED3 */
LED1_OFF(); /* 熄滅LED1 */
Delay(0xAFFFF); /* 插入延時(shí) */
LED4_ON(); /* 點(diǎn)亮LED4 */
LED2_OFF(); /* 關(guān)閉LED2 */
LED3_OFF(); /* 關(guān)閉LED3 */
Delay(0xAFFFF); /* 插入延時(shí) */
LED4_OFF(); /* 關(guān)閉LED4 */
}
}
生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)