Embedded System Design - Chapter 5: Software development for an embedded system
1. Software diagram
• Software diagram is a diagram help software
developers and program managers to interpret
software application relationships, actions, and processes.
– Software architecture diagram: describes the high level
structure of a software system
– Program flowchart: demonstrates how a program works within a system
– Data flow diagram: illustrates the flow of information in a process
– State machine diagram: presents decision flows of a state machine
• Software diagram is a diagram help software
developers and program managers to interpret
software application relationships, actions, and processes.
– Software architecture diagram: describes the high level
structure of a software system
– Program flowchart: demonstrates how a program works within a system
– Data flow diagram: illustrates the flow of information in a process
– State machine diagram: presents decision flows of a state machine
Bạn đang xem 20 trang mẫu của tài liệu "Embedded System Design - Chapter 5: Software development for an embedded system", để tải tài liệu gốc về máy hãy click vào nút Download ở trên.
File đính kèm:
- embedded_system_design_chapter_5_software_development_for_an.pdf
Nội dung text: Embedded System Design - Chapter 5: Software development for an embedded system
- 8/9/2016 ĐẠI HỌC QUỐC GIA TP.HỒ CHÍ MINH TRƯỜNG ĐẠI HỌC BÁCH KHOA KHOA ĐIỆN-ĐIỆN TỬ BỘ MÔN KỸ THUẬT ĐIỆN TỬ Embedded System Design 5. Software development for an embedded system 1. Software diagram 2. C Programming for PIC 3. Timer and interrupt 1 1. Software diagram • Software diagram is a diagram help software developers and program managers to interpret software application relationships, actions, and processes. – Software architecture diagram: describes the high level structure of a software system – Program flowchart: demonstrates how a program works within a system – Data flow diagram: illustrates the flow of information in a process – State machine diagram: presents decision flows of a state machine Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 5 2 1
- 8/9/2016 1. Software diagram • Software block diagram ‐ Example Program flowchart State machine diagram Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 5 5 1. Software diagram • Data flow diagram(DFD) Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 5 6 3
- 8/9/2016 2. C programming for PIC • Reference – Martin Bates, “Programming 8‐bit PIC Microcontrollers in C”, Newnes, 2008 • Many C compilers for PIC: – MikroC (www.mikroe.com) – PICC18 (www.htsoft.com) – MPLAB C18, C30 (www.microchip.com) – CCS C (www.microchipc.com/reviews/CCS_C/) Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 5 9 Outline 2.1 PIC16 C Getting Started 2.4 PIC16 C Sequence Control ● Simple program and test circuit ● While loops ● Variables, looping, and decisions ● Break, continue, goto ● If, else, switch ● SIREN program 2.5 PIC16 C Functions and Structure 2.2 PIC16 C Program Basics ● Program structure ● Variables ● Functions, arguments ● Looping ● Global and local variables ● Decisions 2.6 PIC16 C Input and Output ● RS232 serial data 2.3 PIC16 C Data Operations ● Serial LCD ● Variable types ● Calculator and keypad ● Floating point numbers 2.7 PIC16 C More Data Types ● Characters ● Arrays and strings ● Assignment operators ● Pointers and indirect addressing ● Enumeration Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 5 10 5
- 8/9/2016 Figure 2.2 ISIS dialogue to attach program Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 5 13 Setup IO Ports • Port modes – #use fast_io(port): leaves the state of the port the same unless re‐configured – #use fixed_io(port_outputs=pin, pin): permanently sets up the data direction register for the port – #use standard_io(port): default for configuring the port every time it’s used • Set directions – set_tris_a(value); – value = get_tris_a(); • Read / write IO ports – value = input_A(); // read value from a port – value = input(PIN_B0); // read value from a pin – output_A(value); // write value to a port – output_high(pin); //set an output to logic 1 – output_low(pin); //set an output to logic 0 – Output_bit(pin, value); // Outputs the specified value to the specified I/O pin Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 5 14 7
- 8/9/2016 Listing 2.2 Variables /* Source code file: VARI.C Author, date, version: MPB 11-7-07 V1.0 Program function: Outputs an 8-bit variable Simulation circuit: OUTBYTE.DSN / #include "16F877A.h" void main() { int x; // Declare variable and type x=99; // Assign variable value output_D(x); // Display the value in binary } Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 5 17 Looping • Most real‐time applications need to execute continuously until the processor is turned off or reset. • In C this can be implemented as a “ while ” loop, as in Listing 2.3 . /* Source code file: ENDLESS.C Author, date, version: MPB 11-7-07 V1.0 Program function: Outputs variable count Simulation circuit: OUTBYTE.DSN / #include "16F877A.h” void main() { int x; // Declare variable while(1) // Loop endlessly { output_D(x); // Display value x++; // Increment value } } Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 5 18 9
- 8/9/2016 Loop Control • The program can be simplified by combining the input function with the condition statement as follows: – if (input(PIN_C0)) output_high(PIN_D0); • The conditional sequence can also be selected by a while condition. • In Program WHILOOP.C ( Listing 2.5 ) – the input is tested in the loop condition statement and the output flashed on and off while the switch is open (input high). – If the switch is closed, the flash loop is not executed and the LED is switched off. Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 5 21 Listing 2.5 Conditional loop /* Source code file: WHILOOP.C Author, date, version: MPB 11-7-07 V1.0 Program function: Input controls output loop Simulation circuit: INBIT.DSN / #include "16F877A.h" #use delay (clock=1000000) // MCU clock = 1MHz void main(){ while(1) { while(input(PIN_C0)); // Repeat while switch open { output_high(PIN_D0); delay_ms(300); // Delay 0.3s output_low(PIN_D0); delay_ms(500); // Delay 0.5s } output_low(PIN_D0); // Switch off LED } } Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 5 22 11
- 8/9/2016 SIREN Program • A program combining some of these basic features is shown in SIREN.C ( Listing 2.7 ). • This program outputs to a sounder rather than an LED, operating at a higher frequency. • The output is generated when the switch is closed (input C0 low). • The delay picks up the incrementing value of “ step” giving a longer pulse each time the for loop is executed. • This causes a burst of 255 pulses of increasing length (reducing frequency), repeating while the input is on. • Note that 255 is the maximum value allowed for “ step, ” as it is an 8‐bit variable. Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 5 25 Listing 2.7 Siren Program /* Source code file: SIREN.C Author, date, version: MPB 11-7-07 V1.0 Program function: Outputs a siren sound Simulation circuit: INBIT.DSN / #include "16F877A.h" #use delay (clock=1000000) void main() { int step; while(1) { while(!input(PIN_C0)) // loop while switch ON { for(step=0;step<255;step++) // Loop control { output_high(PIN_D0); // Sound sequence delay_us(step); output_low(PIN_D0); delay_us(step); } } } } Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 5 26 13
- 8/9/2016 Table 2.1 A basic set of CCS C components Compiler Directives #include source files Include another source code or header file #use functions(parameters) Include library functions C Blocks main(condition) {statements } Main program block while(condition) {statements } Conditional loop if(condition) {statements } Conditional sequence for(condition) {statements } Preset loop C Functions delay_ms(nnn) Delay in milliseconds delay_us(nnn) Delay in microseconds output_x(n) Output 8-bit code at Port X output_high(PIN_nn) Set output bit high output_low(PIN_nn) Set output bit low input(PIN_nn) Get input Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 5 29 2.3 PIC16 C Data Operations ● Variable types ● Floating point numbers ● Characters ● Assignment operators A main function of any computer program is to carry out calculations and other forms of data processing. Data structures are made up of different types of numerical and character variables, and a range of arithmetical and logical operations are needed. Microcontroller programs do not generally need to process large volumes of data, but processing speed is often important. Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 5 30 15
- 8/9/2016 Figure 2.5 Variable Types Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 5 33 Table 2.5 ASCII Codes Low High Bits Bits 0010 0011 0100 0101 0110 0111 0000 Space 0 @ P ` p 0001 ! 1 A Q a q 0010 " 2 B R b r 0011 # 3 C S c s 0100 $ 4 D T d t 0101 % 5 E U e u 0110 & 6 F V f v 0111 ' 7 G W g w 1000 ( 8 H X h x 1001 ) 9 I Y i y 1010 * : J Z j z 1011 + ; K [ k { 1100 , N ^ n ~ 1111 / ? O _ o Del Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 5 34 17
- 8/9/2016 Table 2.7:Conditional Operators Operation Symbol EXAMPLE Equal to == if(a == 0) b=b+5; Not equal to != if(a != 1) b=b+4; Greater than > if(a > 2) b=b+3; Less than = if(a >= 4) b=b+1; Less than or equal to <= if(a <= 5) b=b+0; Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 5 37 2.4 PIC16 C Sequence Control ● While loops ● Break, continue, goto ● If, else, switch • Conditional branching operations are a basic feature of any program. • These must be properly organized so that the program structure is maintained and confusion avoided. • The program then is easy to understand and more readily modified and upgraded. Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 5 38 19
- 8/9/2016 Listing 2.9 DOWHILE.C contains both types of ‘while’ loop // DOWHILE.C // Comparison of WHILE and DO WHILE loops #include "16F877A.H” main() { int outbyte1=0; int outbyte2=0; int count; count=0; // This loop is not while (count!=0) // executed { output_C(outbyte1); outbyte1++; count ; } count=0; // This loop is do // executed { output_C(outbyte2); outbyte2++; count ; } while (count!=0); while(1){}; } Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 5 41 Break, Continue, and Goto It may sometimes be necessary to break the execution of a loop or block in the middle of its sequence ( Figure 2.8 ). The block must be exited in an orderly way, and it is useful to have the option of restarting the block (continue) or proceeding to the next one (break). Occasionally, an unconditional jump may be needed, but this should be regarded as a last resort, as it tends to threaten the program stability. It is achieved by assigning a label to the jump destination and executing a goto label. The use of these control statements is illustrated in Listing 2.10 . The events that trigger break and continue are asynchronous (independent of the program timing) inputs from external switches, which allows the counting loop to be quit or restarted at any time. Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 5 42 21
- 8/9/2016 Figure 2.9 Comparison of If and If Else YES NO Condition Condition True? True? NO YES If If Else block block block Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 5 45 Figure 2.10 Switch case branching structure Test Variable Value = 1? YES Procedure 1 NO Value = 2? YES Procedure 2 NO Value = 3? YES Procedure 3 NO Value = n? YES Procedure n NO Default Procedure Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 5 46 23
- 8/9/2016 2.5 PIC16 C Functions and Structure ● Program structure ● Functions, arguments ● Global and local variables • The structure of a C program is created using functions ( Figure 2.11 ). This is a block of code written and executed as a self‐contained process, receiving the required parameters (data to be processed) from the calling function and returning results to it. Main() is the primary function in all C programs, within which the rest of the program is constructed. • When running on a PC, main() is called by the operating system, and control is returned to the OS when the C program is terminated. In the microcontroller, main() is simply used to indicate the start of the main control sequence, and more care needs to be taken in terminating the program. Bộ •mônNormally, Kỹ Thuật Đ theiệnT programử - ĐHBK runs in a continuousChapter 5 loop, but if not, the final 49 Figure 2.11 Hierarchical C program structure LEVEL 0 LEVEL 1 LEVEL 2 void fun1() { statements Main() } { statements fun1() statements statements void fun2(arg) void fun3 { { statements statements statements fun2(arg) fun3 statements } } return(val) } Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 5 50 25
- 8/9/2016 Global and Local Variables • Now, assume that we wish to pass a value to the function for local. The simplest way is to define it as a global variable, which makes it available throughout the program. In program FUNC2.C, Listing 2.13 , the variable count, holding the delay count, hence the delay time, is global. • If there is no significant restriction on program memory, global variables may be used. However, microcontrollers, by definition, have limited memory, so it is desirable to use local variables whenever possible within the user functions. This is because local variables exist only during function execution, and the locations used for them are freed up on completion of function call. This can be confirmed by watching the values of C program variables when the program is executed in simulation mode —the local ones become undefined once the relevant function block is terminated. • If only global variables are used and the functions do not return results to the calling block, they become procedures. Program FUNC3.C, Listing 2.14 , shows how local variables are used. Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 5 53 Listing 2.13 Passing a parameter to the function // FUNC2.C #include "16F877A.H” int8 outbyte=1; // Declare global variables int16 n,count; void out() // Function block { while (outbyte!=0) { output_C(outbyte); outbyte++; for(n=1;n<count;n++); } } main() { count=2000; out(); // Call function while(1); } Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 5 54 27
- 8/9/2016 Chapter 5 57 2.8 PIC16 C Compiler Directives ● Include and use directives ● Header file listing and directives Compiler directives are typically used at the top of the program to set up compiler options, control project components, define constant labels, and so on before the main program is created. They are preceded by the hash symbol to distinguish them from other types of statements and do not have a semicolon to end the line. Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 5 58 29
- 8/9/2016 Chapter 5 61 Chapter 5 62 31
- 8/9/2016 Chapter 5 65 3. Timer and Interrupt • Interrupt: – Interrupts allow an external event to initiate a control sequence that takes priority over the current MCU activity. – The interrupt service routine (ISR) carries out some operation associated with the port or internal device that requested the interrupt. – Interrupts are frequently used with hardware timers, which provide delays, timed intervals and measurement. Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 5 66 33
- 8/9/2016 C Interrupts • CCS C Interrupt Functions Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 5 69 Interrupt example C1 U1 4MHz 13 33 OSC1/CLKIN RB0/INT 14 34 OSC2/CLKOUT RB1 R1 15pF 1 35 MCLR/Vpp/THV RB2 10k 36 X1 RB3/PGM 2 37 RA0/AN0 RB4 C2 3 38 RA1/AN1 RB5 4 39 RA2/AN2/VREF- RB6/PGC 5 40 RA3/AN3/VREF+ RB7/PGD 15pF 6 RA4/T0CKI 7 15 RA5/AN4/SS RC0/T1OSO/T1CKI 16 RC1/T1OSI/CCP2 8 17 RE0/AN5/RD RC2/CCP1 9 18 RE1/AN6/WR RC3/SCK/SCL 10 23 RE2/AN7/CS RC4/SDI/SDA 24 RC5/SDO 25 RC6/TX/CK 26 RC7/RX/DT U2 RP1 19 1 20 9 RD0/PSP0 20 2 19 8 RD1/PSP1 21 3 18 7 RD2/PSP2 22 4 17 6 RD3/PSP3 27 5 16 5 RD4/PSP4 28 6 15 4 RD5/PSP5 29 7 14 3 RD6/PSP6 30 8 13 2 RD7/PSP7 9 12 PIC16F877 10 11 1 220R Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 5 70 35
- 8/9/2016 Interrupt statements • enable_interrupts(level); – Enables the interrupt at the given level. – Examples: enable_interrupts(GLOBAL); enable_interrupts(INT_TIMER0); enable_interrupts(INT_TIMER1); • Disable_interrupts(level) – Disable interrupt at the given level • ext_int_edge(H_TO_L); – Enables the edge on which the edge interrupt should trigger. This can be either rising or falling edge. Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 5 73 Class Assignment 1. Write C code to enable an external interrupt at RB0 with the trigger low to high 2. Write a C program to control 4 output pins RC0‐RC3 from 4 input pins RB4‐RB7 using port interrupt. Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 5 74 37
- 8/9/2016 Counter/Timer Operation • Timer0 is an 8‐bit register that can count pulses at RA4; for this purpose, the input is called T0CKI (Timer0 clock input). • Timer1 is a 16‐bit register that can count up to 0xFFFF (65,535) connected to RC0 (T1CKI) . • The count can be recorded at any chosen point in time; alternatively, an interrupt can be generated on overflow to notify the processor that the maximum count has been exceeded. • If the register is preloaded with a suitable value, the interrupt occurs after a known count. • Timer0 has a prescaler that divides by up to 128; • Timer1 has one that divides by 2, 4, or 8; • Timer2 has a prescaler and postscaler that divide by up to 16. Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 5 77 Timer Functions Functions Description Examples Setup_timer_x Setup timer setup_timer_0(RTCC_INTE RNAL | RTCC_DIV_8); Set_timerx(value) Set the value of the Set_timer0(81); timer Get_timerx() Get the value of the int x = get_timer0(); timer Setup_ccpx(mode) Set PWM, capture, or setup_ccp1(ccp_pwm); compare mode Set_pwmx_duty(value) Set PWM duty cycle set_pwm1_duty(512); Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 5 78 39
- 8/9/2016 Timer Functions • Create delay by timers n N = 2 –(T * Fclock) / (4*Prescaler) . N: the count number . n: bit number of timer (Timer 0 & 2: n=8, Timer1: n = 16) . T: delay time . Fclock: frequency of crystal . Prescaler: prescaler number Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 5 81 The program that carries out the function of a counting circuit counts from 00 to 19 and displays on two 7-seg leds connected to port C Chapter 5 82 41
- 8/9/2016 Compare Mode • Generate a timed output in conjunction with Timer1. • The 16‐bit CCPR register is preloaded with a set value, which is continuously compared with the Timer1 count. When the count matches the CCPR value, the output pin toggles and a CCP interrupt is generated. If this operation is repeated, an interrupt and output change with a known period can be obtained. Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 5 85 Capture mode • The CCP pin is set to input and monitored for a change of state. • When a rising or falling edge (selectable) is detected, the timer register is cleared to 0 and starts counting at the internal clock rate. • When the next active edge is detected at the input, the timer register value is copied to the CCP register. The count therefore corresponds to the period of the input signal. With a 1MHz instruction clock, the count is in microseconds Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 5 86 43