您的位置 首页 厂商

STM32F407VG IO位操作

includedefineBITBAND(addr,bitnum)((addr0xF0000000)+0x2000000+((addr0xFFFFF)5)+(bitnum2))defineMEM_AD

  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);//设置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;//设置PD12,13,14,15输出
  76. GPIOD->MODER|=0x55000000;
  77. GPIOD->OTYPER&=0xFFFF0FFF;//设置PD12,13,14,15推挽输出
  78. GPIOD->OSPEEDR&=0x00FFFFFF;//PD12,13,14,15速度100m
  79. GPIOD->OSPEEDR|=0xff000000;
  80. SYSCFG->CMPCR=0x00000001;//运用IO补偿单元,当GPIO速度超越50M的时分要考虑运用此设置
  81. GPIOD->PUPDR&=0x00FFFFFF;//PD12,13,14,15无上拉无下拉
  82. }
  83. voidkey_init(void)
  84. {
  85. RCC->AHB1ENR|=0x00000001;//使能GPIOA时钟
  86. GPIOA->MODER&=0xFFFFFFFC;//设置PA0输入
  87. GPIOA->PUPDR&=0xFFFFFFFC;//设置PA0为下拉,由于PA0的输入状况为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. }

阐明:以上程序是在以下两个材料下修正而成的。

(1)“STM32F407VG用位带别号区完成IO位操作例程”(杨年荣)http://wenku.baidu.com/view/4057a658804d2b160b4ec083.html

(2)“stm32f407学习之四————GPIO(操作寄存器)” http://blog.csdn.net/w471176877/article/details/7957171

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/changshang/259577.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部