程序代写代做代考 chain compiler 18/09/2014

18/09/2014
Embedded Systems Design ELEN90066
Lecture 16 C Code chain
David Jahshan
Notes from last lecture
• Remember to incrementally assemble your PCB
• Remember to limit the current on your power supply
• Soldering is all about heat transfer, make sure you have solder on the tip
Example Exam Questions
• Referring to page 8 of the ATMEGA16 datasheet (it will be in the exam), describe what happens when the following instructions are executed by the ATMEGA16 (Only obvious instructions might be asked, and not their short names. For example you will see Load Immediate Register not ldi.)
• What is the most important part of soldering
Tool Chain • You write .c and .h files
• Compiler converts to .hex file which is uploaded to MCU.
http://www.avrfreaks.net/wiki/index.php/Documentation:AVR_GCC/AVR_GCC_Tool_Collection
Challenge
• For those who know c, here is a challenge to keep you entertained:
• Rewrite the following code without a conditional:
• unsigned char z,y;
• if(z<10) y++; C Code Basics Preprocessor • Madeupofinstructionstothepreprocessor – #include • Indicate what other files to include • #include “game_console.h” //relative to this file • #include //relative to lib search path
– #define
• Macros starts with #define terminates with newline
• #define NAME REPLACE
• Inserts REPLACE for every NAME
• Never put code into #defines only items that will be precomputed
1

18/09/2014
C Code Basics variables
• Variables
– Must declare variables before use
– Must be very careful, about types use only what you
need.
• unsigned char, uint8. 0 to 255
• char -127 to 127
• unsigned int, uint16. 0 to 65535 • int -32767 to 32767
– If you use an int rather than a char you waste 1 instruction each time you use it.
– If it does not need to be negative it should be unsigned.
C Code Basic Instructions
• Instructions
– Lines end with a ;
– You can assign a value to another variable
• variable = 10;
• variable = variable + 10; or variable +=10; • variable++; variable–;
• Comments
/*Multiline Comment */ //Short line comment code;//after code comment
C Code Basics Conditional
• Conditional
– if (condition)
{
}
else
{
}
if (condition); else if (condition);
C Code Basics Conditions
• if(variable1 == variable2)
– Remember == is not = an assignment.
• == equal, != no equal, > greater than, >= greater or equal to, < less than, <= less than or equal to. C Code Basic Loops • while(condition){} • while(1){} //infinite loop useful for mcu code – You never want to end code execution, what will it do? • for(x=0;x<10;x++){} – x++ do loop then increment – ++x increment then do loop C Code Basics Case statements • Case statements – switch (variable) { case ‘A’: instruction1; instructionn; break; //if you leave out break you also do ‘B’ case’B’: instruction1; break; default: instruction1; } 2 18/09/2014 C Code Basics Functions • int main(void) { return(1); } • Afunctioncanbecreatedby – returntype functionname(inputtype variable){} • Ifthefunctiondoesnotappearinthefileand before it is called, it needs to be declared. – int functionname(int, int); C Code Basics Arrays • To declare – unsigned char my_array[] = {1,23,17,4,-5,100}; – unsigned char my_array[10]; – There is no guarantee this array is 10 zeros, can be any value. • To access – variable = my_array[5]; Good code design for MCU • Using malloc, calloc are dangerous, no operating system to catch faults. • Always use the smallest data type it is faster • Remember how many cycle each operation takes (floating point divides are massive 250 clock cycles per divide) • Plan carefully RAM usage, you don’t have much RAM (1kbyte) Next Lecture • How to write an interrupt service routine • What does the code compile down to • How to write clean code 3