<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          stm32流水燈實驗

          作者: 時間:2016-11-13 來源:網(wǎng)絡(luò) 收藏
          test.c 包括3個頭文件

          #include "stm32f10x_lib.h" stm32的一些庫函數(shù)
          #include "sys.h" 系統(tǒng)時鐘初始化及一些延時函數(shù)
          #include "led.h" 流水燈相關(guān)程序

          本文引用地址:http://www.ex-cimer.com/article/201611/316397.htm

          =========================================================================

          先看一下 stm32f10x_lib.h

          里面又包括一個頭文件 #include "stm32f10x_map.h"

          接下來是類似

          #ifdef _ADC
          #include "stm32f10x_adc.h"
          #endif /*_ADC */

          #ifdef _BKP
          #include "stm32f10x_bkp.h"
          #endif /*_BKP */

          #ifdef _CAN
          #include "stm32f10x_can.h"
          #endif /*_CAN */

          ........

          #ifdef _GPIO 如果定義了_GPIO 則包含GPIO相關(guān)的頭文件頭 定義這些宏是在stm32f10x_conf.h中定義的

          #include "stm32f10x_gpio.h"
          #endif /*_GPIO */
          .......

          再看這個頭文件 stm32f10x_map.h

          又包括幾個頭文件

          /* Includes ------------------------------------------------------------------*/
          #include "stm32f10x_conf.h" 類似 #define _GPIOA 宏定義
          #include "stm32f10x_type.h" 類似 typedef signed long s32; 的一些數(shù)據(jù)類型
          #include "cortexm3_macro.h"

          stm32f10x_map.h定義了

          /*------------------------ General Purpose and Alternate Function IO ---------*/
          typedef struct
          {
          vu32 CRL;
          vu32 CRH;
          vu32 IDR;
          vu32 ODR;
          vu32 BSRR;
          vu32 BRR;
          vu32 LCKR;
          } GPIO_TypeDef;
          類似的結(jié)構(gòu)體

          ......

          #define PERIPH_BASE ((u32)0x40000000)

          #define APB2PERIPH_BASE (PERIPH_BASE + 0x10000)

          #define GPIOC_BASE (APB2PERIPH_BASE + 0x1000)

          然后來了重要的部分

          #ifdef _GPIOC
          #define GPIOC ((GPIO_TypeDef *) GPIOC_BASE)
          #endif /*_GPIOC */

          把從GPIOC_BASE 開始的內(nèi)存強制類型轉(zhuǎn)換成 GPIO_TypeDef 這種類型的結(jié)構(gòu)體

          在led.h 中這樣調(diào)用 GPIOC->ODR=(GPIOC->ODR&~LED0)|(x ? LED0:0)

          0x40011000 +12的地址賦值 ,即完成對相應(yīng)寄存器的賦值

          ===================================================================

          再看一下 sys.h 簡單的寄存器賦值,對照手冊看就很容易理解

          //us延時函數(shù)
          void delay_us(unsigned int us)
          {
          u8 n;
          while(us--)for(n=0;n}
          //ms延時函數(shù)
          void delay_ms(unsigned int ms)
          {
          while(ms--)delay_us(1000);
          }

          //把所有時鐘寄存器復(fù)位
          void RCC_DeInit(void)
          {
          RCC->APB2RSTR = 0x00000000;//外設(shè)復(fù)位
          RCC->APB1RSTR = 0x00000000;
          RCC->AHBENR = 0x00000014; //flash時鐘,閃存時鐘使能.DMA時鐘關(guān)閉
          RCC->APB2ENR = 0x00000000; //外設(shè)時鐘關(guān)閉.
          RCC->APB1ENR = 0x00000000;
          RCC->CR |= 0x00000001; //使能內(nèi)部高速時鐘HSION
          RCC->CFGR &= 0xF8FF0000; //復(fù)位SW[1:0],HPRE[3:0],PPRE1[2:0],PPRE2[2:0],ADCPRE[1:0],MCO[2:0]
          RCC->CR &= 0xFEF6FFFF; //復(fù)位HSEON,CSSON,PLLON
          RCC->CR &= 0xFFFBFFFF; //復(fù)位HSEBYP
          RCC->CFGR &= 0xFF80FFFF; //復(fù)位PLLSRC, PLLXTPRE, PLLMUL[3:0] and USBPRE
          RCC->CIR = 0x00000000; //關(guān)閉所有中斷
          }

          //外部8M,則得到72M的系統(tǒng)時鐘
          void Stm32_Clock_Init(void)
          {
          unsigned char temp=0;
          u8 timeout=0;
          RCC_DeInit();
          RCC->CR|=0x00010000; //外部高速時鐘使能HSEON

          timeout=0;
          while(!(RCC->CR>>17)&&timeout<200)timeout++;//等待外部時鐘就緒

          //0-24M 等待0;24-48M 等待1;48-72M等待2;(非常重要!)
          FLASH->ACR|=0x32;//FLASH 2個延時周期

          RCC->CFGR|=0X001D2400;//APB1/2=DIV2;AHB=DIV1;PLL=9*CLK;HSE作為PLL時鐘源
          RCC->CR|=0x01000000; //PLLON

          timeout=0;
          while(!(RCC->CR>>25)&&timeout<200)timeout++;//等待PLL鎖定

          RCC->CFGR|=0x00000002;//PLL作為系統(tǒng)時鐘
          while(temp!=0x02&&timeout<200) //等待PLL作為系統(tǒng)時鐘設(shè)置成功
          {
          temp=RCC->CFGR>>2;
          timeout++;
          temp&=0x03;
          }
          }

          ==============================================================================

          led.h

          #define LED0 (1<<10)// PC10
          #define LED1 (1<<11)// PC11
          #define LED2 (1<<12)// PC12
          #define LED3 (1<<2) // PD2

          #define LED0_SET(x) GPIOC->ODR=(GPIOC->ODR&~LED0)|(x ? LED0:0)
          #define LED1_SET(x) GPIOC->ODR=(GPIOC->ODR&~LED1)|(x ? LED1:0)
          #define LED2_SET(x) GPIOC->ODR=(GPIOC->ODR&~LED2)|(x ? LED2:0)
          #define LED3_SET(x) GPIOD->ODR=(GPIOD->ODR&~LED3)|(x ? LED3:0)
          //LED IO初始化
          void led_init(void)
          {
          RCC->APB2ENR|=1<<4; //使能PORTC時鐘
          GPIOC->CRH&=0XFFF000FF;
          GPIOC->CRH|=0X00033300;//PC.10/11/12推挽輸出
          GPIOC->ODR|=0X1C00; //PC.10/11/12 輸出高
          RCC->APB2ENR|=1<<5; //使能PORTD時鐘
          GPIOD->CRL&=0XFFFFF0FF;
          GPIOD->CRL|=0X300; //PD.2推挽輸出
          GPIOD->ODR|=1<<2; //PD.2輸出高
          }



          關(guān)鍵詞: stm32流水燈實

          評論


          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();