Operácie

AVR Example2.c

Z SensorWiki

Verzia z 21:54, 6. november 2008, ktorú vytvoril Balogh (diskusia | príspevky)
/* ************************************************************************** */
/*                                                                            */
/*  Monoliticke mikropocitace - Priklad 1.2                                   */
/*                                                                            */
/*  Program zasvieti/zhasne LED na PD5 podla stavu prepinaca na PD2           */
/*  Ako Priklad 1.1, len doplnene o makra ktore (mozu) program sprehladnit    */
/*                                                                            */
/*  Autor: Richard Balogh <balogh@elf.stuba.sk>                               */
/*  Historia:                                                                 */
/*            19.2.2006 prva pokusna verzia                                   */
/*  Prenositelnost:                                                           */
/*                                                                            */
/* ************************************************************************** */
#include <avr/io.h>          // spolu s -mmcu=atmega16 nahra spravny .h subor
                             // ktory potrebujeme kvoli PORTx, PINx a DDRx

#define SW1 (PIND & 0x??)
#define LEDon  PORTD &= ~(_BV(PD5)) // cbi(PORTD,PD5); ale cbi je nepodporovane 
#define LEDoff (PORTD |= _BV(PD5))  // sbi(PORTD,PD5); ale sbi je nepodporovane

int main (void)
{
                      
     DDRD = 0x??;            // bin: 1010 0010  1 = out, 0 = in
    PORTD = 0x??;            // bin: 1x11 110x  1 = LED zhasni, 1 = pull-up ON

    do{
   
      if ( SW1 )            // bin:  PIND & 0000 0100 = sw1 OFF?
         LEDoff;            // bin  PORTD + 0010 0000 = nastav PD5, LED OFF
      else                  // t.j. ak sw1 ON:
         LEDon;             // bin: PORTD & 1101 1111 = vynuluj PD5, LED ON

    } while(1);            // toto rob stale dokola

    return (0);            // formalita, nikdy sem neprideme
}


Späť na cvičenie...