us ‘C’ Data types in C Decision Control Instruction Loop Control Instruction KEIL IDE (Practical) using P89v51RD2 Microcontroller 2 Online Course | Embedded C Programming for c51 Microcontroller | L.Boaz | JPCAS | [email protected]
for a specific task Dedicated system Fixed into the chip of Software code Inject the code by Electrical Pulses Hardware – To store the code Software – To control the HW 3 Online Course | Embedded C Programming for c51 Microcontroller | L.Boaz | JPCAS | [email protected]
Rapid movement of code and data from external addresses Separate buses : Address, Data and Control. Can be used for any number of program execution such as word process, gaming, developing code. 4 Online Course | Embedded C Programming for c51 Microcontroller | L.Boaz | JPCAS | [email protected]
to store information temporarily 2. CPU also has what is called the ALU (Arithmetic Logical Unit) A. Arithmetic – Addition, Subtraction, Multiplication and Division B. Logical – AND (&), OR(|), NOT(~) 3. Every CPU has program Counter which is to point to the address of next instruction to be executed. 4. The function of Instruction decoder is to interpret the instruction fetched into the CPU. Just like a dictionary storing the meaning of each instruction and what steps the CPU should take upon receiving a given Instruction 6 Online Course | Embedded C Programming for c51 Microcontroller | L.Boaz | JPCAS | [email protected]
2. All the ALU and multiple programs can be performed 3. All the peripherals are connected from external 4. The code is stored on external Flash or ROM (Raspberry Pi) 5. It need more circuitry components 6. The Instruction is fetched from the flash and moves into the RAM to process by the CPU 7 Online Course | Embedded C Programming for c51 Microcontroller | L.Boaz | JPCAS | [email protected]
chip 2. Only one application software is burned inside the flash or ROM memory 3. Rapid movement of code and data from Internal memory 4. Designed for a dedicated task only – (CD ROM driver, mouse, Hard Disk Controller) 5. Small in size 8 Online Course | Embedded C Programming for c51 Microcontroller | L.Boaz | JPCAS | [email protected]
design an embedded system we need an IDE which is installed in a PC, Laptop or a mobile phone then a microcontroller with sufficient flash or ROM memory to store the hex or binary file and a cable to transfer the code from the computer to the microcontroller. 9 Online Course | Embedded C Programming for c51 Microcontroller | L.Boaz | JPCAS | [email protected]
•Debug – Error check Code Level •Linker – Combines all codes together and create a single executable file. •Compiler – Converts into final HEX Code Object Code •Through Code burner Software the HEX file is stored into the flash memory of the Microcontroller. •ISP or IAP Support Code Burning 10 Online Course | Embedded C Programming for c51 Microcontroller | L.Boaz | JPCAS | [email protected]
(IDE) is a software suite that consolidates basic tools required to write and test software. An IDE typically contains a code editor, a compiler or interpreter, and a debugger, accessed through a single graphical user interface (GUI). The user writes and edits source code in the code editor. The compiler translates the source code into a readable language that is executable for a computer. The debugger tests the software to solve any issues or bugs. An IDE can also contain features such as programmable editors, object and data modeling, unit testing, a source code library and build automation tools. An IDE's toolbar looks much like a word processor's toolbar. The toolbar facilitates color-based organization, source-code formatting, error diagnostics and reporting, and intelligent code completion. 11 Online Course | Embedded C Programming for c51 Microcontroller | L.Boaz | JPCAS | [email protected]
standardize the loading of Executable machine codes into a ROM chip. It is a binary file or machine code It is stored in Microcontroller’s Flash or ROM memory by code burner 12 Online Course | Embedded C Programming for c51 Microcontroller | L.Boaz | JPCAS | [email protected]
starts with a colon CC, the Count BYTE AAAA is for Address. 16 bit Address TT is for Type. DD – Information or code byte SS – Single Byte (Check Sum) All the data are displayed as Hexadecimal data in HEX file while you open upon your computer. For example if you see the numbers 0-9 it is not decimal but hexadecimal. So read by two numbers or a byte. 13 Online Course | Embedded C Programming for c51 Microcontroller | L.Boaz | JPCAS | [email protected]
DB Start Line No of Data Bytes 02 00 C7 Program Data Address Check Sum Type : 03 0000 00 0200C7 34 : 10 (16 Byte) 00AB 00 7589…..99 5E 14 Online Course | Embedded C Programming for c51 Microcontroller | L.Boaz | JPCAS | [email protected]
Nibble 4 Byte or 2 Nibble 8 Word or 2 Byte 16 Data or Datum No of Bits Binary 01011b Decimal 4 HEX 0xF4 15 Online Course | Embedded C Programming for c51 Microcontroller | L.Boaz | JPCAS | [email protected]
time consuming to develop the code Easier to modify and update Have function Libraries C code is portable to other microcontroller with little or no modification. Data types can save large amount of space Can be readable 17 Online Course | Embedded C Programming for c51 Microcontroller | L.Boaz | JPCAS | [email protected]
HEX files Data types or Keywords 32 Keywords available in C break case char continue default do else float for goto if int return short signed unsigned void while 18 Online Course | Embedded C Programming for c51 Microcontroller | L.Boaz | JPCAS | [email protected]
unsigned char is an 8 bit data type value from 0-255 or 00 to FF Use this instead of int or signed char to reduce the memory of hex file Uses Counter value 19 Online Course | Embedded C Programming for c51 Microcontroller | L.Boaz | JPCAS | [email protected]
is to represent – or + value D6-D7 -127 to +127 (7 bits for the magnitude and 8th bit is sign) Uses: To represent the temperature value 20 Online Course | Embedded C Programming for c51 Microcontroller | L.Boaz | JPCAS | [email protected]
65535 or 0000 to FFFFH Used to define 16 bit variable such as memory address Also used to set counter values It took two bytes of RAM D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0 Byte 2 Byte 1 Magnitude 21 Online Course | Embedded C Programming for c51 Microcontroller | L.Boaz | JPCAS | [email protected]
registers. It allows access to the single bits of the sfr (carry, sign, zero, auxiliary carry) In embedded C it is used to access a single pin from a whole port sbit sensor1 = P2^0 It allows access the pin no 1 from the PORT2 of the microcontroller. 23 Online Course | Embedded C Programming for c51 Microcontroller | L.Boaz | JPCAS | [email protected]
of instructions. Sequential flow is the most common and straight- forward, where programming statements are executed in the order that they are written - from top to bottom in a sequential manner. 27 Online Course | Embedded C Programming for c51 Microcontroller | L.Boaz | JPCAS | [email protected]
printf(“Plant Tree"); } // if-then if ( booleanExpression ) { true-block ; } The if statement is also known as a decision making statement, as it makes a decision on the basis of a given condition or expression. The block of code inside the if statement is executed is the condition evaluates to true. However, the code inside the curly braces is skipped if the condition evaluates to false, and the code after the if statement is executed. 29 Online Course | Embedded C Programming for c51 Microcontroller | L.Boaz | JPCAS | [email protected]
true-block ; } else { false-block ; } if (T >= 25) { printf(“Room Temperature is Hot"); printf("Keep it Cool\n"); } else { printf(“Room Temperature is normal\n");} In an if...else statement, if the code in the parenthesis of the if statement is true, the code inside its brackets is executed. But if the statement inside the parenthesis is false, all the code within the else statement's brackets is executed instead. 30 Online Course | Embedded C Programming for c51 Microcontroller | L.Boaz | JPCAS | [email protected]
block-1 ; } else if ( booleanExpr-2 ) { block-2 ; } else if ( booleanExpr-3 ) { block-3 ; } else if ( booleanExpr-4 ) { ...... } else { elseBlock ; } if (T >= 80) { printf(“Your room is under fire\n"); } else if (T >= 40) { printf(“Check your Electricity\n"); } else if (T >= 30) { printf(“Fan is ON and AC is OFF\n"); } else if (T >= 20) { printf(“AC and Fan is running\n"); } else { printf(“Cool and Breeze\n"); } The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. The nested if...else statement allows you to check for multiple test expressions and execute different codes for more than two conditions. 31 Online Course | Embedded C Programming for c51 Microcontroller | L.Boaz | JPCAS | [email protected]
In a switch-case statement, a break statement is needed for each of the cases. If break is missing, execution will flow through the following case. You can use either an int or char variable as the case-selector. 32 Online Course | Embedded C Programming for c51 Microcontroller | L.Boaz | JPCAS | [email protected]
from the number of choices is called as switch or switch-case-default. Break statement when used in a switch takes the control outside the switch. A float expression cannot be tested using switch. Faster than if statement switch(integer expression) { case constant 1: do this; break; case constant 2: do this; break; case constant 3: do this; break; default: do this; } 33 Online Course | Embedded C Programming for c51 Microcontroller | L.Boaz | JPCAS | [email protected]
while break continue This involves repeating some portion of the program either a specified number of times or until a particular condition is being satisfied. 34 Online Course | Embedded C Programming for c51 Microcontroller | L.Boaz | JPCAS | [email protected]
of times Tests the condition before executing any of the statements within the while loop. General form of while Intitialise loop counter; while(test loop counter using a condition) { Do this; And this; Increment loop counter; } 35 Online Course | Embedded C Programming for c51 Microcontroller | L.Boaz | JPCAS | [email protected]
is true); Tests the condition after having executed the statements within the loop. It would execute its statements at least once, even if the condition fails for the first time. 36 Online Course | Embedded C Programming for c51 Microcontroller | L.Boaz | JPCAS | [email protected]
about a loop in a single line. For(initialise counter; test counter; increment or decrement counter ) { do this; and this; } 37 Online Course | Embedded C Programming for c51 Microcontroller | L.Boaz | JPCAS | [email protected]
inside any loop, control automatically passes to the first statement after the loop. Break statement takes the control out of the case. Break is usually associated with an if statement. 38 Online Course | Embedded C Programming for c51 Microcontroller | L.Boaz | JPCAS | [email protected]
any loop, control automatically passes to the beginning of the loop. It is usually associated with an if statement. 39 Online Course | Embedded C Programming for c51 Microcontroller | L.Boaz | JPCAS | [email protected]
is used to transfer the normal flow of a program to the specified label in the program. { ……… go to label; ……… label: statement; } 40 Online Course | Embedded C Programming for c51 Microcontroller | L.Boaz | JPCAS | [email protected]
a list of elements of the same type, identified by a pair of square brackets [ ]. To use an array, you need to declare the array with 3 things: a name, a type and a dimension (or size, or length). To create an array, you need to known the length (or size) of the array in advance, and allocate accordingly. Once an array is created, its length is fixed and cannot be changed. At times, it is hard to ascertain the length of an array The syntax is: type arrayName[arraylength]; To store sensor values To create buffer for ADC 41 Online Course | Embedded C Programming for c51 Microcontroller | L.Boaz | JPCAS | [email protected]
to be used many times. Instead of re-writing the codes many times, it is better to put them into a "subroutine", and "call" this "subroutine" many time - for ease of maintenance and understanding. Subroutine is called function. The benefits of using functions are: Divide and conquer: construct the program from simple, small pieces or components. Modularize the program into self-contained tasks. Avoid repeating codes: It is easy to copy and paste, but hard to maintain and synchronize all the copies. Software Reuse: you can reuse the functions in other programs, by packaging them into library codes. Two parties are involved in using a function: a caller who calls the function, and the function called. The caller passes argument(s) to the function. The function receives these argument(s), performs the programmed operations within the function's body, and returns a piece of result back to the caller. 42 Online Course | Embedded C Programming for c51 Microcontroller | L.Boaz | JPCAS | [email protected]
development company. It provides several development tools like • IDE (Integrated Development environment) • Project Manager • Simulator • Debugger • C Cross Compiler , Cross Assembler, Locator/Linker 43 Online Course | Embedded C Programming for c51 Microcontroller | L.Boaz | JPCAS | [email protected]
or assembly. Compile or assemble source files. Correct errors in source files. Link object files from compiler and assembler. Test linked application. 44 Online Course | Embedded C Programming for c51 Microcontroller | L.Boaz | JPCAS | [email protected]