#define PORTX.x in Avr Studio 5 (ATmega16)

8.3k views Asked by At

I'm writing a new and special library whith new algorithms and abilities for KS0108 GLCD Driver. I'm using ATMega16. My dot matrix GLCD dimension is 128x64.

How can I use #define code to define different port pins?
for example: #define GLCD_CTRL_RESTART PORTC.0

IDE: AVR Studio 5
Language: C
Module: 128x64 dot matrix GLCD
Driver: KS0108
Microcontroller: ATMega16

Please explain which headers I should use? and also write a full and very simple code for ATMEga16.

2

There are 2 answers

0
ouah On BEST ANSWER

In ATmega, pin values are assembled in PORT registers. A pin value is the value of a bit in a PORT. ATmega doesn't have a bit addressable IO memory like some other processors have, so you cannot refer to a pin for reading and writing with a single #define like you suggest.

What you can do instead, if it helps you, is to define macros to read or write the pin value. You can change the name of the macros to suit your needs.

#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();     
0
user1163557 On

thanks a lot, but i found this answer in AVR Freaks at here:

BV=Bit Value.

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