ATMEL%20Programming%20Guide%20v4.0

ATMEL%20Programming%20Guide%20v4.0 - M AE 3 780 M...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
MAE 3780 MECHATRONICS FALL 200 9 BEGINNER’S GUIDE TO C PROGRAMMING FOR THE ATMEL MEGA32 There are a few guidelines you will want to remember when programming the Atmel Mega32 microprocessor in the WinAVR development environment. These points are very important and can easily lead to programming errors, so they are stressed up front. Semicolons . When programming, all lines other than directives (statements beginning with #) and function blocks must be followed by a semicolon ( ; ) to end the line. Many people run into errors because they forget to put semicolons at the end of the lines that require them. Braces . In all functions, if statements longer than one line, and other loops such as for and while , you need to use braces { } to mark off the beginning and ending of that block of code. Remember that if you do not use braces, only one line of code is considered to be in the function/block/loop. Indentation . Although it does not affect how the code compiles in C, when you look at functions with nested loops and if statements, it will help visually if you use a format that is easy to decipher. Take notice of how indentation is used in the lab sample code, and try to follow that system. Use a different tab spacing for each nested loop, and close off that tab when the loop is closed. This way, both you and anyone helping with your code will easily be able to follow what is going on. Whitespace and new lines also do not affect how the code is compiled, so use this to your advantage to make long, complex lines of code easier to read. Thus, the following are the same: x=3; x = 3; x = 3 ; Commenting your code can be extremely helpful, both to describe what you are doing and to temporarily remove parts of the code from operation. When you wish to comment out a bit of code, you have two options. You can use two backslashes ( // ) at the end of a line to comment everything to the right of them (similar to the % in Matlab), or you can span multiple lines of text with a slash-star ( /* ) and star-slash ( */ ) at the beginning and end of text to be commented. Counting starts at 0 . In C, arrays and bytes are numbered starting at 0, not 1. For example, in the binary number z=0b00000001, which is simply 1 in decimal, the number 1 is stored in bit 0. If you tried to access this bit, you would use z.0, not z.1. The same goes for arrays and the pins on each port of the chip. To find what the decimal value of an 8-bit number is, you simply add the decimal value of each digit to get the total. If all values are ones, then you have 1+2+4+…+128=255. If all values are zero, then you have 0. In this way, 8-bit numbers range from 0 to 255, not 1 to 256.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/02/2010 for the course MAE 3780 taught by Professor Wickenhieser during the Fall '08 term at Cornell University (Engineering School).

Page1 / 22

ATMEL%20Programming%20Guide%20v4.0 - M AE 3 780 M...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online