標(biāo)題:
LED雙燈閃爍實(shí)驗(yàn) 寄存器版程序分享 51單片機(jī)到STM32過渡
[打印本頁]
作者:
king@
時(shí)間:
2017-3-13 15:43
標(biāo)題:
LED雙燈閃爍實(shí)驗(yàn) 寄存器版程序分享 51單片機(jī)到STM32過渡
51單片機(jī)到STM32單片機(jī)的過渡,寄存器版程序分享!
本例程使用MDK4.12版本編譯好,進(jìn)入MDK文件夾點(diǎn)擊project.uvproj即可打開例程
1.MDK文件夾
--- MDK啟動軟件
2.obj文件夾
--- 包含編譯產(chǎn)生的中間文件
--- 最終可執(zhí)行的HEX文件程序
3.src文件夾(包含所有的源代碼)
---包含main.c文件
/* 本實(shí)驗(yàn)使得GPIOD8和GPIOD9管腳控制STM32VCT6的LED1和LED2兩個(gè)燈進(jìn)行閃爍 */
#define __IO volatile
typedef unsigned int uint32_t;
typedef __IO uint32_t vu32;
typedef unsigned short int uint16_t;
#define GPIO_Pin_0 ((uint16_t)0x0001) /*!< Pin 0 selected */
#define GPIO_Pin_1 ((uint16_t)0x0002) /*!< Pin 1 selected */
#define GPIO_Pin_2 ((uint16_t)0x0004) /*!< Pin 2 selected */
#define GPIO_Pin_3 ((uint16_t)0x0008) /*!< Pin 3 selected */
#define GPIO_Pin_4 ((uint16_t)0x0010) /*!< Pin 4 selected */
#define GPIO_Pin_5 ((uint16_t)0x0020) /*!< Pin 5 selected */
#define GPIO_Pin_6 ((uint16_t)0x0040) /*!< Pin 6 selected */
#define GPIO_Pin_7 ((uint16_t)0x0080) /*!< Pin 7 selected */
#define GPIO_Pin_8 ((uint16_t)0x0100) /*!< Pin 8 selected */
#define GPIO_Pin_9 ((uint16_t)0x0200) /*!< Pin 9 selected */
#define GPIO_Pin_10 ((uint16_t)0x0400) /*!< Pin 10 selected */
#define GPIO_Pin_11 ((uint16_t)0x0800) /*!< Pin 11 selected */
#define GPIO_Pin_12 ((uint16_t)0x1000) /*!< Pin 12 selected */
#define GPIO_Pin_13 ((uint16_t)0x2000) /*!< Pin 13 selected */
#define GPIO_Pin_14 ((uint16_t)0x4000) /*!< Pin 14 selected */
#define GPIO_Pin_15 ((uint16_t)0x8000) /*!< Pin 15 selected */
#define GPIO_Pin_All ((uint16_t)0xFFFF) /*!< All pins selected */
#define RCC_APB2Periph_AFIO ((uint32_t)0x00000001)
#define RCC_APB2Periph_GPIOA ((uint32_t)0x00000004)
#define RCC_APB2Periph_GPIOB ((uint32_t)0x00000008)
#define RCC_APB2Periph_GPIOC ((uint32_t)0x00000010)
#define RCC_APB2Periph_GPIOD ((uint32_t)0x00000020)
/************ GPIOD <*************/
typedef struct
{
__IO uint32_t CRL;
__IO uint32_t CRH;
__IO uint32_t IDR;
__IO uint32_t ODR;
__IO uint32_t BSRR;
__IO uint32_t BRR;
__IO uint32_t LCKR;
} GPIO_TypeDef;
typedef struct
{
__IO uint32_t CR;
__IO uint32_t CFGR;
__IO uint32_t CIR;
__IO uint32_t APB2RSTR;
__IO uint32_t APB1RSTR;
__IO uint32_t AHBENR;
__IO uint32_t APB2ENR;
__IO uint32_t APB1ENR;
__IO uint32_t BDCR;
__IO uint32_t CSR;
} RCC_TypeDef;
/********* GPIOD管腳的內(nèi)存對應(yīng)地址 *******/
#define PERIPH_BASE ((uint32_t)0x40000000)
#define APB2PERIPH_BASE (PERIPH_BASE + 0x10000)
#define GPIOD_BASE (APB2PERIPH_BASE + 0x1400)
#define GPIOD ((GPIO_TypeDef *) GPIOD_BASE)
/************ RCC時(shí)鐘 <*************/
#define AHBPERIPH_BASE (PERIPH_BASE + 0x20000)
#define RCC_BASE (AHBPERIPH_BASE + 0x1000)
#define RCC ((RCC_TypeDef *) RCC_BASE)
void Delay(vu32 nCount);
int main(void) //main是程序入口
{
/* 使能APB2總線的時(shí)鐘,對GPIO的端口D時(shí)鐘使能 */
RCC->APB2ENR |= RCC_APB2Periph_GPIOD;
/*-- GPIO Mode Configuration速度,輸入或輸出 -----------------------*/
/*-- GPIO CRL Configuration 設(shè)置IO端口低8位的模式(輸入還是輸出)---*/
/*-- GPIO CRH Configuration 設(shè)置IO端口高8位的模式(輸入還是輸出)---*/
GPIOD->CRH &= 0xFFFFFF00; //清除對位8位9的配置
GPIOD->CRH |= 0x00000033; /* 設(shè)置GPIOD的PD8和PD9配置為通用推挽模式輸出50MHZ(PD.8和PD.9的推挽輸出)*/
while (1)
{
/* 對BSRR設(shè)置為1,則GPIO輸出為1 */
GPIOD->BSRR = GPIO_Pin_8;
GPIOD->BSRR = GPIO_Pin_9;
Delay(0x2FFFFF);
/* 對BRR設(shè)置為1,則GPIO輸出為0 */
GPIOD->BRR = GPIO_Pin_8;
GPIOD->BRR = GPIO_Pin_9;
Delay(0x2FFFFF);
}
}
void Delay(vu32 nCount) //通過不斷for循環(huán)nCount次,達(dá)到延時(shí)的目的口
{
for(; nCount != 0; nCount--);
}
復(fù)制代碼
0.png
(61.59 KB, 下載次數(shù): 107)
下載附件
2017-3-15 01:02 上傳
完整代碼下載:
LED雙燈閃爍實(shí)驗(yàn)(STM32-寄存器版).7z
(12.36 KB, 下載次數(shù): 9)
2021-10-29 17:27 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1