2012-01-22 26 views
1

我正在为KS0108 GLCD驱动程序编写全新的专用库,并提供新的算法和功能。我正在使用ATMega16。我的点阵GLCD尺寸是128x64。#在Avr Studio 5(ATmega16)中定义PORTX.x

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

IDE:AVR工作室5
语言:Ç
模块:128X64点阵GLCD
司机:KS0108
微控制器:ATMega16

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

回答

0

在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();  
1

非常感谢,但是我发现在AVR单片机这个答案在here

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