|
Intro to Physical Computing Syllabus Research & Learning Other Class pages
ITP Help Pages |
AVRC Programming-ProgramAVR C PROGRAMMING WITH AVR STUDIO 4 - FIRST PROGRAM This is a simple blinking LED program. We will look at basic structure and cover specifics about the syntax. First let's 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
This include is inside the project folder and uses the "" around the name
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)
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
//these are the include files. They are outside the project folder
//this include is in your project folder
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); }; } Let's 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
These include files are in the WinAVR folder. If it's installed correctly, you should have nothing to worry about. //these are the include files. They are outside the project folder
This is another include file. Make sure it is in the project folder. //this include is in your project folder
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 0x00 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); What's with the Parenthases? it's 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 it's 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 } Compile and Program your code |