Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 44 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
44
Dung lượng
3,18 MB
Nội dung
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 Software development for an embedded system Software diagram C Programming for PIC Timer and interrupt 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ện Tử - ĐHBK Chapter 8/9/2016 1. Software diagram • Draw a software diagram: – – – – – Use a rectangle for a process Use a rounded rectangle for a terminator Use a diamond shape for a decision Use a parallelogram for data Use a rectangle with two vertical lines for predefine process N Y process terminator Bộ môn Kỹ Thuật Điện Tử - ĐHBK decision data predefine process Chapter 1. Software diagram • Example for software architecture diagram – Show the connections between hardware and software – Show the connections with other systems – Show the interface with users Examples Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 8/9/2016 1. Software diagram • Software block diagram ‐ Example Program flowchart Bộ mơn Kỹ Thuật Điện Tử - ĐHBK State machine diagram Chapter 5 1. Software diagram • Data flow diagram(DFD) Bộ mơn Kỹ Thuật Điện Tử - ĐHBK Chapter 8/9/2016 Group discussion • Discus about below software diagram: •Decision block must have YES/NO branches •A process block must have input and output START Init LCD Read temperature T Turn OFF heater T>T_đặt? Turn ON heater Oven control system Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter Team work • Draw a software diagram for your class project Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 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ện Tử - ĐHBK Chapter Outline 2.1 PIC16 C Getting Started ● Simple program and test circuit ● Variables, looping, and decisions ● SIREN program 2.2 PIC16 C Program Basics ● Variables ● Looping ● Decisions 2.3 PIC16 C Data Operations ● Variable types ● Floating point numbers ● Characters ● Assignment operators Bộ môn Kỹ Thuật Điện Tử - ĐHBK 2.4 PIC16 C Sequence Control ● While loops ● Break, continue, goto ● If, else, switch 2.5 PIC16 C Functions and Structure ● Program structure ● Functions, arguments ● Global and local variables 2.6 PIC16 C Input and Output ● RS232 serial data ● Serial LCD ● Calculator and keypad 2.7 PIC16 C More Data Types ● Arrays and strings ● Pointers and indirect addressing ● Enumeration Chapter 10 8/9/2016 2.1 PIC16 C Getting Started Microcontroller programs contain three main features: ● Sequences of instructions ● Conditional repetition of sequences ● Selection of alternative sequences Listing 2.1 A program to output a binary code /* Source code file: OUTNUM.C Author, date, version: MPB 11-7-07 V1.0 Program function: Outputs an 8-bit code Simulation circuit: OUTBYTE.DSN *******************************************************/ #include "16F877A.h" // MCU select void main() { output_D(255); } Bộ môn Kỹ Thuật Điện Tử - ĐHBK Figure 2.1 Bộ môn Kỹ Thuật Điện Tử - ĐHBK // Main block // Switch on outputs Chapter 11 MPLAB IDE Screenshot Chapter 12 8/9/2016 Figure 2.2 ISIS dialogue to attach program Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 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ện Tử - ĐHBK Chapter 14 8/9/2016 2.2 PIC16 C Program Basics PIC16 C Program Basics ● Variables ● Looping ● Decisions •The purpose of an embedded program is • to read in data or control inputs, • to process them and operate the outputs as required •The program for processing the data usually contains repetitive loops and conditional branching, which depends on an input or calculated value Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 15 Variables • Variables: – is a label attached to the memory location where the variable value is stored – automatically assigned to the next available location or locations (many variable types need more than 1 byte of memory). – must be declared at the start of the program block, so that the compiler can allocate a corresponding set of locations. – Only alphanumeric characters (a–z, A–Z, 0–9) can be used for variable names • Variable values – in decimal by default – in hexadecimal with the prefix 0x, for example, 0xFF • By default, the CCS compiler is not case sensitive, Bộ mơn Kỹ Thuật Điện Tử - ĐHBK Chapter 16 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; x=99; output_D(x); // Declare variable and type // Assign variable value // Display the value in binary } Bộ môn Kỹ Thuật Điện Tử - ĐHBK 17 Chapter 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) { output_D(x); x++; } // Loop endlessly // Display value // Increment value } Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 18 8/9/2016 Decision Making • The simplest way to illustrate basic decision making is to change an output depending on the state of an input. • Figure 2.4 show test circuit with input switch •The effect of the program is to switch on the output if the input is high •The switch needs to be closed before running to see this effect •The LED cannot be switched off again until the program is restarted Bộ môn Kỹ Thuật Điện Tử - ĐHBK Listing 2.4 Chapter 19 IF statement /* Source code file: IFIN.C Author, date, version: MPB 11-7-07 V1.0 Program function: Tests an input Simulation circuit: INBIT.DSN *******************************************************/ #include "16F877A.h" void main() { int x; output_D(0); // Declare test var // Clear all outputs while(1) // Loop always { x = input(PIN_C0); // Get input if(x==1)output_high(PIN_D0); // Change out } } Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 20 10 8/9/2016 Chapter 59 Chapter 60 30 8/9/2016 Chapter 61 Chapter 62 31 8/9/2016 Chapter 63 Chapter 64 32 8/9/2016 Chapter 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ện Tử - ĐHBK Chapter 66 33 8/9/2016 3. Timer and Interrupt • PIC16F877 has 14 interrupt sources No Interrupt Label Interrupt Source INT_EXT External interrupt detect on RB0 INT_RB Change on Port B detect INT_TIMER0 (INT_RTCC) Timer 0 overflow INT_TIMER1 Timer 1 overflow INT_TIMER2 Timer 2 overflow INT_CCP1 Timer 1 capture or compare detect INT_CCP2 Timer 2 capture or compare detect Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 67 3. Timer and Interrupt • PIC16F877 has 14 interrupt sources No Interrupt Label Interrupt Source INT_TBE USART transmit data done INT_RDA USART receive data ready 10 INT_SSP Serial data received at SPI or I2C 11 INT_BUSCOL I2C collision detected 12 INT_PSP Data ready at parallel serial port 13 INT_AD Analog‐to‐digital converter complete 14 INT_EEPROM EEPROM write completion Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 68 34 8/9/2016 C Interrupts • CCS C Interrupt Functions Bộ môn Kỹ Thuật Điện Tử - ĐHBK 69 Chapter Interrupt example C1 15pF C2 15pF 4MHz X1 U1 13 14 10 OSC1/CLKIN OSC2/CLKOUT MCLR/Vpp/THV RB0/INT RB1 RB2 RB3/PGM RB4 RB5 RB6/PGC RB7/PGD RA0/AN0 RA1/AN1 RA2/AN2/VREFRA3/AN3/VREF+ RA4/T0CKI RA5/AN4/SS RC0/T1OSO/T1CKI RC1/T1OSI/CCP2 RE0/AN5/RD RC2/CCP1 RE1/AN6/WR RC3/SCK/SCL RE2/AN7/CS RC4/SDI/SDA RC5/SDO RC6/TX/CK RC7/RX/DT RD0/PSP0 RD1/PSP1 RD2/PSP2 RD3/PSP3 RD4/PSP4 RD5/PSP5 RD6/PSP6 RD7/PSP7 PIC16F877 33 34 35 36 37 38 39 40 R1 10k 15 16 17 18 23 24 25 26 19 20 21 22 27 28 29 30 U2 10 RP1 20 19 18 17 16 15 14 13 12 11 220R Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 70 35 8/9/2016 Interrupt example #include " 16F877A.h " #use delay(clock = 4000000) #int_ext // Interrupt name void isrext() { output_D(255); delay_ms(1000); } void main() { int x; enable_interrupts(int_ext); enable_interrupts(global); ext_int_edge(H_TO_L); while(1) { output_D(x); x + + ; delay_ms(100); } } Bộ môn Kỹ Thuật Điện Tử - ĐHBK // Interrupt service routine // ISR action // Enable named interrupt // Enable all interrupts // Interrupt signal polarity // Foreground loop Chapter 71 Interrupt statements • #int_xxx – Tells the compiler that the code immediately following is the service routine for this particular interrupt – The interrupt name is preceded by #(hash) to mark the start of the ISR definition and to differentiate it from a standard function block – An interrupt name is defined for each interrupt source. • enable_interrupts(int_ext); – Enables the named interrupt by loading the necessary codes into the interrupt control registers Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 72 36 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ện Tử - ĐHBK Chapter 73 Class Assignment Write C code to enable an external interrupt at RB0 with the trigger low to high 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ện Tử - ĐHBK Chapter 74 37 8/9/2016 3. PIC16 Hardware Timers • The PIC 16F877 has three hardware timers built in: – Timer0: 8‐bit, originally called RTCC, the real‐time counter clock – Timer1: 16‐bit – Timer2: 8‐bit • The principal modes of operation – Counters for external events – Timers using the internal clock Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 75 Counter/Timer Operation • A counter/timer register consists of a set of bistable stages (flip‐flops) connected in cascade (8, 16, or 32 bits) Flag is set to when overflow (7 to 0) • An 8‐bit counter counts up from 0x00 to 0xFF Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 76 38 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ện Tử - ĐHBK 77 Chapter 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 timer Set_timer0(81); Get_timerx() Get the value of the timer int x = get_timer0(); Setup_ccpx(mode) Set PWM, capture, or compare mode setup_ccp1(ccp_pwm); Set_pwmx_duty(value) Set PWM duty cycle set_pwm1_duty(512); Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 78 39 8/9/2016 Timer Functions • Setup_timer_0(mode) – RTCC_INTERNAL, RTCC_EXT_L_TO_H or RTCC_EXT_H_TO_L – RTCC_DIV_2, RTCC_DIV_4, RTCC_DIV_8, RTCC_DIV_16, RTCC_DIV_32, RTCC_DIV_64, RTCC_DIV_128, RTCC_DIV_256 • Setup_timer_1(mode) – T1_DISABLED, T1_INTERNAL, T1_EXTERNAL, T1_EXTERNAL_SYNC – T1_CLK_OUT – T1_DIV_BY_1, T1_DIV_BY_2, T1_DIV_BY_4, T1_DIV_BY_8 • Example: – – – – setup_timer_0(RTCC_INTERNAL | RTCC_DIV_8) setup_timer_1 ( T1_DISABLED ); //disables timer1 setup_timer_1 ( T1_INTERNAL | T1_DIV_BY_4 ); setup_timer_1 ( T1_INTERNAL | T1_DIV_BY_8 ); Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 79 Timer Functions • setup_timer_2 (mode, period, postscale) – mode may be one of T2_DISABLED, T2_DIV_BY_1, T2_DIV_BY_4, T2_DIV_BY_16 – period is a int 0‐255 that determines when the clock value is reset, – postscale is a number 1‐16 that determines how many timer overflows before an interrupt: (1 means once, 2 means twice, and so on) Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 80 40 8/9/2016 Timer Functions • Create delay by timers N = 2n – (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ện Tử - ĐHBK 81 Chapter 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 82 41 8/9/2016 PWM mode • In Pulse Width Modulation mode, a CCP module can be used to generate a timed output signal. • This provides an output pulse waveform with an adjustable high (mark) period • CCS C functions: Set_pwm1_duty(value); Set_pwm2_duty(value); duty cycle = value / [ 4 * (PR2 +1 ) ] PR2 is the count value of timer 2 Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 83 PWM mode ‐ Example #include " 16F877A.h " void main() { setup_ccp1(ccp_pwm); // Select timer and mode set_pwm1_duty(500); // Set on time setup_timer_2(T2_DIV_BY_16,249,1); // Clock rate & output //period while(1) { } // Wait until reset } Produce an output at CCP1 of 250Hz (4ms) and a mark-space ratio of 50% with a 4-MHz MCU clock Explain? Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 84 42 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ện Tử - ĐHBK Chapter 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ện Tử - ĐHBK Chapter 86 43 8/9/2016 Exercise – Timer Interrupt 1) Calculate the frequency of the pulse on PIN B0 created by the program on the right figure, given that FOSC = 4MHz 2) Write the program that create a 2Hz pulse on PIN_B1, given that FOSC = 4MHz and dutycycle = 20% Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 87 Class Assignment Write a program for PIC16F877 to create rectangle pulses 2KHz at RB1 using interrupt Timer 0 Write a C program for PIC16F877 to create rectangle pulses 0.5KHz and 1KHz at RC0 and RC1 with duty cycle 50%. Use Timer1 interrupt with 4MHz OSC Write the program that create a 2Hz pulse on PIN_B1, given that FOSC = 4MHz and dutycycle = 20% Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 88 44