5362 views|18 replies

931

Posts

3

Resources
The OP
 

Why can't I read the pin status? [Copy link]

 
    I use the 4+3-bit DIP switch to let the user set two data at will, and the program reads the two data and then processes them accordingly. I group the 0~3 bits of the PC port and the 4~6 bits as a group. The GPIO configuration code is as follows: void port_C_Config(void) { // gpio_deinit(GPIOC); // Reset the PC port rcu_periph_clock_enable(RCU_GPIOC); gpio_mode_set(GPIOC, GPIO_MODE_INPUT, GPIO_PUPD_PULLDOWN,GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6); } I started with the pin in pull-up mode, and the external DIP switch was used to control the ground connection, but I found that the voltage of the corresponding pin of the microcontroller was only about 0.9~1.2V. Later, I had to change it to pull-down mode, and the external DIP switch was used to control the connection to Vcc to ensure that it can be set correctly. To read the pin data, I started from the high bit, multiplied it by 2 and then added the value of the lower bit. First, I used the following code to directly read and calculate the data bit by bit, but the result was zero. devi = gpio_input_bit_get(GPIOC,GPIO_PIN_6); devi = (devi * 2) + gpio_input_bit_get(GPIOC,GPIO_PIN_5); devi = (devi * 2) + gpio_input_bit_get(GPIOC,GPIO_PIN_4); Then I used the if statement to judge the state of the read pin and then calculate, but still could not get the result. if (SET == gpio_input_bit_get(GPIOC,GPIO_PIN_3)) stan = 1; else stan = 0; if (SET == gpio_input_bit_get(GPIOC,GPIO_PIN_2)) stan = (stan * 2) + 1; else stan = stan * 2; if (SET == gpio_input_bit_get(GPIOC,GPIO_PIN_1)) stan = (stan * 2) + 1; else stan = stan * 2; if (SET == gpio_input_bit_get(GPIOC,GPIO_PIN_0)) stan = (stan * 2) + 1; else stan = stan * 2;

This post is from GD32 MCU

Latest reply

GPIO_PUPD_PULLDOWN GPIO_PUPD_PULLUP  Details Published on 2018-9-12 16:58
 

931

Posts

3

Resources
2
 
    Why can't I read the data from the pin? Is there something wrong with the settings? I have checked the hardware connection repeatedly and there is no error. When the pin is set to pull up and the pin is unused, the data read should be 1, but I read zero; when the pin is set to pull down and the power supply is 5V with the external DIP switch, why can't I read 1?
This post is from GD32 MCU
 
 
 

693

Posts

7

Resources
3
 
When you operate the PC port, please check whether the clock is configured in the code. If it is configured, check whether the IO port is initialized first. Initialization is very important, otherwise your operation will be useless.
This post is from GD32 MCU
 
 
 

931

Posts

3

Resources
4
 
This post was last edited by hujj on 2018-9-9 10:11 void port_C_Config(void) { // gpio_deinit(GPIOC); //Reset PC port rcu_periph_clock_enable(RCU_GPIOC); gpio_mode_set(GPIOC, GPIO_MODE_INPUT, GPIO_PUPD_PULLDOWN,GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6); } The port clock is configured (see the code above), but the pin speed is not configured, because I only use it for input. I have used the initialization command gpio_deinit(), but there is no change. I will try again. Thanks for your reply!
This post is from GD32 MCU

Comments

Are all the peripheral clocks used enabled? Also, do the gpio ports support 5V? It would be better to report the chip you use, so that it is easier to answer. It is too tiring to just guess after reading the code without any comments.  Details Published on 2018-9-9 14:35
 
 
 

931

Posts

3

Resources
5
 
I changed the configuration code of the PC port to alternate mode, but I still can't read the pin status. I don't know why. Please help me, thank you! void port_C_Config(void) { gpio_deinit(GPIOC); //Reset PC portrcu_periph_clock_enable(RCU_GPIOC); gpio_af_set(GPIOC,GPIO_AF_0,GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6); gpio_mode_set(GPIOC,GPIO_MODE_AF,GPIO_PUPD_PULLUP,GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6); // gpio_bit_set(GPIOC,GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6); gpio_bit_write(GPIOC,GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6, SET); }
This post is from GD32 MCU
 
 
 

424

Posts

8

Resources
6
 
hujj posted on 2018-9-9 10:02 void port_C_Config(void) { // gpio_deinit(GPIOC); // reset PC port rcu_periph_c ...
Are all the peripheral clocks used enabled? In addition, does the gpio port support 5V? It is best to report the chip you use, so that it is easier to answer. It is too tiring to read the code without comments and just guess.
This post is from GD32 MCU
 
 
 

931

Posts

3

Resources
7
 
I used the GD32F350 development board for this event, and connected the dip switches with the PC0~PC6 pins, so that users can set two parameters according to actual needs (one with a four-position dip switch and the other with a three-position dip switch). The relevant code for configuring the port is as follows: void port_C_Config(void) { gpio_deinit(GPIOC); //Reset the PC port rcu_periph_clock_enable(RCU_GPIOC); //Configure the port clock //The following code is to configure the pins. The gpio_mode_set() function is used to set the PC0~PC6 pins to input mode and pull-up resistor mode. The test found that the voltage of the pin was only about 0.9~1.2V when the dip switch was not turned on. I tried to change it to the pull-down resistor mode and pull it up externally through the dip switch, but the microcontroller still could not get the data. I configured the pins to the following alternating mode but it did not achieve the expected effect. gpio_af_set(GPIOC,GPIO_AF_0,GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6); gpio_mode_set(GPIOC,GPIO_MODE_AF,GPIO_PUPD_PULLUP,GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_ PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6); // gpio_bit_set(GPIOC,GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6); gpio_bit_write(GPIOC,GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6,SET); }
This post is from GD32 MCU
 
 
 

931

Posts

3

Resources
8
 
This post was last edited by hujj on 2018-9-9 16:15 I tried to use the three pins PB0, PB1, and PB11, and I was able to get data, but the pins of the PC port still could not get data. It should be a configuration problem of the port or pin. Could it be that the port has not been enabled yet? The code for using the PB port is as follows: rcu_periph_clock_enable(RCU_GPIOB); gpio_mode_set(GPIOB,GPIO_MODE_INPUT,GPIO_PUPD_PULLUP,GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_11); But I used similar code to configure the PC port but it was unsuccessful. What is the reason? I hope you can help me analyze it. Thank you! Follow-up note: The three pins PB0, PB1, and PB11 can only get data at the beginning. Later, I was unable to get data in the test. I don't know why?
This post is from GD32 MCU
 
 
 

931

Posts

3

Resources
9
 
    I added a 0.5 second delay program in the while loop of the main function, and finally saw clearly that the data read from the PB port is changing periodically (my dip switch has not changed), that is, the read data is unstable. I measured the corresponding pins, and the high level is only between 0.8 and 1.2V, which obviously does not meet the standard of 1.69~2.11V in the data sheet. Maybe this is the reason why the read data is unstable or cannot be read? The factor may be that my USB port power supply capacity is insufficient, but I measured that the 5V end of the development board has 4.63V and the 3.3V end has 3.27V, which should not be the reason. I used a digital multimeter to measure, and it is not the deviation caused by the meter connection. In addition to the weak pull-up ability of the microcontroller I/O port, I can't think of any other reasons. Data sheet screenshot: The read data on the screen or the lower right corner is unstable and changes periodically.

This post is from GD32 MCU
 
 
 

931

Posts

3

Resources
10
 
    I changed three pins including PB0 to pull-down resistors, and then used dip switches to pull them up externally. When all were pulled up, stable data could be read. Otherwise, the data was still periodically changing. From this, I judged that some of these pins might be occupied by other programs. I built the project based on the ADC program, and have not yet analyzed what resources the original program used.
This post is from GD32 MCU
 
 
 

931

Posts

3

Resources
11
 
    I changed the connection of LCD5110 to PC port and it can be driven normally, indicating that PC port should be enabled normally, but I don't know why I still can't read data. I set the DIP switch to PB port and can read data, but there is a problem. No matter whether the pin is set to pull-up resistor or pull-down resistor, the voltage of the corresponding pin is between 0.8~1.6V and will not show high level or low level.
This post is from GD32 MCU
 
 
 

931

Posts

3

Resources
12
 
This problem has been perfectly solved by switching to the PB port and adding external pull-up resistors. Thanks again to the forum friends who responded enthusiastically!
This post is from GD32 MCU

Comments

Internal pull-up resistors won't work?  Details Published on 2018-9-10 18:52
 
 
 

530

Posts

4

Resources
13
 
hujj posted on 2018-9-10 12:42 This problem has been perfectly solved by switching to PB port and adding external pull-up resistors. Thanks again to the forum friends who responded enthusiastically!
Is the internal pull-up resistor not okay? It should be OK
This post is from GD32 MCU

Comments

You may not have noticed the previous post. When my development board set the internal pull-up resistor or pull-down resistor, the pin voltage level was between 0.8 and 1.2V, which did not meet the data sheet standard, so I had to add an external pull-up resistor.  Details Published on 2018-9-10 21:29
 
 
 

931

Posts

3

Resources
14
 
This post was last edited by hujj on 2018-9-10 21:30
Published by Media Student on 2018-9-10 18:52 Is the internal pull-up resistor not working? It should be OK
You may not have noticed the previous post. When my development board is set with internal pull-up resistors or pull-down resistors, the pin voltage level is between 0.8~1.2V, which does not meet the standard of the data sheet, so I have to add external pull-up resistors. I don't know if there is a problem with my settings or the board, and I don't know if it is an isolated case or a common problem.
This post is from GD32 MCU

Comments

Generally, the internal pull-up voltage of this processor is 3.3V, which is compatible with 5V. The pull-up resistor I set is 3.3V. I will try the following  Details Published on 2018-9-11 19:21
 
 
 

530

Posts

4

Resources
15
 
hujj posted on 2018-9-10 21:29 You may not have noticed the previous post. When my development board sets the internal pull-up resistor or pull-down resistor, the pin level is between 0.8~1.2V, which cannot reach...
Generally, the internal pull-up of this processor is 3.3V, which is compatible with 5V. The pull-up resistor I set has an input of 3.3V. I will try the following
This post is from GD32 MCU
 
 
 

931

Posts

3

Resources
16
 
    According to the data sheet, the high level of a 3.3V power supply should be above 1.99V. However, when I measured, the voltage on the pin was usually less than 1.2V when using a pull-up resistor, and occasionally reached 1.6V, which was significantly lower than 1.99V. This caused the read-in state to be unstable, and I couldn't find the cause, so I had to connect an external pull-up resistor.
This post is from GD32 MCU
 
 
 

5303

Posts

454

Resources
17
 
GPIO_PUPD_PULLDOWN

GPIO_PUPD_PULLUP
This post is from GD32 MCU
 
 
 

931

Posts

3

Resources
18
 
I used this function, but couldn't read any valid data, so I had to add an external pull-up resistor to solve the problem.
This post is from GD32 MCU
 
 
 

931

Posts

3

Resources
19
 
This post was last edited by hujj on 2018-9-12 20:34 I used this function, but could not read valid data, so I had to add an external pull-up resistor to solve the problem. (The network speed was slow, and it was repeated after submission)
This post is from GD32 MCU
 
 
 

Guess Your Favourite
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list