Search This Blog

Tuesday, September 4, 2012

Starting programming in C with AVR

Here are few examples related to various data typed in AVR. These are examples to get one started with using data types.

Data Types revisited
Data Type
Size in Bits
Data Range/Usage
Unsigned char
8 bit
0 to 255
Char
8 bit
-128 to +128
Unsigned int
16 bit
0 to 65535
Int
16 bit
-32768 to +32768
Unsigned long
32 bit
0 to 4,294,967,295
Long
32 bit
-2,147,483,648 to +2,147,483,648
Float
32 bit
1.175e-38 to 3.402e38
Double
32 bit
1.175e-38 to 3.402e38

Using Unsigned Char

Examples
Example 1
//send values 00 - ff to say port B
#include <avr/io.h> // standard avr header file

int main(void)
{
                unsigned char x;
                DDRB = 0xFF; // making all pins of port b as output
                for(x = 0; x<=255; x++)
                {
                                PORTB = x;
                }
                while(1);
                //remember x cannot store value > 255 as its an unsigned char
}
Example 2
// toggle pins of port b 100 times
#include <avr/io.h> // standard avr header file

int main(void)
{
                unsigned char x;
                DDRB = 0xFF; // making all pins of port b as output
                PORTB = 0xAA; // port b 10101010           
                for(x = 0; x<=100; x++)
                {
                                PORTB = ~PORTB;
                }
                while(1);
                //remember x cannot store value > 255 as its an unsigned char
}

Using Signed Char

Example
// program to send values -2 to 2 to port B
#include <avr/io.h>// standard avr header file

int main(void)
{
                char num[] = {-2,-1,0,1,2};
                unsigned char x;

                DDRB = 0xFF;
                // make all pins of port b output
                for(x = 0; x<5; x++)
                {
                                PORTB = num[x];
                }
               
                while(1);
                //remember num can store value between -128 to +128
                // and array in C starts for 0
}

Similarly we can use other data types. Here is toggling example at various values. Demonstrating when to use what type.

Examples
Unsigned int
// toggle pins of port b 50000 times
#include <avr/io.h> // standard avr header file

int main(void)
{
                unsigned int x;
                DDRB = 0xFF; // making all pins of port b as output
                PORTB = 0xAA; // port b 10101010           
                for(x = 0; x<=50000; x++)
                {
                                PORTB = ~PORTB;
                }
                while(1);
                //remember x cannot store value > 65535 as its an unsigned int
}
Unsigned long
// toggle pins of port b 5,00,000 times
#include <avr/io.h> // standard avr header file

int main(void)
{
                unsigned long x;
                DDRB = 0xFF; // making all pins of port b as output
                PORTB = 0xAA; // port b 10101010           
                for(x = 0; x<=500000; x++)
                {
                                PORTB = ~PORTB;
                }
                while(1);
                //remember x can have value  < 2,147,483,648 as its an unsigned long
}

Time Delay can be created
1)      Using a simple for loop
2)      Using predefined C functions
3)      Using AVR timers

I don’t use a simple for loop unless if its value is given along with the driver library. I only use predefined C function to create delay.
Using predefined C function to create a delay
//create 10 milli sec delay
#include <util/delay.h>
#include <avr/io.h>

// delay in milliseconds
void delay_ms(unsinged int t)
{
                _delay_ms(t);
}
int main(void)
{
                DDRB = 0xFF;
                //port b output
                while(1)
                {
                                PORTB = 0xFF;
                                delay_ms(10);
                                PORTB = 0X55;
                                delay_ms(10);
                }
}

Harvard and Von-Neumann Architecture

In processor we need 2 kind of memory spaces program(to store code) and data.
Code – provides instructions to CPU.
Data – provides information to be processed to CPU.

The CPU uses buses to access the code ROM and data RAM memory spaces. The early computers used the same bus for accessing both code and data. Such an architecture is commonly known as von-neumann architecture. This means in von-neumann architecture there is only 1 bus for both data and program code. Meaning its going to be slow down the CPU’s processing speed as one has to wait for other to finish using the buses.

So to speed up the things, harvard architecture was introduced. In this we have separate buses for both program code and data memory. This means we need four sets of buses.
1)      To carry data in and out of CPU.
2)      To carry code into CPU.
3)      To carry address for accessing data.
4)      To carry address for accessing code.

Harvard architecture is usually implemented on microcontrollers.
Von Neumann is usually implemented on processors like x86 etc etc.
But why??
Implementing Harvard on microcontroller is damn easy as both RAM and ROM are on the same chip with distance of micron scale. But implementing it on x86 type PC its very expensive as in x86 both RAM and ROM are outside the CPU. Separate buses(wire traces) will make the motherboard very large and expensive.

Say for example a Pentium processor with 64 bit data bus and 32 bit address bus would require say about 100 wire traces in case of Von Neumann which will become 200 if Harvard architecture is used.

Harvard architecture will make it necessary that large number of pins comes out of processor itself. So for this reason we donot see Harvard architecture on our desktops and laptops.

The AVR uses Harvard architecture internally, but they still use von Neumann architecture if they need external ROM or RAM.
Von-Neumann and Harvard Architecture