Return to site

Proton Basic Serial Interrupt Protocol

broken image


To enable the interrupt function on PORTB internal pullups SET bit 3 (BSF INTCON, 3) to 1. This will generate an interrupt if any HIGH-LOW or LOW-HIGH transition occurs on PORTB PB4-PB7. Testing the state of bit 0 if 1 shows the interrupt occurred. No further interrupts can occur until bit 0 is cleared to 0. Using serial communication with interrupts. In our post on interrupts in 8051, we had mentioned that we would be covering serial interrupts (RI and TI) in this post. In the case of serial communication, the interrupts behave a little differently from what we had seen in the post on interrupts. Receiving data from the Serial Keypad controller. 2-9 Assembler coded Keypad decoder. 2-10 Section 3. Experimenting with Serial Eeproms. Giving the PIC a memory. 3-1 Microwire Interface principals. 3-3 SPI Interface principals. 3-4 12C Interface principals. 3-6 12C serial eeprom Interface principals. 3-8 Interfacing to the 24C32, 12C serial. Proton ide L298 dc motor pıc16f84a Proton ide PWM HPWM Digital To Analogue Conversion PIC16F877 Proton ide DS18S20 2X16 LCD PIC16F877 Proton ide DS275, the MAX232 PIC16F877 Proton ide 32.768Khz DS1307 PIC16F876A Proton ide I2C protocol. 24LC256 SDA and SCL. PIC16F876 Proton ide LCD, PIC16F877 LED PIC16F877 Proton ide LM35DZ 2X16 LCD PIC16F877A.

Proton Basic Serial Interrupt Protocol Number

I found that when combining the HW and SW serial implementations on a PIC®, it often occurs that you miss characters because of the rather strict timing restraints of the software implementation.
I have put together an interrupt based serial buffer that allows the HW serial port to be buffered automatically. This way the 'normal' processing can continue and the SW serial port is not affected.
This piece of code works on the 18F[24]5x as far as I have tested and does serial buffering. It operates in a circular array way.
When more then 65 characters are received, the first one will be overwritten and all 64 previous characters will be lost! So regular checking of the input buffer is still required.
It should be possible to implement this processing with buffers of sizes which are a power of 2 (2-4-8-16-32-64-128-256) without any problems. When using other sizes, makes sure that the generated code does not use the internal variables!!
Proton basic serial interrupt protocol diagram
Code is constructed in such way that no compiler variables are altered when in the interrupt routine. This way the code should not interfere with the 'normal' run of the program.
Example Code
'Hardware serial port buffer
DIM BUFFER[64] AS BYTE
DIM BUFH AS BYTE
DIM BUFT AS BYTE
DIM DUM AS BYTE
ON_INTERRUPT goto INTERRUPT_ROUTINE 'hardware interrupt
GOTO OVER_INTERRUPT
INTERRUPT_ROUTINE:
CONTEXT SAVE 'Only needed for 16F devices
' INTERRUPT HANDLING WHEN HW USART RECEIVES A CHARACTER
IF PIR1.5 = 1 THEN
CLEAR PIR1.5
BUFFER[BUFT] = RCREG
INC BUFT
BUFT = BUFT & 63
ENDIF
CONTEXT RESTORE
OVER_INTERRUPT:
Main_Loop:
GOSUB INIT
' Other things to do...
' -------------------------------------------------------------
INIT:
HRSIN {1,main_loop}, DUM 'initiate the Hardware Serial Port
'if I don't do this, it does not work
PIE1.5=1 'settings for 18F458
INTCON=$C0
RETURN
ReadChar:
WHILE BUFT = BUFH:WEND
DUM= BUFFER[BUFH]
INC BUFH
BUFH = BUFH & 63
RETURN
contributed by Tom Van Den Eede.
by Lewis Loflin

The Microchip PIC series of microcontrollers have several sources of hardware interrupts. These are RISC microcontrollers with 35 instructions. To configure interrupts or other hardware functions are setup by configuring various bits in selected registers, in particular here the INTCOM register. While I use the PIC16F84A as an example, this works exactly the same in the PIC16F628A, etc. Also see PIC16F628A interrupt map.

Here I'll start with hardware interrupts, which add incredible power to these low-cost devices. Fig. 1 illustrates the interrupt control structure in a PIC16F84A. By setting and clearing selected bits in the INTCON register we enable a specific device or any combination of them. In this series I'm interested in the PORTB pullups great for detecting switch closings, and TMR0 excellent is for setting up time delays. This is a general discussion of hardware interrupts.


Fig. 2

To enable any interrupt the SET bit 7 to 1 in the interrupt control register. (BSF INTCON, 7) Then it's a matter of selecting which specific interrupt we want. SET bit 5 for the timer 0 (TMR0) overflow. To determine if an overflow occurred with TMR0 check bit 2 for an overflow condition if SET or 1. This is often referred to as a 'flag' bit for testing. Once SET no further interrupts can occur until the bit is cleared to 0. (BCF INTCON, 2)

To enable the interrupt function on PORTB internal pullups SET bit 3 (BSF INTCON, 3) to 1. This will generate an interrupt if any HIGH-LOW or LOW-HIGH transition occurs on PORTB PB4-PB7. Testing the state of bit 0 if 1 shows the interrupt occurred. No further interrupts can occur until bit 0 is cleared to 0. (BCF INTCON, 0)

Great, we have the interrupts programmed and ready to go what's next? When an interrupt occurs the program counter executing the main program saves the PC in the STACK a FIFO buffer used for temporary storage then jumps to what is called an interrupt service routine.

Proton Basic Serial Interrupt Protocol Code

If one has done Arduino programming the above should be somewhat familiar. This is what really goes on 'under the hood' of microcontrollers. It differs between them but all have 'vectors' that point to particular memory locations during resets and interrupts.

Above is the Arduino version but in C++.

Proton Basic Serial Interrupt Protocol Diagram


Above is a sample ISR routine called when a switch was pressed on say PB7. The program counter after being saved on the STACK jumps to location 0x04 and executes the code that follows until it encounter a 'RETFIE' command then retrieves the original PC and resumes execution of whatever code it was processing at the time of the interrupt. Note: this code illustrates saving the W and STATUS registers on entering the ISR then restoring before the 'RETFIE' command. I'd highly advise doing that.

This ISR simply called a separate subroutine after loading 0x01 into the W register that simply inverted the state on a LED at PORTB PB0. It then called subroutine DELAY_100mS, then set to 0 in this case INTCON bit 0 interrupt flag, restored the original W and STATUS values.

That completes this basic introduction to the PIC16F84A interrupts. I use that part because it's easier to illustrate, but everything here appliesto other PICs that simply have more interrupt devices to choose from. I'll be looking direct examples of what we just discussed elsewhere.

  • YouTube videos:




  • Microchip PIC related videos:




broken image