Embedded Programming

blinking light

This week was to upload a program to a microcontroller, I choose ubuntu as the os

Preparation

I first started to gather the drivers. avrdude,gcc-avr,avr-libc

  • sudo apt-get avrdude
  • sudo apt-get gcc-avr
  • sudo apt-get avr-libc

I first tried to transfer the hello.ftdi.44.program program to the chip. One way to confirm your programmer (from previous week) is to lsusb and look for . You need to be very careful when you connect the FTDI cable to the hello board, since it may fry your board. The black cord is GND. Then download Neil's c file and make file cd to the download directory, and then I went to the class programming site and uploaded my first programm to the hello world (Fig1).

Fig1. success!!

Programming

the following is the c program, I wrote the comments inside the code. For the make file, It was a slight modification to change the PR0JECT= variable to the c program file name. Fig2 shows that important pins are led = Pin10 = PA3(ADC3/T0/PCINT3) and button = Pin 6 = PA7(PCINT7/ICP/OC0B/ADC7).

c files blink_led.c and button_led.c

Fig2. last weeks board image

Uploading

All was fine, except that I needed to add administrative premission to transfer the programs. Other than that, I was glad that both my programmer and hello chip was working. Basic workflow is to go through the steps below

  • (sudo)make -f yoursourcecode.c.make
  • (sudo)make -f yoursourcecode.c.make program-usbtiny-fuses
  • (sudo)make -f yoursourcecode.c.make program-usbtiny

Fig3. error

Fig4. lights off by default

Fig5. pressing and lights are on

source code


  #include <avr/io.h>
  #include <util/delay.h>


  #define blink_delay 10

  // pin info

  // led = Pin10 = PA3(ADC3/T0/PCINT3)
  // button = Pin 6 = PA7(PCINT7/ICP/OC0B/ADC7)

  int main(void){

    DDRA |= _BV(PA3); // led

    // same as DDRA = DDRA | _BV(PA3)

    //DDRA in page66 of datasheet
    //|= bitwise inclusive OR and assignment operator -> 011 | 001 = 011
    //_BV(bit) ... converts a bit number to a byte value,
    // you need to include avr/io.h to use
    // this means that PA3 is a bit...

    // Looping forever
    while(1){
      PORTA |= _BV(PA3); // turn on
      // PORTA example in page56 of datasheet

      _delay_ms(blink_delay);

      PORTA = 0; // turn off
      // clears PORTA?

      _delay_ms(blink_delay);
    }

    return 1;
  }
              

  #include <avr/io.h>
  #include <avr/interrupt.h>
  #include <util/delay.h>


  #define blink_delay 10


  #define bit_mask(x) (1 << (x)) // bit mask, this is similar to  _BV(x)??
  #define check_bit(x,b) x&b // checks the bit 0?1?
  #define set_bit(x,b) x|=b; // sets the bit
  #define clear_bit(x,b) x&=~b; // sets the bit oppisite to the bit
  #define flip_bit(x,b) x^=b; // flip


  // pin info

  // led = Pin10 = PA3(ADC3/T0/PCINT3)
  #define led_pin 3
  // button = Pin 6 = PA7(PCINT7/ICP/OC0B/ADC7)
  #define button_pin 7

  // first readed Dan's class page
  // http://fab.cba.mit.edu/classes/863.14/people/dan_chen/embedded-programming/index.html
  // but he seems to connect the button to a PB pin....
  // I looked around other attempts and found this code readable
  // http://fab.cba.mit.edu/classes/863.14/people/sarah_edris/files/week07/hello.ftdi.44.echo.c


  int main(void){

    // sets direction of the led_pin of port A as output
    set_bit(DDRA,bit_mask(led_pin))
    // sets direction of the button_pin of port A as input
    clear_bit(DDRA,bit_mask(button_pin))

    // Looping forever
    while(1){
      if(check_bit(PINA,bit_mask(button_pin))){
        clear_bit(PORTA,bit_mask(led_pin))
      }else{
        set_bit(PORTA,bit_mask(led_pin))
      }
    }
    return 1;
  }