Monday, May 9, 2016

Example 4-1 from Make: AVR Programming (2014)

This post is based on classroom demonstration of Example 4-1 from book Make: AVR Programming by Elliot Williams. The demonstration has three variations in cylonEyes.c listing on pp. 60-61. The Proteus simulation model is shown below. Then comes program codes and a short movie clip.

// Example 4-1. cylonEyes.c modified listing 1
// ------- Preamble -------- //
#include <avr/io.h>     /* Defines pins, ports, etc */
#include <util/delay.h> /* Functions to waste time */
#define DELAYTIME 1000  /* milliseconds */
#define LED_PORT  PORTB
#define LED_PIN   PINB
#define LED_DDR   DDRB
int main(void) {
// -------- Inits --------- //
uint8_t i = 0;
LED_DDR = 0xff;         /* Data Direction Register B: all set up for output */
// ------ Event loop ------ //
while (1) {
            while (i < 7) {
                            LED_PORT = (1 << i);    /* illuminate only i'th pin */
                            _delay_ms(DELAYTIME);   /* wait */
                            i = i + 1; /* move to the next LED */
                          }
            while (i > 0) {
                            LED_PORT = (1 << i);    /* illuminate only i'th pin */
                            _delay_ms(DELAYTIME);   /* wait */
                            i = i - 1; /* move to the previous LED */
                          }
          } /* End event loop */
return (0);
}

// Example 4-1. cylonEyes.c modified listing 2
// ------- Preamble -------- //
#include <avr/io.h>     /* Defines pins, ports, etc */
#include <util/delay.h> /* Functions to waste time */
#define DELAYTIME 1000  /* milliseconds */
#define LED_PORTB   PORTB
#define LED_PINB    PINB
#define LED_DDRB    DDRB
#define LED_PORTD   PORTD
#define LED_PIND    PIND
#define LED_DDRD    DDRD
int main(void) {
// -------- Inits --------- //
uint8_t i = 0;
uint8_t j = 0;
LED_DDRB = 0xff;         /* Data Direction Register B: all set up for output */
LED_DDRD = 0xff;
// ------ Event loop ------ //
while (1) {
            while (i < 7) {
                            LED_PORTB = (1 << i);    /* illuminate only i'th pin */
                            LED_PORTD = (1 << j);
                            _delay_ms(DELAYTIME);    /* wait */
                            i = i + 1;               /* move to the next LED */
                            j = i;
                          }
            while (i > 0) {
                            LED_PORTB = (1 << i);    /* illuminate only i'th pin */
                            LED_PORTD = (1 << j);
                            _delay_ms(DELAYTIME);   /* wait */
                            i = i - 1;              /* move to the previous LED */
                            j = i;
                          }
          } /* End event loop */
return (0);
}

// Example 4-1. cylonEyes.c modified listing 3
// ------- Preamble -------- //
#include <avr/io.h>     /* Defines pins, ports, etc */
#include <util/delay.h> /* Functions to waste time */
#define DELAYTIME 1000  /* milliseconds */
#define LED_PORTB   PORTB
#define LED_PINB    PINB
#define LED_DDRB    DDRB
#define LED_PORTD   PORTD
#define LED_PIND    PIND
#define LED_DDRD    DDRD
int main(void) {
// -------- Inits --------- //
uint8_t i = 0;
uint8_t j = 7;
LED_DDRB = 0xff;         /* Data Direction Register B: all set up for output */
LED_DDRD = 0xff;
// ------ Event loop ------ //
while (1) {
            while ((i < 7)&&(j > 0)) {
                            LED_PORTB = (1 << i);    /* illuminate only i'th pin */
                            LED_PORTD = (1 << j);
                            _delay_ms(DELAYTIME);    /* wait */
                            i = i + 1;               /* move to the next LED */
                            j = j - 1;
                          }
            while ((i > 0)&&(j < 7)) {
                            LED_PORTB = (1 << i);    /* illuminate only i'th pin */
                            LED_PORTD = (1 << j);
                            _delay_ms(DELAYTIME);   /* wait */
                            i = i - 1;              /* move to the previous LED */
                            j = j + 1;
                          }
          } /* End event loop */
return (0);
}


No comments:

Post a Comment