Week Seven: Embedded Programming

This week the assignment was to program the board that we modified in week 5 to do something. First I tried programming my board with the hello echo program from the class page for this week. While testing this, I realized that there was a connection that hadn't been made on my board when I milled it. This had happened because I tried to connect two nodes in Eagle by naming them the same thing and for some reason, Eagle didn't see them as connected. I fixed this by adding a jumper cable to make the missing connection. When making my program for this week, I first started with a template that I found online for an RGB LED rainbow fader. It was written in C, and I use vim for a text editor. This template used a function analogWrite() from an arduino library to send the PWM to the pins. I couldn't figure out how to get just that library or the code for just that function without downloading the arduino IDE, so I looked into other approaches. Later, I realized that there was a template for an RGB LED fader on the class page for next week (output devices), so I decided to modify that code instead. First I added in that it wouldn't start the fading until the button was pressed. Then I flipped all of the signals for turning the LEDs on and off, because the RGB LED that I have has a shared anode instead of a shared cathode and so I had the pins normally high and pulled them down to turn the LEDs on. I also added that when the button was pressed, the LED would go through one sequence and then stop.

//
// hello.RGB.44.c
//
// RGB LED software PWM hello-world
//
// Template taken from:
// Neil Gershenfeld
// 11/10/10
//
// (c) Massachusetts Institute of Technology 2010
// This work may be reproduced, modified, distributed,
// performed, and displayed for any purpose. Copyright is
// retained and must be preserved. The work is provided
// as is; no warranty is provided, and users accept all
// liability.
//
// Adapted by
// Liz Schell
// 11/3/15
//

#include 
#include 

#define output(directions,pin) (directions |= pin) // set port direction for output
#define input(directions,pin) (directions &= ~pin)
#define set(port,pin) (port |= pin) // set port pin
#define clear(port,pin) (port &= (~pin)) // clear port pin
#define pin_test(pins,pin) (pins & pin) // test for port pin
#define bit_test(byte,bit) (byte & (1 << bit)) // test for bit set
#define PWM_delay() _delay_us(25) // PWM delay

#define red_port PORTB
#define green_port PORTA
#define blue_port PORTA
#define led_direction DDRB
#define red (1 << PB2)
#define green (1 << PA7)
#define blue (1 << PA3)

#define button_pin PINA
#define button (1 << PA2)
int main(void) {
   //
   // main
   //
   unsigned char count, pwm;
   //
   // set clock divider to /1
   //
   CLKPR = (1 << CLKPCE);
   CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);
   //
   // initialize LED pins
   //
   set(red_port, red);
   output(DDRB, red);
   set(green_port, green);
   output(DDRA, green);
   set(blue_port, blue);
   output(DDRA, blue);
   input(DDRA,button);
   set(button_pin,button);

    int sequence = 0;

while(1){
   while (0 != pin_test(button_pin,button)){
       _delay_ms(2);
   }

   while (0 == pin_test(button_pin, button)){
        sequence = 1;
   }

   while (sequence){
       //
       // off -> red
       //
      for (count = 0; count < 255; ++count) {
         set(red_port,red);
         for (pwm = count; pwm < 255; ++pwm)
            PWM_delay();
         clear(red_port,red);
         for (pwm = 0; pwm < count; ++pwm)
            PWM_delay();
         }
      //
      // red -> green
      //
      for (count = 0; count < 255; ++count) {
         clear(red_port,red);
         set(green_port,green);
         for (pwm = count; pwm < 255; ++pwm)
            PWM_delay();
         set(red_port,red);
         clear(green_port,green);
         for (pwm = 0; pwm < count; ++pwm)
            PWM_delay();
         }
      //
      // green -> blue
      //
      for (count = 0; count < 255; ++count) {
         clear(green_port,green);
         set(blue_port,blue);
         for (pwm = count; pwm < 255; ++pwm)
            PWM_delay();
         set(green_port,green);
         clear(blue_port,blue);
         for (pwm = 0; pwm < count; ++pwm)
            PWM_delay();
         }
      //
      // blue -> on
      //
      for (count = 0; count < 255; ++count) {
         clear(blue_port,blue);
         set(green_port,green);
         set(red_port,red);
         for (pwm = count; pwm < 255; ++pwm)
            PWM_delay();
         clear(blue_port,blue);
         clear(green_port,green);
         clear(red_port,red);
         for (pwm = 0; pwm < count; ++pwm)
            PWM_delay();
         }

      //
      // on -> off
      //
      for (count = 0; count < 255; ++count) {
         clear(blue_port,blue);
         clear(green_port,green);
         clear(red_port,red);
         for (pwm = count; pwm < 255; ++pwm)
            PWM_delay();
         set(blue_port,blue);
         set(green_port,green);
         set(red_port,red);
         for (pwm = 0; pwm < count; ++pwm)
            PWM_delay();
         }
      sequence = 0;
      }
    }
   }