微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > STM32F407VG IO位操作

STM32F407VG IO位操作

时间:11-25 来源:互联网 点击:

  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)

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top