Page 1 of 1

Adding interrupts to FCD files

Posted: Fri Aug 08, 2008 10:52 am
by Steve
Image

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
Immediately following this section are 4 other sections detailing each particular interrupt in the above list. We are going to add a new interrupt called “USART_RX”, so change the count value to “5” and add “5=USART_RX” in the [Interrupts] section as shown below:

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
Adding the USART interrupt section

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
(Note that the “HandlerCode” and “EnableCode” entries are long and might be wrapped over 2 or more lines above)

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
This code is executed each time you include an “Interrupt” icon in your Flowcode program and select “enable”. It basically sets up a few registers which make the chip “interrupt” its current program whenever incoming data is received by the USART module.

DisableCode:

Code: Select all

	pie1.RCIE = 0; // Disable USART receive interrupts
This code effectively turns the interrupt off and is executed whenever Flowcode encounters an “Interrupt” icon with “disable” selected.

HandlerCode:

Code: Select all

if (pir1 & (1 << RCIF))
{
  FCM_%n(); // call selected macro
}
The HandlerCode snippet will be placed in the general interrupt handler routine and will be executed each time an active interrupt is trapped by the chip. At this point, we do not know which actual interrupt has been triggered, so the first line looks at a register to see if the RCIF interrupt has occurred (this is the USART receive interrupt).

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 
Instead, we must make sure we read the RCREG register to clear the USART receive interrupt. It is assumed that this will be done within the macro that is called by the interrupt, probably using a call to the “ReceiveRS232Char” hardware macro.

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.

Re: Adding interrupts to FCD files

Posted: Mon Jan 11, 2010 9:36 am
by tanlipseong
Hi Steve,

I have added RS232 interrupt into .FCD file and wrote a program to try out, the program can compile and written to the F887 chip, however i can not get it to display data from RS232 to LCD (LCD always display initialize value "0" just like the interrupt never happened).

I have confirm data do arriving at RX pin of F887 with scope (from anotherF887 through hard wired Tx to Rx and Gnd) .

attached are the fcd file and the test program i wrote, please help see what is wrong with it.

Thanks in advance.
FCF and FCD file.zip
(5.07 KiB) Downloaded 656 times

Re: Adding interrupts to FCD files

Posted: Mon Jan 11, 2010 11:23 am
by Steve
I'm sorry, but deugging a program like this will be difficult without access to the proper hardware. Plus, it is beyond the level of support that we can offer.

I suggest you start at the basics and create a simple RS232 program that works without the interrupt, and then add the interrupt feature.