
Introduction
In Flowcode, each chip has a β€definition fileβ€ (the β€FCDβ€ file) which describes many aspects of the chip such as its pin-out, the code it uses to perform ADC conversions, how many PWM outputs it has, etc. The FCD file also includes information in the interrupts.
By default, Flowcode exposes some common interrupts for each chip, including timer interrupts and external pin interrupts. By modifying the FCD file of a particular chip, we can expose additional interrupts which will allow our Flowcode programs to do more.
This article will show how to expose the USART receive interrupt in the PIC16F877A chip.
Interrupts in the FCD file
The FCD files can be found in the β€FCDβ€ subdirectory of your Flowcode installation. By default, this is C:\Program Files\Matrix Multimedia\Flowcode\FCD.
They are standard text files that contain a number of β€sectionsβ€ (such as [Device] and [Pins]), and each section contains a number of different entries (e.g. EepromSize=256 and PortPin10=0x0402). The section we will first look at is the [Interrupts] section.
The [Interrupts] section of the FCD file lists the interrupts available within Flowcode for this particular chip. If you open up the β€16F877A.FCDβ€ file in a text editor (e.g. Notepad), you can see this section near the end of the file. By default, it looks like this:
Code: Select all
[Interrupts]
;first 3 should always be 1=TMR0, 2=RB0INT, 3=PORTB
;any other interrupts need to also have this set: "intcon.PEIE=1;\n"
GeneralInit="intcon.GIE=1;\n"
Count=4
1=TMR0
2=RB0INT
3=PORTB
4=TMR1
Code: Select all
[Interrupts]
;first 3 should always be 1=TMR0, 2=RB0INT, 3=PORTB
;any other interrupts need to also have this set: "intcon.PEIE=1;\n"
GeneralInit="intcon.GIE=1;\n"
Count=5
1=TMR0
2=RB0INT
3=PORTB
4=TMR1
5=USART_RX
We also need to add a new section for this USART receive interrupt. It is shown below:
Code: Select all
[USART_RX]
Name="USART Receive"
UseExplicitHandlerCode=1
FlagReg=
FlagBit=
HandlerCode="if (pir1 & (1 << RCIF))\n{\n FCM_%n(); // call selected macro\n}\n"
UseExplicitEnableCode=1
EnReg=
EnBit=
EnableCode="intcon.PEIE = 1; // Enable peripheral interrupts\nintcon.GIE = 1; // Enable global interrupts\npie1.RCIE = 1; // Enable USART receive interrupts\nrcsta.CREN=1; // Enable reception\n"
DisableCode="pie1.RCIE = 0; // Disable USART receive interrupts\n"
OptCnt=0
TmrOpt=0
The important bits of this section are the 3 pieces of code. These have a special β€escape characterβ€ in them which you need to be aware of: β€\n’, which represents a β€new lineβ€ character when the code is turned into a C program. Another common escape character is β€\t’, which is the TAB character.
Interrupt code in more detail
Looking at these in more detail (and changing the β€\n’ to a real newline), these pieces of code are as follows:
EnableCode:
Code: Select all
intcon.PEIE = 1; // Enable peripheral interrupts
intcon.GIE = 1; // Enable global interrupts
pie1.RCIE = 1; // Enable USART receive interrupts
rcsta.CREN=1; // Enable reception
DisableCode:
Code: Select all
pie1.RCIE = 0; // Disable USART receive interrupts
HandlerCode:
Code: Select all
if (pir1 & (1 << RCIF))
{
FCM_%n(); // call selected macro
}
If it has, then the β€FCM_%n();β€ line tells Flowcode to run the Flowcode macro defined in the β€Interruptβ€ icon which enables the interrupt.
Normally, we would then add a line similar to the following to turn the interrupt off, but this is not required with the USART interrupt:
Code: Select all
clear_bit(pir1, RCIF); // clear interrupt
Using the interrupt in your own programs
For convenience, I have created a modified version of the FCD file for the 16F877A chip. Place this file into the FCD folder and it will be displayed as a new chip when you restart Flowcode - 16F877A (with USART_RX)β€.
I have also created a very simple program using this new interrupt to receive characters from a PC and display them onto the LCD. It is set up to use 9600 BAUD and no flow control.