Can PBP & 16f88 really do 9600,8,E,1


Closed Thread
Results 1 to 39 of 39
  1. #1

    Default Can PBP & 16f88 really do 9600,8,E,1

    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.

  2. #2
    Join Date
    Dec 2005
    Posts
    1,073


    Did you find this post helpful? Yes | No

    Default

    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 MELabs
    Last edited by dhouston; - 8th October 2009 at 17:54.

  3. #3
    Join Date
    Jun 2005
    Location
    Up the bush, Western Plains, NSW Au
    Posts
    216


    Did you find this post helpful? Yes | No

    Default

    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)
    Peter Moritz.
    Up the bush, Western Plains,
    New South Wales,
    Australia.

  4. #4
    Join Date
    Dec 2005
    Posts
    1,073


    Did you find this post helpful? Yes | No

    Default

    µ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...
    Code:
    DEFINE HSER_RCSTA 90h
    DEFINE HSER_TXSTA 20h
    DEFINE HSER_BAUD 9600
    DEFINE HSER_EVEN 1
    DEFINE HSER_BITS 9

  5. #5


    Did you find this post helpful? Yes | No

    Default Will this work?

    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?

    Code:
    '-------------------------------------
    ' 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

  6. #6
    Join Date
    Dec 2005
    Posts
    1,073


    Did you find this post helpful? Yes | No

    Default

    loop should be loop:

    And you have omitted
    Code:
    DEFINE HSER_RCSTA 90h
    DEFINE HSER_TXSTA 20h
    Last edited by dhouston; - 22nd October 2009 at 11:17.

  7. #7


    Did you find this post helpful? Yes | No

    Default

    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/show...=hserin+parity

  8. #8
    Join Date
    Dec 2005
    Posts
    1,073


    Did you find this post helpful? Yes | No

    Default

    Ahhh! I should have looked at RXSTA & TXSTA in the datasheet. The PBP docs are a bit too cryptic at times.

  9. #9


    Did you find this post helpful? Yes | No

    Default

    Thanks I've made some changes to my code and it is working sort of.

    Code:
    '-------------------------------------
    ' 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?

    Code:
    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?

  10. #10
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    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.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  11. #11
    Join Date
    Dec 2005
    Posts
    1,073


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by retepsnikrep View Post
    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.

  12. #12


    Did you find this post helpful? Yes | No

    Default

    Thanks for those comments.

    Any idea why this line won't compile?

    Code:
    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 ]

  13. #13
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    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

    Code:
    HSERIN [WAIT("$AA"), STR DATAIN\11]		'Receive 11 bytes into array DATAIN
    Last edited by ScaleRobotics; - 23rd October 2009 at 15:21.
    http://www.scalerobotics.com

  14. #14


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by scalerobotics View Post
    Try adding quotes on your $AA

    Code:
    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

  15. #15
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by retepsnikrep View Post
    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.
    http://www.scalerobotics.com

  16. #16
    Join Date
    Dec 2005
    Posts
    1,073


    Did you find this post helpful? Yes | No

    Default

    I don't see what is causing the syntax error.

    There is a sample program that shows various inputs using SerIn here...I 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 $.
    Last edited by dhouston; - 23rd October 2009 at 16:00.

  17. #17


    Did you find this post helpful? Yes | No

    Default

    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

    Code:
    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.
    Code:
    HSERIN [WAIT ($87)]		'Wait for packet start Byte $87  
    HSERIN [STR BCMDATA\23]		'Receive following 23 bytes into array BCMDATA

  18. #18
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    Can you show your whole program that you are trying to compile? And also your configs?

    This compiles perfectly well here:
    Code:
    DATAIN var byte[11]
    
    HSERIN [WAIT($AA), STR DATAIN\11]
    http://www.scalerobotics.com

  19. #19
    Join Date
    Dec 2005
    Posts
    1,073


    Did you find this post helpful? Yes | No

    Default

    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.
    Code:
    HSerIn 15,init,[WAIT($FF,$05),addr)
    Last edited by dhouston; - 23rd October 2009 at 17:40.

  20. #20
    Join Date
    Dec 2005
    Posts
    1,073


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by dhouston View Post
    I get the same syntax error (Expected "]") on a similar (mine includes a timeout label & value) statement using PBP 2.50 and MPASM.
    Code:
    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?

  21. #21


    Did you find this post helpful? Yes | No

    Default

    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

    Code:
    '-------------------------------------
    ' 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.

    Code:
     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.

  22. #22
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by retepsnikrep View Post
    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??
    Dave
    Always wear safety glasses while programming.

  23. #23
    Join Date
    Dec 2005
    Posts
    1,073


    Did you find this post helpful? Yes | No

    Default

    Code:
    HSEROUT [$AA,str BCMDATA\11]
    will be slightly faster.

  24. #24


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mackrackit View Post
    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/BATTSC..._INTERFACE.jpg

    I appreciate the help thanks.

  25. #25


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by dhouston View Post
    Code:
    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.

  26. #26
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    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?
    Dave
    Always wear safety glasses while programming.

  27. #27


    Did you find this post helpful? Yes | No

    Default Progress

    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.
    Code:
    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.

    Code:
    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

  28. #28
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Sounds like you are getting it.

    I would add
    Count1 = 0
    just before the for/next loop.
    Dave
    Always wear safety glasses while programming.

  29. #29


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mackrackit View Post
    I would add
    Count1 = 0
    just before the for/next loop.
    Why? Doesn't the command below do that?

    "For Count1 = 0 to 10"

  30. #30
    Join Date
    Dec 2005
    Posts
    1,073


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by retepsnikrep View Post
    If I define an array thus.
    Code:
    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.

    Quote Originally Posted by retepsnikrep View Post
    Now onto for next loops.
    Code:
    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.
    Last edited by dhouston; - 25th October 2009 at 12:43.

  31. #31
    Join Date
    Dec 2005
    Posts
    1,073


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by retepsnikrep View Post
    Why? Doesn't the command below do that?

    "For Count1 = 0 to 10"
    He probably meant Checksum=0 before entering the loop.

  32. #32
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by dhouston View Post
    He probably meant Checksum=0 before entering the loop.
    Yup. Sorry about that.
    Dave
    Always wear safety glasses while programming.

  33. #33


    Did you find this post helpful? Yes | No

    Default

    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.

  34. #34


    Did you find this post helpful? Yes | No

    Default

    Hmm, my checksum routine does not work and gives $ff as the last byte when using this data which I think is wrong.



    My code is here any thoughts?

    Code:
    '-------------------------------------
    ' 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!!!

  35. #35
    Join Date
    Dec 2005
    Posts
    1,073


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by retepsnikrep View Post
    0x87,0x40,0x58,0x15,0x6E,0x10,0x01,0x32,0x2F,0x2F, 0x04,0x39
    Code:
    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 & $7F
    For your example, checksum=0x39
    Last edited by dhouston; - 25th October 2009 at 22:18.

  36. #36


    Did you find this post helpful? Yes | No

    Default

    Thanks Dave.

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

  37. #37
    Join Date
    Dec 2005
    Posts
    1,073


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by retepsnikrep View Post
    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.

  38. #38


    Did you find this post helpful? Yes | No

    Default Ring Buffer

    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?

  39. #39
    Join Date
    Dec 2005
    Posts
    1,073


    Did you find this post helpful? Yes | No

    Default

    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.

Similar Threads

  1. PBP Book
    By Bruce in forum Off Topic
    Replies: 83
    Last Post: - 4th October 2021, 12:55
  2. PBP migration from 16F88 to 16F1827
    By RussMartin in forum mel PIC BASIC Pro
    Replies: 20
    Last Post: - 21st June 2010, 18:14
  3. PBP, ASM and LST files
    By HenrikOlsson in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 12th January 2010, 13:43
  4. Compiler differences between PBP 2.33 & 2.46
    By nikopolis in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 2nd May 2006, 19:01
  5. Newby- PBP wont compile for 18F (MPLAB)
    By jd76duke in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 17th December 2005, 23:30

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts