sssssssss
DESCRIPTION
ssssssfgvgvh bjbhvvjbTRANSCRIPT
Session2 – CECE Electronics Team
Session 1.
Assignment.
I/O Ports.
Key pa d i n te r fa c in g .
L c d i n te r fa c in g .
Interrupt.
Libraries Routines:
Keypad_Init : Initializes port for working with keypad.
char keypadPort at PORTD; //Definition
Keypad_Init(); // Call function
Keypad_Key_Press : Reads the key from keypad when key
gets pressed.
Return: The code of a pressed key (1..16).
If no key is pressed, returns 0.
char kp;
kp = Keypad_Key_Press();
Keypad_Key_Click : the function waits until some key is
pressed and released. When released, the function returns
1 to 16, depending on the key.
char kp;
kp = Keypad_Key_Click();
do
{
kp=0;
kp= Keypad_Key_Click();
}while (!kp);
switch (kp) {
case 1: kp = 49; break; // 1
………… and so on
Lcd_Init : Initializes Lcd module.
Lcd_Out : Prints text on Lcd starting from specified position.
Both string variables and literals can be passed as a text.
Lcd_Init();
Lcd_Out(1, 3, "Hello!");
Lcd_Chr : Prints character on Lcd at specified position. Both
variables and literals can be passed as a character.
Lcd_Chr(2, 3, 'i');
Keypad with LCD Dual Segment
Definition:
An interrupt is an asynchronous signal indicate for an event which
needs processor’s attention immediately regardless to the instruction it
executes at this moment.
It’s like a Doorbell.
CECE Academic Team
Interrupt Flag (IF):
A bit that is automatically set if the interrupt source (event)
happens.
Global Interrupt Enable (GIE):
Enables (if set) all un-masked interrupts (interrupts with
IE=1) or disables (if cleared) all interrupts.
Interrupt Enable (IE):
If the GIE was ‘1’, this bit forces the CPU to respond to the
interrupt signal when IF=1 when the waited event happens.
Interrupt service routine (ISR):
The code which the CPU jumps to when an interrupt
happens.
Actually, the CPU will automatically jumps to the (interrupt
vector) 0004h.
From there the code should be written to force the MCU to
jump to the ISR address.
When the event (interrupt source) happens, the following
steps happen:
The corresponding interrupt flag (IF) will equal ‘1’.
If the interrupt enable (IE)was ‘1’, and the global interrupt
enable (GIE)was ‘1’ also, the GIE is cleared by hardware to
disable any further interrupt . To avoid responding to further
interrupts.
The return address is pushed into the stack and the PC is
loaded with 0004h (the interrupt vector).
In the ISR, you can determine the source of interrupt by
polling the interrupt flag bits and do the required action for
it.
(Context Switching) You have to save key registers values
before interrupt has happened e.g. W register and STATUS
register. This has to be implemented in software
If you use high level language the compiler will do it for you.
In the end of the ISR, you’ve to clear the IF in software then
set the GIE bit in the end of the ISR code.
Many peripherals use interrupts when finishing its job (ex.
USART, ADC, Timers …), or when there is a problem needs
to be configured (ex. EEPROM Write Complete ).
Here, we’ll talk about the simplest type of interrupt, and the
rest of interrupt sources should be handled in the same
way.
Very important :
If you configure the MCU to respond to more than one
interrupt source at the same time, to determine which one
has happened, pull their flags in the ISR (check which one
equals 1).
RB0/INT
The RB0/INT pin is a single external interrupt source.
When the waited edge (+ve or –ve edge as selected)
happens on RB0 pin, the interrupt signal is generated.
It can be configured to react to signal raising edge or signal
falling edge.
Steps :
Configure the interrupt source to work as you want, here :
falling or rising edge.
Set the GIE bit.
Set the IE bit of the interrupt.
Write the ISR.
CECEAcademic Team
void main( ) {
INTCON.GIE=1;
INTCON.INTE=1;
trisc=0;
portc=0;
while(1);
}
void interrupt( ) {
INTCON.GIE=0;
INTCON.INTF=0;
portc.f0=1;
delay_ms(500);
portc.f0=0;
delay_ms(500)
}