cc-preprocessoratmega16avr-studio5

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


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.


Solution

  • 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();