We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39
Chapter 3
Programming Embedded Systems
Dr. Dereje Shiferaw Negash, AAIT
Contents • Embedded System Design Steps • Assembly programming of 8051 • Assembly programming of PIC • C programming
Dr. Dereje Shiferaw Negash, AAIT
Embedded System Design Steps • An embedded system contains hardware and software system • Design of an embedded system means – Specifying hardware required – Specifying sensors and actuators to be used – System integration and Writing the code required – Testing the system performance
Dr. Dereje Shiferaw Negash, AAIT
Embedded system design • Specifying the hardware required – This needs the understanding of the objective to be achieved or what the embedded system has to do – Hardware required means • The processor to be used • Required interface (external memory, I/O ports etc) • Power supply and clock • Reset circuits for the processor – In the selection, the cost and performance should be optimized Dr. Dereje Shiferaw Negash, AAIT Embedded system design • Specify sensors and actuators required – In design of embedded system, there will always be a control to be performed – Hence the external environment to be sensed and the output signal required needs to be identified – This will determine the sensors and actuators to be used • Sensors can have digital or analog output • Actuators can be driven by analog or digital signal • Required interface like ADC or DAC need to identified Dr. Dereje Shiferaw Negash, AAIT Design of Embedded systems • System integration and code writing – System integration is assembling the hardware parts • Connecting the microcontroller with the system inputs • Connecting external ports and memory • Connecting sensors and actuators required – Code writing • Program for an embedded system can be written in assembly language or HLP • Assembly language programming needs assembler while HLP needs compiler • The best practice is to use HLP languages and emulators to write the program and test its performance before downloading it to the processor Dr. Dereje Shiferaw Negash, AAIT Embedded system design • System integration and code writing – Code writing • There are various types of software used for developing embedded systems • Some of them even are used to test the hardware integration also (protus 8) • The most widely used ones are – Code composer studio (CSS) – Kiel uvision – Xilinx etc – EDSIM51
Dr. Dereje Shiferaw Negash, AAIT
Embedded system design • The code writing/ firm ware development/ consists of – Writing the source code using editor – Translating the code using assembler or compiler – Debugging for error – Simulating the system (microcontroller with peripherals , emulator, starter kits, mother board and daughter cards ) – Downloading the code to the target device or burning the EEPROM Dr. Dereje Shiferaw Negash, AAIT Embedded system design example • Autonomous guided vehicle – The vehicle is to navigate through an environment where there are obstacles – The vehicle has to avoid obstacles • Two bump detectors • Ultrasound detector • Two motor actuators • Encoder to calculate distance • Motor drive circuit – Microcontroller reads the environment and drives the actuator using a program Dr. Dereje Shiferaw Negash, AAIT Embedded system design example
Dr. Dereje Shiferaw Negash, AAIT
Connecting devices to parallel port • Connecting switch – Push button, toggle switch, micro switch – Micro switch
Dr. Dereje Shiferaw Negash, AAIT
Connecting devices to parallel port • LED – Used as indicators, display etc – Need a current of 10mA to 20mA
Dr. Dereje Shiferaw Negash, AAIT
Assembly programming • Program is a list of instructions combined together to do a specific task • Assembly programming using mnemonics – Has four columns • Label : name given to a program line • Mnemonic: specific code for the task to be performed • Operand : data needed to complete the instruction • Comment: clarification
Dr. Dereje Shiferaw Negash, AAIT
Assembly language programming • Assembler directives – Are instructions for the assembler which it uses to process the assembly language program – Some common directives are • Org :start address of a program • Equ :define a constant • End : end program • List :specify processor type • #include :include additional files
Dr. Dereje Shiferaw Negash, AAIT
Assembly language programming • Flow control – This is done by using subroutines and branching instructions – Branching instructions • Conditional or unconditional – JMP, JC, JNZ • Used to implement looping or branching – Subroutine • Is a small program which is repeatedly used in a program • It may be a delay or other special function code
Dr. Dereje Shiferaw Negash, AAIT
Assembly language programming • Writing delays – Load a register by a data value that can give a specified time delay – Decrement the data – Loop back • Table look up – This is accessing a list of data which is stored in a separate memory location – This is used when displaying or manipulating a list of data values Dr. Dereje Shiferaw Negash, AAIT C Programming • Developed in Bell laboratories in 1970s • Of higher level languages, C is the closest to assembly languages – bit manipulation instructions – pointers (indirect addressing) • Most microcontrollers have available C compilers • Writing in C simplifies code development for large projects. • A c program contains – Comment – Declaration – Main directive – Program statementsDr. Dereje Shiferaw Negash, AAIT C programming • Like most high level languages, C is a modular programming language (but NOT an object oriented language) • Each task can be encapsulated as a function. • Entire program is encapsulated in “main” function
Dr. Dereje Shiferaw Negash, AAIT
C programming • Simple program /* simple program #include <c8051F020.h> /* define SFR Unsigned char counter Void main (void) { P1=0; Counter=1; P1=counter; }
Dr. Dereje Shiferaw Negash, AAIT
C programming • Key words – Are reserved words which are recognized by the compiler – This words should not be used for other variables or functions – Used for • Data types • Flow control • Other uses
Dr. Dereje Shiferaw Negash, AAIT
Variables in C • All variables must be declared at top of program, before the first statement. • Declaration includes type and list of variables. Example: void main (void) { int var, tmp; • Types: – int (16-bits in our compiler) – char (8-bits) – short (16-bits) – long (32-bits) – sbit (1-bit) – others that we will discuss later
Dr. Dereje Shiferaw Negash, AAIT
Data types in C
Dr. Dereje Shiferaw Negash, AAIT
Statements in c • When we write a program in c, we write statements to accomplish a task • The most important statement is assignment • Assignment statement:
variable = constant or expression or variable
examples: upper = 60;
I = I + 5; J = I; Dr. Dereje Shiferaw Negash, AAIT Statements in c • The other types of statements are arithmetic or logic operations • Operations and symbols in c – Arithmetic: +, -, *, / – Relational comparisons: >, >=, <, <= – Equality comparisons: ==, != – Logical operators: && (and), || (or) – Increment and decrement: ++, -- – Example: if (x != y) && (c == b) { a=c + d*b; a++; } Dr. Dereje Shiferaw Negash, AAIT Comparison of Assembly and C • Example program for adding two 16 bit numbers #include <c8051f020.h> $INCLUDE (C8051F020.inc) XL equ 0x78 void main (void) { XH equ 0x79 int x, y, z; //16-bit variables YL equ 0x7A z = x + y; YH equ 0x7B ljmp Main } ; Disable watchdog timer Main: mov 0xFF, #0DEh mov 0xFF, #0ADh mov a, XL add a, YL mov XL, a mov a, XH addc a, YH mov XH, a nop end
Dr. Dereje Shiferaw Negash, AAIT
Reserved words for program flow
Dr. Dereje Shiferaw Negash, AAIT
Data storage class
Dr. Dereje Shiferaw Negash, AAIT
Pre-processor directives
Dr. Dereje Shiferaw Negash, AAIT
Loop statements • While loop:
while (condition) { statements }
while condition is true, execute statements
if there is only one statement, we can lose the {}
Example: while (1) ; // loop forever
Dr. Dereje Shiferaw Negash, AAIT
Loop statements • For statement:
for (initialization; condition; increment)
{statements}
initialization done before statement is executed
condition is tested, if true, execute statements
do increment step and go back and test condition again
repeat last two steps until condition is not true
Dr. Dereje Shiferaw Negash, AAIT
Decision statements • If if (condition1) {statements1} else if (condition2) {statements2} … else {statements}
Dr. Dereje Shiferaw Negash, AAIT
Decision statements • switch (expression) { case const-expr: statements case const-expr: statements default: statements }
Dr. Dereje Shiferaw Negash, AAIT
Arrays in C temp_array[0] • Useful for storing data temp_array[1] temp_array[2] temp_array[3] ... type arr_name[dimension] temp_array[253] temp_array[254] temp_array[255] char temp_array[256]
Array elements are stored in adjacent locations in memory.
Dr. Dereje Shiferaw Negash, AAIT
Example C program to Switch/LED • #include <c8051F020.h> • #pragma SRC // Need this to generate .SRC file • void PORT_Init (void); • char Get_SW(void) { • #pragma ASM • mov a, P3 • anl a, #80h ; mask all but P3.7 • mov R7, a ; function value (char) returned in R7 • #pragma ENDASM • } • void Set_LED(void) { • #pragma ASM • setb P1.6 • #pragma ENDASM • } • void Clr_LED(void) { • #pragma ASM • clr P1.6 • #pragma ENDASM • } • void PORT_Init (void){ XBR2 = 0x40; // Enable crossbar and enable P1.6 (LED) as push-pull output} • P1MDOUT |= 0x40; // enable P1.6 (LED) as push-pull output • } • void main(void) { • PORT_Init(); • while (1) • if (Get_SW()) Set_LED(); • else Clr_LED(); • }
Dr. Dereje Shiferaw Negash, AAIT
Functions • The basis for modular structured programming in C.
return-type function-name(argument declarations)
{ declarations and statements }
Dr. Dereje Shiferaw Negash, AAIT
Example – no return value or arguments void SYSCLK_Init (void) { // Delay counter int i; // Start external oscillator with 22.1184MHz crystal OSCXCN = 0x67; // Wait for XTLVLD blanking interval (>1ms) for (i = 0; i < 256; i++) ; // Wait for crystal osc. to settle while (!(OSCXCN & 0x80)) ; // Select external oscillator as SYSCLK OSCICN = 0x88; }
Dr. Dereje Shiferaw Negash, AAIT
Example – with arguments void Timer3_Init (int counts) { // Stop timer, clear TF3, use SYSCLK as timebase TMR3CN = 0x02; // Init reload value TMR3RL = -counts; // Set to reload immediately TMR3 = 0xffff; // Disable interrupts EIE2 &= ~0x01; // Start timer TMR3CN |= 0x04; }
Dr. Dereje Shiferaw Negash, AAIT
Example – with return value char ascii_conv (char num) { return num + 30; }
Dr. Dereje Shiferaw Negash, AAIT
Header Files • Use to define global constants and variables // 16-bit SFR Definitions for 'F02x sfr16 TMR3RL = 0x92; // Timer3 reload value sfr16 TMR3 = 0x94; // Timer3 counter sfr16 ADC0 = 0xbe; // ADC0 data sfr16 DAC0 = 0xd2; // DAC data sfr16 DAC1 = 0xd5; // Global CONSTANTS #define SYSCLK 22118400 // SYSCLK frequency in Hz sbit LED = P1^6; // LED='1' means ON sbit SW1 = P3^7; // SW1='0' means switch pressed #define MAX_DAC ((1<<12)-1) // Maximum value of the DAC register 12 bits #define MAX_INTEGRAL (1L<<24) // Maximum value of the integral // Function PROTOTYPES void SYSCLK_Init (void); void PORT_Init (void); void ADC0_Init (void); void DAC_Init (void); void Timer3_Init (int counts); void ADC0_ISR (void); Dr. Dereje Shiferaw Negash, AAIT