我正在编写一个新的和特殊的图书馆与新的算法和能力的KS0108液晶显示驱动程序。我在用ATMega16。我的点阵GLCD尺寸是128x64。
我如何使用#定义代码来定义不同的端口引脚?
例如:#定义GLCD_CTRL_RESTART PORTC.0
IDE:AVR 5
语言:C
模块:128x64点阵GLCD
驱动程序:KS0108
微控制器:ATMega16
请解释一下我应该使用哪些标头?并为ATMEga16编写完整和非常简单的代码。
发布于 2012-01-22 16:29:09
在ATmega中,引脚值被组装在端口寄存器中。引脚值是端口中位的值。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-23 11:37:44
非常感谢,但我在这里的AVR网站上找到了这个答案
BV=Bit值
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.
Cliffhttps://stackoverflow.com/questions/8962272
复制相似问题