Operácie

AVR Example2.c

Z SensorWiki

Verzia z 07:24, 12. október 2012, ktorú vytvoril Balogh (diskusia | príspevky)
(rozdiel) ← Staršia verzia | Aktuálna úprava (rozdiel) | Novšia verzia → (rozdiel)
/* ************************************************************************** */
/*                                                                            */
/*  Monoliticke mikropocitace - Priklad 1.2                                   */
/*                                                                            */
/*  Program zasvieti/zhasne LED na PORTB.5 podla stavu prepinaca na PORTB.4   */
/*  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                                  */
/*            12.10.2012 uprava pre Acrob/Arduino (PD -> PB)                  */
/*  Verzia:                                                                   */
/*            studentska bez konkretnych hodnot                               */
/*                                                                            */
/* ************************************************************************** */

#include <avr/io.h>          // spolu s -mmcu=atmega328 nahra spravny .h subor
                             // ktory potrebujeme kvoli PORTx, PINx a DDRx

#define SW1 (PINB & 0x??)
#define LEDon  PORTB &= ~(_BV(5)) // cbi(PORTB,5); ale cbi je nepodporovane 
#define LEDoff (PORTB |= _BV(5))  // sbi(PORTB,5); ale sbi je nepodporovane

int main (void)
{
                      
     DDRB = 0x??;            // bin: 00?? 0000  1 = out, 0 = in
    PORTB = 0x??;            // bin: 00?? 1111  1 = LED zhasni, 1 = pull-up ON

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

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

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


Späť na cvičenie...