You are on page 1of 2

Online Security

This site has no rating

Physical Computing
It's all about people talking
Search

ABOUT TUTORIALS CODE REFERENCE LIBRARY VENDOR LIST

Tutorial Writing your first AVR C program Blinking LED


This is a simple blinking LED program. We will look at basic structure and cover specifics about the syntax.

First lets talk about the files that surround the main document you created. These are called includes and they bring code into the main document. This code adds or augments the functionality of your code. Includes can be found
in the WinAvr folder and in the current project folder.

They look like this:

This include is outside the project folder and uses the <> around the name
#include <avr/io.h>

This include is inside the project folder and uses the around the name
#include my_defines.h

For this simple tutorial, we will add our own include file. Ours is called mytimer.h. Download the file, unzip it, and place the mytimer.h file in the project folder.

you can download the file HERE

The overall structure of a simple piece of code will look like this:

At the top we offer a few details


/*
Title: title.c
Author: you
Date Created: 01/01/01
Last Modified: 01/01/01
Purpose: what does this code do
*/

Our include(s)
#include <include_file_name.h>

A main loop:
int main (void){

//code goes here

When we apply this structure, our code looks like this:

/*
Title: blinking_led.c
Author: you
Date Created: 01/01/01
Last Modified: 01/01/01
Purpose: This is a for loop that blinks an LED on PORTC
*/
//set your clock speed
#define F_CPU 4000000UL
//these are the include files. They are outside the project folder
#include <avr/io.h>
#include <avr/delay.h>
//this include is in your project folder
#include myTimer.h

int main (void)


{
//Set PORTC to all outputs
DDRC = 0xFF
//create an infinite loop
while(1) {
//this turns pin C0 on and off
//turns C0 HIGH
PORTC |=(1<<0)
//PAUSE 250 miliseconds
delay_ms(250)
//turns C0 LOW
PORTC &= ~(1 << 0)
//PAUSE 250 miliseconds
delay_ms(250)
}
}

Lets take the code apart:

These are the details


/*
Title: blinking_led.c
Author: you
Date Created: 01/01/01
Last Modified: 01/01/01
Purpose: This is a for loop that blinks an LED on PORTC
*/

This defines your clock speed.


//set your clock speed
#define F_CPU 4000000UL

These include files are in the WinAVR folder. If its installed correctly, you should have nothing to worry about.
//these are the include files. They are outside the project folder
#include <avr/io.h>
#include <avr/delay.h>

This is another include file. Make sure it is in the project folder.


//this include is in your project folder
#include myTimer.h

Here is the body of the code.

We create a container. The rest of the code will go inside this container
int main (void)
{
This is the rest of the code
//Set PORTC to all outputs
DDRC = 0xFF
The DDRC indicates port C on our chip. Likewise, DDRB would indicate port B

The 0xFF is hexidecimal for 11111111. Hexidecimal is shorthand for binary. We want to tell the eight pins in PORT C that each of them is an input. If we wanted to set half to output and the other half to
input (11110000) we would say DDRC=0xF0. And if we wanted to set all the
pins of PORT C to input (00000000) we would say 000

Here we create a loop that will continue for as long as there is power connected to the circuit.
//create an infinite loop
while(1) {

This is a curious thing that, have you been programming higher level languages until now, you may not recognise. The literal reading of the statement is set pin 0 on PORTC high(+5). First, PORTC is
called. This is followed by the bitwise OR( | ) and the equals sign(=).
//this turns pin C0 on and off
//turns C0 HIGH
PORTC |=(1<<0)
What is happening here is that every pin on PORTC is being compared to the state(s) proposed by the equasion in the parens. Inside the parentheses we see the shift left command (<<). The syntax
is PORTx |=(the value<<number positions). In this example, x = C and the value = 1 and the number positions shifted is 0 meaning pin0 on PORTC will go high. If the number positions shifted is (1<<3),
the bit is shifted three times, followed by zeros.

This is the syntax for delay or pause: delay_ms(millisecondsX)


//PAUSE 250 miliseconds
delay_ms(250)

This line is similar to the turning the pin high with a few differences. The literal reading of the statement is set pin 0 on PORTC LOW(+0). First, PORTC is called. This is followed by the bitwise AND( & ) and
the equals sign(=).
//turns C0 LOW
PORTC &= ~(1 << 0)
Whats with the Parenthases? its exactly the same as above. The code (1<<) says we are shifting 1 zero positions to the left. What makes the difference is that before the parenthases we are using
the Ones Compliment operator(~). This turns the 1 into its compliment, 0. Instead of saying shift the value of 0 zero positions, we are saying shift the compliment of 1 zero positions.

This is the syntax for delay or pause: delay_ms(millisecondsX)


//PAUSE 250 miliseconds
delay_ms(250)
}
This is the end of the while loop
}

Copyright 2017 Physical Computing - All Rights Reserved


Powered by WordPress & the Atahualpa Theme by BytesForAll. Discuss on our WP Forum