EEE305 Microcontroller Systems Lecture : Using Interrupts with Embedded C using PIC microcontrollers Teaching resources on on www.eej.ulst.ac.ukwww.eej.ulst.ac.uk.

Slides:



Advertisements
Similar presentations
Microprocessor and Microcontroller Based Systems Instructor: Eng.Moayed N. EL Mobaied The Islamic University of Gaza Faculty of Engineering Electrical.
Advertisements

Interrupts, Low Power Modes and Timer A (Chapters 6 & 8)
Input/Output (I/O) Ports and Interfacing
Input/Output Ports and Interfacing ELEC 330 Digital Systems Engineering Dr. Ron Hayne Images Courtesy of Ramesh Gaonkar and Delmar Learning.
ECE 447 Fall 2009 Lecture 9: TI MSP430 Interrupts & Low Power Modes.
Introduction to Interrupts, Timers and the Seven Segment Display
CSC Timers Since this is a microcontroller it mainly finds itself in embedded devices Quite often embedded devices need to synchronize events The.
6-1 I/O Methods I/O – Transfer of data between memory of the system and the I/O device Most devices operate asynchronously from the CPU Most methods involve.
Butterfly I/O Ports CS-212 Dick Steflik. I/O for our labs To get data into and out of our Butterfly its a little trickier than using printf and scanf.
Railway Foundation Electronic, Electrical and Processor Engineering.
EEE305 Microcontroller Systems Lecture 4: The PIC microprocessor and the Microchip C Compilers Teaching resources on on
Railway Foundation Electronic, Electrical and Processor Engineering.
EEE305 Microcontroller Systems Lecture 5B: Simple I/O Embedded C using PIC microcontrollers Teaching resources on on
INTERRUPTS PROGRAMMING
Timers and Interrupts Shivendu Bhushan Sonu Agarwal.
EEE527 Embedded Systems Lecture 8: Practical Interrupts Ian McCrumRoom 5B18, Tel: voice mail on 6 th ring Web site:
Department of Electronic & Electrical Engineering Embedded system Aims: Introduction to: Hardware. Software Ideas for projects ? Robotics/Control/Sensors.
UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)
Input/Output Ports and Interfacing
16F877A. Timer 0 The Timer0 module timer/counter has the following features: –8-bit timer/counter –Readable and writable –8-bit software programmable.
Interrupts, Counter and Timers. Interrupts (1) Interrupt-driven I/O uses the processor’s interrupt system to “interrupt” normal program flow to allow.
Timers The timers of the PIC16C7X microcontroller can be briefly described in only one sentence. There are three completely independent timers/counters.
PIC16F877 ISR Points to note Interrupt Triggers –Individual interrupt flag bits are set, regardless of the status of their corresponding mask bit, PEIE.
10/24/2015 Amrita Vishwa Vidyapeetham 1 Key Board & LED Interfacing.
MICROCONTROLLER SYSTEMS Part 1. Figure 1.1Elements of a digital controller CPU Central Processing Unit Input Peripherals Output Peripherals ROM Read Only.
Chapter 2 Introducing the PIC Mid-Range Family and the 16F84A The aims of this chapter are to introduce: The PIC mid-range family, in overview The overall.
ECS642U Embedded Systems Cyclic Execution and Polling William Marsh.
Accessing I/O Devices Processor Memory BUS I/O Device 1 I/O Device 2.
CSNB374: Microprocessor Systems Chapter 5: Procedures and Interrupts.
Timers and Interrupts Anurag Dwivedi. Let Us Revise.
Chapter 7 Larger Systems and the PIC 16F873A The aims of this chapter are to introduce: The architecture of the 16F873A microcontroller; The 16F873A memory.
1 © Unitec New Zealand Interrupt Lecture 6 Date: - 20 Sept, 2011 Embedded Hardware ETEC 6416.
Teachers Name : Suman Sarker Telecommunication Technology Subject Name : Microcontroller & Embedded System Subject Code : 6871 Semester : 7th Department.
1 Interrupts, Resets Today: First Hour: Interrupts –Section 5.2 of Huang’s Textbook –In-class Activity #1 Second Hour: More Interrupts Section 5.2 of Huang’s.
12/16/  List the elements of 8255A Programmable Peripheral Interface (PPI)  Explain its various operating modes  Develop a simple program to.
Interrupts  An interrupt is any service request that causes the CPU to stop its current execution stream and to execute an instruction stream that services.
Chapter 5 - Interrupts.
Embedded Systems Design 1 Lecture Set 8 MCS-51 Interrupts.
EE/CS-352: Embedded Microcontroller Systems Part V The 8051 Assembly Language Interrupts.
Working with Time: Interrupts, Counters and Timers
Lecture 3 CSE 341 – Microprocessors Lecture 3 Md. Omar Faruqe UB 1228
Polled IO versus Interrupt Driven IO
Interrupts ELEC 330 Digital Systems Engineering Dr. Ron Hayne
Interrupts ELEC 330 Digital Systems Engineering Dr. Ron Hayne Images Courtesy of Ramesh Gaonkar and Delmar Learning.
Aum Amriteswaryai Namah:. SUB ROUTINES Instead of repeating same program segments at all locations, they are written and stored separately; Each such.
80C51 Block Diagram 1. 80C51 Memory Memory The data width is 8 bits Registers are 8 bits Addresses are 8 bits – i.e. addresses for only 256.
Lecture – 8 Interrupt 1. Outline Introduction Handling interrupt Interrupt sources Switching interrupt Peripheral interrupts Using two interrupts Conclusions.
DEPARTMENT OF ELECTRONICS ENGINEERING V-SEMESTER MICROPROCESSOR & MICROCONTROLLER 1 CHAPTER NO microcontroller & programming.
Special Features. Device Configuration bits Revision Device Configuration bits Revision On-chip Power-on Reset (POR) Revision On-chip Power-on Reset (POR)
One more PIC18F252 example and revision for exam B222L Branislav Vuksanovic, UoP, ECE.
TIMERS.
Chapter 10 Interrupts. Basic Concepts in Interrupts  An interrupt is a communication process set up in a microprocessor or microcontroller in which:
Timer modules in PIC 16F877.  The PIC 16F877 basically has three independent timer modules,  denoted by the symbols, TIMER-0, TIMER1,andTIMER2. . These.
An Introduction to Embedded Systems, and the PIC Microcontroller Lecture 8: Interrupts The aims of this session are to explore the structure and application.
Microprocessor Systems Design I
CS501 Advanced Computer Architecture
Microprocessor Systems Design I
Timer and Interrupts.
Microprocessor Systems Design I
UNIT – Microcontroller.
Interrupts In 8085 and 8086.
Interrupts, Counter and Timers
Microprocessors Timers
Interrupts, Tasks and Timers
Interrupts Interrupt is a process where an external device can get the attention of the microprocessor. The process starts from the I/O device The process.
Lecture 9: TI MSP430 Interrupts & Low Power Modes
8051 Micro Controller.
PIC18 Interrupt Programming
The Programmable Peripheral Interface (8255A)
PIC18 Interrupt Programming
Presentation transcript:

EEE305 Microcontroller Systems Lecture : Using Interrupts with Embedded C using PIC microcontrollers Teaching resources on on My office 5B18, telephone My 1/ 27/03/14

Normal Code On startup the processor goes to address zero and starts executing the code it finds there. Usually a “goto” instruction is placed there, to an address in program memory. For example the simple C program #include #define _XTAL_FREQ #define LED0 RB0 void main(){do{RB0=0;RB0=1;}while(1);} Generates code; 27/03/14 2

Compiled Code F7 _main: F7 l5: F bcf3,5;RP0=0, select bank F bcf3,6;RP1=0, select bank F bcf6,0;volatile FA 1406 bsf6,0;volatile FB 2FF7 gotol FC __end_of_main: The intel HEX file below is produced by MPLAB – the programmer uses it. I have inserted extra spaces to show the fields of each line. Note 16 bits given as LSB:MSB This is known as little Endian (LSB stored first in memory). Also note that the HEX files has byte addresses twice that of the word addresses in the PIC : A12 8A11 FC2F 18 :10 0FEE F72F A12 8A11 B7 :02 0FFE 00 F72F CB  0FFE / 2 = 07F7 : FF  this is the end of file record. One Intel Hex format is shown below, a type of 00 is actual data, (see wikipedia for detail) 27/03/14 3

The complete truth 07FC __end_of__initialization: FC 0183 clrf FD 120A 118A 2FF7 ljmp_main;jump to C main() function As well as the code on the previous slide the compiler will insert a prologue and epilogue to your code, and place a jump instruction at address zero. The prologue is called the “startup code” and the epilogue is (usually) never reached. Some compilers give you the source code the startup routine sometimes called crt.as so you can modify it. The simple C code here needs no startup. : A12 8A11 FC2F 18 :10 0FEE F72F A12 8A11 B7 :02 0FFE 00 F72F CB At address zero we have the code below, SFR 0xA is the high bits of the PC : A12 8A11 FC2F 18  This represents PIC instructions of 120A, 118A 2FFC Table 15.2 allows us to convert these (bold text shows the field splits) 0000 : 120A;bcf 0xA,4; : 118A;bcf 0xA,3; : 2FFC;goto 7FC; /03/14 4

Interrupts When a machine powers up or is reset, it begins executing main code at address zero. Later on when certain other hardware events occur it begins executing at address 0x04 if certain bits are set in various registers. For this to work the PIC is designed so that it can stop what it is doing in main code, temporarily save any registers it is currently using, store the current value of the program counter and then do a “JUMP to address 0x04” After the code at address 0x04 is executed the reverse applies – the registers are restored and a “JUMP back to the saved address” undertaken. This allows the machine to appear to do two things at once! We call the “event” that causes the jump an INTERRUPT and we call the code executed an INTERRUPT SERVICE ROUTINE or ISR. The saving of registers is known as a CONTEXT SAVE and the exit from the ISR causes a CONTEXT RESTORE. 27/03/14 5

Sources of INTERRUPTs (DS 12.10) The PIC16F877 has 14 possible sources of interrupt. The INTCON register is full of flag bits and one of these gets set if an interrupt event occurs. There are other relevant registers as well – PIR1, PIR2 and PIE1 and PIE2 As well as the FLAG bits there are also ENABLE bits. Individual and global. For an interrupt to occur a global Interrupt enable must be set as well as an individual interrupt enable. The GIE bit is bit 7 of INTCON If both of these are not set you can still go and look at the FLAG bit in your main code but no actual INTERRUPT (jump to ISR) will occur. You have done this already when you checked RCIF or TXIF to use the serial port. When a “RETURN FROM INTERRUPT” is done the GIE bit gets set again so that interrupts get re-enabled. (On entry to an ISR the GIE is initially reset to avoid interrupting the interrupter! It is also reset on powerup. ) The ISR MUST clear the source of interrupt before exiting the ISR or it just gets interrupted again. Usually this means writing code to reset the appropriate IF flag. 27/03/14 6

Sources of Interrupt (DS Fig 12-9) The RCIF and TXIF are from the serial port, the ADIF is from the ADC. We shall only consider timer zero (T0IF) and signals affecting PORTB – there are two ways to generate an interrupt from PORTB. The peripherals on the left can be disabled using PEIE. 27/03/14 7

PORTB interrupts PORTB INT INTERRUPT (bit 0 only) An external interrupt on the RB0/INT pin is edge triggered, either rising, if bit INTEDG (OPTION_REG ) is set, or falling, if the INTEDG bit is clear. When a valid edge appears on the RB0/INT pin, flag bit INTF (INTCON ) is set. This interrupt can be disabled by clearing enable bit INTE (INTCON ). Flag bit INTF must be cleared in software in the ISR before re-enabling this interrupt. PORTB INTERRUPT ON CHANGE (bits 7 to 4 only) If set to be inputs, the input pins (of RB7:RB4) are compared with the old value latched on the last read of PORTB. The “mismatch” of RB7:RB4 are OR’ed together generates the RB Port Change Interrupt with flag bit RBIF (INTCON ). The interrupt can be enabled/disabled by setting/clearing enable bit RBIE (INTCON ) 27/03/14 8

Using Interrupt on Change This interrupt can wake the device from SLEEP. The user, in the Interrupt Service Routine, can clear the interrupt in the following manner: a) Any read or write of PORTB. This will end the mismatch condition. b) Clear flag bit RBIF. A mismatch condition will continue to set flag bit RBIF. Reading PORTB will end the mismatch condition and allow flag bit RBIF to be cleared. The interrupt-on-change feature is recommended for wake-up on key depression operation and operations where PORTB is only used for the interrupt-on-change feature. Polling of PORTB is not recommended while using the interrupt-on-change feature. This interrupt-on-mismatch feature, together with software configureable pull-ups on these four pins, allow easy interface to a keypad and make it possible for wake-up on key depression. 27/03/14 9

There are 3 ways of using a timer. 1Clear it, let it count and keep checking to see if it has reached a target value 2Load it with 0xFF-n and keep checking T0IF to see if it has set, polling from main code 3Set up an interrupt and write an ISR to handle the TMR0 interrupt (when T0IF sets) TMR0 INTERRUPT An overflow (FFh -> 00h) in the TMR0 register will set flag bit T0IF (INTCON ). The interrupt can be enabled/disabled by setting/clearing enable bit T0IE (INTCON ) You need the GIE set and you have to write a ‘0’ into T0IF to clear it before returning TMR0 27/03/

The INTERRUPT SFR (Special File Registers) INTCON is repeated through all banks, the OPTION register below is in banks 1 and 3 Whilst we use the PIR and PIE SFRs for most peripherals, PORTB and TMR0 are in the INTCON OPTION 27/03/

INTERRUPTs in C – see -> 5.9 INTERRUPTS The MPLAB XC8 compiler incorporates features allowing interrupts to be fully handled from C code. Interrupt functions are often called Interrupt service Routines, or ISRs Writing an Interrupt Service Routine The function qualifier interrupt may be applied to a C function definition so that it will be executed once the interrupt occurs. The compiler will process the interrupt function differently to any other functions, generating code to save and restore any registers used and return using a special instruction. An interrupt function must be declared as type void interrupt and may not have parameters. This is the only function prototype that makes sense for an interrupt function since they are never directly called in the source code. You should NOT call functions that might have been executing when the interrupt occurred – this is called RE-ENTRANCY and XC8 does not allow this, you code might work most of the time so this would be a really dodgy way to code. VERY hard to debug/test so be careful. Create two versions of every function needed in both domains … e.g func1() and func1_ISR() 27/03/

An example of an interrupt function int tick_count; void interrupt tc_int(void){ if (TMR0IE && TMR0IF) { TMR0IF=0; tick_count++; return; } // process other interrupt sources here, if required } // back in main you can poll tick_count. If it is 16 bits you might need to disable //interrupts before and after reading it. di() and ei() are used for this. Code generated by the compiler will be placed at the interrupt vector address which will execute this function after any context switch that is required Enabling Interrupts Two macros are available, once you have included, which control the masking of all available interrupts. These macros are ei(), which enable or unmask all interrupts, and di(), which disable or mask all interrupts. 27/03/

Using Timer0 to multiplex 7 seg displays If an interrupt happens 100 times a second then you switch between each of the 4 displays every interrupt and the human eye will think all 4 displays are active. Assume main code has a global variable called digit_now_on and the 4 digit number to be displayed stored in an unsigned char array called digits. Main clears digit_now_on on powerup and enables the timer INTERRUPT. Any time main code wants to display a number it splits it into 4 separate digits. DISABLES Interrupts, plants the 4 numbers into the array and RE-ENABLES Interrupts. The ISR reads digit_now_on, reads the appropriate (single) digit and outputs it. It then increments digit_now_on, putting it back to zero if it is 4 so it will cycle 0-3. The ISR needs to output 0001, 0010, 0100 or 1000 if digit_now_on is 0-3. This switches on the appropriate transistor that enables each seven seg display. (1<<digit_now_on) works! With care the same patterns can be used to read a keypad… saves i/o lines! 27/03/

Keypad For example the Rows are driven with a pattern so that one row is active and the other 3 are inactive. Normally it is handier to have the columns pulled up to 5 volts through a resistor, since PORTB for example has internal pullups (if enabled - see the RBPU bit 7 of the OPTION reg.) Then the ideal pattern to drive the ROWs is {1110,1101,1011,0111} repeating. Unfortunately we need {0001,0010,0100 and 1000} to drive the 7-segment drivers so we must add pull down resistors to the Column lines. Also it would be useful to use the interrupt on change facility on RB3-RB7 but it is inconvenient because the programmer uses RB6 and RB /03/

PICking the PIC PINs (sic) 27/03/

27/03/ (PORT B) 6 (PORT A) 4 (PORT D) 4 (PORT C) 2 (PORT D) 3 (PORT E) PICking the PIC PINs (sic) PicKIT Programmer Serial To PC

27/03/ PICking the PIC PINs (sic) PORTA – RA0 wired to Rth – the Thermistor – name it Rth PORTA – RA1,2,3,4 inputs from Keypad – name it COL1 to COL4 PORTB –RB0-RB3 Output scan pattern – name it ROW1 to ROW4 PORTB – RB6,RB7 used by PicKIT Programmer PORTC – RC0-RC3 wired to LCD data4 to data7 – name it LCD_d3 to LCD_d7 PORTC – RC4,RC5 wired to RS and EN on LCD, – name it LCD_RS and LCD_EN (plus ground R/WBAR ) PORTC – RC6 TxD serial data to PC PORTC – RC7 RxD serial data from PC PORTD – to seven segments (and decimal point)– name it a-g and dp PORTE – not used Leaves RA5, RB4 and RB5 and 3 bits of PORTE free. We can drive the Piezo buzzer to add audio feedback for keyclicks

Code We have code for ADC, serial and LCD 7-seg code will use variables digit_now_on and data[4]. Keypad routine will get read a keystroke if COL1 to COL4 are not you can readROW1 to ROW4 and combine with COL1 to COL4 to form a byte. Simplest to just have a switch statement. The aim is to make a thermometer, outputting on 7 segment displays, LCD displays and PC screens. A keypad can set an alarm threshold and when it is reached your code should do something – beep, flash 7-segs, print messages or whatever. The key approach is to start with small piece of workign code and evolve it testing at each stage to make sure you have not broken anything. 27/03/14 E305 19

Suggested development 1.Get the LCD working 2.Get the serial port working 3.Get the thermistor reading (0-1023) 4.Get a linearised temperature 15 to 36 degrees 5.Get the 7 segment going – one digit in main 6.Get the 7 segment going - 4 digits/interrupt 7.Get the keypad going 8.Get the Alarm going. 9.Write up as you go along, but put it all together and hand in. 10.Hand in After Easter – Friday at end of revision week. 27/03/14 E Week 10 Week 11 Week 12