Interrupts

Coding and general discussion relating to user created compiler modules

Moderators: David Barker, Jerry Messina

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Fri Mar 18, 2011 2:04 pm

It looks like you renamed the modified USART.BAS file to USARTM.BAS. There's a potential problem with that... ISRRX needs to be able to see it as well (it includes USART.BAS too), and it won't find the right one if it's renamed.

Change the name back to USART.BAS. Either put it in your project folder, or better yet you can place it in the Swordfish/UserLibrary folder.
When SF looks for an include file, it looks in the following folders (in this order)
1) the project folder
2) Swordfish/UserLibrary
3) Swordfish/Library

The first one it finds is the one it uses.

Also, try this code and see what happens

Code: Select all

// device and clock...
device = 18F67J60
clock = 25

config FOSC = HSPLL
config FOSC2 = ON
config CP0 = OFF // Turn Code Protect On And Off

#option USART_BRGH = true
#option USART_BRG16 = true

include "DL_Timer.bas"
include "utils.bas"
include "USART.bas"
include "Convert.bas"
include "isrrx.bas"

dim LED as PORTB.5
dim LED1 as PORTB.4
dim VID_SWITCH as PORTB.3

// variables...
dim RXData(12) as byte
dim ByteCount as byte
dim I as byte
dim DelayTime as word
dim SecCount as word

dim PacketFlag as boolean
dim TMR0IP as INTCON2.2

const ipLow = 1, ipHigh = 2

event LEDFlash()
    toggle(LED)
    SecCount = SecCount + 1000
end event

interrupt Timer0_ISR(ipLow)
    if Timer0.InterruptFlag =1 then
        Timer0.SInterrupt()
    endif
end interrupt


PacketFlag = false
ByteCount = 0
DelayTime = 10000
SecCount = 0

// setup serial port, ISRRX, etc
// clear buffer to start

output(LED)
output(LED1)
output(VID_SWITCH)

USART.SetBaudrate(br4800)
ISRRX.Initialize // init the ISRRX module!

TMR0IP = 0 // set tmr0 intr to low priority
Timer0.Initialize(LEDFlash)
Timer0.SetPrescaler(Timer0.PS256)
Timer0.EightBit = 0
Timer0.Preload=49911
Timer0.EnableInterrupt()
Timer0.Enabled()
enable(Timer0_ISR) ' Start Interrupts


while true

    if SecCount = DelayTime then
        USART.Write("Hello World",13,10)
        toggle(LED1)
        SecCount = 0
    end if

    while ISRRX.DataAvailable()
        RXData(ByteCount) = ISRRX.ReadByte()
        inc(ByteCount)
        if (ByteCount = 12) then
            PacketFlag = true
            break       // exit 'while' loop if packet received
        endif
    wend

    if (PacketFlag) then
        USART.Write("Byte Count Value Is ",DecToStr(ByteCount), 13 ,10)
        ' Timer0.Disabled()
        // echo packet back out the uart
        for I = 0 to (ByteCount-1)
            USART.WriteByte(RXData(I))
        next

        if ((RXData(0) = $FE) and (RXData(1) = $1F) and (RXData(3) = $04)) then
            USART.Write("MATCH",$13,$10)
            toggle(LED1)
        endif
        // prepare for next packet
        ByteCount = 0
        PacketFlag = false
        ' Timer0.Enabled()
    endif

wend

Widgetman
Posts: 136
Joined: Sun Dec 16, 2007 7:39 pm
Location: Florida

Same result

Post by Widgetman » Fri Mar 18, 2011 2:23 pm

Hi,
I made the changes and did as you said. I saved the USART.BAS file in my project folder, and also in the library folder under the swordfish path. I still get a partial packet out. I saw you added a break in the loop. This did not seem to do much except now I get out 4-5 charcters instead of 7-8. I did also notice however that the Bytecount message comes out twice like the packet flag is not being detected fast enough after I set it false in the loop. I have also tried making the RXData buffer larger and does not seem to matter as well. The loop is definitly detecting 12 bytes in a packet though. If I send a packet a second time I get the last part of the first packet out and part of the second. This is the same as before.
Thanks

Widgetman
Posts: 136
Joined: Sun Dec 16, 2007 7:39 pm
Location: Florida

Kinda Figured It Out

Post by Widgetman » Fri Mar 18, 2011 4:55 pm

Hi,
Well after playing around with a few ideas I kinda figured it out, but I still see a issue I do not understand. When I was typing a packet in the TX window of the Serial terminal in Swordfish I was putting spaces in between Bytes to make it easier to read for me. I forgot a space is also a charcter. Listed below is what I sent to make all 12 bytes come out. It still breaks it up into two lines and sends the byte count message twice. I have no idea what I am missing there. Any ideas ?
Thanks

Send:
FE1F0002FE1F0004FE1F0008


Get:
FE1F0002FE1FByte Count Value Is 12
0004FE1F0008Byte Count Value Is 12

Widgetman
Posts: 136
Joined: Sun Dec 16, 2007 7:39 pm
Location: Florida

Solved a bit more

Post by Widgetman » Fri Mar 18, 2011 5:00 pm

Hi All,
Ok it seems if I comment out the byte count message the packet comes all out in one spurt. It still does not match though. I guess I have to work some more on that. Thanks for all the patience and help Jerry

Widgetman
Posts: 136
Joined: Sun Dec 16, 2007 7:39 pm
Location: Florida

Post by Widgetman » Fri Mar 18, 2011 6:42 pm

Hi All
I have been playing around with more dieas and I have narrowed it down to the USART.Write call which is breaking up my packets. Wehn I comment out the line below I get the same packet I send. When I add back in the line of code to print the bytes in a string it breaks my packet up. Works the same with or without the DelayMS(100) line. I guess I don't understand exactly what USART.Write does and will have to study the module some.


Expected:

Hello World
FE1F0002FE1F0004FE1F0008


Add back In This Line:

'USART.Write("RXData Packet Is ",RXData(0),RXData(1),RXData(2),RXData(3),RXData(4),RXData(5),RXData(6),RXData(7),RXData(8),RXData(9),RXData(10),RXData(11),13,10)

And I Get:

FE1F0002FE1FRXData Packet Is FE1F0002FE1F
0004FE1F0008RXData Packet Is 0004FE1F0008
Hello World


Here Is Main Loop:

While true

If SecCount = DelayTime Then
USART.Write("Hello World",13,10)
Toggle(LED1)
SecCount = 0
End If

while ISRRX.DataAvailable()
RXData(ByteCount) = ISRRX.ReadByte()
ByteCount = ByteCount + 1
if (ByteCount = 12) then
PacketFlag = true
break // exit 'while' loop if packet received
endif
wend

if (PacketFlag) then

PacketFlag = false
' Timer0.Disabled()
// echo packet back out the uart
for I = 0 to (ByteCount-1)
USART.Write(RXData(I))
next

'USART.Write("RXData Packet Is ",RXData(0),RXData(1),RXData(2),RXData(3),RXData(4),RXData(5),RXData(6),RXData(7),RXData(8),RXData(9),RXData(10),RXData(11),13,10)
delayms(100)
if (RXData(0) = $FE) then
ByteMatch = ByteMatch + 1
endif
if (RXData(1) = $1F) then
ByteMatch = ByteMatch + 1
endif
if (RXData(3) = $02) then
ByteMatch = ByteMatch + 1
endif
// prepare for next packet
ByteCount = 0
ByteMatch = 0
if ByteMatch > 0 then
USART.Write ("ByteMatch Count Is ",ByteMatch,13,10)
end if
'ISRRX.Reset
' Timer0.Enabled()
endif


Wend

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Fri Mar 18, 2011 7:26 pm

When you type in a terminal emulator, each character you type is a byte. Typing the two characters "FE" sends two bytes... ASCII "F" ($46) and ASCII "E" ($45).

Before you changed things, when you had
Send:
FE1F0002FE1F0004FE1F0008

Get:
FE1F0002FE1FByte Count Value Is 12
0004FE1F0008Byte Count Value Is 12
That was correct. You have TWO strings of 12 characters:
"FE1F0002FE1F"
and
"0004FE1F0008"

If you want to interpret the ASCII characters as a string of "hex bytes", you'll have to convert the ASCII characters into hex data.

PS - you can put the code back the way it was. It's working... at least from what I can tell.

Widgetman
Posts: 136
Joined: Sun Dec 16, 2007 7:39 pm
Location: Florida

Cold Beer

Post by Widgetman » Fri Mar 18, 2011 8:40 pm

Hi All,
Thanks a bunch for clarifying that Jerry. I will play with it this weekend and hopefully can get it detecting packets ok. I definitely owe you a cold beer if ya ever get to FL.

Post Reply