STM32连接到地后将GPIO引脚拉高工作出现的问题

2019-07-14 18:03发布

我想用STM32_smart v2开发板从GPIO到地面的7个跳线制作廉价可靠的7位输入接口。使用内部上拉电阻。我第一次没有将任何GPIO连接到地面的时候电压显示3.3V。在第一次连接后,我使用的GPIO从1V到2.7V,即使不再连接到地面也是如此。这是为什么?有人能解答一下吗? 1.png
  1. #include "stm32f10x.h"
  2. //#include "stm32_eval.h"
  3. #include "stm32f10x_gpio.h"
  4. #include "stm32f10x_rcc.h"
  5. #include "stm32f10x_usart.h"
  6. #include "stm32f10x_exti.h"
  7. #include "stm32f10x_tim.h"
  8. #include "stdio.h"
  9. //#include "tm1637.h"
  10. #include "misc.h" /* High level functions for NVIC and SysTick (add-on to CMSIS functions) */


  11. #define TRUE 1
  12. #define FALSE 0
  13. #define NoONE 0
  14. #define TEAM_A 1
  15. #define TEAM_B 2

  16. //Timer ISR sets this flag to communicate with the main program.
  17. static __IO uint8_t TimerEventFlag;   
  18. static __IO uint8_t TimerTicks=0;   
  19. static __IO uint8_t ControlTeam=NoONE;
  20. static __IO unsigned int TeamATime;
  21. static __IO unsigned int TeamBTime;
  22. static __IO unsigned int InitialTime=65;

  23. unsigned int timeformat(unsigned int seconds){
  24.     unsigned int min=0;
  25.     unsigned int sec=0;
  26.     min=(seconds/60);
  27.     sec=seconds%60;
  28.     return min*100+sec;
  29. }

  30. void Configure_DIP(void){
  31.     GPIO_InitTypeDef GPIO_InitStruct;
  32.         /* Set pin as input */
  33.     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
  34.     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_9|GPIO_Pin_8;
  35.     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
  36.     GPIO_Init(GPIOA, &GPIO_InitStruct);

  37.     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
  38.     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
  39.     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
  40.     GPIO_Init(GPIOB, &GPIO_InitStruct);
  41. }
  42. /* Configure pins to be interrupts */
  43. void Configure_AButton(void) {
  44.     /* Set variables used */
  45.     GPIO_InitTypeDef GPIO_InitStruct;
  46.     EXTI_InitTypeDef EXTI_InitStruct;
  47.     NVIC_InitTypeDef NVIC_InitStruct;

  48.     /* Enable clock for GPIOA */
  49.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

  50.     /* Set pin as input */
  51.     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
  52.     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
  53.     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
  54.     GPIO_Init(GPIOA, &GPIO_InitStruct);

  55.     /* Add IRQ vector to NVIC */
  56.     /* PA0 is connected to EXTI_Line0, which has EXTI0_IRQn vector */
  57.     NVIC_InitStruct.NVIC_IRQChannel = EXTI0_IRQn;
  58.     /* Set priority */
  59.     NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x00;
  60.     /* Set sub priority */
  61.     NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x00;
  62.     /* Enable interrupt */
  63.     NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
  64.     /* Add to NVIC */
  65.     NVIC_Init(&NVIC_InitStruct);

  66.     /* Tell system that you will use PA0 for EXTI_Line0 */
  67.     GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);

  68.     /* PA0 is connected to EXTI_Line0 */
  69.     EXTI_InitStruct.EXTI_Line = EXTI_Line0;
  70.     /* Enable interrupt */
  71.     EXTI_InitStruct.EXTI_LineCmd = ENABLE;
  72.     /* Interrupt mode */
  73.     EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
  74.     /* Triggers on rising and falling edge */
  75.     EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling;
  76.     /* Add to EXTI */
  77.     EXTI_Init(&EXTI_InitStruct);
  78. }

  79. /* Configure pins to be interrupts */
  80. void Configure_BButton(void) {
  81.     /* Set variables used */
  82.     GPIO_InitTypeDef GPIO_InitStruct;
  83.     EXTI_InitTypeDef EXTI_InitStruct;
  84.     NVIC_InitTypeDef NVIC_InitStruct;

  85.     /* Enable clock for GPIOC */
  86.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

  87.     /* Set pin as input */
  88.     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
  89.     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;
  90.     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
  91.     GPIO_Init(GPIOA, &GPIO_InitStruct);


  92.     NVIC_InitStruct.NVIC_IRQChannel = EXTI9_5_IRQn;
  93.     NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x00;
  94.     NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x00;
  95.     NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
  96.     NVIC_Init(&NVIC_InitStruct);

  97.     GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource5);

  98.     EXTI_InitStruct.EXTI_Line = EXTI_Line5;
  99.     EXTI_InitStruct.EXTI_LineCmd = ENABLE;
  100.     EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
  101.     EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling;
  102.     EXTI_Init(&EXTI_InitStruct);

  103. }

  104. int main(void)
  105. {
  106.   //Holds the structure for the GPIO pin initialization:
  107.   GPIO_InitTypeDef GPIO_InitStructure;  

  108.   uint16_t bitwise_time=0;    //for value from the DIP switches.
  109.   uint16_t portA_vals;
  110.   uint16_t portB_vals;
  111.   uint16_t iden16=1;


  112.     //Configure_AButton();
  113.     //Configure_BButton();
  114.     Configure_DIP();


  115. //init game settings
  116.     portA_vals=~GPIO_ReadInputData(GPIOA);
  117.     portB_vals=~GPIO_ReadInputData(GPIOB);
  118.     //bit operations
  119.     if(portA_vals&iden16<<10){
  120.         bitwise_time=bitwise_time|1<<0;}
  121.     if(portA_vals&iden16<<9){
  122.         bitwise_time=bitwise_time|1<<1;}
  123.     if(portA_vals&iden16<<8){
  124.         bitwise_time=bitwise_time|1<<2;}
  125.     if(portB_vals&iden16<<15){
  126.         bitwise_time=bitwise_time|1<<3;}
  127.     if(portB_vals&iden16<<14){
  128.         bitwise_time=bitwise_time|1<<4;}
  129.     if(portB_vals&iden16<<13){
  130.         bitwise_time=bitwise_time|1<<5;}
  131.     if(portB_vals&iden16<<12){
  132.         bitwise_time=bitwise_time|1<<6;}
  133. //set that as teams times
  134.     if(bitwise_time==0){
  135.         InitialTime=30;
  136.     }else if(bitwise_time==127){
  137.         InitialTime=99*60;
  138.     }else{
  139.         InitialTime=bitwise_time*60;
  140.     }
  141.     TeamATime=InitialTime;
  142.     TeamBTime=InitialTime;

  143.   // Configure SysTick Timer
  144.   /*System timer tick is used to measure time.
  145.     The Cortex-M3 core used in the STM32 processors has a dedicated timer
  146.     for this function. Its frequency is set as a fraction of the constant
  147.     SystemCoreClock (defined in file system_stm32f10x.c in the
  148.     STM32F10x Standard Peripheral Library directory.)
  149.     We configure it for 1 msec interrupts*/
  150.     if (SysTick_Config(SystemCoreClock/10))  while (1); //If it does not work, stop here for debugging.
  151. //loop
  152.   while (1) {
  153.     asm("nop"); //doing nothing. should put into some energy save mode
  154.   }//End while(1)

  155. } //END main()


  156. // Systic interrupt handler
  157. //Every 100 msec, the timer triggers a call to the SysTick_Handler.
  158. void SysTick_Handler (void){
  159.     TimerTicks++;

  160. }

  161. /* Set interrupt handlers */
  162. /* Handle AButton interrupt */
  163. void EXTI0_IRQHandler(void) {
  164.     /* Make sure that interrupt flag is set */
  165.     if (EXTI_GetITStatus(EXTI_Line0) != RESET) {
  166.         /* Do your stuff when PA0 is changed */
  167.         ControlTeam=TEAM_A;
  168.         /* Clear interrupt flag */
  169.         EXTI_ClearITPendingBit(EXTI_Line0);
  170.     }
  171. }

  172. /* Handle BButton interrupt */
  173. void EXTI9_5_IRQHandler(void) {
  174.     /* Make sure that interrupt flag is set */
  175.     if (EXTI_GetITStatus(EXTI_Line5) != RESET) {
  176.         asm("nop");
  177.         if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_5)==0){
  178.             ControlTeam=TEAM_B;
  179.         }
  180.         /* Clear interrupt flag */
  181.         EXTI_ClearITPendingBit(EXTI_Line5);
  182.     }
  183. }


  184. #ifdef USE_FULL_ASSERT
  185.   void assert_failed ( uint8_t* file, uint32_t line)
  186.   {
  187.     /* Infinite loop */
  188.     /* Use GDB to find out why we're here */
  189.     while (1);
  190.   }
  191. #endif
复制代码

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。