[英]avr programming in atmel studio
我在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;
}
}
}
}
問題出在您的#define
語句中,因為宏擴展會像這樣塑造您的代碼:
if(PINB 4 == 0) c = 0;
這是語法錯誤,因為PINB在m32定義文件中定義為寄存器。 也許您是說#define c1 PINB4
嗎?
PINB
是一個8位寄存器,可以通過位操作進行處理。
您可以使用掩碼測試PINB
的某些位。
當您想知道PINB
的第4位是否為0
,可以用0b00010000
屏蔽PINB
。
我猜您想通過#define c1 PINB 4
做同樣的事情。
因此可以定義4個宏,如下所示:
#define c1 (PINB & 0b00010000) //4th bit
#define c2 (PINB & 0b00100000) //5th bit
#define c3 (PINB & 0b01000000) //6th bit
#define c4 (PINB & 0b10000000) //7th bit
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.