Escolar Documentos
Profissional Documentos
Cultura Documentos
Physical Computing
It's all about people talking
Search
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.
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.
The overall structure of a simple piece of code will look like this:
Our include(s)
#include <include_file_name.h>
A main loop:
int main (void){
/*
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
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>
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 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.