PDA

View Full Version : Can PBP & 16f88 really do 9600,8,E,1



retepsnikrep
- 8th October 2009, 17:03
I have an 16f88 project which needs to communicate with an rs485 bus.

The bus is fixed and I can't change the protocols so I need to be able to rxd & txd via the usart 9600,8,E,1 data.

Can hserout/hserin or serin/out really do 8 data bits & an even parity bit?

11 bits in all inc a start & stop bit.

I know the pic usart can do 9 bit data transmission but it is not 100% clear I can do what I need in PBP without poking registers and driving the usart directly.

Can someone give me a definitive answer? Any useful links would be great as well. I'm happy with the electronics side of this project, it's the pbp serial commands which are confusing me.

dhouston
- 8th October 2009, 17:45
Have you read the manual entry for HSERIN? It has DEFINES for 8 bits + parity and for odd or even parity.

EDIT: My print manual lists DEFINE HSER_BITS 9 (Use 8 bits + parity) for both HSerIn and HSerIn2 but, it is omitted in the online manual at MELabshttp://www.melabs.com/resources/pbpmanual/5_27-5_29.htm#528

muddy0409
- 8th October 2009, 19:36
I have very successfully used an 'F88 at 9600. Used a 20MHz crystal and used debug / debugin rather than the normal serout stuff. No probs at all (so far)

dhouston
- 8th October 2009, 23:28
µeLabs Support tells me the online version of the manual is out of date. So you should be able to do what you need with...
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 20h
DEFINE HSER_BAUD 9600
DEFINE HSER_EVEN 1
DEFINE HSER_BITS 9

retepsnikrep
- 22nd October 2009, 07:06
My first 16f88 code is here. Microstudio says it compiles correctly. I now need to try and load it into the pic!!

It should recieve 12 bytes of 9600,8,E,1 data via Usart RX the then squirt it out the other side via Usart TX.

Any glaring errors or ommisions?



'-------------------------------------
' PBP Fake BCM CODE 21/10/09 V.04
'-------------------------------------

@ DEVICE PIC16F88,INTRC_OSC_NOCLKOUT
@ DEVICE PIC16F88,PROTECT_OFF
@ DEVICE PIC16F88,WDT_OFF
@ DEVICE PIC16F88,PWRT_ON
@ DEVICE PIC16F88,MCLR_OFF
@ DEVICE PIC16F88,BOD_OFF
@ DEVICE PIC16F88,LVP_OFF
@ DEVICE PIC16F88,CPD_OFF
@ DEVICE PIC16F88,DEBUG_OFF
@ DEVICE PIC16F88,CCPMX_OFF

DEFINE OSC 8 ; set oscilator speed to 8mHz
DEFINE HSER_BAUD 9600
DEFINE HSER_BITS 9
DEFINE HSER_EVEN 1
DEFINE HSER_CLROERR 1 ; Clear overflow automatically

OSCCON=%01111000 '8 Mhz
ANSEL = 0 'ALL DIGITAL
CMCON = 7 'COMPARATORS OFF
INTCON = 0 ;Disable interrupts
TRISB = %00000100 ;RB2(RX) as input, others to Output
TRISA = %11111111 ;SET PORTA AS INPUTS

PORTB.4 = 1
DATAIN VAR BYTE[12]
loop
HSERIN [str DATAIN\12]
HSEROUT [str DATAIN\12]
goto looP
END

dhouston
- 22nd October 2009, 11:13
loop should be loop:

And you have omitted
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 20h

retepsnikrep
- 22nd October 2009, 11:32
Dave

Thanks for that!

However I saw this earlier post which seems to imply the defines you mention are not required??

http://www.picbasic.co.uk/forum/showthread.php?t=6718&highlight=hserin+parity

dhouston
- 22nd October 2009, 12:14
Ahhh! I should have looked at RXSTA & TXSTA in the datasheet. The PBP docs are a bit too cryptic at times.

retepsnikrep
- 22nd October 2009, 20:37
Thanks I've made some changes to my code and it is working sort of.



'-------------------------------------
' PBP Fake BCM CODE 21/10/09 V.04
'-------------------------------------

@ DEVICE PIC16F88,INTRC_OSC_NOCLKOUT
@ DEVICE PIC16F88,PROTECT_OFF
@ DEVICE PIC16F88,WDT_OFF
@ DEVICE PIC16F88,PWRT_ON
@ DEVICE PIC16F88,MCLR_OFF
@ DEVICE PIC16F88,BOD_OFF
@ DEVICE PIC16F88,LVP_OFF
@ DEVICE PIC16F88,CPD_OFF
@ DEVICE PIC16F88,DEBUG_OFF
@ DEVICE PIC16F88,CCPMX_OFF

DEFINE OSC 8 'Set oscilator speed to 8mHz
DEFINE HSER_BAUD 9600 'Set Baud rate to 9600bps
DEFINE HSER_BITS 9 'Set to 9 bit mode
DEFINE HSER_EVEN 1 'Set Even Parity
DEFINE HSER_CLROERR 1 'Clear overflow error automatically

OSCCON=%01111000 '8 Mhz
ANSEL = 0 'ALL DIGITAL
CMCON = 7 'COMPARATORS OFF
INTCON = 0 'Disable interrupts
TRISB = %00000100 'SET PORTB RB2(RX) as input, others to OUTPUT
TRISA = %11111111 'SET PORTA AS INPUTS

DATAIN VAR BYTE[12] 'Define DATAIN as a byte array (12 Bytes)

loop: 'Start of Communications Loop
HSERIN [str DATAIN\12] 'Receive 12 bytes into array DATAIN
HSEROUT [str DATAIN\12] 'Transmit 12 bytes from array DATAIN
goto looP 'Goto Loop

END



Now what I want to do is use a qualifier so that the Hserin waits until $AA is received before it starts filling the array. I also want the $AA in the array though? Possible?

So it should receive twelve bytes in all like this.

0xAA,0x10,0x00,0x00,0x00,0x20,0x40,0x61,0x10,0x01, 0x00,0x74

Does this look right?




loop: 'Start of Communications Loop
HSERIN [WAIT($AA), str DATAIN\12] 'Receive 12 bytes into array DATAIN
HSEROUT [str DATAIN\12] 'Transmit 12 bytes from array DATAIN
goto looP 'Goto Loop



I read that a delay may be required after the last send in the HSEROUT section or it may not transmit the last byte correctly?

Archangel
- 23rd October 2009, 02:51
You might want to get out of the habit of using the label LOOP: as ver 2.6 made loop a reserved word being used in the Do Loop routine.

dhouston
- 23rd October 2009, 12:40
Now what I want to do is use a qualifier so that the Hserin waits until $AA is received before it starts filling the array. I also want the $AA in the array though? Possible?I don't think so but I haven't actually tested this. Add the timeout and test.

retepsnikrep
- 23rd October 2009, 13:45
Thanks for those comments.

Any idea why this line won't compile?



HSERIN [WAIT($AA), str DATAIN\11] 'Receive 11 bytes into array DATAIN


I want it to wait until a $AA appears before filling the array.

I've tried square brackets, round brackets no $ etc etc!!!!!

I get two errors bad expression and expected ]

ScaleRobotics
- 23rd October 2009, 14:58
That compiles for me (but micro code studio forces the capitalization of STR). However, since you are using asci, try adding quotes on your $AA



HSERIN [WAIT("$AA"), STR DATAIN\11] 'Receive 11 bytes into array DATAIN

retepsnikrep
- 23rd October 2009, 15:14
Try adding quotes on your $AA



HSERIN [WAIT("$AA"), STR DATAIN\11] 'Receive 11 bytes into array DATAIN


Tried that :( still get error expected ], tried sqaure brackets same result + syntax error

ScaleRobotics
- 23rd October 2009, 15:32
Tried that :( still get error expected ], tried sqaure brackets same result + syntax error

I will get the same error, but only if I do not define DATAIN array before the wait line.

dhouston
- 23rd October 2009, 15:53
I don't see what is causing the syntax error.

There is a sample program that shows various inputs using SerIn here...http://www.melabs.com/resources/samples/pbp/ser2mod.basI believe HSerIn uses the same syntax.

Also, lose the quote marks - you are waiting for $AA (i.e. 0xaa, a hex byte, binary %10101010) not ascii AA which would be "AA" without the $.

retepsnikrep
- 23rd October 2009, 16:36
Nope still no joy won't compile. :(

Just to clear up any confusion I'm waiting for a byte value to appear on the usart ($87) not an ascii string. I then want to gather the following data into the array. It's 9600 8,E,1 (24 bytes total in the packet inc the $87 start byte)

This compiles



HSERIN [WAIT ($87)] 'Wait for byte $87 (135 Dec)

as does this

HSERIN [WAIT (135)] 'Wait for byte $87 (135 Dec)



Looks like I will have to wait for the identifier then use another HSERIN command to get the following bytes. Problem is there is no break in the data and it follows on immdiately after the $87 if received. I hope it doesnt miss the start of the second byte :(

I'll try this it compiles corectly.


HSERIN [WAIT ($87)] 'Wait for packet start Byte $87
HSERIN [STR BCMDATA\23] 'Receive following 23 bytes into array BCMDATA

ScaleRobotics
- 23rd October 2009, 16:48
Can you show your whole program that you are trying to compile? And also your configs?

This compiles perfectly well here:


DATAIN var byte[11]

HSERIN [WAIT($AA), STR DATAIN\11]

dhouston
- 23rd October 2009, 17:33
What version of PBP? Using MPASM or PM?

I get the same syntax error (Expected "]") on a similar (mine includes a timeout label & value) statement using PBP 2.50 and MPASM.
HSerIn 15,init,[WAIT($FF,$05),addr)

dhouston
- 23rd October 2009, 20:39
I get the same syntax error (Expected "]") on a similar (mine includes a timeout label & value) statement using PBP 2.50 and MPASM.
HSerIn 15,init,[WAIT($FF,$05),addr)Sorry, the closing ")" was a typo - my router quit yesterday and I had to manually type that line instead of copying it across my network.

The reason my test failed was because addr was not declared. The error message is a bit cryptic.

Have you declared the correct number of array elements for DATAIN?

retepsnikrep
- 23rd October 2009, 20:56
My complete code is here. Sadly I have now deleted the troublesome line and replaced it with two lines. I am using PBP 2.50 and PM



'-------------------------------------
' PBP BCM CODE 22/10/09 V.04 8mhz
'-------------------------------------

@ DEVICE PIC16F88,INTRC_OSC_NOCLKOUT
@ DEVICE PIC16F88,PROTECT_OFF
@ DEVICE PIC16F88,WDT_OFF
@ DEVICE PIC16F88,PWRT_ON
@ DEVICE PIC16F88,MCLR_ON
@ DEVICE PIC16F88,BOD_OFF
@ DEVICE PIC16F88,LVP_OFF
@ DEVICE PIC16F88,CPD_OFF
@ DEVICE PIC16F88,DEBUG_OFF
@ DEVICE PIC16F88,CCPMX_OFF

DEFINE OSC 8 'Set oscilator speed to 8mHz
OSCCON=%01111000 '8 Mhz

'DEFINE OSC 4 'Set oscilator speed to 4mHz
'OSCCON =%01100000 '4 Mhz

DEFINE HSER_BAUD 9600 'Set Baud rate to 9600bps
DEFINE HSER_BITS 9 'Set to 9 bit mode
DEFINE HSER_EVEN 1 'Set Even Parity
DEFINE HSER_CLROERR 1 'Clear overflow error automatically

ANSEL = 0 'ALL DIGITAL
CMCON = 7 'COMPARATORS OFF
INTCON = 0 'Disable interrupts
TRISB = %00000100 'SET PORTB RB2(RX) as input, others to OUTPUT
TRISA = %11111111 'SET PORTA AS INPUTS

'Variables

BCMDATA VAR BYTE[12] 'Define BCMAADATA as a byte array (12 Bytes)
Count1 VAR BYTE 'Define Count1 as a byte variable (Used in For/Next Loops)
CheckSum VAR BYTE 'Define CheckSum as a byte variable


Commloop: 'Start of Communications Loop

HSERIN [WAIT ($87)] 'Wait for Packet start Byte $87
HSERIN [STR BCMDATA\11] 'Receive 11 bytes into array BCMDATA

'Gosub CalcSum 'Gosub CalcSum to Calculate Checksum

Hserout [$87] 'Transmit Packet start Byte $87
HSEROUT [str BCMDATA\11] 'Transmit 11 bytes from array BCMDATA

HSERIN [WAIT ($AA)] 'Wait for Packet start Byte $AA
HSERIN [STR BCMDATA\11] 'Receive 11 bytes into array BCMDATA

'Gosub CalcSum 'Gosub CalcSum to Calculate Checksum

Hserout [$AA] 'Transmit Packet start Byte $AA
HSEROUT [str BCMDATA\11] 'Transmit 11 bytes from array BCMDATA


Pause 32 'Pause for 16ms
HSEROUT [$AA,$10,$00,$00,$00,$20,$40,$61,$10,$01,$00,$74] 'Transmit 12 bytes
pause 32 'Pause for 16ms
HSEROUT [$87,$40,$58,$15,$6E,$10,$01,$32,$2F,$2F,$04,$39] 'Transmit 12 bytes
pause 32 'Pause for 16ms

goto CommlooP 'Goto Loop

CalcSum: 'Calculate Packet CheckSum Routine
CheckSum = 0 'Clear CheckSum Variable
For Count1 = 0 to 10 'For Count1 = 0 to 10 (Start 11 byte loop)
CheckSum = CheckSum + BCMDATA[Count1] 'Add Bytes
Next Count1 'Repeat until 11 bytes added
CheckSum = NOT CheckSum 'Not CheckSum
CheckSum = CheckSum + 1 'Add 1 to CheckSum
BCMDATA[12] = CheckSum AND $7F 'AND result
Return 'Return


END



However i have just tried this.



HSERIN [WAIT($87), str BCMDATA\11] 'Receive 12 bytes into array DATAIN



And it compiles correctly, might have been me with a typo!!!!! Arrggh. I let you know. Thanks for the help so far. This program is just proof of concept at the moment I know the "gosub" is not called at the moment. I'm trying to get the hserin/out working first.

Anything with my configs or program that would stop a pickit2 icsp working I can't get that to work for love or money, but the chip works fine in a standalone programmer. I've checked the circuit and pickit2 voltages and it all looks fine. Just can't find device 99% of the time.

mackrackit
- 23rd October 2009, 21:13
Anything with my configs or program that would stop a pickit2 icsp working I can't get that to work for love or money, but the chip works fine in a standalone programmer. I've checked the circuit and pickit2 voltages and it all looks fine. Just can't find device 99% of the time.
Do you have capacitors across VDD and VSS??

dhouston
- 23rd October 2009, 22:02
HSEROUT [$AA,str BCMDATA\11] will be slightly faster.

retepsnikrep
- 23rd October 2009, 22:39
Do you have capacitors across VDD and VSS??

My board is pretty standard with the ICSP pins and a 10k pullup on MCLR pin.

It is powered by a 7805 100ma reg from a 12v bench psu. 1000uf on each side of reg + 0.1uf on vdd/vss pins of 16f88. I tried adding extra cap 10uf tantalum across vdd/vss no change. Are my 1000uf a bit big?

Tried powering it from a 9v battery via reg. No apparent change.

Connections checked on board using pickit2 software and it is supplying the correct voltages inc 12v on MCLR under trouble shooting mode.

The chip does work in the board when programmed off board and is communicating as per my current software.

Once every few trys it will communicate briefly but always reports errors or can't find device.

Once the chip is running in the board I can't communicate with it. If I insert it blank into the board I can communicate initially but it does not program sucessfully and then I loose comms and have to erase it in my other programmer and program it with that!

The board looks like this.

http://www.solarvan.co.uk/bcm/fakebcm004.jpg

My untidy schematic here.

http://www.solarvan.co.uk/bcm/BATTSCI_RS485_INTERFACE.jpg

I appreciate the help thanks.

retepsnikrep
- 23rd October 2009, 22:40
HSEROUT [$AA,str BCMDATA\11] will be slightly faster.


Thanks that's helpful. I'm pretty clued up with picaxe basic but am now trying to move over and the syntax for PBP is quite a bit different.

mackrackit
- 23rd October 2009, 23:18
The schematic looks OK to me.
Does the PicKit work with other chips?
Do you have a regular breadboard to put the chip in and try the PicKit from there?

retepsnikrep
- 25th October 2009, 10:36
I'm making good progress with my code thanks.

I have a couple of questions about Arrays and the addressing of them with pbp.

If I define an array thus.


BCMDATA VAR BYTE[11] 'Define BCMDATA as a byte array (11 Bytes)


I get an array 11 bytes long and the address of the first byte is

BCMDATA[0] and the last BCMDATA[10] ?

Is that correct.

Now onto for next loops.



CalcSum: 'Calculate Packet CheckSum Routine

For Count1 = 0 to 10 'For Count1 = 0 to 10 (Start 11 byte loop)
CheckSum = CheckSum + BCMDATA[Count1] 'Add Bytes
Next Count1 'Repeat until 11 bytes added
CheckSum = NOT CheckSum 'Not CheckSum
CheckSum = CheckSum + 1 'Add 1 to CheckSum
BCMDATA[10] = CheckSum AND $7F 'AND result
Return 'Return


In this code how many times does it complete?
And on the first pass through the loop Count1 =0 does it not?
And on the last pass through the loop Count1 = 9 or 10

So if count1 = 9 it goes through the loop, if it = 10 it jumps over it?

Just checking my understanding. Thanks

mackrackit
- 25th October 2009, 11:05
Sounds like you are getting it.

I would add
Count1 = 0
just before the for/next loop.

retepsnikrep
- 25th October 2009, 11:22
I would add
Count1 = 0
just before the for/next loop.

Why? Doesn't the command below do that?

"For Count1 = 0 to 10"

dhouston
- 25th October 2009, 12:04
If I define an array thus.


BCMDATA VAR BYTE[11] 'Define BCMDATA as a byte array (11 Bytes)

I get an array 11 bytes long and the address of the first byte is

BCMDATA[0] and the last BCMDATA[10] ?

Is that correct.Correct.


Now onto for next loops.


CalcSum: 'Calculate Packet CheckSum Routine

For Count1 = 0 to 10 'For Count1 = 0 to 10 (Start 11 byte loop)
CheckSum = CheckSum + BCMDATA[Count1] 'Add Bytes
Next Count1 'Repeat until 11 bytes added
CheckSum = NOT CheckSum 'Not CheckSum
CheckSum = CheckSum + 1 'Add 1 to CheckSum
BCMDATA[10] = CheckSum AND $7F 'AND result
Return 'Return


In this code how many times does it complete?
And on the first pass through the loop Count1 =0 does it not?
And on the last pass through the loop Count1 = 9 or 10

So if count1 = 9 it goes through the loop, if it = 10 it jumps over it?
It goes through the loop 11 times (0-10) with Count1=10 during the final pass. When it exits the loop, Count1=11. The loop counter is incremented and tested at the bottom of a For/Next loop. The test occurs at the top of a While/Wend loop.

As written, your code writes a value to BCMDATA[10] in the loop and then overwrites BCMDATA[10] after exiting the loop. I suspect this is not what you want.

While the following link is written for Visual Basic, I think all versions of Basic work the same way.http://msdn.microsoft.com/en-us/library/zxkf5z4b%28VS.71%29.aspx

dhouston
- 25th October 2009, 15:34
Why? Doesn't the command below do that?

"For Count1 = 0 to 10"

He probably meant Checksum=0 before entering the loop.

mackrackit
- 25th October 2009, 17:05
He probably meant Checksum=0 before entering the loop.
Yup. Sorry about that.

retepsnikrep
- 25th October 2009, 17:49
Thanks for that. I have corrected my program.

I do not load 'checksum' with 0 because I load it with one of the packet header characters $87 or $AA before I call the 'Calcsum' routine

'Checksum' then already contains the first byte (header) of the packet when it enters, it then adds the content of the array 0-9 to the byte and does the checksum thing storing the result in position 10. So the packet is twelve bytes in all when sent out.

$87 header + 10 data bytes from array + byte 11 from array which is calculated checksum (sum of previous 11 bytes inc header byte)

I am collecting the checksum when reading in the data at the moment (and ignoring it) I will probably do the caluclation on it as well soon to make sure the incomming data is valid before I start messing about with it!!

Thanks for all your help so far.

retepsnikrep
- 25th October 2009, 19:14
Hmm, my checksum routine does not work and gives $ff as the last byte when using this data which I think is wrong.

http://www.solarvan.co.uk/bcm/CheckSum.jpg

My code is here any thoughts?



'-------------------------------------
' PBP BCM CODE 25/10/09 V.15 8mhz
'-------------------------------------

@ DEVICE PIC16F88,INTRC_OSC_NOCLKOUT
@ DEVICE PIC16F88,PROTECT_OFF
@ DEVICE PIC16F88,WDT_ON
@ DEVICE PIC16F88,PWRT_ON
@ DEVICE PIC16F88,MCLR_ON
@ DEVICE PIC16F88,BOD_OFF
@ DEVICE PIC16F88,LVP_OFF
@ DEVICE PIC16F88,CPD_OFF
@ DEVICE PIC16F88,DEBUG_OFF
@ DEVICE PIC16F88,CCPMX_OFF

DEFINE OSC 8 'Set oscilator speed to 8mHz
OSCCON=%01111000 '8 Mhz

'DEFINE OSC 4 'Set oscilator speed to 4mHz
'OSCCON =%01100000 '4 Mhz

DEFINE HSER_BAUD 9600 'Set Baud rate to 9600bps
DEFINE HSER_BITS 9 'Set to 9 bit mode
DEFINE HSER_EVEN 1 'Set Even Parity
DEFINE HSER_CLROERR 1 'Clear overflow error automatically

ANSEL = 0 'ALL DIGITAL
CMCON = 7 'COMPARATORS OFF
INTCON = 0 'Disable interrupts
TRISB = %00000100 'SET PORTB RB2(RX) as input, others to OUTPUT
TRISA = %11111111 'SET PORTA AS INPUTS

'Variables

BCMDATA VAR BYTE[11] 'Define BCMDATA as a byte array (11 Bytes)
Count1 VAR BYTE 'Define Count1 as a byte variable (Used in For/Next Loops)
CheckSum VAR BYTE 'Define CheckSum as a byte variable
Led1 VAR PORTB.0 'Define PortB.0 as Led1
Led2 VAR PORTB.1 'Define PortB.1 as Led2

'Constants


'Main Program Start

Commloop: 'Start of Communications Loop

HSERIN [WAIT($87), STR BCMDATA\11] 'Wait for packet start $87 then Receive 11 bytes into array BCMDATA

BCMDATA[2] = $15 'Load Soc Byte with $15 (19 Bars)
BCMDATA[3] = $6F 'Load Soc Byte with $6F (19 Bars)
CheckSum = $87 'Load Checksum with packet start byte value
Gosub CalcSum 'Gosub CalcSum to Calculate Checksum

HSEROUT [$87,str BCMDATA\11] 'Transmit 12 bytes inc packet start byte $87

HSERIN [WAIT($AA), STR BCMDATA\11] 'Wait for packet start $AA then Receive 11 bytes into array BCMDATA
'CheckSum = $AA 'Load Checksum with packet start byte value
'Gosub CalcSum 'Gosub CalcSum to Calculate Checksum

HSEROUT [$AA,str BCMDATA\11] 'Transmit 12 bytes inc packet start byte $AA

goto CommlooP 'Goto Loop

CalcSum: 'Calculate Packet CheckSum Routine

For Count1 = 0 to 9 'For Count1 = 0 to 9 (Start 10 byte loop)
CheckSum = CheckSum + BCMDATA[Count1] 'Add Bytes
Next Count1 'Repeat until 10 bytes added
CheckSum = NOT CheckSum 'Not CheckSum
CheckSum = CheckSum + 1 'Add 1 to CheckSum
BCMDATA[10] = CheckSum AND $7F 'AND result
Return 'Return


END



My understanding of how the checksum is generated comes from this info.

"Here's what I figured out so far: There are basically 10 data bytes per message. Before the first is the message type (AA or 87), and after the last is the checksum. You can compute it by adding the other 11 bytes, negating that (2's complement), then and'ing by 0x7f.
It makes the entire message add to 0 when you and by 0x7f."


And here is an known good sample.

0x87,0x40,0x58,0x15,0x6E,0x10,0x01,0x32,0x2F,0x2F, 0x04,0x39

Checksum byte is 0x39 at end.


Ooppps I think I have been an idiot and included the AND part in my routine which should not be there and in fact is done by the receiving device to see if the result is 0!!!

dhouston
- 25th October 2009, 22:16
0x87,0x40,0x58,0x15,0x6E,0x10,0x01,0x32,0x2F,0x2F, 0x04,0x39
checksum=BCMDATA[0]
For i=1 To 10
checksum=checksum+BCMDATA[i]
Next i
checksum=~checksum '1's complement
checksum=checksum+1 '2's complement
checksum=checksum & $7FFor your example, checksum=0x39

retepsnikrep
- 26th October 2009, 05:09
Thanks Dave.

I was using logical operators instead of maths ones!!!

dhouston
- 26th October 2009, 11:16
I was using logical operators instead of maths ones!!!Yeah - that's one area where various implementations of Basic vary widely - you need to study the documentation, ask lots of questions and take note of examples posted here as well as those on meLabs web site.

For example, the syntax for bit arrays is rather obscure. AFAIK it's only documented in an example in the Repeat/Until explanation in the manual. I'm not sure I would have ever grasped it without an example posted by Melanie.

retepsnikrep
- 7th November 2010, 00:17
Thanks to the members on here my project has been working well for over a year.
But now I need to enhance it and move to using a ring buffer to receive the incomming data and
some sort of interrupt driven routine as i need more time for code execution and can't afford to wait
for the data.

So I need to work directly with the USART hardware and use darell's interrupts.
I have been reading up on here but i'm pretty confused.

So if i explain what is happening then perhaps members can chip in with some more advice.

My application (16F88 chip running at 8mhz) has to receive data on a 9600, 8,E,1 in 12 byte packets on the USART RX Line,manipulate the data if reqd and then squirt if back out of the USART TX port on a different Line in same format.
I have no control over the incomming data and the packets have about 15ms between them.
The bytes in the packets are back to back no gaps.

I have been receiving the packets using hserin and then squrting them back out via hserout, but this
only leaves a couple of ms before next packet is arriving. :( No time to do anything especially when
i am trying to use the debug command on another pin to send some data to an LCD.

I can't really afford to drop packets so any advice appreciated.

I understand the USART has a two byte RX/TX buffer. Can it RX & TX at the same time?

Any simple examples of a ring buffer doing USART RX and TX as described?

dhouston
- 7th November 2010, 12:12
Using a 20MHz clock would give you a few more clock cycles (i.e. more statements can be executed in the same amount of time) between input & output. Other than that, you'd probably need a bit of ASM optimization to make significant gains.