Micro controller basics (refresher for line follower project)

I had to take a break from building and designing my line follower robot, because I ran into a brick wall when I realised  I have forgotten EVERYTHING I knew about c-programming.

To get up to speed from the beginning to now, check out the previous posts:

Part 1 - The design statement
Part 2 - Basic design guidelines
Part 3 - Build and test chassis and drive platform

In the previous posts, I said that I was going to use the PIC18F45K20, but I am going to go with the +Atmel Microcontroller ATMEGA328P instead for two simple reasons:
        -  it is easier to come by than the PIC
        -  and the programming kit is half the price of the PICKit3.

Atmel Microcotroller
Now we have to design and build the electronics around the microcontroller.  This will be determined by many factors, so some applications will have differently designed circuits than others.  So it is a good idea to look at the datasheet for he microcontroller you choose, to be able to know what components you will need.  Click here for the full datasheet.

Since micro's all work more or less the same, check out Newbie Hack for a fantastic tutorial series on the ATMEGA32, from the basics, right through to some pretty cool projects.

Here is a pinout diagram for the ATMEGA328P (note that it is the same mirco used on the +Arduino board:

ATMEGA328 Pin Mapping
The datasheet will have detailed explanations about everything.  Here are the descriptions of each pin abbreviation (extracts from datasheet):
  • Vcc indicates the pin where the positive terminal for the power supply or battery must be connected (around 3 - 5.5Vdc)
  • GND is the "ground" terminal and this is where the negative terminal of the power supply or battery gets connected.
  • PB#, PC#, PD# indicates PORT B/C/D and the bit number.  Each port has 8-bits from bit 0 (least significant bit) to bit 7 (most significant bit).
  • AVcc - supply voltage pin for the ADC (Analog to Digital Converter).  It should be connected externally to Vcc even if the ADC is not used.  If the ADC is used, it should be connected to Vcc through a low-pass filter.
 
ATmega328 Port B Pins Alternate Functions
  • SPI - Serial Peripheral Interface
 ATmega328 Port C Pins Alternate Functions 

ATmega328 Port D Pins Alternate Functions
  • USART - Universal Synchronous/Asynchronous  Receiver/Transmitter - used to facilitate serial communication using the RS232 protocol

ATmega328 Electrical Characteristics  
Note the DC current per I/O pin since that will determine the size of the resistor you will need to connect in series with a LED for example.  I will use a 680ohm resistor which limits the current to about 7mA, which is well within the limit.  (By the way another useful app I have on my phone is ElectroDroid. It does everything from Ohm's law to resistor colour bands, inductor colour codes, communication port and micro controller pin outs).

ATmega328 Common DC Characteristics


Now, I have not programmed any microprocessor in years and this is my first time to do a project with and +Atmel Microcontroller - Using AVR Studio 5 and 6 I had to figure out some things from the word go:
  • I had written/copied a couple of simple programs to refresh my C-programming skills and when I wanted to compile all of them under the same project with AVR Studio, it reported that "make: no rules to build...".  I fixed this by making a new project for each example since they were not at all related. Here is one example program using a switch and an LED as demonstrated by +Android Apps - AVR Control:
 //Example using 1 pin as input for a switch and 1 as output for LED
//on the same port
#include <avr/io.h>


void main()
{
    //set PA0  as output
    DDRA |= (1<<PA0);    //DDR = Data Direct Register for PORTA
    //set PA5 as input
    DDRA  &=~ (1<<PA5);
    While(1)
    {
        //check if switch is pressed
        if (!(PORTA & (1<<PA5)))
        //if true – switch LED on
        PORTA &=~(1<<PA0);
    }
}

This type of examples can be found by simply searching for simple C-tutorials, but it is quite handy to have it immediately available on you phone or tablet.  The app also explains the electronic circuitry used for each example and gives a detailed explanation of the registers used and how they work.
  • I also had trouble when I wanted to flash/program/transfer the chip using the AVRISPmk2 (AVR In System Programmer version 2).  It kept on reporting that "the target seems to have lost power or ISP is disconnected".  As stated above, AVcc should be connected to Vcc even though the ADC is not being used, so that was my mistake.  If the problem persists click here for a possible solution, though I am sure there must other simpler reasons for this error.  Let me know if you experience any problems in this regard.
After refreshing my C-programming skills on all the examples in the 'AVR control' app, which covers basic examples for all the following features, I will continue with my "Designing a line follower robot" posts:
  • PORT (i/o control)
  • UART/USART
  • ADC
  • Analog Comparitor
  • External interrupts
  • EEPROM control
  • IIC/I2C/TWI (two wire interface)
  • SPI
  • Timers
 Thank you for visiting my blog, please correct me if I have slipped up, leave a comment and visit again for progress on my line follower project.
Post a Comment

Popular Posts