AVR Example3.c

Z SensorWiki
Prejsť na: navigácia, hľadanie
/* ************************************************************************** */
/*                                                                            */
/*  Monoliticke mikropocitace - Priklad 1.3                                   */
/*                                                                            */
/*  Program zasvieti/zhasne zltu LED na PORTB.5 s periodou ?? [s]             */
/*                                                                            */
/*  Autor: Richard Balogh <balogh@elf.stuba.sk>                               */
/*  Historia:                                                                 */
/*            3. 3. 2006 prva pokusna verzia                                  */
/*           12.10. 2012 upravene pre Acrob / Arduino (portD / portB)         */
/*  Verzia:                                                                   */
/*            Pre studentov, chybajuce hodnoty                                */
/*  Prenositelnost:                                                           */
/*                                                                            */
/* ************************************************************************** */

#include <avr/io.h>        

#define LEDon  PORTB &= ~(_BV(5)) // cbi(PORTB,PB5); ale cbi je nepodporovane 
#define LEDoff (PORTB |= _BV(5))  // sbi(PORTB,PB5); ale sbi je nepodporovane

#define F_CPU 16000000

void delay_ms(unsigned int ms);

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

    do{
   
         LEDoff;             // bin  PORTB + 0010 0000 = nastav PB5, LED OFF
         delay_ms(500);      // cakaj 500 ms (0,5 s)
         LEDon;              // bin: PORTD & 1101 1111 = vynuluj PB5, LED ON
         delay_ms(500);      // cakaj 500 ms (0,5 s)
         
    } while(1);             // toto rob stale dokola

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


void delay_ms(unsigned int ms)
{
  unsigned int index;

  while (ms)
  {
   index = F_CPU / ????;    // vypocitajte, kolko treba, aby sme dostali 1ms!!
        
   while (index)
   {
    asm volatile ("nop");
    index--;
   }

   ms--;
  }
}

Logo Ardiuino.png Upravená verzia pre prostredie Arduino

/* ************************************************************************** */
/*                                                                            */
/*  Monoliticke mikropocitace - Priklad 1.3A                                  */
/*                                                                            */
/*  Program zasvieti/zhasne zltu LED na PORTB.5 s periodou ?? [s]             */
/*                                                                            */
/*  Autor: Richard Balogh <balogh@elf.stuba.sk>                               */
/*  Historia:                                                                 */
/*            3. 3. 2006 prva pokusna verzia                                  */
/*           12.10. 2012 upravene pre Acrob / Arduino (portD / portB)         */
/*           14.10. 2013 upravene pre Arduino IDE jazyk                       */
/*  Verzia:                                                                   */
/*            Pre studentov, chybajuce hodnoty                                */
/*  Prenositelnost:                                                           */
/*                                                                            */
/* ************************************************************************** */

#define LEDon  PORTB &= ~(_BV(5)) // cbi(PORTB,PB5); ale cbi je nepodporovane 
#define LEDoff (PORTB |= _BV(5))  // sbi(PORTB,PB5); ale sbi je nepodporovane

#define F_CPU 16000000

void delay_ms(unsigned int ms);


  void setup() {

     DDRB = 0b????????;            // bin: ???? ????  1 = out, 0 = in
    PORTB = 0b????????;            // bin: ???? ????  1 = LED zhasni, 1 = pull-up ON

}

void loop() {

         LEDoff;             // bin  PORTB + 0010 0000 = nastav PB5, LED OFF
         delay_ms(500);      // cakaj 500 ms (0,5 s)
         LEDon;              // bin: PORTD & 1101 1111 = vynuluj PB5, LED ON
         delay_ms(500);      // cakaj 500 ms (0,5 s)
         
  
}

void delay_ms(unsigned int ms)
{
  unsigned int index;

  while (ms)
  {
   index = F_CPU / ????;    // vypocitajte, kolko treba, aby sme dostali 1ms!!
        
   while (index)
   {
    asm volatile ("nop");
    index--;
   }

   ms--;
  }
}