{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
MAE 3780 M ECHATRONICS 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. Hex and Binary . Numbers can be expressed in many ways, but there are three ways we will focus on: decimal (standard) base-10, binary (base-2), and hex (base-16). Binary is effective because it is the language of the hardware (“machine language”): zeros and ones. You can write a number in binary using the prefix “0b”, like this: 15 = 0b0000 1111 1
Image of page 1

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

View Full Document Right Arrow Icon
MAE 3780 M ECHATRONICS FALL 2008
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern