ST意法半导体
直播中

臧超楠

7年用户 190经验值
私信 关注
[问答]

GPIO结构参数未初始化

GPIO结构已定义,但在main中使用之前未初始化。一切正常但我仍然试图找到一个可以在GPIO_Init中使用的值。 GPIOx-> CR2与另一个值进行“或”运算,我不知道从哪里得到初始值GPIOx-> CR2。我唯一能想到的是该地址中有一个随机数。


这是固件附带的给定程序。我是C的新手,这可能是一个简单的问题。

谢谢,
克里斯



在Discovery微控制器附带的主要功能中。主要做的第一件事就是调用GPIO_Init。我把它包括在这里。

void GPIO_Init(GPIO_TypeDef * GPIOx,

uint8_t GPIO_Pin,
GPIO_Mode_TypeDef GPIO_Mode)
{


/ * * ---------------------- /
/ *检查参数* /
/ * ---------------------- * / assert_param(IS_GPIO_MODE(GPIO_Mode));

assert_param(IS_GPIO_PIN(GPIO_Pin)); / *将相应位重置为CR2寄存器中的GPIO_Pin * /

/ * * ----------------------------- /

/ *输入/输出模式选择* /
/ * ----------------------------- * / if((((uint8_t)(GPIO_Mode))&(uint8_t)0x80 )!=(uint8_t)0x00)/ *输出模式* /

{
if((((uint8_t)(GPIO_Mode))&(uint8_t)0x10)!=(uint8_t)0x00)/ *高级别* /
{
GPIOx-> ODR | = GPIO_Pin;
} else / *低级别* /
{
GPIOx-> ODR& =(uint8_t)(〜(GPIO_Pin));
}
/ *设置输出模式* /
GPIOx-> DDR | = GPIO_Pin;
} else / *输入模式* /
{
/ *设置输入模式* /
GPIOx-> DDR& =(uint8_t)(〜(GPIO_Pin));
}

我想你可以看到GPIOx-> CR2和GPIOx-> ODR和GPIOx-> DDR正被用来设置位。(用RED概述)我的想法是它们没有被函数GPIO_Init初始化,所以任何可以使用碰巧在该地址中的数字。

我是uC和C的初学者。感谢您的帮助。

克里斯
#infitize-a-structure#memory-mapped-peripherals

以上来自于谷歌翻译


以下为原文




The GPIO struct is defined but it is not initialized before it is used in main.  Everything works but I am stuck with trying to find a value that I can use in GPIO_Init.  GPIOx->CR2 is OR'd with another value and I don't know where to get the initial value GPIOx->CR2.  The only thing I can think is that a random number is in that address.


This is in the given program that comes with the firmware.  I am new to C and this is probably a simple problem.   

Thanks,
Chris  



In the main function that comes with the Discovery microcontroller.  The first thing that main does is to call GPIO_Init.   I have included it here.

void GPIO_Init(GPIO_TypeDef* GPIOx,

                uint8_t GPIO_Pin,
                GPIO_Mode_TypeDef GPIO_Mode)
{
   
   
   /*----------------------*/
   /* Check the parameters */
   /*----------------------*/  assert_param(IS_GPIO_MODE(GPIO_Mode));

   assert_param(IS_GPIO_PIN(GPIO_Pin));  /* Reset crresponding bit to GPIO_Pin in CR2 register */

  /*-----------------------------*/

   /* Input/Output mode selection */
   /*-----------------------------*/  if ((((uint8_t)(GPIO_Mode)) & (uint8_t)0x80) != (uint8_t)0x00) /* Output mode */

   {
     if ((((uint8_t)(GPIO_Mode)) & (uint8_t)0x10) != (uint8_t)0x00) /* High level */
     {
       GPIOx->ODR |= GPIO_Pin;
     } else /* Low level */
     {
       GPIOx->ODR &= (uint8_t)(~(GPIO_Pin));
     }
     /* Set Output mode */
     GPIOx->DDR |= GPIO_Pin;
   } else /* Input mode */
   {
     /* Set Input mode */
     GPIOx->DDR &= (uint8_t)(~(GPIO_Pin));
   }

I think you can see that GPIOx->CR2 and GPIOx->ODR and GPIOx->DDR are being used to set bits.(Outlined in RED)  My thinking is that they are not initialized by the function GPIO_Init yet so any number that happens to be in that address could be used.

I am a beginner in both uC's and C.  Thanks for your help.

Chris  
#initialize-a-structure #memory-mapped-peripherals

回帖(5)

张建军

2019-1-14 11:28:32
你能举例说明你在谈论什么吗?
 
 
“我是C的新手”
 
 您是否也是一般编程和/或微控制器和/或数字电子设备的新手?

以上来自于谷歌翻译


以下为原文




Can you show an example to illustrate what you're talking about?


''I am new to C''

Are you also new to programming in general, and/or to microcontrollers, and/or digital electronics?
举报

臧超楠

2019-1-14 11:37:19
以上来自于谷歌翻译


以下为原文
举报

张建军

2019-1-14 11:42:54
“感谢您回复我的帖子。”
 
 
 别客气。
 
“我是C和uC的新手”
 
 不要试图一次学习太多新东西!
 在继续将其应用于微控制器之前,在PC或其他“普通”平台上学习“C”编程可能更容易......
 
 这里有一些'C'的启动提示:http://blog.antronics.co.uk/2011/08/08
 
 
“在数字电子领域有一些经验”
 
 那么你熟悉IO的概念吗?
 这意味着IO寄存器在CPU的正常存储器映射中显示为正常的存储器位置 - 因此CPU只需正常的存储器读取和访问就可以访问外设。写道。
 
 因此,通过在这些位置定位“C”变量,您的“C”程序只需访问变量即可访问外设寄存器。
 
 这就是您展示的代码所做的事情 - 使用a将端口的所有寄存器组合在一起。
 
 因此,您不会初始化结构 - 读取结构只会给出硬件寄存器中的值!

以上来自于谷歌翻译


以下为原文








''Thanks for responding to my post.''


You're welcome.   

''I am new to C and uC's''
   
Don't try to learn too much new stuff all at once!
It might be easier to learn the 'C' programming  on a PC or other ''normal'' platform before moving on to apply it to microcontrollers...

Some 'C' starting tips here: http://blog.antronics.co.uk/2011/08/08


''have some experience in digital electronics''

So are you familiar with the concept of IO?
It means that the IO registers appear in the CPU's normal memory map as normal memory locations - so the CPU accesses the peripherals just with normal memory reads & writes.

Therefore, by locating 'C' variables at these locations, your 'C' program can access the peripheral registers by just accessing the variables.

This is what the code you showed is doing - using a  to group all the registers of a port together.

Therefore you do not initialise the structure - reading the structure just gives the values from the hardware registers!
举报

张建军

2019-1-14 11:51:59
IO ...使用a将端口的所有寄存器组合在一起''
 
 
请注意,这是一个非常常见的&广泛使用的技术。

以上来自于谷歌翻译


以下为原文










IO ... using a  to group all the registers of a port together''


Note that this is a very common & widely used technique.
举报

更多回帖

发帖
×
20
完善资料,
赚取积分