【问题标题】:avr programming in atmel studioatmel 工作室中的 avr 编程
【发布时间】:2015-01-04 12:50:29
【问题描述】:

我在 atmel studio 中遇到此代码错误 错误是:expected ')' before numeric 这是我的代码(我用它作为 atmega32a 的键盘代码):

#include <util/delay.h>
#define c1 PINB 4 
#define c2 PINB 5 
#define c3 PINB 6 
#define c4 PINB 7

unsigned char scan[4]={0XFE,0XFD,0XFB,0XF7};
unsigned char arrkey[16]={1,2,3,20,4,5,6,30,7,8,9,40,10,0,11,50};

unsigned char keypad() {
    unsigned char r,c,k;
    DDRB=0X0F;
    PORTB=0XFF; 

    while(1) {
        for (r=0; r<4; r++) {
            c=4;
            PORTB=scan[r];
            _delay_us(10);
            if(c1 == 0) c=0;
            if(c2 == 0) c=1;
            if(c3 == 0) c=2;
            if(c4 == 0) c=3;

            if (!(c==4)) {
                k=arrkey[(r*4)+c];
                while(c1==0);
                while(c2==0);
                while(c3==0);
                while(c4==0);
                _delay_ms(50);
                return k;
            }
        }
    } 
}

【问题讨论】:

  • 你能正确缩进你的程序吗?
  • 这个错误发生在哪一行?
  • 在所有只有一个语句的 if 代码中

标签: c avr


【解决方案1】:

问题出在您的 #define 语句中,因为宏扩展会像这样塑造您的代码:

if(PINB 4 == 0) c = 0;

这是一个语法错误,因为 PINB 在 m32 定义文件中被定义为一个寄存器。 也许你的意思是#define c1 PINB4

【讨论】:

    【解决方案2】:

    PINB是一个8位寄存器,可以通过位操作来处理。
    您可以使用掩码测试PINB 的一些位。
    当你想知道PINB的第4位是否为0时,可以用0b00010000屏蔽PINB
    我猜你想通过#define c1 PINB 4 做同样的事情。

    这4个宏so可以定义如下:

    #define c1 (PINB & 0b00010000) //4th bit  
    #define c2 (PINB & 0b00100000) //5th bit  
    #define c3 (PINB & 0b01000000) //6th bit  
    #define c4 (PINB & 0b10000000) //7th bit
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多