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);
                }
}

No comments:

Post a Comment