The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.

QUICK STUDY EMBEDDED SYSTEM APPLICATIONS

Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by chungpkb2011, 2023-07-22 21:12:55

QUICK STUDY EMBEDDED SYSTEM APPLICATIONS

QUICK STUDY EMBEDDED SYSTEM APPLICATIONS

QUICK STUDY EMBEDDED SYSTEM APPLICATIONS POLITEKNIK KOTA BHARU


Published and printed by: Department of Electrical Engineering Politeknik Kota Bharu KM. 24, Kok Lanas, 16450 Ketereh, Kelantan Quick Study Embedded System Application Edition issue 2023 © Nur Filzah binti Mohd Fauzey © Suriati binti Ramle All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, photocopying, recording or otherwise, without the prior written permission of the Department of Electrical Engineering, Kota Bharu Polytechnic. Quick Study Embedded System Application/ Nur Filzah binti Mohd Fauzey & Suriati binti Ramle


iii APPRECIATION Thanks to Allah s.w.t for the strength that has been given to us in preparing this book. We would also like to take this opportunity to thank the Head of the Electrical Engineering Department, Encik Kaswadi Bin Abdul Ghani for the encouragement given. The same speech was also addressed to our e-Learning Coordinator of the Electrical Engineering Department, Madam Sheilani binti Shaari who also has contributed ideas to further strengthen the writing of this book. Thanks also to those who have played a direct or indirect role in the success of this book. Lastly, special thank you to our beloved family who has given us lot of moral support. Nur Filzah binti Mohd Fauzey Suriati binti Ramle Jabatan Kejuruteraan ELektrik Politeknik Kota Bharu KM. 24, Kok Lanas, 16450 Ketereh, Kelantan


iv ABSTRACT This book is produced to help students who take the DEC40053 Embedded System Applications course. This course is compulsory for students under Diploma in Electronic Engineering (Communication) (DEP) program and is an elective course for Diploma in Electrical Engineering (DET) and Diploma in Electrical & Electronics Engineering (DEE) students. Data Communication & Networking Quick Study is a book produced to help students in reviewing lessons quickly and easily. It contains a collection of past examination questions from the June 2019 session until June 2018 complete with answers according to the scoring system used in the examination at the polytechnic. Data Communication & Networking Quick Study is written using English and align with the syllabus to make it easier for students to use it. In the writing of this book, each question is grouped according to the topics found in the Embedded System Applications course. The answers provided are written concisely so that it is easier for students to understand.


v ABOUT AUTHOR Nur Filzah binti Mohd Fauzey was born in Kota Bharu, Kelantan on 12 Februari 1983. Successfully obtained excellent results at the SPM level and then continued his studies Bachelor's Degree in Electrical Engineering, Universiti Malaysia Pahang (2007). She started her job as Assistant Engineer at Perunding IMS (M&E Consultant) pada 2007 – Jun 2008 and as a Assistant Manager at Telekom Berhad (2008- Feb 2009). She is currently working as a lecturer at the Department of Electrical Engineering, Kuala Terengganu Polytechnic on Februari 2009 and she is currently working as a lecturer at the Department of Electrical Engineering, Kota Bharu Kelantan Polytechnic. Suriati Binti Ramle was born in Pasir Mas, Kelantan on 12 December 1983. Successfully obtained excellent results at the SPM level and then continued her studies to the Matriculation level in Dungun, Terengganu (2001). Obtained a Bachelor Of Engineering (Electrical) with Honour from Tun Hussein Onn University (2006). And continued her studies on Master in Technical and Vocational Education at Tun Hussein Onn University (2008). She started her job as a lecturer in the Department of Electrical Engineering, Sultan Mizan Zainal Abidin Polytechnic (2009). She is currently working as a lecturer at the Department of Electrical Engineering, Kota Bharu Kelantan Polytechnic. She has been teaching in the field of computer for 13 years. In the field of administration, she was the Secretary for program DEE at the Department of Electrical Engineering of Kota Bharu Polytechnic. She is active and contributes a lot in the development programs of students, school children, government employees and even private agencies.


vi TABLE OF CONTENTS APPRECIATION iii ABSTRACT iv TABLE OF CONTENTS vi 1 INTRODUCTION TO EMBEDDED SYSTEM AND PIC PROGRAMMING IN C 1 At the end of the chapter, student should be able to; know about concept of embedded system, C programming for PIC microcontroller use in digital I/O and timer delay. 2 PIC TIMER PROGRAMMING IN C 6 At the end of the chapter, student should be able to; know about concept of embedded system, C programming for PIC microcontroller use in digital I/O and timer delay. 3 INTERRUPT PROGRAMMING IN C 15 At the end of the chapter, student should be able to; -know about Interrupts in the PIC and apply C program to interface PIC with external devices using interrupts 4 HARDWARE INTERFACING 25 At the end of the chapter, student should be able to; -Know about the features of external devices that can be interface PIC with PIC microcontroller,-Use the suitable internal features of PIC to interface with the external devices.-Design real time embedded system application base by uses and suitable internal features of PIC and program it using C program. 5. APPENDIX 34


1 1 INTRODUCTION TO EMBEDDED SYSTEM AND PIC PROGRAMMING IN C At the end of the chapter, student should be able to; know about concept of embedded system, C programming for PIC microcontroller use in digital I/O and timer delay. THEORY • PIC18F4550 has 33 input/output (I/O) pin divided into 5 ports. Refer Table 1 Table 1 • The rest of the pins are designated as Vdd (Vcc), Vss (GND), OSC1, OSC2, MCLR (reset) and another set of Vdd and Vss. • In addition as I/O ports, each ports have other functions such as ADC, timers, interrupts & serial communications pins. PIC registers can be access by 2 methods: 1. Byte-addressable Example : TRISB = 0b00001111; // send binary 00001111 // to PortB 2. Bit-addressable Example : TRISB5 = 0; // give logic LOW to bit 5 of TRISB RB5 = 1; // give logic HIGH to bit 5 of PORTB Quick Study -DEC40053 (Chapter 1)


2 QUESTION 1 List TWO (2) advantages and TWO (2) disadvantages of microcontroller. 4 marks QUESTION 2 Explain the function of TRISX, PORTX and LATX registers. 6 marks QUESTION 3 A programmer decides to toggle all LED of PORTC 45,000 times on a project board. Write the coding using C language for the task. (Initial value of PORTC = C7H). Assume no delay time is required. 10 marks QUESTION 4 List THREE (3) embedded systems attached to a smart home system. 3 marks QUESTION 5 A programmer decides to make a home security system using a touch card at pin RB3, a smoke sensor at pin RC5, a temperature sensor at pin RC6 while a magnetic solenoid, buzzer and LED at PORT D. Complete the pins input and output declaration for the program using bit addressable format in C language. 6 marks QUESTION 6 Figure 1 shows a block diagram of a washing machine. Implement a C language program to configure the input and output pins using byte addressable format. 6 marks Figure 6 Quick Study -DEC40053 (Chapter 1)


3 QUESTION 7 Describe microcontroller in embedded system. 3 marks QUESTION 8 Referring to the Diagram 8, calculate the value of registers PORTA, PORTD and PORTC after each instruction. 6 marks Diagram 8 QUESTION 9 Diagram 9 show the connection between LDR, LAMP and MOTOR with PIC16F/18F. Build a C Language program to configure the input output pins using byte addressable format 6 marks Diagram 9 Quick Study -DEC40053 (Chapter 1) Quick Study -DEC40053 (Chapter 1)


4 ANSWERS: QUESTION 1 (4m) Advantages i. Price cheap ii. Less power consumed. iii. Use less space iv. Build in ROM, RAM, I/O port and Timer etc. Disadvantages: i. Limited amount of memory, I/O ports and timers. ii. Cannot add any external memory, I/O ports and timers. iii. Can only do a specific and limited task. iv. Different manufacturer using different set of instruction QUESTION 2 (6m) TRISX register (data direction register) - to make the port as input or output port. (2m) PORTX register - reads/write the levels on the pins of the device and bidirectional. (2m) LATX register - (output latch) -useful for read-modify and write operations on the I/O port (2m) QUESTION 3 (10m) #include<p18f458.h> 1 void main (void) 1/2 { 1/2 unsigned int z; 1 TRISC=0; 1 for(z=0; z<=45000; z++) 2 { 1/2 PORTC=0x C7; // C7= 1100 0111 1 PORTC=0X 38; // ~C7= 0011 1000 @PORTC=~PORTC 1 } 1/2 while(1) } Quick Study -DEC40053 (Chapter 1)


5 QUESTION 4 (3m) • Room temperature control • Lighting control system • Surveillance security system • Auto gate control system (choose 3 only) QUESTION 5 (6m) TRISB3=1 TRISC5=1 TRISC6=1 TRISDx=0 (x can be 0-7) TRISDx=0 (x can be 0-7) TRISDx=0 (x can be 0-7) QUESTION 6 (6m) TRISA=0b00000100 TRISB=0b00100000 TRISC=0b10000000 QUESTION 7 (3m) Microcontroller is the data processing “brain” of an embedded system. (1.5m) It receive an input signal, process the information, and then produce output. (1.5m) QUESTION 8 (6m) PORTA=01010101 + 10101010 = 11111111 PORTC=01010101 OR 10101010 = 11111111 PORTD=01010101 AND 10101010 = 00000000 QUESTION 9 (6m) #include <xc.h> Void main (void) { TRISB = 0b00000100; 2m TRISC = 0b00000000; 2m TRISD = 0b00000000; 2m Quick Study -DEC40053 (Chapter 1)


6 2 PIC TIMER PROGRAMMING IN C At the end of the chapter, student should be able to; know about concept of embedded system, C programming for PIC microcontroller use in digital I/O and timer delay. THEORY The Timer0 module incorporates the following features: • Software selectable operation as a timer or counter in both 8-bit or 16-bit modes • Readable and writable registers • Dedicated 8-bit, software programmable prescaler • Selectable clock source (internal or external) • Edge select for external clock • Interrupt-on-overflow The T0CON register (Figure 6) controls all aspects of the module’s operation, including the prescale selection. It is both readable and writable. Timer0 can operate as either a timer or a counter; the mode is selected with the T0CS bit of the T0CON register. In Timer mode (T0CS = 0), the module increments on every clock by default unless a different prescaler value is selected. Timer0 incrementing is inhibited for two instruction cycles following a TMR0 register write. The user can work around this by adjusting the value written to the TMR0 register to compensate for the anticipated missing increments. The Counter mode is selected by setting the T0CS bit (= 1). In this mode, Timer0 increments either on every rising or falling edge of pin RA4/T0CKI. The incrementing edge is determined by the Timer0 Source Edge Select bit, T0SE of the T0CON register; clearing this bit selects the rising edge. Figure 1 below shows registers associated with Timer0. Figure 1 Quick Study -DEC40053 (Chapter 2)


7 Prescaler : - An 8-bit counter is available as a prescaler for the Timer0 module. The prescaler is not directly readable or writable; its value is set by the PSA and T0PS<2:0> bits of the T0CON register which determine the prescaler assignment and prescale ratio. Clearing the PSA bit assigns the prescaler to the Timer0 module. When the prescaler is assigned, prescale values from 1:2 through 1:256 in integer powerof-2 increments are selectable. Timer0 Interrupt :- The TMR0 interrupt is generated when the TMR0 register overflows from FFh to 00h in 8-bit mode, or from FFFFh to 0000h in 16-bit mode. This overflow sets the TMR0IF flag bit. The interrupt can be masked by clearing the TMR0IE bit of the INTCON register. Before re-enabling the interrupt, the TMR0IF bit must be cleared by software in the Interrupt Service Routine. Timer1/3/5 Operation :- The Timer1/3/5 module is a 16-bit incrementing counter which is accessed through the TMRxH:TMRxL register pair. Writes to TMRxH or TMRxL directly update the counter. When used with an internal clock source, the module is a timer and increments on every instruction cycle. When used with an external clock source, the module can be used as either a timer or counter and increments on every selected edge of the external source. Timer1/3/5 is enabled by configuring the TMRxON and TMRxGE bits in the TxCON and TxGCON registers,respectively. Quick Study -DEC40053 (Chapter 2)


8 QUESTION 1 Every timer needs a clock pulse to tick. Give TWO (2) examples of clock source and differentiate their function. 4marks QUESTION 2 Explain the operation modes of TIMER0. 6marks QUESTION 3 Given crystal oscillator frequency = 4MHz and the value of TMR0H:TMR0L = BAD7H. Referring to Appendix, calculate the time delay generated by TIMER0 if T0CON register are set to 0x05. Refer Appendix. 10marks QUESTION 4 Describe the function of TIMER register and list their associate special functions register SFR in TIMER0 for PIC16/ PIC18. 4marks QUESTION 5 Explain the function of TMR0IF in TIMER0 operation. 6marks QUESTION 6 Calculate the time delay, if PIC18 is supplied with a XTAL=20MHz and uses TIMER0 register shows Table 6. Refer Appendix. 10marks Table 6 Special Function Register Data T0C0N 0x05 TMR0L 0xF7 TMR0H 0xC2 Quick Study -DEC40053 (Chapter 2)


9 QUESTION 7 Give crystal oscillator frequency =20MHz and the time delay generated by Timer0 is 0.5ms. Referring to Appendix 1, calculate the value that need to be loaded into TMR0H and TMR0L register if T0CON register is set to 0x08H. 8marks QUESTION 8 By referring to Appendix 1, given a crystal oscillator frequency = 10 MHz and the value of TMR0H : TMR0L = ABCD hex. Calculate the time delay generated by TIMER0 if T0CON register is set to 0x02 10marks QUESTION 9 Refer Appendix 1. Given crystal oscillator frequency is 20MHz and the value of TMR0H:TMR0L equal to C2F7H. Calculate the time delay generated by Timer 0 if T0CON are set to 00000101. 10marks Quick Study -DEC40053 (Chapter 2)


10 ANSWERS: QUESTION 1 (4m) Internal clock source is fed into the timer by crystal frequency and used for time delay generation. It's called a timer. ……………………………2 m External clock sources is feed pulse through one of the PIC and this used for counter. ………………………………………………..2m QUESTION 2 (6m) Timer0 can be used as a counter or a timer to generate time delay Timer0 can be used to count in 8bit or 16bit mode For 8bit mode, it can count for maximum of 256 times (0-255) before overflow occurs. For 16bit mode, it can count for maximum of 256 times (0-65 535) before overflow occurs. When the timer reaches its maximum value of FFFFH (16bit) and FFH (8bit), it rolls over to 0000 and TMR0IF is set to 1. QUESTION 3 (10m) Time Delay = n xTcy N = max – BAD7+1 …........2m = FFFF – BAD7 + 1 …........1m = 4529H @ 17705D …......1m Fcy = 4m/4 x 1/64 = 15625Hz …..........2m Tcy = 1/15625 = 64us …..............2m Time Delay = 17705 x 64us = 1.13s …..........2m Quick Study -DEC40053 (Chapter 2)


11 QUESTION 4 (4m) Function Timer: 1. Generate time delay 2. As counter to count events happening outside the microcontroller SFRs 1. T0CON – timer 0 configuration 2. TMR0L – readable and writeable 8-bit register least byte 3. TMR0H – readable and writeable 8-bit register most byte QUESTION 5 (6m) When Timer0 reaches its maximum value of FF (8bit) and FFFF (16bit), it rolls over to 0000 and TMR0IF is set to 1. This process is called overflow If TMR0IF = 0, Timer0 did not overflow If TMR0IF = 1, Timer0 has overflowed Flag TMR0IF must be reset manually in program Flag TMR0IF is located in INTCON register. QUESTION 6 ( 10m ) No of bit,n = 16bit Prescaler = use prescaler Ration prescaler, ps = 1:64 TMR0 = 0xC2F7 = 49911 Quick Study -DEC40053 (Chapter 2)


12 Fcpu = fosc/4 = 20M/4 = 5Mhz Tcpu = 1/fcpu = 1/5Mhz = 0.2us No. Of ticks = 2n – TMR0 = 216 – 49911 = 65535 –49911 = 15625 Time delay = no.of ticks x Tcpu x ps = 15625 x 0.2u x 64 = 0.2 s QUESTION 7 (8m) T0CON = 0b 00001000 ………….1m Fcy = Fosc / 4 = 20M/4 = 5MHz ……………..1m Quick Study -DEC40053 (Chapter 4)


13 Tcy = 1/Fcy = 1/5M = 0.2us …………….1m Delay Time = n x Tcy 0.5m = n x (0.2u) n = 0.5m/0.2u = 2500 ticks ………………..1m n = FFFF – YYXX +1 = 65535 – YYXX +1 YYXX = 65536 – 2500 ……………1m = 63036 …………………..1m = F63C H TMR0H : 0xF6 TMR0L : 0x3C ………….2m QUESTION 8 (10m) Prescaler = 8 from T0CON = 00000010 Fcy = Fosc x 1 = 10 M x 1 4 prescaler 4 8 = 312500 Hz Tcy = 1 / 312500 = 3.2 µs QUESTION 9 (10m) Number of ticks, n = FFFFh – ABCDh + 1 = 5433h = 21555d ticks Delay time = n x Tcy = 21555 x 3.2 µ = 69 ms Quick Study -DEC40053 (Chapter 2)


14 T0CON = 00000101 => Prescalar = 16 bits, 64 Fcy = F 4 ÷ prescaler Fcy = 20M 4 ÷ 64 = 78125 Hz Tcy = 1/Fcy = 1/78125 = 1.28 x 10-5s Number of tick, n = Max no – TMR0H:TMR0L + 1 = FFFF – C2F7 + 1 = 3D09H = 15625 tick Time delay = n x Tcy = 15625 x 12.8µ = 0.2 saat Quick Study -DEC40053 (Chapter 2)


15 3 INTERRUPT PROGRAMMING IN C At the end of the chapter, student should be able to; -know about Interrupts in the PIC and apply C program to interface PIC with external devices using interrupts THEORY External Interrupts INT0, INT1 and INT2 The PIC18 has three external hardware interrupts. Pins RB0, RB1, and RB2, designated as INT0, INT1, and INT2 respectively, are used as external hardware interrupts. See Figure 4-1. Upon activation of these pins, the PIC18 gets interrupted in whatever it is doing and jumps to the interrupt vector to perform the Interrupt Service Routine. All three hardware interrupts are direct to vector table location at 0008H memory. Figure 4-1: External Hardware Interrupt Pins These interrupts are edge triggered interrupts i.e. triggered by either rising edge or by falling edge. The edge trigger bit is present in an INTCON2 register. INTCON2: Interrupt Control Register INTEDG0: External Interrupt 0 Edge select bit 1= Interrupt on Rising Edge 0= Interrupt on Falling Edge INTEDG1: External Interrupt 1 Edge select bit 1= Interrupt on Rising Edge 0= Interrupt on Falling Edge INTEDG2: External Interrupt 2 Edge select bit 1= Interrupt on Rising Edge 0= Interrupt on Falling Edge Quick Study -DEC40053 (Chapter 3)


16 INTCON GIE/GIEH: Global Interrupt Enable bit When IPEN is disabled, GIE enables all interrupts that sets When IPEN is enabled, GIEH enables all high priority interrupts that sets PEIE/GIEL: Peripheral Interrupt Enable bit When IPEN is disabled, PEIE enables all peripheral interrupts that sets When IPEN is enabled, GIEL enables all low priority interrupts INT0IE: INT0 External Interrupt Enable bit. Enables the INT0 external interrupt that sets INT0IF: INT0 External Interrupt Flag bit. Sets when INT0 external interrupt occur Figure 4-2: Programming External Interrupt TMR0 Interrupt In 8-bit mode (which is the default), an overflow in the TMR0 register (FFh --> 00h) will set flag bit, TMR0IF. In 16-bit mode, an overflow in the TMR0H:TMR0L register pair (FFFFh ---> 0000h) will set TMR0IF. The interrupt can be enabled/disabled by setting/clearing enable bit, TMR0IE of the INTCON register. Interrupt priority for Timer0 is determined by the value contained in the interrupt priority bit, TMR0IP of the INTCON2 register. Figure 4-3: Programming Timer Interrupt Quick Study -DEC40053 (Chapter 3)


17 QUESTION 1 Describe the interrupt and polling methods in a microcontroller. 4marks QUESTION 2 Explain the differences between Enable Bit and Flag Bit. Give ONE (1) example for each bit. 6marks QUESTION 3 You are given a project to connect a door sensor to pin RB0 and buzzer to pin RC7. Using INT0 external hardware interrupt, write a program using C language when every time the door is opened, the buzzer activates. Refer Appendix. 10marks QUESTION 4 List TWO(2) types of interrupt and TWO (2) source of interrupt in PIC microcontroller. 4marks QUESTION 5 Every interrupt source has three bits to control its operation. Interpret the functions of Interrupt Enable (IE) and Interrupt Flag (IF) bits for PIC18 interrupt operation. 6marks QUESTION 6 Referring to Diagram 6, RB0, RB1 and RB2 are used as interrupt pin. Write the instructions to configure RB0, RB1 and RB2 pin as an external interrupts, INT0, INT1 and INT2 in C language. Refer Appendix. 10marks Diagram 6 Quick Study -DEC40053 (Chapter 3)


18 QUESTION 7 List FOUR (4) sources of interrupt. 4marks QUESTION 8 Explain the function of Interrupt Enable (IE) and Interrupt Flag (IF) with suitable example. 4marks QUESTION 9 Refer to Appendix A1(c). As a programmer, you are given a project to connect a door sensor to pin RB1 and a buzzer to pin RD0. Using INT1 external hardware interrupt, write a C program to activate the buzzer each time the door is opened. 10marks QUESTION 10 Refer to Appendix and based on Diagram 10. By using external hardware interrupt, construct a C language program to make LED1 ON when SW1 is pressed. 10marks Diagram 10 Quick Study -DEC40053 (Chapter 3) Quick Study -DEC40053 (Chapter 3)


19 QUESTION 11 Write a program using timer 0 interrupt (16 bit mode, no prescalar) to create a square wave on pin RB7 while data from PORTC is being sent to PORTD. Use TMR0H:TMR0L = F63CH. Assume crystal oscillator are 10 MHZ. 10marks Quick Study -DEC40053 (Chapter 4)


20 ANSWER INTERRUPT: QUESTION 1 Interrupt is the signal send to the microcontroller to mark the event that requires immediate attention. ………………………………………………….2m Polling is a situation where microcontroller accesses at the exact time interval the external devices and gets the require information. ……………………………2m QUESTION 2 Enable bit – it can be used to enable / disable the related interrupt. ……………..1m When set to ‘1’ - it is enables the interrupt When set to ‘0’ - it is disable the interrupt ……………………….1m Example : GIE,INT0IF, PEIE ………………………………………1m Flag bit – It is set automatically by the related hardware when the interrupt condition occurs.1m When set to ‘1’ - the interrupt occurs …………………………..1m When set to ‘0’ - the interrupt does not occurs Example : TMR0IF , TMR1IF ……………………………………1m QUESTION 3 #include <xc.h> ..............0.5m void main (void) ................1m { TRISB0 = 1; .............1m TRISC7 =0; ...............1m INT0IF =0; ...............1m INT0IE =1; ….............1m GIE =1; …..................1m While(1) ..............0.5m } void interrupt isr (void) …...........1m { LATC7 = 1; .............1m Quick Study -DEC40053 (Chapter 3)


21 INT0IF =0; .................1m } QUESTION 4 Type of interrupt : 1. Internal / software ……………1m 2. External / Hardware ………….1m Interrupt source: ( choose 2 of answers) 1. Timer 2. CCP 3. Serial Communication 4. USART 5. ADC QUESTION 5 Interrupt enable is interrupt enable bit to enable or disable the interrupt source, Timer0. If TMR0IE = 0, interrupt Timer0 will be disable If TMR0IE = 1, interrupt Timer0 will be enable Interrupt flag is interrupt flag bit to indicated whether an interrupt has occurred. If TMR0IF =0 ; interrupt did not occurred If TNR0IF=1; interrupt has occurred QUESTION 6 TRISB0 =1; TRISB1=1; TRISB2=1; GIE=1; INT0IF =0; INT0IE=1; INT1IF=0; Quick Study -DEC40053 (Chapter 3)


22 INT1IE=1; INT2IF=0; INT2IE=1; QUESTION 7 i. Timers (Timer 0,1,2,3) ii. External hardware interrupt : in RB0 (INT0), RB1 (INT1), RB2 (INT2) iii. PORTB Change Interrupt : Pin RB4 – RB7 iv. Interrupt for serial communication USART v. CCP vi. ADC QUESTION 8 Interrupt Enable : to enable or disable the interrupt source ………1m Example : INT0IE =1; ……………………….1m Interrupt Flag : to indicate whether an interrupt has occurred ………..1m Example : INT0IF=0; ………………………1m QUESTION 9 #include <xc.h> void main (void) { TRISD0 = 0; TRISB1 = 1; GIE = 1; INT1IE =1; INT1IF =0; while(1) Quick Study -DEC40053 (Chapter 3)


23 RD0 = 0; } void INT1_ISR (void) { RD0 = 1; INT1IF = 0; } QUESTION 10 #include <xc.h> void main (void) { TRISC6 = 0; TRISB0 = 1; GIE = 1; INT0IE =1; INT0IF =0; while(1) RC6 = 0; } void INT0_ISR (void) { RC6 = 1; INT0IF = 0; } QUESTION 11 #include <xc.h> void main(void) { ADCON1=0x0E; Quick Study -DEC40053 (Chapter 3) Quick Study -DEC40053 (Chapter 3)


24 TRISB7=0; TRISC = 0xFF; TRISD = 0x00; T0CON = 0x80; //timer0, 16 bit, no prescalar TMR0H = 0xF6; TMR0L = 0x3C; GIE = 1; TMR0IF = 0; TMR0IE = 1; while (1) { PORTD=PORTC; } } void T0_ISR (void) { RB7=~RB7; TMR0H = 0xF6; TMR0L = 0x3C; TMR0IF = 0; } Quick Study -DEC40053 (Chapter 3)


25 Quick Study -DEC40053 (Chapter4) 4 HARDWARE INTERFACING At the end of the chapter, student should be able to; -Know about the features of external devices that can be interface PIC with PIC microcontroller,-Use the suitable internal features of PIC to interface with the external devices.- Design real time embedded system application base by uses and suitable internal features of PIC and program it using C program. THEORY Know external devices that can be interface with PIC microcontroller Introduction • PIC18F4550 come in different packages such as DIP (dual in-line package), QFP (quad flat package) and LLC (Leadless chip carrier) • They all have many pins that are dedicated to various functions such as I/O, ADC, timer and interrupts. • Figure 4(a) shows the pins for PIC18F4550. Figure 4(a)


26 The ADC peripheral of the PIC18 has the following characteristics: 1. It is a 10-bit ADC. 2. PIC18f4550 have 13 channels. 3. The converted output binary data is held by two special function register called ADRESL (AD result Low) and ADRESH (AD result High). 4. The A/D Control Register (ADCONx) used to configured conversion clock source, channel selection, port configuration control bits, ADC on/off, and start/end of conversion. 5. We have option of using VDD (VCC), the voltage source of the PIC18 chip itself as Vref or connecting it to an external voltage source for the Vref. It allows the implementation of differential Vref voltage using Vref(+) and Vref(-). 6. The conversion time is dictated by the Fosc . The conversion time cannot be shorter than 1.6ms. Some of the major characteristics of the ADC are as follows: 1. Resolution 2. Conversion time 3. Vref 4. Digital Data Output Where, Dout= digital value Vin= analog input voltage Step size = Vref/Resolution. PWM Pulse Width Modulation (PWM) is the one of the simple and most commonly used technique to produce analog voltages from digital signals. It has a wide variety of applications such as Digital to Analog Converter (DAC), DC Motor Speed Control, Sine Wave Inverters, Brightness control etc. A period of a pulse consists of an ON cycle and an OFF cycle. The fraction for which the signal is ON over a period is known as a duty cycle. Quick Study -DEC40053 (Chapter 4)


27 Quick Study -DEC40053 (Chapter 4)


28 QUESTION 1 Sketch a simple circuit connection to interface a microcontroller chip with ONE (1) switch (active low) to pin RC0 and ONE (1) LED (common cathode) to pin RD4. 5 marks QUESTION 2 Refer to Diagram 2, draw a 16X2 LCD interfacing with microcontroller circuit. The circuit must consist the connection of a variable resistor used to control contrast LCD and also the connection of power supply to the LCD. Use all PORTD pins for data interfacing and the register select (RS), Read/Write (R/W) and Enable (E) pins should be connect to RB0, RB1 and RB2. 5 marks Diagram 2 QUESTION 3 Two switches are connected to a microcontroller chip and a two way DC motor is interfaced to the microcontroller using a H Bridge chip. The DC motor will rotate according to the condition of both switches. Analyze and write a program using C language for the operation of the DC motor, refer to circuit Diagram 3 and Table 3 10 marks Table 3 Quick Study -DEC40053 (Chapter 3)


29 Diagram 3 QUESTION 4 The system to monitoring temperature will be design to install in Electrical Engineering Department. The system has ON/OFF switch as a manual reset switch, temperature sensor LM34/LM35 and zener diode LM 336-2.5 is connect parallel to the potentiometer 10K to regulate 2.5V at pin RA3. The system can be used to read and display the temperature using 8 LEDs to show temperature in binary numbers. The requirements for the design is when the analog signal (RAO) is sent to the microcontroller it will display the temperature value on the LEDS (PORTD). Design the schematic circuit of the system and build the program in C to function the system. [Given register control for ADC is setting as ADCONO=81H and ADCON1-C5H]. 20 marks QUESTION 5 Explain the function of Pulse Width Modulation (PWM) module in the PIC. 3 marks QUESTION 6 An 8 bit ADC has reference voltage, Vref-2.56V. Verify D0-D7 output if the analog input is 1.7V. 6 marks Quick Study -DEC40053 (Chapter 4)


30 QUESTION 7 Identify the minimum hardware connection for microcontroller to be operate using suitable circuit diagram. 3 marks QUESTION 8 Music toy using piezo buzzers were created to make a different tune for children. The tunes will be repeated using 500 Hz, 1 KHz and 3 KHz PWM frequencies. Diagram A4 (b) show the PWM timing requirement to control the sound of piezo buzzer. By using Frequency Oscillator = 8Mhz, and Prescale = 16, calculate the value of PR2 and duty cycle for each PWM frequency using the following formula: PR2 = [(Fosc / (FPWM X 4 X N))-1] 6 marks QUESTION 9 As a programmer, one of the assignment of your company is to design a robot's eye that will appear red at night time. However, during daylight the eye will appear green. Construct a suitable program that will fulfill the company requirement. Use any PORT in PIC16F/18F. The block diagram is given in Diagram 10. 6 marks Diagram 10 Quick Study -DEC40053 (Chapter 4) Quick Study -DEC40053 (Chapter 4)


31 ANSWERS: QUESTION 1 (5m) i. Switch circuit - 1 mark ii. LED circuit - 1 mark iii. Input pin labelling - 1 mark iv. Output pin labelling - 1 mark v. VCC and GND - 1 mark QUESTION 2 (5m) i. RS pin and RB0 connection and labelling 1 mark ii. R/W pin and RB1 connection and labelling – 1 mark iii. Enable pin and RB2 connection and labelling – 1 mark iv. Data pin and PORTD connection and labelling – 1 mark v. VDD and VSS – 1 mark QUESTION 3 (10m) #include <xc.h> void main (void) { TRISD = 0b00000011; // RD0 & RD1 as input, RD6 & RD7 as output Quick Study -DEC40053 (Chapter 3)


32 while (1) { if ( RD0==0 && RD1==1) { LATD6=0; // motor rotate clockwise LATD7=1; } else if (RD0==1 && RD1==0) { LATD6=1; // motor rotate anti clockwise LATD7=0; } else { LATD6=0; LATD7=0; } } } QUESTION 4 (20m) Total mark for diagram = 8 marks Quick Study -DEC40053 (Chapter 4)


33 //Program C- Read and Display Temperature System #include<p18f458.h> void main (void) { unsigned char L_Byte, H_Byte, Bin_Temp; TRISD=0; TRISAbits.TRISAO=1; TRISAbits.TRISA3-1; ADCONO=0 x81; ADCON1 = 0XC5: while (1) { MSDelay(1): ADCONbits.GO= 1; //start converting While(ADCONObits.DONE = =1); wait for EOC L Byte-ADRESL //save the low byte H Byte-ADRESH, L_Byte>>= 3 L_Byte&=0X3F; H_Byte<<=6; H_Byte&-0XC0; Bin_Temp = L_Byte || H_Byte; PORTD=Bán_Temp; } } Total mark for program = 12 marks QUESTION 5 (3m) • PWM is a technique used to generate analog output signal using digital signals. • It is commonly used to control average power delivered to a load, motor speed control, generating analog voltage levels and for generating analog waveforms. Quick Study -DEC40053 (Chapter 4)


34 QUESTION 6 (6m) Dout = Vin / step size Where Step size = Vref / Num. of steps = 2.56 / 28 = 0.01 So Dout = 1.7 / 0.01 = 170 Dout = 170 (decimal) Dout = 10101010 (binary) QUESTION 7 (3m) Power supply, oscillator crystal, RESET, QUESTION 8 (6m) PR2=[Fosc/(Fpwm x 4 x N)]-1 where N is Prescale (1:16) PR2 = [8MHz/(500x4x16)] -1 = 249 Duty Cycle = 75% x 249 = 186.75 PR2 = [8MHz/(1kx4x16)] -1 = 124 Duty Cycle = 75% x 124 = 93 PR2 = [8MHz/(3kx4x16)] -1 = 40.7 Duty Cycle = 75% x 40.7 = 30.52 RA0/AN0 2 RA1/AN1 3 RA2/AN2/VREF-/CVREF 4 RA3/AN3/VREF+ 5 RA4/T0CKI/C1OUT/RCV 6 RA5/AN4/SS/LVDIN/C2OUT 7 RA6/OSC2/CLKO 14 OSC1/CLKI 13 RB0/AN12/INT0/FLT0/SDI/SDA 33 RB1/AN10/INT1/SCK/SCL 34 RB2/AN8/INT2/VMO 35 RB3/AN9/CCP2/VPO 36 RB4/AN11/KBI0/CSSPP 37 RB5/KBI1/PGM 38 RB6/KBI2/PGC 39 RB7/KBI3/PGD 40 RC0/T1OSO/T1CKI 15 RC1/T1OSI/CCP2/UOE 16 RC2/CCP1/P1A 17 VUSB 18 RC4/D-/VM 23 RC5/D+/VP 24 RC6/TX/CK 25 RC7/RX/DT/SDO 26 RD0/SPP0 19 RD1/SPP1 20 RD2/SPP2 21 RD3/SPP3 22 RD4/SPP4 27 RD5/SPP5/P1B 28 RD6/SPP6/P1C 29 RD7/SPP7/P1D 30 RE0/AN5/CK1SPP 8 RE1/AN6/CK2SPP 9 RE2/AN7/OESPP 10 RE3/MCLR/VPP 1 U1 PIC18F4550 R1 10k C1 1nF C2 1nF X1 CRYSTAL Quick Study -DEC40053 (Chapter 4)


35 QUESTION 9 (6m) #include <xc.h> #define LEDred RB2 #define LED green RB4 #define LDR RD 0 void main (void) { TRISB = 0; TRISD=0B00000001; while(1) { if(LDR=0) { LEDred=1; LED green=0; } Else { LEDred=0; LEDgreen=1; }}} Quick Study -DEC40053 (Chapter 4) Quick Study -DEC40053 (Chapter 4)


36 APPENDIX


37 ADCON0 REGISTER


38 ADCON1 REGISTER ADCON2 REGISTER


39


Click to View FlipBook Version