Mcu-notes-01

From wikipost
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Presentation Notes for the Microcontroller Introduction at Westlakes Amateur Radio Club

Data sheets


Hardware setup:

  • connection diagram
  • batch/script files
  • avrdude


Avrdude program script

  • Windows
@echo off

set PROJECT=blinkled
set SPEED=16000000UL
set PORT=\\.\USBSER000
set GCC_DEVICE=attiny85
set PGM_DEVICE=t85
set PGM_PROTO=avrispv2
set GCC_OPTIMISATION=-Os
set AVRTOOLS=C:\winavr-20100110\bin

echo compile program
avr-gcc -g %GCC_OPTIMISATION% -Wall -mcall-prologues -DF_CPU=%SPEED% -mmcu=%GCC_DEVICE%   -c -o %PROJECT%.o %PROJECT%.c

echo create object file
avr-gcc -g %GCC_OPTIMISATION% -Wall -mcall-prologues -DF_CPU=%SPEED% -mmcu=%GCC_DEVICE% %PROJECT%.o -o %PROJECT%.obj

echo convert object file to hex file
avr-objcopy  -R .eeprom -O ihex %PROJECT%.obj %PROJECT%.hex


echo write program to chip
avrdude -F -c %PGM_PROTO% -P %PORT% -p %PGM_DEVICE% -U flash:w:%PROJECT%.hex  

echo avr-list output:
echo ----------------
%AVRTOOLS%\avr-size -C --mcu=%GCC_DEVICE%  %PROJECT%.obj > %PROJECT%_avr-size.txt

type %PROJECT%_avr-size.txt

pause
  • Linux



Demo Programs for the ATtiny85:

  • Blink led

/*

  Demo program: blinkled.c

  This program continously loops, bringing PB4 high and low with one-second intervals. 
 

                        ATtiny85
               
                       +--- ---+
          (RST)   PB5 -|1 |_| 8|- VCC 
                  PB3 -|2     7|- PB2    (SCK)          
    +---|<|------ PB4 -|3     6|- PB1    (MISO)         
    |   led   +-- GND -|4     5|- PB0    (MOSI)
    |         |        +-------+
    +--/\/\/--+
        1k



   Pololu programming cable pinout (top view)

                          +---+
    ----------------  gnd |o o| rst
    ribbon cable     mosi |o o| sck
    -----red--------  vdd |o o| miso
                          +---+

    NOTE: The T85 must be externally powered with 5V (4.5V does NOT work!)
          A t85 driving a standard 2x16 LCD without backlit LED on draws
          about 8.5mA so with 100mA to spare on a USB port you could consider
          grabbing 5V from the Pololu programmer. The 5V pin on the 
          programmer is labeled: 'VBUS (+5V)'


*/


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

#define LED PB4    // Define ext led pin on PB4
#define OUTPUT 0
#define INPUT  1
#define LOW    0
#define HIGH   1


void pinMode(int pin,int state)
{ 
  if (state == OUTPUT)
  {
    (DDRB |= (1 << pin)); 
  } else { 
    (DDRB &= ~(1 << pin)); 
  }
} 

void digitalWrite(int pin, int state)
{
  // make sure port is set to output
  DDRB |= (1 << pin);

  if (state == HIGH)
  { 
    (PORTB |= (1 << pin)); 
  } else {
    (PORTB &= ~(1 << pin));
  }
} 



int main( void ) 
{

  pinMode(LED,OUTPUT);    // Set output direction on LED 

  while ( 1 ) 
  {

      digitalWrite (LED,HIGH);
      _delay_ms(1000);
      digitalWrite (LED,LOW);
      _delay_ms(1000);

  }

  return 0;

}
  • Read push button state
  • Analog to Digital
  • PWM (pulse width modulation)


Web links:

(AVR)

(Other)