PDA

View Full Version : PIC to PIC TX RX and PIC to PC COM RX ..a little help please



Dennis
- 23rd November 2009, 21:26
Hi all
(sorry about the long post!)

Thanks to all those who have helped me and lots of late nights reading and 'PIC'-ing, I have finally reached a stage where I have and LCD connected, a keypad, capturing input from the keypad and have been playing around with SEROUT with the intention of transmitting a byte or a few bytes wirelessly.
To begin testing (after playing around with serout) I have interconnected my two pics (16f887 and 18f4520) by mean of wires.
Like this :
PIC 16f887 PIC 18F4520
PORTC.6(TX)-----PORTC.7(RX)
PORTC.7(RX)-----PORTC.6(TX)

I have setup my code to send "key pressed or received value is" as a loop text string using serout from
PIC 16f887 to the 18f4520.


loopy:
high LED ' First LED on
Pause 1000 ' Delay for .5 seconds
low led
Pause 1000 ' Delay for .5 seconds
SerOut PORTC.6,T2400,["are you receiving this tx from 16f887 ",datatx]
pause 1000
Goto loopy ' Go back to loop and blink LED forever
End


The 18f4520 is set to receive using serin

SerIn PORTC.7,T2400,DataRX

I press a key on the 4x4 keypad connected to the 18f4520,
the keypress is stored as a variable (myvar) and myvar is made to equal datatx)
I then display datatx (the keypress) on the lcd)
Then I fetch the input (RX) from the other PIC using SERIN
The input is held in variable datarx.
I then display datarx on the LCD

Once that's done I send the same variable datarx to the PC's com port using serout.

And end off with the string "end of transmission" on LCD.
I then use the serout line

Well .... it works.

Problem is it only happens once ! yes one time and then LCD goes blank and no matter how many keystrokes I press nothing else happens :-(

So.... I think perhaps the serout/serin where my problems lie.
Maybe full RX buffers or something else.
Maybe I'm not passing the variables correctly.

I would really appreciate if somone could have a quick look at my code and possibly tell me what I am missing or doing wrong.

Kind regards

Dennis

Here is the code for each PIC
PIC16f887


'*************************************
'Transmit to PIC 18f4520 serout loop
'*************************************
'@__config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_ON & _LVP_OFF & _CP_OFF

'Ocsillator selections here
'OSCCON = %01100001 for 4mhz, or OSCCON = %01110001for 8mhz
' OSCCON = %01110001 'Int CLK 8MHz
' ANSEL= %11111111 '$0F = disable A/D converter
' option_reg = 7 'switch pull-ups ON
'END of oscillator selections
'timer/oscillator defines
' define osc 8 '8MHz
'END of timer/oscillator defines
'clear the ports



OSCCON = %01110001 'Int CLK 8MHz
ANSEL = %00000000 'All digital
OPTION_REG.7 = 0 'Weak pull-ups enabled

DEFINE OSC 8 '8MHz
PORTA=0
PORTB=0
PORTC=0
PORTD=0
PORTE=0
'end of port clear

'Port IO directions and presets for port pins begin here
'TRISX = %76543210 << tris bit order numbering
'TRISA = %11111111 << 'All pins are outputs
'// Define port pins as inputs and outputs ...
TRISA = %00000000 'example only - TRISB = %00001111 ;Make B4-B7 outputs, B0-B3 inputs,
TRISB = %11111111 'for 4x4 keypad all input
TRISC = %10000000
TRISD = %00000000
TRISE.0 = 0
TRISE.1 = 0
TRISE.2 = 0
'End of Port IO directions and presets for port pins begin here


'variables begin here
myvar var byte
datatx var byte
datarx var byte
'end of variables


LED var PORTd.0 ' Alias PORTD.0 to LED



loopy:
high LED ' LED on
Pause 1000 ' Delay for .5 seconds
low led 'LED off
Pause 1000 ' Delay for .5 seconds
SerOut PORTC.6,T2400,["are you receiving this tx from 16f887 ",datatx]
pause 1000
Goto loopy ' Go back to loop and blink LED forever
End
'End of all code


PIC 18F4520



'*************************************
'Keypress display on LCD and TX to wherever
'*************************************

'Ocsillator selections here
OSCCON = $70 'Int CLK 8MHz
OSCTUNE.6 = 1 'PLL 4x
ADCON1= %00001111 '$0F = disable A/D converter
cmcon = 7
INTCON2.7 = 0 'switch pull-ups ON
'END of oscillator selections
'timer/oscillator defines
DEFINE OSC 32 '4x 8MHz
'END of timer/oscillator defines

'Port IO directions and presets for port pins begin here
'TRISX = %76543210 << tris bit order numbering
'TRISA = %11111111 'All pins are outputs
'// Define port pins as inputs and outputs ...
TRISA = %00000000 'example only - TRISB = %00001111 ;Make B4-B7 outputs, B0-B3 inputs,
TRISB = %11111111 'for 4x4 keypad all input
TRISC = %10000000
TRISD = %00000000
TRISE.0 = 0
TRISE.1 = 0
TRISE.2 = 0
'End of Port IO directions and presets for port pins begin here


'variables begin here
myvar var byte
datatx var byte
datarx var byte
'end of variables

'LCD defines begin here
DEFINE LCD_BITS 4 'defines the number of data interface lines (4 or 8)
DEFINE LCD_DREG PORTD 'defines the port where data lines are connected to
DEFINE LCD_DBIT 4 'defines the position of data lines for 4-bit interface (0 or 4)
DEFINE LCD_RSREG PORTD 'defines the port where RS line is connected to
DEFINE LCD_RSBIT 2 'defines the pin where RS line is connected to
DEFINE LCD_EREG PORTD 'defines the port where E line is connected to
DEFINE LCD_EBIT 3 'defines the pin where E line is connected
'DEFINE LCD_RWREG 0 'defines the port where R/W line is connected to (set to 0 if not used)
'DEFINE LCD_RWBIT 0 'defines the pin where R/W line is connected to (set to 0 if not used)
DEFINE LCD_COMMANDUS 2000 'defines the delay after LCDOUT statement
DEFINE LCD_DATAUS 200 'delay in micro seconds
'END of LCD DEFINES

'includes begin here
INCLUDE "modedefs.bas"
include "c:\pbp\samples\keypad.bas"
'end of includes

'Keypad code begins here
DEFINE KEYPAD_ROW 4 ' 4 ROW keypad
DEFINE KEYPAD_ROW_PORT PORTB ' ROW port = PORTB
DEFINE KEYPAD_ROW_BIT 0 ' ROW0 = PORTB.4
DEFINE KEYPAD_COL 4 ' 4 COL keypad
DEFINE KEYPAD_COL_PORT PORTB ' COL port = PORTB
DEFINE KEYPAD_COL_BIT 4 ' COL0 = PORTB.0
DEFINE KEYPAD_DEBOUNCEMS 200 ' debounce delay = 200 mSec
DEFINE KEYPAD_AUTOREPEAT 1 ' use auto-repeat feature
'end keypad code

'Main code begins here

Pause 2000 ' Wait for LCD to startup
start:
'read keypress variable
@ READKEYPAD _myvar
LOOKUP myvar,[0,"123A456B789C*0#D"],myvar 'use lookup table to diplay proper keypress
datatx = myvar 'pass the variable to solve strange character problem
lcdout datatx 'display the variable on the LCD

lcdout $fe,1 'clear lcd screen
'receive section
SerIn PORTC.7,T2400,DataRX 'receive data from other pic (16f887) and store as variable datarx
LCDOUT datarx 'displays data received from serin datarx
pause 2000
lcdout $fe,1 'clear lcd screen
'receive section ends


'transmit section
'send datarx(received data from toher pic) to pc serial port
serout PORTC.6,T2400,["the key pressed or received value is ",datarx] 'SerOut PinNumber2,T2400,DataRec (FIXED!!
pause 1000
'transmit section ends
LCDout "end of transmission"
lcdout $fe,1 'clear lcd screen

goto start

'End of all code

mackrackit
- 24th November 2009, 10:06
A little history to get you started... :)
http://www.picbasic.co.uk/forum/showthread.php?t=1944&highlight=idle
let us know if the above helps.

Dennis
- 24th November 2009, 20:34
Hi Dave

thanks for the history lesson re serin ..very informative ...
The posting was a little confusing in that (unless I missed it!) it is not clear on how to get the port to dile low IDLE high or IDLE low.
Is is as simple as somthing like
portX.Y low or high after the serin line ?
Note that I am not sending anything over RF yet ... the two PIC's are just wired together at this stage.

Should I rather try DEBUG or serin/out 2 ?
Do they suffer from the same issues?

I would like to make sure I get everything ready before I attach the wireless RF and TX , maximizing both power and rate on the TX/RX pairs, other wise why bother ...not so ?

Any further help would be much appreciated :-)

Kind regards

Dennis

mackrackit
- 24th November 2009, 21:41
For PIC to PIC Debug is not the way. SERIN is good for basic stuff. SERIN2 has a whole lot more options and uses the same syntax as HSERIN when you need it. The nice thing about the SERIN/2 is it can be set for TRUE or INVERTED while HSERIN is TRUE only requiring an inverter to connect to things like a PC, but that is a minor thing with the other benifits of hardware serial.

http://www.melabs.com/resources/samples.htm
Scroll down to the ser2mod examples for...examples....

When it comes time to idle low or high that can be done with a resistor 100K. Or in your code.

Dennis
- 24th November 2009, 21:58
SERIN2
or
HWSER right ?

You mentioned being able to set the IDLE in software if I am going to be using SERIN...
That's what I was asking in the last post :-)

In the post you pointed me to it mentions the issues/problems with SERIN and to set it IDLE ...
SO my question is how do you set the IDLE using software ?

Is there a document regarding the IDLE setting methods ?

Kind regards

Dennis

mackrackit
- 24th November 2009, 22:32
Normally the mode takes care of the idle state
http://www.picbasic.co.uk/forum/showpost.php?p=60192&postcount=7

Charles Linquis
- 25th November 2009, 13:28
I hate to sound like a broken record - but... Always use the hardware serial port if your device has one. You will be glad you did.

Dennis
- 25th November 2009, 21:20
Thanks Dave and Charles :-)

But where do I start with hardware ports?

I got the impression reading through the forums that software serin/out and serin2/serout2 and debug would be easier to play around with.
I ssetting up the hardware option just as easy ?

I also read somewhere that serin needs to be setup to look for something first before it starts accepting data ? Is this why I after one keypress, nothing else appears on the LCD ?

Is there a some code I could use just to test the TX/RX function between 2 PIC's using wires ( see my diagram in earlier posts) , once I have that working I can then move onto TX and RX using wireless

Something like setup PIC A to send a string or a byte to PIC B and then have PIC B send the same byte to hyperterminal(pc).

Could I use serin2 first then serout (not serout2) to send the string or byte to hyperterminal(pc).

Have I made the correct choice for send and receive ports ..on both pics PORTC.6 and PORTC.7

Any help or advice would really be appreciated.

Kind regards

Dennis

Bill Legge
- 25th November 2009, 23:06
Dennis,

A few thoughts on linking PICs by RS232:

1. I've done it - unfortunately, deleted the code after getting it working.

2. I used SERIN2 and SEROUT2, by habit rather than for good technical reasons.

3. HSEROUT/HSERIN do pass much of the processing down to the chip hardware and save lots of processing time and code space. However, if you are still in the 'getting something working stage' I'd use SEROUT/SERIN to avoid additional DEFINES and so on needed for HSEROUT/IN.

4. I've had a short read through your code and not spotted any problems. I'd suggest that you temporarily abandon the keypad business and write a very basic routine to send/receive one byte variable PIC-Hyperterm and then PIC-PIC. Start using SEROUT/SERIN then move on to SEROUT2/SERIN2 and then HSEROUT/HSERIN.

5. Have you got carriage returns (Decimal '13') at the end of each transmission?

6. I've not looked up the data for your chips but what is TRISX?

7. Just had a longer look at the code and I think your sequencing is wrong? Should be:

a. PICA. Press key
b. PICA. Send to the other PIC (I think you receive here?)
c. PICB. Receive data
d. PICB. Send data
e. PICA. Receive data
f. PICA. Display data on LCD
g. PICA. Press key......

Watch those CRs!

8. I assume that each end of the PIC to PIC RS232 link is symetrical i.e both have RS232 chips for level conversion or both do not?

Regards Bill legge

Dennis
- 26th November 2009, 01:34
Hi Bill

Thanks a million for the informative response and advice :-)
Here is my reply to your examination :
1.Damn, just my luck ! Nice to hear you came right though :-0
2.I wouldn't mind using either have those but found issues with serout2 and the data displayed in hyperterminal so used serout.
What are your thoughts on using DEBUG ?
3.I did notice in reading through other forum posts and the manual that DEFINES are required.
4.I was thinking of doing exactly the same thing just to remove also possible room for error :-)
5. No I do not have carriage returns at the end of each transmission are you referring to 10,13 at the end of serout ?
I was under the notion that I could just SEROUT ping and port at 2400T and the variable name all in a loop.... is this where my problem is...apart from being a little rusty and having to re-live been a PIC newbie again ?
6.TRISX is a very secret register which you enable my moving the shift in the time space continum on a PIC 18f4520 ....:-) lol just kidding.
OK jokes aside TRISX is the comment I placed there so that I could remember how to manipulate TRIS for IO.
7. Sequencing is correct ...just realised I have not added the connection diagram :-( will add it to this post now.
I am sending from PIC 16f887 PIC 18F4520
PORTC.6(TX)-----PORTC.7(RX)
PORTC.7(RX)-----PORTC.6(TX)
so it's PIC16F887 >>PIC18F4520 >>PC(hyperterminal)
out in | out
TX RX | TX
|
LCD IS HERE (wired to 18F4520)
Once again ...where are the CR's and where should they go exaclty ?
8.The only place there is a MAX232N is on the TX side of the 18F4520 so that is can connect to PC serial port, in which case I am able to send and receive data to and from the PC :-)

Hope this helps , will post more info just now to make it simpler to read ;-)
Any more info would be much appreciated.

Kind regards
Dennis

mackrackit
- 26th November 2009, 02:02
Just to add to the confusion...:D
DEFINES are not required.
Demo:


X VAR BYTE
START:X = X + 1
HIGH PORTD.2 :PAUSE 250:LOW PORTD.2:PAUSE 250
RCSTA=$90:TXSTA=$24:SPBRG=129:HSEROUT ["HSER ",DEC X,13,10]
PAUSE 250:RCSTA=$0:SEROUT2 PORTC.6,84,["SER ",DEC X,13,10]
GOTO START
Sometimes complete control is handy.

Dennis
- 26th November 2009, 02:12
Hi Dave ...how are you ?

Mmmm that sure got me :-)

An here I am still trying to at least get some comms going :-( !

I have the champagne ready ...now just to find the ship to smash it open on and the people to join my 'I can finally TX/RX' between PIC's celebrations...

All I can say for now is... WATCH THIS SPACE ! LOL

Kind regards

Dennis

mackrackit
- 26th November 2009, 02:22
Smoke and mirrors???
:)

Here is a simple example to build on

SEND:


SEROUT PORTC.4,T2400,[9,3]


RECEIVE:


SERIN PORTC.4,T2400,[9],net

IF net = 3 THEN DO_SOMETHING

Connect VSS and the two PIC pins together.

Dennis
- 26th November 2009, 21:49
Dave

Will try the operation with only your code...
Can one run SERIN and SEROUT on same pic as I would like to send the received data to hyperterminal and also to the LCD. So would the fol.lowing code be correct then ?
Oh and why did you choose PORTC.4 ? Looking at the pinouts for both pics I see PORTC.6 is TX and PORTC.7 is RX -are these for hardware serial , ie. HSERIN and HSEROUT and should I use them for SERIN and SEROUT or is it better to use PORTC.4 in case I want to use them for HSERIN/OUT later ?

Here's the code

Transmitter PIC (16F887)


SEROUT PORTC.4,T2400,[9,3]


Receiver PIC (18F4520)


main:

SERIN PORTC.4,T2400,[9],net 'prepare to receive the data from other PIC
IF net = 3 LCDOUT "data is",net 'look for the number 3
SEROUT PORTC.5,T2400,[9,3] 'send received data to hyperterminal via max232n to PC
end if
goto main
end


Smoke and mirrors indeed :-)

Kind regards

Dennis

mackrackit
- 26th November 2009, 22:07
I went with PORTC4 for no good reason other than to show that the bit banging routines work on most any pin. You are correct about HSERN only working on the hardware pins.

What I can see of your code(using phone) it looks like it should work.

Dennis
- 26th November 2009, 22:18
Dave

Thanks a million for the feedback

Will try it right away and feedback asap :-)

Kind regards

Dennis

Dennis
- 27th November 2009, 02:14
Hi Dave

Good news and a big thank you !!
It's working...
I am transmitting from 16F887 to 18F4520 and then from 18F4520 to and LCD and to PC serial port.

One tiny issue I have is with the serin command.
It displays a string fine but the variable is always a wingdings character unless I use a lookup ...(see my code below for serout on receiver)
So I have three questions now ....
1. Is there any way to set the serin command to send the variable (net) in the code example so that it appears as the number 3 ?
2.Could you possibly show me a code snippet to accomplish the same thing using debug and also hser please
3. Bearing in mind I want to eventually to accomplish all of this using RF modules, and may decide to use hserin/out ,and all I want to do is relaible transmit 3 or possible 4 bytes, would I have to use an external oscillator or would the internal suffice ?

Kind regards
Dennis

mackrackit
- 27th November 2009, 02:34
Try
DEC net

OSCs..... Some will not agree but many times times the internal is not stable enough at higher baud rates to be reliable.

mackrackit
- 27th November 2009, 03:08
To set DEBUG up


DEFINE DEBUG_MODE 0 ' Debug sending TRUE serial data
DEFINE DEBUG_REG PORTC ' Debug Port = PortC
DEFINE DEBUG_BIT 4 ' Debug.bit = PortC.4
DEFINE DEBUG_BAUD 2400 ' Default baud rate = 2400


Send


DEBUG 9,3


Receive


DEBUGIN [WAIT("9"),DEC net]


The same syntax works for HSERIN/OUT

Dennis
- 28th November 2009, 00:41
Hi Dave :-)

How are you ?

Tried the code exactly as you suggested ...

On the receiver end it seems like debug is just WAITing forever to receive the number 9 match but it never seems to get it, so that's where the program stops.
I thought it might be the order of the numbers so I changed their order but that didn't fix the problem.

Is it possible that the problem could be on the receiver PIC as I still have a serout line to send the data to the PC serial port to view in hyperterminal.
Is it ok to run debug and serin in the same PIC ( wondering if this is a silly question) or should I use debug to transmit to hyperterminal as well ?

Could the problem be that I have the DEFINE statements in the wrong place ? I noticed an extra space character in the code snippet you posted between mode and 0 .. removed that and recompiled but still no joy :-(

Any suggestions ?

Here's the code :
Receiver


'debug defines begin here - necessary for degug operation
DEFINE DEBUG_MODE 0 ' Debug sending TRUE serial data
DEFINE DEBUG_REG PORTC ' Debug Port = PortC
DEFINE DEBUG_BIT 4 ' Debug.bit = PortC.4
DEFINE DEBUG_BAUD 2400 ' Default baud rate = 2400
'debug defines end here

'Main code begins here

Pause 2000 ' Wait for LCD to startup

test:
lcdout $fe,1
lcdout "starting tx rx"
'*******************************************
'*SERIN PORTC.4,T2400,[9],net 'prepare to receive the data from other PIC
'*this lines works perfectly to send data
'*******************************************
'
'****heres the same thing using debugin (RX)- check/adjust defines for settings
DEBUGIN [WAIT("9"),DEC net]
'****end of debug line
lcdout "rx complete"
pause 1000
lcdout $fe,1 'clear lcd screen
IF net = 3 then
lcdout $fe,1
LCDOUT "data is ",dec net 'look for the number 3
pause 2000
endif
lcdout $fe,1
lcdout "begin serout to hyperterminal"
pause 1000
LOOKUP net,[0,"123A456B789C*0#D"],net 'use lookup table to diplay proper display in hyperterminal
' send net variable to hyperterminal using serout
serout PORTC.3,T2400,["the key pressed or received value is ",net,10,13]
lcdout $fe,1
goto test
end


Transmitter


'debug defines begin here
DEFINE DEBUG_MODE 0 ' Debug sending TRUE serial data
DEFINE DEBUG_REG PORTC ' Debug Port = PortC
DEFINE DEBUG_BIT 4 ' Debug.bit = PortC.4
DEFINE DEBUG_BAUD 2400 ' Default baud rate = 2400
'end of debug defines

LED var PORTd.0 ' Alias PORTD.0 to LED



loopy:
high LED ' LED on
Pause 1000 ' Delay for .5 seconds
low led 'LED off
Pause 1000 ' Delay for .5 seconds
'*this serout lines works perfectly :-)
'SEROUT PORTC.4,T2400,[9,3]
'**another example - SerOut PORTC.6,T2400,["are you receiving this tx from 16f887 ",datatx]
'DEBUG line starts here
DEBUG 3,9
'end of debug line

pause 1000
Goto loopy ' Go back to loop and blink LED forever
End
'End of all code

Dennis
- 28th November 2009, 01:23
Hi again Dave

Thanks for your help so far , I really am learning a lot :-)

To double check that nothing on the circuit had changed I loaed the code for the serin and serout examples from earlier and all still works , which tells me the problem has something to do with the debug code.

Reading through the help and forums and manual I noticed that DEBUGIN requires it's own set of defines .
I'm still checking to see if the baud rate must be defines eg. debugin_baud ..which I tried anyway and the compiler didn't complain.
So I tried it the code again with the updated degug statements but still no joy :-(

Here's the updated receiver code:
Receiver


'debugin defines
DEFINE DEBUGIN_REG PORTC ' Set Debugin port
DEFINE DEBUGIN_BIT 4 ' Set Debugin pin bit
DEFINE DEBUGIN_MODE 0 ' Set Debugin mode: 0 = true, 1 = inverted
Define DEBUGIN_BAUD 2400 'set Debugin baud rate
'Debugin defines end here
'debugin commands end here

'debug defines begin here - necessary for degug operation
' DEFINE DEBUG_MODE 0 ' Debug sending TRUE serial data
' DEFINE DEBUG_REG PORTC ' Debug Port = PortC
' DEFINE DEBUG_BIT 4 ' Debug.bit = PortC.4
' DEFINE DEBUG_BAUD 2400 ' Default baud rate = 2400
'debug defines end here

'Main code begins here

Pause 2000 ' Wait for LCD to startup

test:
lcdout $fe,1
lcdout "starting tx rx"
'*******************************************
'*SERIN PORTC.4,T2400,[9],net 'prepare to receive the data from other PIC
'*this lines works perfectly to send data
'*******************************************
'
'****heres the same thing using debugin (RX)- check/adjust defines for settings
DEBUGIN [WAIT("9"),DEC net]
'****end of debug line
lcdout "rx complete"
pause 1000
lcdout $fe,1 'clear lcd screen
IF net = 3 then
lcdout $fe,1
LCDOUT "data is ",dec net 'look for the number 3
pause 2000
endif
lcdout $fe,1
lcdout "begin serout to hyperterminal"
pause 1000
LOOKUP net,[0,"123A456B789C*0#D"],net 'use lookup table to diplay proper display in hyperterminal

serout PORTC.3,T2400,["the key pressed or received value is ",net,10,13]
lcdout $fe,1
lcdout datatx
pause 1000
goto test
end


Kind regards

Dennis

mackrackit
- 28th November 2009, 02:20
Try
DEBUG "9",DEC 3

Dennis
- 28th November 2009, 02:46
tried it ...still no joy :(

Dennis
- 28th November 2009, 02:47
Dave ...

Do I need the defines on the receiver for debugin ???

mackrackit
- 28th November 2009, 02:56
Yes, you need DEBUGIN and DEBUG defines.

I am giving you this stuff from memory and evidently I have forgotton something. When I get back to the shop tonight I will test it and see what I am missing.

Maybe someone will fill in my blanks

mackrackit
- 28th November 2009, 06:17
The send code


DEFINE DEBUG_MODE 1 ' Debug sending INVERTED serial data
DEFINE DEBUG_REG PORTB ' Debug Port = PortC
DEFINE DEBUG_BIT 3 ' Debug.bit = PortC.4
DEFINE DEBUG_BAUD 2400 ' Default baud rate = 2400

DEBUG "9",DEC 3,$d,$a


The receive code


DEFINE DEBUGIN_MODE 1 ' Debug sending INVERTED serial data
DEFINE DEBUGIN_REG PORTB ' Debug Port = PortC
DEFINE DEBUGIN_BIT 4 ' Debug.bit = PortC.4
DEFINE DEBUGIN_BAUD 2400 ' Default baud rate = 2400

DEBUGIN [WAIT("9"),DEC NET]

I just ran this in real life, it works.
Sorry for giving it wrong before.

Dennis
- 28th November 2009, 11:12
IT WORKS !!!
WOOOOOOHOOOOOOOOOO you ROCK ! :-)

Thank you thank you thank you :-)

By the way that memory of yours was pretty damn close :-)
Seems inverted mode was needed ?

What are the $d and $a for in the line below by the way ?


DEBUG "9",DEC 3,$d,$a

And now for HSERIN and OUT

A few questions first ..

1. With HSERIN and OUT , is it a necessity to use the onboard TX/RX pins , in my case PORTC. and PORTC.7 ?

2. Is it pretty much a case of replacing the debug lines with HSERIN as you said in a n earlier posting ?

Thanks again Dave

Dennis

kevlar129bp
- 28th November 2009, 15:11
Hello Dennis,

I don't know if Dave is online now, so I thought I'de jump in and answer your 2 questions real quick...As Dave has helped me immensely on the same serial in out topic...Thanks again Dave!

1) The $d $a is a "Carriage Return - Line Feed" terminator
2) Yep, the hardware ports have to be used for HSERIN/OUT.

Hope I helped and didn't offend you Dave :)

Enjoying this thread, and learning from it too!

Chris

Dennis
- 28th November 2009, 15:36
Thanks so much :-)

I really appreciate it :-)

I haven't tried it yet as I was awaiting Dave's instruction and possibly a code snippet.
The reason for this is that I like to having a working reference first and then experiment.
I have learned that by doing the wrong things first you battle even more and as the late nights roll by you find your concentration span dwindling more and more no matter how excited or motivated you are.
Not only that, you also find it's so frustrating and de-motivating when things just don't work and you'd like to get on with the next step but you can't because something you have done incorrectly (something as small as a character in the wrong place) is holding you back.Even if someone just points you to a document or flames you for putting the code in the wrong place without the code tags. You learn from this ..logical baby steps count !

It is so nice to have the chance to be involved with such a helpful community who share a common interest. I long for the day when I too have strong enough 'code eyes' and experience to also help others here :-)
Thank you all !!

Kind regards

Dennis

mackrackit
- 28th November 2009, 16:06
Hope I helped and didn't offend you Dave
This is a community effort, the more giving the better.
And it is kinda hard to offend me anyways :)

yup, the DEBUG and HSER syntax is pretty much the same. The DEFINES are different and like Chris said the hardware pins have to be used.

Earlier in this thread I gave an example of using HSER with out DEFINES but for most cases I do use them. Start that way.

If are are going to communicate with a PC and/or some other things an inverter setup is needed, RS232 chips are mostly used for this.

There are some calculations needed for the DEFINES and I cheat by using this
http://www.picbasic.co.uk/forum/showthread.php?t=4994

I am not at my machine again and I learned my lesson about going from memory so no snippets from me at this time. Chris??? Do you have one to get him started???

Dennis
- 28th November 2009, 16:13
Dave ..

Thanks again, but fading memory is better than no memory ( lol or is it ?)

So even if it's wrong it will send me hurtling off in the HSER direction on a voyage of discovery and when I'm truly stumped I will ask for assistance then and only then !

Kind regards

Dennis

Dennis
- 28th November 2009, 16:26
What an awesome calculator proggie ;-)

A true GEM !

Thanks a million

Dennis

mackrackit
- 28th November 2009, 17:32
Thank mister e for the MultiCalc, I use it often.

http://melabs.com/resources/samples/pbp/usart.bas
Play with the above to get started with the USART. Notice they did not use DEFINES.

The program will echo your keyboard with one PIC connected to a PC. The terminal program that is part of MCS is good for this setup. I think the above sample is for 4Mhz. So check the values with MultiCalc.

Keep us posted...

Dennis
- 28th November 2009, 19:03
Hi dave

Thanks again :-)

For my 16f887 i have the following set for using the internal clock


OSCCON = %01110001 'Int CLK 8MHz
ANSEL = %00000000 'All digital
OPTION_REG.7 = 0 'Weak pull-ups enabled

DEFINE OSC 8 '8MHz


So I use 8 MHZ not so ?

For the 184520 the following is set


'Ocsillator selections here
OSCCON = $70 'Int CLK 8MHz
OSCTUNE.6 = 1 'PLL 4x
ADCON1= %00001111 '$0F = disable A/D converter
cmcon = 7
INTCON2.7 = 0 'switch pull-ups ON
'END of oscillator selections
'timer/oscillator defines
DEFINE OSC 32 '4x 8MHz
'END of timer/oscillator defines


So do I choose 4MHz or 32 MHz ?

thanks a million

Kind regards
Dennis

mackrackit
- 28th November 2009, 20:53
For the 887 use 8Mhz for the calcs.

I am not sure about the 4520, I have not used USART with PLL enabled.
But...
I think the USART uses the primary OSC so try 4Mhz. 50 50 chance of it working :p

Dennis
- 28th November 2009, 22:11
Hi Dave

Could I set both PICS to 4 MHz internal OSC ?
And once it's working try variations ?

mackrackit
- 28th November 2009, 22:18
That would be the way to trouble shoot.
Just make sure they are running at the expected speed with a blinky and a stop watch or meter if you have one..

Dennis
- 28th November 2009, 23:09
Hi again Dave

I think I am almost ready for the next step ....
RF and RF module TX RX
I have been reading posts all through the forums, even some of yours from years back ...
Now for the next step ... to try the same or a similar thing without the wires.

Should I start off with sering or plunge straight into HSER ?

The plan is to be able to send 3 bytes using the most reliable method.

So the question is where do I start and what do I start with ?

Kind regards
Dennis

mackrackit
- 29th November 2009, 03:14
So do I choose 4MHz or 32 MHz ?
I have it on good authority that the PLL speed is the one to use. Thanks Bruce!!!

What radio modules are you using? I have only used the ones from rentron.com.

As for reliability, all of the serial methods (HSER,SER,SER2..) are good. Are you planning to use interrupts with the serial input or just wait and time out, check later sort of thing?

Dennis
- 29th November 2009, 11:43
Hi Dave

:-)

So are you saying I should use 8MHz OSC and 4x PLL for the 18F4520 and use 32 MHz when I do the HSER DEFINE calculations then ?
And on the 16F887 use it's max INTOSC which is 8 MHZ for the HSER calculations ?
OR....
set both PIC's at 4 or 8 MHz and then calculate the HSER DEFINEs accordingly ?

Something like this maybe


‘For 8MHz @ 2400bps

RCSTA = $90 ' Enable serial port & continuous receive
TXSTA = $20 ' Enable transmit, BRGH = 0
SPBRG = 51 ' 2400 Baud @ 8MHz, 0.17%

DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER_TXSTA 20h ' Enable transmit, BRGH = 0
DEFINE HSER_SPBRG 51 ' 2400 Baud @ 8MHz, 0.17%
DEFINE HSER_CLROERR 1 ' Clear overflow automatically

‘For 4 MHz @ 2400bps

RCSTA = $90 ' Enable serial port & continuous receive
TXSTA = $20 ' Enable transmit, BRGH = 0
SPBRG = 25 ' 2400 Baud @ 4MHz, 0.17%

DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER_TXSTA 20h ' Enable transmit, BRGH = 0
DEFINE HSER_SPBRG 25 ' 2400 Baud @ 4MHz, 0.17%
DEFINE HSER_CLROERR 1 ' Clear overflow automatically



A quick question here ...
Which comes first ... DEFINE's or register settings ?

Chat soon

Dennis

Dennis
- 29th November 2009, 12:55
Hi Dave

Have been reading the very interesting HSER info form all over.

Let's say that I have the PIC's connected as follows :
PIC 16F887 -- PIC 18F4520 -- PC(com port)

At PIC 16F887 the code is


'USART defines and register settings begin here - use for HSERIN/OUT
RCSTA = $90 ' Enable serial port & continuous receive
TXSTA = $20 ' Enable transmit, BRGH = 0
SPBRG = 51 ' 2400 Baud @ 8MHz, 0.17%

DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER_TXSTA 20h ' Enable transmit, BRGH = 0
DEFINE HSER_SPBRG 51 ' 2400 Baud @ 8MHz, 0.17%
DEFINE HSER_CLROERR 1 ' Clear overflow automatically
' Check port settings PORTC.6 = TX and PORTC.7 = RX
' For PIC to PIC TX to RX C.6 to C.7 and visa versa
' Don't forget TRISC=%10000000
'USART defines and register settings end here

main: 'tx loop begins
high LED ' LED on
Pause 1000 ' Delay for 1 seconds
HSERout "9",DEC 3,$d,$a
low LED ' LED off
pause 1000
goto main


On the 18F4520 (middleman RX and TX PIC in my setup)


'USART defines and register settings begin here - use for HSERIN/OUT
RCSTA = $90 ' Enable serial port & continuous receive
TXSTA = $20 ' Enable transmit, BRGH = 0
SPBRG = 51 ' 2400 Baud @ 8MHz, 0.17%

DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER_TXSTA 20h ' Enable transmit, BRGH = 0
DEFINE HSER_SPBRG 51 ' 2400 Baud @ 8MHz, 0.17%
DEFINE HSER_CLROERR 1 ' Clear overflow automatically
' Check port settings PORTC.6 = TX and PORTC.7 = RX
' For PIC to PIC TX to RX C.6 to C.7 and visa versa
' Don't forget TRISC=%10000000
'USART defines and register settings end here

main: 'tx and rx loop begins here
high LED 'LED on
Pause 1000 ' delay for 1 second
'HSER lines begine here
HSERIN [WAIT("9"),DEC NET] '<< this is the format for the receiver or if you're looping
LCDOUT $fe,1
LCDOUT "I received the number .." DEC NET
HSERout "I received the number",DEC NET,$d,$a 'data to COM port on pc
'HSER lines end here
goto main
end


And on the PC hyperterminal or MCS serial tool is waiting to receive and/or send back to PIC

Have I got it right ?

Kind regards

Dennis

Dennis
- 29th November 2009, 14:03
Dave ...........

IT works :-)

16F887 runnning @ 8 MHz internal clock
to 18F4520 running @ 32 MHz internal clock
to PC com port @ 2400bps 8N1

and the readout in plain text at MCS serial tool/hyperterminal is...
I received the number 93
I received the number 93
I received the number 93 etc
:-)
Not sure why it's not just displaying the number 3 which is what I was expecting ... a bug in the code for HSER lines perhaps ????

But besides that .....YAY !

And now for the next mission ....
RF TX/RX ....

Only 5 step process for now ( or more LOL depending on the TX/RX protocol,modules and more I guess --wish me luck !)

1. Wire up RF modules to PIC's
2. Remove existing TX/RX wires between PIC's ( keep avaiable for tests)
3. Wire the transmitter PIC's TX to RF TX module
4. Wire the receiver PIC's RX to RF RX module
5. Test for comms :-) << hold thumbs

Any tips would be highly appreciated and noted !

Kind regards

Dennis

mackrackit
- 29th November 2009, 16:52
HSERout "9",DEC 3,$d,$a
Should be
HSERout ["9",DEC 3,$d,$a]

Maybe that is why you are getting the "9"???



RCSTA = $90 ' Enable serial port & continuous receive
TXSTA = $20 ' Enable transmit, BRGH = 0
SPBRG = 51 ' 2400 Baud @ 8MHz, 0.17%

DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER_TXSTA 20h ' Enable transmit, BRGH = 0
DEFINE HSER_SPBRG 51 ' 2400 Baud @ 8MHz, 0.17%
DEFINE HSER_CLROERR 1 ' Clear overflow automatically
Using DEFINE the part in BOLD is not needed.
Other than that it looks OK to me...

For the radios... What modules are you using? Did you read the stuff from Dave Houston, http://davehouston.org/ ?

There is some disagreement on the forum about the preamble/synch protocol. I think Dave's method is the way to go.

Dennis
- 29th November 2009, 18:54
Hi Dave :-)

As reagrds the 93 compiler picked that one up :-)
So your code-eyes are working!!

I have still have that small problem somewhere and can't quite figure out where :-( the 93 issue
It seems like HSERIN is happy to pass the number 9 into the variable as well, even though thats the number it's supposed to wait for !

Think it's somewhere in the HSERIN/OUT lines ... and I don't think I can move on to the radio portion unless I find the problem :-(
Do you think you could please cast your 'code eyes' over my code please.

Here's the Transmitter code


'*************************************
'Transmit to PIC 18f4520 serout loop
'*************************************
'@__config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_ON & _LVP_OFF & _CP_OFF

'Ocsillator selections here
'OSCCON = %01100001 for 4mhz, or OSCCON = %01110001for 8mhz
' OSCCON = %01110001 'Int CLK 8MHz
' ANSEL= %11111111 '$0F = disable A/D converter
' option_reg = 7 'switch pull-ups ON
'END of oscillator selections
'timer/oscillator defines
' define osc 8 '8MHz
'END of timer/oscillator defines
'clear the ports



OSCCON = %01110001 'Int CLK 8MHz
ANSEL = %00000000 'All digital
OPTION_REG.7 = 0 'Weak pull-ups enabled

DEFINE OSC 8 '8MHz
PORTA=0
PORTB=0
PORTC=0
PORTD=0
PORTE=0
'end of port clear

'Port IO directions and presets for port pins begin here
'TRISX = %76543210 << tris bit order numbering
'TRISA = %11111111 << 'All pins are outputs
'// Define port pins as inputs and outputs ...
TRISA = %00000000 'example only - TRISB = %00001111 ;Make B4-B7 outputs, B0-B3 inputs,
TRISB = %11111111 'for 4x4 keypad all input
TRISC = %10100000
TRISD = %00000000
TRISE.0 = 0
TRISE.1 = 0
TRISE.2 = 0
'End of Port IO directions and presets for port pins begin here

'includes begin here
INCLUDE "modedefs.bas"
include "c:\pbp\samples\keypad.bas"
'end of includes

'variables begin here
myvar var byte
datatx var byte
datarx var byte
net var byte
'end of variables


'USART defines and register settings begin here - use for HSERIN/OUT
RCSTA = $90 ' Enable serial port & continuous receive
TXSTA = $20 ' Enable transmit, BRGH = 0
SPBRG = 51 ' 2400 Baud @ 8MHz, 0.17%

DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER_TXSTA 20h ' Enable transmit, BRGH = 0
DEFINE HSER_SPBRG 51 ' 2400 Baud @ 8MHz, 0.17%
DEFINE HSER_CLROERR 1 ' Clear overflow automatically
' Check port settings PORTC.6 = TX and PORTC.7 = RX
' For PIC to PIC TX to RX C.6 to C.7 and visa versa
' Don't forget TRISC=%10000000
'USART defines and register settings end here

'main code begins here
LED var PORTd.0 ' Alias PORTD.0 to LED



loopy:
high LED ' LED on
Pause 1000 ' Delay for 1 seconds


'HSER starts here
HSERout ["9",DEC 3,$d,$a]
'HSERIN [WAIT("9"),DEC NET] << this is the format for the receiver
'HSER ends here

low led 'LED off
Pause 1000 ' Delay for 1 seconds
Goto loopy ' Go back to loop and blink LED forever
End
'End of all code



Here's the receiver code



'************************************************* *
RX from anywhere display on LCD and TX to wherever (PIC 18F4520)
'************************************************* **

'Ocsillator selections here
'DEFINE OSC 8
'OSCCON=%01110000
'or
'DEFINE OSC 4
'OSCCON=%01100000
'OSCTUNE.6 = 0 'PLL Multiplier disabled
'OSCTUNE.6 = 1 'PLL 4x
'Ocsillator selections here
OSCCON = $70 'Int CLK 8MHz
OSCTUNE.6 = 1 'PLL 4x
ADCON1= %00001111 '$0F = disable A/D converter
cmcon = 7
INTCON2.7 = 0 'switch pull-ups ON
'END of oscillator selections
'timer/oscillator defines
DEFINE OSC 32 '4x 8MHz
'END of timer/oscillator defines

'Port IO directions and presets for port pins begin here
'TRISX = %76543210 << tris bit order numbering
'TRISA = %11111111 'All pins are inputs
'// Define port pins as inputs and outputs ...
TRISA = %00000000 'example only - TRISB = %00001111 ;Make B4-B7 outputs, B0-B3 inputs,
TRISB = %11111111 'for 4x4 keypad all input
TRISC = %10010000
TRISD = %00000000
TRISE.0 = 0
TRISE.1 = 0
TRISE.2 = 0
'End of Port IO directions and presets for port pins begin here


'variables begin here
A0 var byte
myvar var byte
datatx var byte
datarx var byte
net var byte
led var PORTD.0
'end of variables

'LCD defines begin here
DEFINE LCD_BITS 4 'defines the number of data interface lines (4 or 8)
DEFINE LCD_DREG PORTD 'defines the port where data lines are connected to
DEFINE LCD_DBIT 4 'defines the position of data lines for 4-bit interface (0 or 4)
DEFINE LCD_RSREG PORTD 'defines the port where RS line is connected to
DEFINE LCD_RSBIT 2 'defines the pin where RS line is connected to
DEFINE LCD_EREG PORTD 'defines the port where E line is connected to
DEFINE LCD_EBIT 3 'defines the pin where E line is connected
'DEFINE LCD_RWREG 0 'defines the port where R/W line is connected to (set to 0 if not used)
'DEFINE LCD_RWBIT 0 'defines the pin where R/W line is connected to (set to 0 if not used)
DEFINE LCD_COMMANDUS 2000 'defines the delay after LCDOUT statement
DEFINE LCD_DATAUS 200 'delay in micro seconds
'END of LCD DEFINES

'includes begin here
INCLUDE "modedefs.bas"
include "c:\pbp\samples\keypad.bas"
'end of includes

'USART defines and register settings begin here - use for HSERIN/OUT

RCSTA = $90 ' Enable serial port & continuous receive
TXSTA = $20 ' Enable transmit, BRGH = 0
SPBRG = 207 ' 2400 Baud @ 32MHz, 0.17%

DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER_TXSTA 20h ' Enable transmit, BRGH = 0
DEFINE HSER_SPBRG 207 ' 2400 Baud @ 32MHz, 0.17%
DEFINE HSER_CLROERR 1 ' Clear overflow automatically

' Check port settings PORTC.6 = TX and PORTC.7 = RX
' For PIC to PIC TX to RX C.6 to C.7 and visa versa
' Don't forget TRISC=%10000000
'USART defines and register settings end here


'Main code begins here

Pause 2000 ' Wait for LCD to startup

test:

high LED

'HSER starts here -change or comment these for desired direction RX /TX

HSERIN [WAIT("9"),DEC NET] '<< this is the format for the receiver or if

' HSERout "I Recived this number ",DEC NET,$d,$a
'HSER ends here

lcdout $fe,1
lcdout "starting"
lcdout $fe,1
lcdout "POSTTX hser is ", dec net 'show variable net colelcted by hserin a few lines up
Pause 1000
Lcdout $fe,1
lcdout "rx complete"
pause 1000
lcdout $fe,1 'clear lcd screen
lcdout dec net 'display status of net variable
pause 3000
IF net = 3 then
lcdout $fe,1
LCDOUT "MATCH for ",dec net 'look for the number 3 and display MATCH string
low LED
pause 2000
endif
lcdout $fe,1
lcdout "begin hserout to hyperterminal" 'show beginning of TX to PC com port
pause 1000
LOOKUP net,[0,"123A456B789C*0#D"],net 'use lookup table to diplay proper display in hyperterminal

'serout PORTC.3,T2400,["the key pressed or received value is ",net,10,13]
HSERout ["I Recived this number ",DEC NET,$d,$a] 'net var value should show in hyperterminal
lcdout $fe,1
lcdout "transmission complete"
pause 1000
goto test
end



As regards the RF modules...
I have the TWS and RWS ones from Rentron and I have some from Telecontrolli namely the RT5 and RRS3

Not sure what you are referring to about the davehouston site ?? What exactly should I be looking at ?
I have communicated with Dave because I was trying to get his TX/RX (which uses pulsein and debug) solution implemented in my project but it seems I can't read the correct pules .. still don't know why :-(

Kind regards

Dennis

mackrackit
- 30th November 2009, 11:02
The Dave Houston page to read, but study his whole site for that matter.
http://davehouston.org/RFTipsTricks.htm

If you are using DEFINE to set up the HSER then do not add these lines. That is what the DEFINES are for.


RCSTA = $90 ' Enable serial port & continuous receive
TXSTA = $20 ' Enable transmit, BRGH = 0
SPBRG = 51 ' 2400 Baud @ 8MHz, 0.17%


The 93 thing has me confused at the moment.
You might try SERIN2/SEROUT2 for giggles while you are playing with this serial stuff...

Dennis
- 30th November 2009, 11:15
Dave ...Thanks a million !

So while the error prevails ...

Do you have any code snippets I could try to start testing the RF portion be it SERIN/DEBUG/HSERIN etc.
Perhaps I will find the error in the code while I am experimenting :-)

Kind regards

Dennis

mackrackit
- 30th November 2009, 12:06
Here is part of something from a past project. The "TRAIN" part is the controversial part. But some how the data slicer needs set up. This is where Houstons site is handy..
SEND:


TRAIN VAR BYTE

TRAIN=$55

SEROUT PORTC.4,T2400,[TRAIN,TRAIN,TRAIN,TRAIN,TRAIN,9,3]

RECEIVE:


SERIN PORTC.4,T2400,[9],net

This is just the basic way, you can make it all more reliable by adding checksums and stuff...but this should get you started.

Dennis
- 3rd December 2009, 01:29
Hi Dave :-)
How are you ?

Well the lines you gave me work on the wire ..just like before...so I decided to try the TX/RX RF pair TLP-434 and RLP-434 from Bruce at Rentron.
And .... it works ...kinda :-)
Instead of seeing the number 3 (DEC 3) it shows me the number 2 (DEC 2) in MCS serial terminal window.
oh and it also seems to bypass the if NET=3 statement/test for some reason ..I don't think it ever 'sees' a 3 coming in.
So now I'm wondering where I have gone wrong :-(

Here's my receiver code :


'*************************************
'Keypress display on LCD and TX to wherever
'*************************************

'Ocsillator selections here
'DEFINE OSC 8
'OSCCON=%01110000
'or
'DEFINE OSC 4
'OSCCON=%01100000
'OSCTUNE.6 = 0 'PLL Multiplier disabled
'OSCTUNE.6 = 1 'PLL 4x
'Ocsillator selections here
OSCCON = $70 'Int CLK 8MHz
OSCTUNE.6 = 1 'PLL 4x
ADCON1= %00001111 '$0F = disable A/D converter
cmcon = 7
INTCON2.7 = 0 'switch pull-ups ON
'END of oscillator selections
'timer/oscillator defines
DEFINE OSC 32 '4x 8MHz
'END of timer/oscillator defines

'Port IO directions and presets for port pins begin here
'TRISX = %76543210 << tris bit order numbering
'TRISA = %11111111 'All pins are inputs
'// Define port pins as inputs and outputs ...
TRISA = %00000000 'example only - TRISB = %00001111 ;Make B4-B7 outputs, B0-B3 inputs,
TRISB = %11111111 'for 4x4 keypad all input
TRISC = %10010000
TRISD = %00000000
TRISE.0 = 0
TRISE.1 = 0
TRISE.2 = 0
'End of Port IO directions and presets for port pins begin here

'variables begin here

net var byte

led var PORTD.0
'end of variables

'includes begin here
INCLUDE "modedefs.bas"
include "c:\pbp\samples\keypad.bas"
'end of includes

main:
high LED
'RF SERIN line here

SERIN PORTC.4,T2400,[9],net
serout PORTC.3,T2400,["the key pressed or received value is ",net,10,13]
low led
IF net = 3 then
low LED
serout PORTC.3,T2400,["the key pressed or received value is ", net,10,13]
pause 100
endif
'LOOKUP net,[0,"123A456B789C*0#D"],net 'use lookup table to diplay proper display in hyperterminal
'************************SEROUT TO HYPERTERMINAL
goto main
end


and the transmitter code




'*************************************
'16F887 Transmit to PIC 18f4520 serout loop
'*************************************
'@__config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_ON & _LVP_OFF & _CP_OFF

'Ocsillator selections here
'OSCCON = %01100001 for 4mhz, or OSCCON = %01110001for 8mhz
' OSCCON = %01110001 'Int CLK 8MHz
' ANSEL= %11111111 '$0F = disable A/D converter
' option_reg = 7 'switch pull-ups ON
'END of oscillator selections
'timer/oscillator defines
' define osc 8 '8MHz
'END of timer/oscillator defines
'clear the ports



OSCCON = %01110001 'Int CLK 8MHz
ANSEL = %00000000 'All digital
OPTION_REG.7 = 0 'Weak pull-ups enabled

DEFINE OSC 8 '8MHz
PORTA=0
PORTB=0
PORTC=0
PORTD=0
PORTE=0
'end of port clear

'Port IO directions and presets for port pins begin here
'TRISX = %76543210 << tris bit order numbering
'TRISA = %11111111 << 'All pins are outputs
'// Define port pins as inputs and outputs ...
TRISA = %00000000 'example only - TRISB = %00001111 ;Make B4-B7 outputs, B0-B3 inputs,
TRISB = %11111111 'for 4x4 keypad all input
TRISC = %10100000
TRISD = %00000000
TRISE.0 = 0
TRISE.1 = 0
TRISE.2 = 0
'End of Port IO directions and presets for port pins begin here

'includes begin here
INCLUDE "modedefs.bas"
include "c:\pbp\samples\keypad.bas"
'end of includes

'variables begin here


LED var PORTd.0 ' Alias PORTD.0 to LED
'RF variables
TRAIN VAR BYTE
TRAIN=$55
net var byte
'Variables end here


loopy:
high LED ' LED on
'for RF use the line below
SEROUT PORTC.4,T2400,[TRAIN,TRAIN,TRAIN,TRAIN,TRAIN,9,3]
low led
goto loopy
end


Any help would be greatly appreciated

Kind regards
Dennis

mackrackit
- 3rd December 2009, 01:43
Getting a 2 ?? Maybe you just need to add 1 :)

Try sending something other than a 9 or waiting for something other than a 9.
It must be seeing the 9 or it would not go on to the next command.

This is the very most basic code, but it does work. I pulled it from my "laser fence". If the beam is broken the TX kicks in and an alarm goes off at the house.

mackrackit
- 3rd December 2009, 01:50
DEC net
Might be the missing link...on the display

If the LED goes low then I would say it is working, but try sending something else and see what happenes.

Dennis
- 4th December 2009, 02:31
Hi Dave

I still haven't solved the number 3 problem
Where should I place the DEC keyword in ? As I try to compile with DEC keyword in, MCS spits out compile errors.

I am using TLP-434 and RLP-434 modules.
With this in mind would you suggext I now start adding in manchester encoding ?
Should I be trying SERIN/OUT2, DEBUG and HSER first ? (these all worked well when the PIC's were wired together)

I see your lines of serin code pre-condition the receiver first, should i be sending more $55's ?

As always any help or suggestions,tips,tricks etc would be greatly appreciated.

Kind regards

Dennis

mackrackit
- 4th December 2009, 03:22
Well crap. The DEC modifier does not work with straight SERIN. Sorry about that.

Yes, setup for SERIN2/SEROUT2
get that working with wires and we will go from there.

The Manchester stuff is not needed at this time.

Dennis
- 5th December 2009, 20:46
Hi Dave

Great news ;-)

It's working :-) and I'm thrilled --thank you!

Using HSERIN both wired and wireless
WOOOOOHOOOOO!

Now I have a question...
I would say the receiver get's maybe one in every 50 sent packets, if I'm lucky ;-(
What do I need to change or try to make this better ?
The reason I am asking is because I would like to press button 3 on the keypad and that is transmitted and receiver number 3 switches on.
Does that make sense ?

Kind regards

Dennis

dhouston
- 5th December 2009, 21:33
I would say the receiver get's maybe one in every 50 sent packets, if I'm lucky ;-(You need 2-3m physical separation between TX & RX. Otherwise, the superregenerative receiver and the transmitter will interfere with each other.

Dennis
- 5th December 2009, 21:52
Hi Dave Houston

Thanks million for the reply.

I have them even farther apart >3m
If I am closer I receiver more transmitted frames, as I move further so the number decreases.

I tried the same transmitter and receivers using your code as well but have no success when I use it but it does work when the two PICS are connected by wire !

I will most certainly try increasing or reducing the range...
and then update a little later :-)
I will also try your code using the PIC 12F629's as well

If you have any more suggestions I would happily try them and submit feedback

Thanks again

Dennis

mackrackit
- 5th December 2009, 23:46
I think the modules you are using are the same as Bruce sells, the RWS/TWS ones?
What are you using for an antenna and how long is it? Should be around 6.5 inches. Ground plane? Anything around that could be causing interference? A picture of the setup might help.

Can you post the code that is sorta working? At this point it could be a number of things. We have to rule out hardware or software.

Now try to tighten things up a bit.



TRAIN VAR BYTE
train = $B3
SYNK VAR BYTE
SYNK = $7E

SEROUT2 PORT??, BAUD?,[TRAIN, SYNK, DEC 23]




SYNK VAR BYTE
SYNK = $7E
MYVAR VAR BYTE

SERIN2 PORT??, BAUD?, [WAIT(SYNK), DEC MYVAR]

Dennis
- 6th December 2009, 02:48
Dave :-)

It's working like a charm :-)....I think, just getting an

extra character on every second line :-)
Range is awesome in the house, reaches all distant corners

--will do range tests tomorrow and feedback to you.

Here's the OLD code

Which worked but wasn't too accurate

TX CODE



TRAIN VAR BYTE
TRAIN=$55


hSEROUT [TRAIN,TRAIN,TRAIN,TRAIN,TRAIN,"9",dec 3,$d,$a]


RX code


HSERIN [WAIT("9"),DEC NET]

HSERout ["I Recived this number ",DEC NET,$d,$a] 'net var

value should show in hyperterminal



Heres the NEW TX code
USING HSER



DEFINE HSER_RCSTA 90h ' Enable serial port & continuous

receive
DEFINE HSER_TXSTA 20h ' Enable transmit, BRGH = 0
DEFINE HSER_SPBRG 207 ' 2400 Baud @ 32MHz, 0.17%
DEFINE HSER_CLROERR 1 ' Clear overflow automatically

test:
TRAIN VAR BYTE
train = $B3
SYNK VAR BYTE
SYNK = $7E

hserout [TRAIN,SYNK,DEC 23]
goto test




and the RX code:



DEFINE HSER_RCSTA 90h ' Enable serial port & continuous

receive
DEFINE HSER_TXSTA 20h ' Enable transmit, BRGH = 0
DEFINE HSER_SPBRG 207 ' 2400 Baud @ 32MHz, 0.17%
DEFINE HSER_CLROERR 1 ' Clear overflow automatically

test:

high led

hserin [WAIT(SYNK), DEC net]


toggle LED
HSERout ["I Recived this number ",DEC NET,$d,$a] 'net var

value should show in hyperterminal

goto test




Here's my receiver output from MCS serial tool window


I Recived this number 2
I Recived this number 23
I Recived this number 2
I Recived this number 23
I Recived this number 2
I Recived this number 23


As you can see I'm receiving DEC 2 and 23.
Where is the number 2 coming from ?
Eerie ?

Now all I need to do is akind of checksum for example, if

the number 23 is received 3 times then it's a VALID TX.

Any more optimizations or tweaks I should try ?

Thanks a million

Kind regards

(From a very happy:-))
Dennis

mackrackit
- 6th December 2009, 03:09
COOL!!!!

Probably the easiest thing to do for this app would be a simple check.


hserin [WAIT(SYNK), DEC net]
IF net = 23 THEN ...


Or build a checksum... but try the above first, keep it simple.

aratti
- 6th December 2009, 07:14
Dennis, you should remove the DEC command from the HSEROUT see the code below.




A0 var BYTE

A0=23
'hserout [TRAIN,SYNK,DEC 23]
hserout [TRAIN,SYNK, A0]


Al.

Dennis
- 6th December 2009, 07:55
Hi Al

How are you?

Thanks for the tip will definitely try it :-)

Kind regards

Dennis

dhouston
- 6th December 2009, 12:16
I tried the same transmitter and receivers using your code as well but have no success when I use it but it does work when the two PICS are connected by wire !If it works wired but not wireless, the most likely explanation is that you have something amiss in the way you have connected the TX/RX modules.

Dennis
- 6th December 2009, 22:37
Hi Al, Dave and Dave

This appear to be an HSEROUT issue!!

When I use HSERIN with a string before the variables like this - check reciver code HSEROUT line
RECEIVER


'variables begin here
A0 var byte
net var byte
led var PORTD.0
SYNK VAR BYTE
SYNK = $7E

'end of variables

test:

hserin [WAIT(SYNK),DEC net]


if net = 23 then
lcdout "i got 23 "
toggle LED
'lcdout $fe,1
endif

HSERout ["got this",DEC net,$d,$a] 'net var show in hyperterminal

got test

end




TRANSMITTER


'RF variables
'A0 var BYTE
'A0=23
'TRAIN VAR BYTE
'TRAIN=$55
TRAIN VAR BYTE
train = $B3
SYNK VAR BYTE
SYNK = $7E

'Variables end here


loopy:

high LED ' LED on
hserout [TRAIN,SYNK,dec 23]
low led 'LED off
Goto loopy ' Go back to loop and blink LED forever
End


then the number 23 as well as the number 2 appears !!
See here:


got this23
got this2
got this23
got this2
got this23
got this2
got this23
got this2
got this23
got this2


If I remove the string of letters , in this case "got this " then every single received line shows only the number 23 which is what we are expecting to see!
See here:


23
23
23
23
23
3
23
23
23
23
23
23

PERFECT except for once in a while the occasional number like 2 or 3 shows up!

And trying Al's suggestion of removing the DEC modifier (only on receiver side and for both hserin and hserout)
I get this


got this2
got this2
got this2
got this2
got this2
got this2
got this2
got this2


Now tell me that's not a strange one


Now...some really awesome observations from my tests here :

First off this is working like a charm !
It's pretty damn close to near perfect in terms of reliable data transfer! (would love any further suggestions re checksums -- I though of something like catching a few instanced of the byte and then accepting it as correct!

Range is awesome ! Right now, as I am typing this the Transmitter is outside my house at the edge of the property - well over 200m and I have taken it upstairs and downstairs every little nook and cranny and it's still spot on !

Speed is fantastic - its so fast the led hardly blinks and the MCS serial tool window is filled up in less than a second ;)

I have both receiver and transmitter hooked up to a 9V battery and then into a 5V regulator with no caps anywhere. My entire house is flooded with 2,4 and 5,8 GHz wireless and there are electrical appliances plentifold.

If the receiver is left in powered on happily waiting for it's signal , as the transmitter is powered on the receiver has already caught at least 6 bytes :-) ...this is how wireless should be in a perfect world :-)
I would be happy to report that the combination of train and sync byte are a winner! Spot on Mackrackit - thank you!



I am extremely happy with verything so far and just want to thank all of you who have guided me with code snippets, corrections and everything else!

Thank you so much !!

Dave Houston I will still definitely be getting you code working too, come rain hail or snow!

All that remain (for now :-)) is the ability to send more than one byte.
I was thinking 3 bytes. (24 bits total)
So my question is this:
Assuming I am to stick with this method , what is the best method (most relaible) to be adding more bytes to be sent ?

Thanks yet again

Kind regards

Dennis

aratti
- 7th December 2009, 08:28
...And trying Al's suggestion of removing the DEC modifier (only on receiver side and for both hserin and hserout)
Dennis, I suggested you to remove the DEC modifier in Transmitter not in the receiver side.

The reason is that you Tx a pure ascii code and convert it to decimal at the receiver side, while now you are modifying the byte twice (at Tx & at Rx) and can yield wierd results.

Al.

mackrackit
- 7th December 2009, 09:36
Thanks Al for pointing out my mistake.

Dennis,
Just for fun, send this straight to the terminal, you might find it interesting.


XNUM VAR BYTE[8]
XNUM = 225 'CHANGE THIS NUMBER AS NEEDED

SEROUT2 GPIO.5,16780,[TRAIN,SYNK,DEC XNUM.7,DEC XNUM.6,DEC XNUM.5,_
DEC XNUM.4,DEC XNUM.3,DEC XNUM.2,DEC XNUM.1,DEC XNUM.0,_
"--",DEC XNUM,$d,$a]


More bytes?
SEND


XNUM VAR BYTE
YNUM VAR BYTE
ZNUM VAR BYTE

XNUM = 225
YNUM = 100
ZNUM = 4
START:
SEROUT2 GPIO.5,16780,[TRAIN,SYNK,XNUM,YNUM,ZNUM,$d,$a]
PAUSE 5000
GOTO START


RECEIVE


XNUM VAR BYTE
YNUM VAR BYTE
ZNUM VAR BYTE

START:
SERIN2 GPIO.5, 16780,[WAIT(SYNK),XNUM,YNUM,ZNUM]

SEROUT2 GPIO.4,16780,["X-",DEC XNUM," Y-",DEC YNUM," Z-",DEC ZNUM,$d,$a]
GOTO START

Dennis
- 7th December 2009, 18:45
Al thanks a million for the clarity on that one .. will check and feedback !

Dave ... ok just for fun so be it !
And if I get greedy would the Var bytes be num num num ? :-)

Expect Feedback .. results may vary :-0

Kind regards

Dennis

Dennis
- 8th December 2009, 00:54
Hi Dave ;-)

That TX RX code you passed on works like a charm too X Y and Z coming through clear as day although I changed it for HSERIN and HSEROUT

Not sure what you want me to try with this code though ??

[code]
XNUM VAR BYTE[8]
XNUM = 225 'CHANGE THIS NUMBER AS NEEDED

SEROUT2 GPIO.5,16780,[TRAIN,SYNK,DEC XNUM.7,DEC XNUM.6,DEC XNUM.5,_
DEC XNUM.4,DEC XNUM.3,DEC XNUM.2,DEC XNUM.1,DEC XNUM.0,_
"--",DEC XNUM,$d,$a]
[code]

Did you want me to TX/RX it ? modify and put on send and receive side ?

Dennis

Dennis
- 8th December 2009, 01:14
Hi Al

I applied your suggestion , unfortunately it didn't work , my receiver side has an IF ..THEN statement looking for the number 23, and implementing your suggestion on the TX side only doesn't seem to send the correct DEC 23 to the RX so the IF condition is never met ...check my receiver side code for clarification.
To confirm , I only left the DEC modifier on the receiver side.. (please note though that at the receiver side I have hserin and hserout statements using the DEC modifier.
I placed your code suggestion on the TX of the first PIC only ..so it looks something like this
TX ----------- hyoerterminal
TX ------RF----- RX
PIC1 PIC2

Will bash around with it a little more and see what comes of it :-)
I'll post result back as soon as I have double checked each step too.

kind regards

Dennis

mackrackit
- 8th December 2009, 02:27
Hi Dave ;-)

That TX RX code you passed on works like a charm too X Y and Z coming through clear as day
Really??? Well how about that...


Not sure what you want me to try with this code though ??

[code]
XNUM VAR BYTE[8]
XNUM = 225 'CHANGE THIS NUMBER AS NEEDED

SEROUT2 GPIO.5,16780,[TRAIN,SYNK,DEC XNUM.7,DEC XNUM.6,DEC XNUM.5,_
DEC XNUM.4,DEC XNUM.3,DEC XNUM.2,DEC XNUM.1,DEC XNUM.0,_
"--",DEC XNUM,$d,$a]
[code]

Did you want me to TX/RX it ? modify and put on send and receive side ?

Dennis
Send directly to a terminal.
You said you wanted three bytes, sometimes 8 bits can be as good. just thought you might see a use... splitting the byte up, multiple outputs on the same port... but if you need 23 then it will not help you this time.

Al was suggesting you only put the DEC on the RX side. That is why I thanked him for pointing out my mistake. I put a DEC on both in a couple of post.