打印

c语言中定义的全局变量,在某个函数中赋值还能被其他函数使用吗?

[复制链接]
20597|21
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
jiangsai|  楼主 | 2013-4-26 16:43 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
为了省下参数传递的麻烦,我把一些多个函数都要用到的变量和结构体都定义成全局的,可以吗?为什么我的程序执行时出现问题,查看修改后的全局变量值没有改变,还是初始值 呢?请各位高手指点。我在一个函数中对全局变量自加运算,再到另一个函数中全局变量值是开始的初值还是上一个函数自加后的值呢?
沙发
杨爱林林| | 2013-4-26 16:52 | 只看该作者
当然是可以用的了 而且比较方便  
如果你没带系统的话  就看使用这个全局变量的先后顺序了  全局变量,当然是最后使用该变量过后的值了 也就是自加过后的值
如果带系统的话  就得保护这个共享资源了  
你可以贴上代码

使用特权

评论回复
板凳
uet_cache| | 2013-4-26 16:56 | 只看该作者
全局变量,要放在程序开头位置。如果其它文件要用,要声明外部变量。

使用特权

评论回复
地板
airwill| | 2013-4-26 18:25 | 只看该作者
全局变量, 当然是可以的.
不过, 我们写程序, 不建议大量使用全局变量.
"我在一个函数中对全局变量自加运算,再到另一个函数中全局变量值是开始的初值还是上一个函数自加后的值呢?"  当然是后者. 这里可能有个概念问题. 假如以变量传递给函数了. 那么修改的是个传递过来的副本, 而不是本身. 除非传递全局变量的地址.

使用特权

评论回复
5
hawksabre| | 2013-4-26 19:02 | 只看该作者
大量使用全局函数   最终程序的改动就是牵一发动全身    不建议使用过多全局函数    模块化的思想倒是不错的思想   楼主   可以借鉴一下

使用特权

评论回复
6
hawksabre| | 2013-4-26 19:02 | 只看该作者
多看看关于编程思想的书籍   我也是班门弄斧    飘过  

使用特权

评论回复
7
jiangsai|  楼主 | 2013-4-27 07:58 | 只看该作者
杨爱林林 发表于 2013-4-26 16:52
当然是可以用的了 而且比较方便  
如果你没带系统的话  就看使用这个全局变量的先后顺序了  全局变量,当然 ...

我在做小的毕业设计,没带系统。。并且只有设了一个全局变量,在main。c的开头设置的。。后面的函数要用到该变量的话要怎么应用呢。。

使用特权

评论回复
8
jiangsai|  楼主 | 2013-4-27 07:59 | 只看该作者
uet_cache 发表于 2013-4-26 16:56
全局变量,要放在程序开头位置。如果其它文件要用,要声明外部变量。

只是在该文件中使用,

使用特权

评论回复
9
jiangsai|  楼主 | 2013-4-27 08:04 | 只看该作者
airwill 发表于 2013-4-26 18:25
全局变量, 当然是可以的.
不过, 我们写程序, 不建议大量使用全局变量.
"我在一个函数中对全局变量自加运算 ...

我想了想可以把所有的子函数都放进main中可以用设全局变量了。。但那样字话,程序就不够好看了。。所以我还是想把子函数放到外面,main中调用。。但全局变量我用不好呢。。在子函数中引用全局变量的话,要做舍呢么特别的说明吗。。代码我一会贴上去。我想实现的就是一个简单的一个按键可以先设定下圈数,按下另外的按键就可以左转或右转相应的圈数

使用特权

评论回复
10
jiangsai|  楼主 | 2013-4-27 08:07 | 只看该作者
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "stm32_eval.h"
#include <stdio.h>
//#include "ARMJISHU_TouchScreen_ADS7843.h"
#include "sys.h"               
       
void USART_Configuration(void);
void RCC_Configuration(void);
void GPIO_Configuration(void);
void Delay(vu32 nCount);
void Delay1(vu32 nCount);
u8 M=0;
/* Values magic to the Board keys */
#define  NOKEY  0
#define  KEY1   1
#define  KEY2   2
#define  KEY3   3
#define  KEY4   4
#define  KEY5   5            
#define  KEY6   6            

/*按键相关定义*/
#define RCC_KEY1                                    RCC_APB2Periph_GPIOB
#define GPIO_KEY1_PORT                              GPIOB   
#define GPIO_KEY1                                   GPIO_Pin_0
#define RCC_KEY2                                    RCC_APB2Periph_GPIOB
#define GPIO_KEY2_PORT                              GPIOB
#define GPIO_KEY2                                   GPIO_Pin_1
#define RCC_KEY3                                    RCC_APB2Periph_GPIOB
#define GPIO_KEY3_PORT                              GPIOB
#define GPIO_KEY3                                   GPIO_Pin_2
#define RCC_KEY4                                    RCC_APB2Periph_GPIOB
#define GPIO_KEY4_PORT                              GPIOB
#define GPIO_KEY4                                   GPIO_Pin_5
#define RCC_KEY5                                    RCC_APB2Periph_GPIOB
#define GPIO_KEY5_PORT                              GPIOB
#define GPIO_KEY5                                   GPIO_Pin_6
#define RCC_KEY6                                    RCC_APB2Periph_GPIOB
#define GPIO_KEY6_PORT                              GPIOB
#define GPIO_KEY6                                   GPIO_Pin_7

/* Private function prototypes -----------------------------------------------*/
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
                                             set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
  
/*static void Delay_ARMJISHU(__IO uint32_t nCount)
{
  for (; nCount != 0; nCount--)
  {
      if(GPIO_ADS7843_INT_VALID)
      {
        ARMJISHU_TouchScreen_ADS7843();
      }       
  }
}                                                   */

void delay(void)
  {
      unsigned long ik ;
      for(ik=0;ik<0x03af8;ik++) ;
  }
void delay1(void)
  {
      unsigned long ik ;
      for(ik=0;ik<0x00af8;ik++) ;
  }

/* Private functions ---------------------------------------------------------*/

/*******************************************************************************
* Function Name  : GPIO_KEY_Config.
* Description    : Configures the Extension Button.
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
void GPIO_KEY_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  /* Configure KEY1 Button */
  RCC_APB2PeriphClockCmd(RCC_KEY1, ENABLE);

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  GPIO_InitStructure.GPIO_Pin = GPIO_KEY1;
  GPIO_Init(GPIO_KEY1_PORT, &GPIO_InitStructure);
    /* Configure KEY2 Button */
  RCC_APB2PeriphClockCmd(RCC_KEY2, ENABLE);

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  GPIO_InitStructure.GPIO_Pin = GPIO_KEY2;
  GPIO_Init(GPIO_KEY2_PORT, &GPIO_InitStructure);
      /* Configure KEY3 Button */
  RCC_APB2PeriphClockCmd(RCC_KEY3, ENABLE);

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  GPIO_InitStructure.GPIO_Pin = GPIO_KEY3;
  GPIO_Init(GPIO_KEY3_PORT, &GPIO_InitStructure);
        /* Configure KEY4 Button */
  RCC_APB2PeriphClockCmd(RCC_KEY4, ENABLE);

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  GPIO_InitStructure.GPIO_Pin = GPIO_KEY4;
  GPIO_Init(GPIO_KEY4_PORT, &GPIO_InitStructure);
        /* Configure KEY5 Button */
  RCC_APB2PeriphClockCmd(RCC_KEY5, ENABLE);

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  GPIO_InitStructure.GPIO_Pin = GPIO_KEY5;
  GPIO_Init(GPIO_KEY5_PORT, &GPIO_InitStructure);
        /* Configure KEY6 Button */
   RCC_APB2PeriphClockCmd(RCC_KEY6, ENABLE);

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  GPIO_InitStructure.GPIO_Pin = GPIO_KEY6;
  GPIO_Init(GPIO_KEY6_PORT, &GPIO_InitStructure);
}
/*******************************************************************************
* Function Name  : ReadKey
* Description    : Reads key from demoboard.
* Input          : None
* Output         : None
* Return         : Return RIGHT, LEFT, SEL, UP, DOWN or NOKEY
*******************************************************************************/
u8 ReadKeyDown(void)
{
  /* 1 key is pressed */
  if(!GPIO_ReadInputDataBit(GPIO_KEY1_PORT, GPIO_KEY1))
  {
    return KEY1;
  }       
    /* 2 key is pressed */
else if(!GPIO_ReadInputDataBit(GPIO_KEY2_PORT, GPIO_KEY2))
  {
    return KEY2;
  }
   /* 3 key is pressed */
else if(!GPIO_ReadInputDataBit(GPIO_KEY3_PORT, GPIO_KEY3))
  {
    return KEY3;
  }

   /* 4 key is pressed */
else if(!GPIO_ReadInputDataBit(GPIO_KEY4_PORT, GPIO_KEY4))
  {
    return KEY4;
  }                  
    /* 5 key is pressed */
else if(!GPIO_ReadInputDataBit(GPIO_KEY5_PORT, GPIO_KEY5))
  {
    return KEY5;
  }
   /* 6 key is pressed */
else if(!GPIO_ReadInputDataBit(GPIO_KEY6_PORT, GPIO_KEY6))
  {
    return KEY6;
  }  
  /* No key is pressed */
  else
  {
    return NOKEY;
  }
}

void motor_Turn_on_1(void)                 //正转
{
                              GPIO_Write(GPIOA, 0x08);
                                                      delay();
                                                      GPIO_Write(GPIOA,0x0C);
                                                      delay();
                                                      GPIO_Write(GPIOA,0x04);
                                                      delay();
                                                      GPIO_Write(GPIOA,0x06);
                                                      delay();       
                                                          GPIO_Write(GPIOA, 0x02);
                                                      delay();
                                                          GPIO_Write(GPIOA, 0x03);
                                                      delay();
                                                          GPIO_Write(GPIOA, 0x01);
                                                      delay();
                                                           GPIO_Write(GPIOA, 0x09);
                                                      delay();
}
void motor_Turn_on_2(void)                 //反转
{
                              GPIO_Write(GPIOA,0x09);
                                                      delay();
                                                          GPIO_Write(GPIOA,0x01);
                                                      delay();
                                                          GPIO_Write(GPIOA,0x03);
                                                      delay();
                                                      GPIO_Write(GPIOA,0x02);
                                                      delay();
                                                          GPIO_Write(GPIOA, 0x06);
                                                      delay();       
                                                           GPIO_Write(GPIOA, 0x04);
                                                      delay();
                                                           GPIO_Write(GPIOA, 0x0C);
                                                      delay();
                                                           GPIO_Write(GPIOA, 0x08);
                                                      delay();       
}
void motor_Turn_on_3(void)                //         正转一圈

{
  u8 j;
  u8 r;
  u8 N=64;

  for(r=0;r<N*M;r++)
  {
  for(j=0;j<8;j++)                 //电机外轴旋转一周,不是里里面面所看到的一周
  {
                              GPIO_Write(GPIOA, 0x08);
                                                      delay();
                                                      GPIO_Write(GPIOA,0x0C);
                                                      delay();
                                                      GPIO_Write(GPIOA,0x04);
                                                      delay();
                                                      GPIO_Write(GPIOA,0x06);
                                                      delay();       
                                                          GPIO_Write(GPIOA, 0x02);
                                                      delay();
                                                          GPIO_Write(GPIOA, 0x03);
                                                      delay();
                                                          GPIO_Write(GPIOA, 0x01);
                                                      delay();
                                                          GPIO_Write(GPIOA, 0x09);
                                                      delay();

  }
}
M=0;
}
void motor_Turn_on_4(void)                //         反转一圈
{
  u8 j;
  u8 r;
  u8 N=64;
  
  for(r=0;r<N*M;r++)
  {
   
  for(j=0;j<8;j++)                 //电机外轴旋转一周,不是里里面面所看到的一周
  {
    GPIO_Write(GPIOA,0x09);
        delay();
        GPIO_Write(GPIOA,0x01);
        delay();
        GPIO_Write(GPIOA,0x03);
        delay();
        GPIO_Write(GPIOA,0x02);
        delay();
    GPIO_Write(GPIOA, 0x06);
        delay();       
        GPIO_Write(GPIOA, 0x04);
        delay();
        GPIO_Write(GPIOA, 0x0C);
        delay();
        GPIO_Write(GPIOA, 0x08);
        delay();       
  }
}
M=0;
}
   void num1(void)               
{

M=M+1;

}
/*void num2(void)               
{
if(M<=0)
{
M=1  ;
}
  M=M-1;
}          */
void motor_ccw00(u8 key)
  {
   
        /* Turn Off Select LED */
    switch(key)
    {
          case 0:
                 
          motor_Turn_on_1();   //反转
          break;

        case 1:
                 
          motor_Turn_on_2();         //正转
          break;

        case 2:                                        //反转n圈
                 
          motor_Turn_on_3();
          break;

        case 3:
               
         motor_Turn_on_4();                //正转n圈
         break;
       case 4:
                 num1();
                                
         break;
        /*         case 5:
                 num2();
                  motor_Turn_on_4();               
         break;                            */
        default:
          GPIO_ResetBits(GPIOA, GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3);
          break;
    }

}   
int main(void)
{                       
//        int m=2, n=1;
    u8 KeyNum = 0;
        #ifdef DEBUG
        debug();
    #endif       

        USART_Configuration();         //必须放在前面
        RCC_Configuration();
        GPIO_Configuration();
    GPIO_KEY_Config();
//        ADS7843_Init();       

    while (1)
   {
                KeyNum=ReadKeyDown();
                   motor_ccw00(KeyNum-1);
            
    }         
}

void USART_Configuration(void)
{
     USART_InitTypeDef USART_InitStructure;

     USART_InitStructure.USART_BaudRate = 115200;

         USART_InitStructure.USART_WordLength = USART_WordLength_8b;

         USART_InitStructure.USART_StopBits = USART_StopBits_1;

         USART_InitStructure.USART_Parity = USART_Parity_No;

         USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

         USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;       

         STM_EVAL_COMInit(COM1, &USART_InitStructure);
}          

void RCC_Configuration()
{
         //定义错误状态变量
         ErrorStatus HSEStartUpStatus;
       
         //将RCC寄存器重新设置为默认值
         RCC_DeInit();

         //打开外部高速时钟晶振
         RCC_HSEConfig(RCC_HSE_ON);

         //等待外部高速时钟晶振工作
         HSEStartUpStatus = RCC_WaitForHSEStartUp();
         if(HSEStartUpStatus == SUCCESS)
         {
                 //设置AHB时钟(HCLK)为系统时钟
                 RCC_HCLKConfig(RCC_SYSCLK_Div1);

                 //设置高速AHB时钟(APB2)为HCLK时钟
                 RCC_PCLK2Config(RCC_HCLK_Div1);

                 //设置低速AHB时钟(APB1)为HCLK的2分频
                 RCC_PCLK1Config(RCC_HCLK_Div2);
               
                 //设置FLASH代码延时
                 FLASH_SetLatency(FLASH_Latency_2);

                 //使能预取指缓存
                 FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

                 //设置PLL时钟,为HSE的9倍频 8MHz * 9 = 72MHz
                 RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

                  //使能PLL
                 RCC_PLLCmd(ENABLE);

                 //等待PLL准备就绪
                 while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);

                 //设置PLL为系统时钟源
                 RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

                 //判断PLL是否是系统时钟
                 while(RCC_GetSYSCLKSource() != 0x08);
         }

         //开启TIM3的时钟
         RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
         //开启GPIOB的时钟和复用功能
         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO,ENABLE);

}

void GPIO_Configuration()
{
         GPIO_InitTypeDef GPIO_InitStructure;

         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//打开外设A,的时钟

         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;

         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//IO口的方向

         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

         GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化外设0,1,2,3,6的端口
}

/**
  * [url=home.php?mod=space&uid=247401]@brief[/url]  Retargets the C library printf function to the USART.
  * @param  None
  * @retval None
  */
PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
  USART_SendData(EVAL_COM1, (uint8_t) ch);

  /* Loop until the end of transmission */
  while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)
  {
  }

  return ch;
}

#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */

void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* Infinite loop */
  while (1)
  {
  }
}
#endif
/**
  * @}
  */

/**
  * @}
  */
/******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
楼主小白一只,只为潜心做毕业设计,,可能会有很多问题,虚心听取大家的指正,多谢各位了。。

使用特权

评论回复
11
jiangsai|  楼主 | 2013-4-27 08:27 | 只看该作者
好像那个M在加一后,在下一个函数中引用时M还是保持着初始值0,

使用特权

评论回复
12
mmuuss586| | 2013-4-27 08:35 | 只看该作者
1、可以;
2、是不是定义了一个常量;

使用特权

评论回复
13
jiangsai|  楼主 | 2013-4-27 08:37 | 只看该作者
外出一下,下午回来回你们

使用特权

评论回复
14
jiangsai|  楼主 | 2013-4-27 08:38 | 只看该作者
mmuuss586 发表于 2013-4-27 08:35
1、可以;
2、是不是定义了一个常量;

是全局变量,你看看那个M为什么在下面的函数应用中还是初值0

使用特权

评论回复
15
杨爱林林| | 2013-4-27 08:38 | 只看该作者
jiangsai 发表于 2013-4-27 07:58
我在做小的毕业设计,没带系统。。并且只有设了一个全局变量,在main。c的开头设置的。。后面的函数要用 ...

你在main.c里面定义全局变量  U8 m=0;
在其他文件中想用的话  只要  extern u8 m;
就好了
还有 你确定 执行过m=m+1;那句了吗

使用特权

评论回复
16
jiangsai|  楼主 | 2013-4-27 19:47 | 只看该作者
杨爱林林 发表于 2013-4-27 08:38
你在main.c里面定义全局变量  U8 m=0;
在其他文件中想用的话  只要  extern u8 m;
就好了

反正按键按了,但m的值好像始终都是初值

使用特权

评论回复
17
jiangsai|  楼主 | 2013-4-27 19:48 | 只看该作者
杨爱林林 发表于 2013-4-27 08:38
你在main.c里面定义全局变量  U8 m=0;
在其他文件中想用的话  只要  extern u8 m;
就好了

其实我觉得就是这个地方粗了问题

使用特权

评论回复
18
jiangsai|  楼主 | 2013-4-27 19:52 | 只看该作者
杨爱林林 发表于 2013-4-27 08:38
你在main.c里面定义全局变量  U8 m=0;
在其他文件中想用的话  只要  extern u8 m;
就好了

我是不想多次使用调用,所以定义了个全局变量。。希望在下次用到M值时用到的是这次M变化之后的值,但实际没有达到那样子的效果。。请问下是什么原因呢

使用特权

评论回复
19
杨爱林林| | 2013-4-27 21:07 | 只看该作者
你在m=m+1;设个断点吧
个人感觉 你程序没执行那步

使用特权

评论回复
20
jiangsai|  楼主 | 2013-4-28 09:27 | 只看该作者
杨爱林林 发表于 2013-4-27 21:07
你在m=m+1;设个断点吧
个人感觉 你程序没执行那步

如何设置断点呢,我先百度下。。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

4

主题

49

帖子

0

粉丝