1

我正在为 KS0108 GLCD 驱动程序编写一个新的特殊库,其中包含新的算法和能力。我正在使用ATMega16。我的点阵 GLCD 尺寸是 128x64。

如何使用#define 代码定义不同的端口引脚?
例如:#define GLCD_CTRL_RESTART PORTC.0

IDE:AVR Studio 5
语言:C
模块:128x64 点阵 GLCD
驱动器:KS0108
微控制器:ATMega16

请解释我应该使用哪些标题?并且还为ATMEga16编写了一个完整且非常简单的代码。

4

2 回答 2

1

在 ATmega 中,引脚值组装在 PORT 寄存器中。引脚值是 PORT 中某个位的值。ATmega 不像其他一些处理器那样有一点可寻址的 IO 内存,所以你不能#define像你建议的那样用一个引脚来引用一个用于读写的引脚。

如果对您有帮助,您可以做的是定义宏来读取或写入引脚值。您可以更改宏的名称以满足您的需要。

#include <avr/io.h>

#define PORTC_BIT0_READ()    ((PORTC & _BV(PC0)) >> PC0)
#define WRITE_PORTC_BIT0(x)  (PORTC = (PORTC & ~_BV(PC0)) | ((x) << PC0))

uint8_t a = 1, b;

/* Change bit 0 of PORTC to 1 */
WRITE_PORTC_BIT0(a);

/* Read bit 0 of PORTC in b */   
b = PORTC_BIT0_READ();     
于 2012-01-22T16:29:09.843 回答
1

非常感谢,但我在这里的 AVR Freaks 中找到了这个答案:

BV=位值。

If you want to change the state of bit 6 in a byte you can use _BV(6) which is is equivalent to 0x40. But a lot us prefer the completely STANDARD method and simply write (1<<6) for the same thing or more specifically (1<<<some_bit_name_in_position_6)

For example if I want to set bit 6 in PORTB I'd use:
Code:
PORTB |=  (1 << PB6);

though I guess I could use:
Code:
PORTB |= _BV(6);

or
Code:
PORTB |= _BV(PB6);

But, like I say, personally I'd steer clear of _BV() as it is non standard and non portable. After all it is simply:
Code:
#define _BV(n) (1 << n)

anyway.

Cliff
于 2012-01-23T11:37:44.340 回答