新聞中心

STM32F407VG IO位操作

作者: 時間:2016-11-25 來源:網(wǎng)絡(luò) 收藏
  1. #include
  2. #defineBITBAND(addr,bitnum)((addr&0xF0000000)+0x2000000+((addr&0xFFFFF)<<5)+(bitnum<<2))
  3. #defineMEM_ADDR(addr)*((volatileunsignedlong*)(addr))
  4. #defineBIT_ADDR(addr,bitnum)MEM_ADDR(BITBAND(addr,bitnum))
  5. //F4XXIO口地址映射
  6. #defineGPIOA_ODR_Addr(GPIOA_BASE+20)//0x40020014
  7. #defineGPIOB_ODR_Addr(GPIOB_BASE+20)//0x40020414
  8. #defineGPIOC_ODR_Addr(GPIOC_BASE+20)//0x40020814
  9. #defineGPIOD_ODR_Addr(GPIOD_BASE+20)//0x40020C14
  10. #defineGPIOE_ODR_Addr(GPIOE_BASE+20)//0x40021014
  11. #defineGPIOF_ODR_Addr(GPIOF_BASE+20)//0x40021414
  12. #defineGPIOG_ODR_Addr(GPIOG_BASE+20)//0x40021814
  13. #defineGPIOH_ODR_Addr(GPIOH_BASE+20)//0x40021C14
  14. #defineGPIOI_ODR_Addr(GPIOI_BASE+20)//0x40022014
  15. #defineGPIOA_IDR_Addr(GPIOA_BASE+16)//0x40020010
  16. #defineGPIOB_IDR_Addr(GPIOB_BASE+16)//0x40020410
  17. #defineGPIOC_IDR_Addr(GPIOC_BASE+16)//0x40020810
  18. #defineGPIOD_IDR_Addr(GPIOD_BASE+16)//0x40020C10
  19. #defineGPIOE_IDR_Addr(GPIOE_BASE+16)//0x40021010
  20. #defineGPIOF_IDR_Addr(GPIOF_BASE+16)//0x40021410
  21. #defineGPIOG_IDR_Addr(GPIOG_BASE+16)//0x40021810
  22. #defineGPIOH_IDR_Addr(GPIOF_BASE+16)//0x40021C10
  23. #defineGPIOI_IDR_Addr(GPIOG_BASE+16)//0x40022010
  24. //F4XXIO口操作,只對單一的IO,n的值范圍[0-15]
  25. #defineGPIOAout(n)BIT_ADDR(GPIOA_ODR_Addr,n)//GPIOA某一位輸出
  26. #defineGPIOAin(n)BIT_ADDR(GPIOA_IDR_Addr,n)//GPIOA某一位輸入
  27. #defineGPIOBout(n)BIT_ADDR(GPIOB_ODR_Addr,n)//GPIOB某一位輸出
  28. #defineGPIOBin(n)BIT_ADDR(GPIOB_IDR_Addr,n)//GPIOB某一位輸入
  29. #defineGPIOCout(n)BIT_ADDR(GPIOC_ODR_Addr,n)//GPIOC某一位輸出
  30. #defineGPIOCin(n)BIT_ADDR(GPIOC_IDR_Addr,n)//GPIOC某一位輸入
  31. #defineGPIODout(n)BIT_ADDR(GPIOD_ODR_Addr,n)//GPIOD某一位輸出
  32. #defineGPIODin(n)BIT_ADDR(GPIOD_IDR_Addr,n)//GPIOD某一位輸入
  33. #defineGPIOEout(n)BIT_ADDR(GPIOE_ODR_Addr,n)//GPIOE某一位輸出
  34. #defineGPIOEin(n)BIT_ADDR(GPIOE_IDR_Addr,n)//GPIOE某一位輸入
  35. #defineGPIOFout(n)BIT_ADDR(GPIOF_ODR_Addr,n)//GPIOF某一位輸出
  36. #defineGPIOFin(n)BIT_ADDR(GPIOF_IDR_Addr,n)//GPIOF某一位輸入
  37. #defineGPIOGout(n)BIT_ADDR(GPIOG_ODR_Addr,n)//GPIOG某一位輸出
  38. #defineGPIOGin(n)BIT_ADDR(GPIOG_IDR_Addr,n)//GPIOG某一位輸入
  39. #defineGPIOHout(n)BIT_ADDR(GPIOH_ODR_Addr,n)//GPIOH某一位輸出
  40. #defineGPIOHin(n)BIT_ADDR(GPIOH_IDR_Addr,n)//GPIOH某一位輸入
  41. #defineGPIOIout(n)BIT_ADDR(GPIOI_ODR_Addr,n)//GPIOI某一位輸出
  42. #defineGPIOIin(n)BIT_ADDR(GPIOI_IDR_Addr,n)//GPIOI某一位輸入
  43. #defineLED1GPIODout(12)//定義LED1GPIOD_Pin_12
  44. #defineLED2GPIODout(13)//定義LED1GPIOD_Pin_13
  45. #defineLED3GPIODout(14)//定義LED1GPIOD_Pin_14
  46. #defineLED4GPIODout(15)//定義LED1GPIOD_Pin_15
  47. #defineKEY1GPIOAin(0)//定義KEY1GPIOC_Pin_0
  48. uint32_tGb_TimingDelay;
  49. voidDelay(uint32_tnTime);
  50. voidLED_init(void);
  51. voidkey_init(void);
  52. voidkey_scan(void);
  53. intmain(void)
  54. {
  55. SysTick_Config(SystemCoreClock/1000);//設(shè)置systemtick一毫秒中斷
  56. RCC->APB2ENR|=(1<<14);//使能syscfg時鐘
  57. LED_init();
  58. key_init();
  59. //LED1=1;//LED燈亮
  60. while(1)
  61. {
  62. //流水燈
  63. LED1=!LED1;
  64. LED2=!LED2;
  65. LED3=!LED3;
  66. LED4=!LED4;
  67. Delay(1000);
  68. //按鍵掃描
  69. key_scan();
  70. }
  71. }
  72. voidLED_init(void)
  73. {
  74. RCC->AHB1ENR|=0x00000008;//使能GPIOD時鐘
  75. GPIOD->MODER&=0x00FFFFFF;//設(shè)置PD12,13,14,15輸出
  76. GPIOD->MODER|=0x55000000;
  77. GPIOD->OTYPER&=0xFFFF0FFF;//設(shè)置PD12,13,14,15推挽輸出
  78. GPIOD->OSPEEDR&=0x00FFFFFF;//PD12,13,14,15速度100m
  79. GPIOD->OSPEEDR|=0xff000000;
  80. SYSCFG->CMPCR=0x00000001;//使用IO補償單元,當(dāng)GPIO速度超過50M的時候要考慮使用此設(shè)置
  81. GPIOD->PUPDR&=0x00FFFFFF;//PD12,13,14,15無上拉無下拉
  82. }
  83. voidkey_init(void)
  84. {
  85. RCC->AHB1ENR|=0x00000001;//使能GPIOA時鐘
  86. GPIOA->MODER&=0xFFFFFFFC;//設(shè)置PA0輸入
  87. GPIOA->PUPDR&=0xFFFFFFFC;//設(shè)置PA0為下拉,因為PA0的輸入狀態(tài)為1
  88. GPIOA->PUPDR|=0x00000002;
  89. }
  90. voidkey_scan(void)
  91. {
  92. if(KEY1==1)//如果按鍵按下
  93. {
  94. Delay(10);//去抖
  95. if(KEY1==1)//如果按鍵按下
  96. {
  97. LED1=!LED1;//LED燈亮
  98. while(KEY1==1);//松手檢測
  99. }
  100. }
  101. }
  102. voidDelay(uint32_tnTime)
  103. {
  104. Gb_TimingDelay=nTime;
  105. while(Gb_TimingDelay!=0);
  106. }
  107. voidSysTick_Handler(void)
  108. {
  109. if(Gb_TimingDelay!=0x00)
  110. {
  111. Gb_TimingDelay--;
  112. }
  113. }

本文引用地址:http://m.butianyuan.cn/article/201611/321486.htm

說明:以上程序是在以下兩個資料下修改而成的。

(1)“STM32F407VG用位帶別名區(qū)實現(xiàn)IO位操作例程”(楊年榮)http://wenku.baidu.com/view/4057a658804d2b160b4ec083.html

(2)“stm32f407學(xué)習(xí)之四————GPIO(操作寄存器)” http://blog.csdn.net/w471176877/article/details/7957171




關(guān)鍵詞: STM32F407VGIO位操

評論


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

關(guān)閉