PDA

View Full Version : Serin Serout Help



MangoKid
- 8th December 2010, 00:43
Currently I am working on a project that involes serial communication between one PIC and another. The task is to flash an led at a rate (77) sent via serial. the code is as follows.

Sender: PIC 16F877A

CLEAR
DEFINE OCS 4
TRISA = %000000000


LOOP1:
SEROUT PORTA.0, 3, [77]
GOTO LOOP1:
END

Receiver: PIC 12F683

CLEAR
ANSEL = 0
CMCON0 = 7
X VAR BYTE
HIGH GPIO.2
INPUT GPIO.1

LOOP1:
SERIN GPIO.1, 3, X
GPIO.3 = 1
PAUSE x
GPIO.3 = 0
GOTO LOOP1
END

any help would be appreciated thanks a lot:)

cncmachineguy
- 8th December 2010, 01:25
my guess is the LED is always on or you just see a blip once. try moving the serin outside of the loop. The way you have it it will be waiting for a serin every time through the loop. next problem is you are turning the led on, waiting "x" time, then turning it off. THEN you go right back to the top of the loop and turn it right back on.so if it doesn't hang on the serin, it turns on almost instantly. try adding a pause after you trun it off also. That is assuming GPIO.3 is your LED.

Darrel Taylor
- 8th December 2010, 01:48
On a 12F683, GPIO.3 is "Input Only" when not used as MCLR.
You'll need to use a different pin for the LED.

And the receiver has no way to know where each byte of serial data starts.
The sender is continually sending data and the receiver looks for data whenever it looks for it.

If the receiver starts looking in the middle of a byte, it doesn't know it's not the beginning of the transmission and will receive an invalid value.

There needs to be some sort of handshaking, so the sender knows when the receiver will be ready to receive something before sending it.
That will take another line between the two chips.
Or, tri-state signaling on the single line (no sample code available).

My recommendation ... add another line between the two chips, use SEROUT2/IN2 with FlowPin control.
You can use GPIO.3 for the serial input (put FlowPin on another pin).

mackrackit
- 8th December 2010, 02:34
Do not forget
INCLUDE "modedefs.bas"

And if you do not have enough pins for the flow control.
From the manual. We sometimes refer to this as "waiting" for a character to know where we are.


The list of data items to be received may be preceded by one or more qualifiers enclosed within brackets. SERIN must receive these bytes in exact order before receiving the data items. If any byte received does not match the next byte in the qualifier sequence, the qualification process starts over (i.e. the next received byte is compared to the first item in the qualifier list). A Qualifier can be a constant, variable or a string constant. Each character of a string is treated as an individual qualifier.

MangoKid
- 8th December 2010, 03:29
thanks for the post unfortunately i have made little progress. I have set the flowpin using Serin2 and serout2 and i am using proper pins. my baud rate may have some issues I'm trying to use the slowest one for now. Currently the circuit does nothing, no flashing or anything, but i have verified with an oscilloscope that data is being sent. Hear is the new code.

send:
CLEAR
DEFINE OCS
TRISA = %000000000

LOOP1:
SEROUT2 PORTA.0\PORTA.1, 3313, [DEC 77]
GOTO LOOP1:
END

recive:
CLEAR
ANSEL = 0
CMCON0 = 7
X VAR BYTE

LOOP1:
SERIN2 GPIO.1\GPIO.3, 3313, [DEC X]
pause x
GPIO.0 = 1
PAUSE x
GPIO.0 = 0
PAUSE X
GOTO LOOP1
END

thanks again any help would be appreciated.

MangoKid
- 8th December 2010, 03:31
Should I be using pull up resistors (22k, 4.7K, 2.2k)?

mackrackit
- 8th December 2010, 03:40
You will need to turn the analog off.
The two links below should help. For now, after you read the one from Melanie, use the one from Darrel.
http://www.picbasic.co.uk/forum/showthread.php?t=561
http://www.picbasic.co.uk/forum/content.php?r=154-All-Digital

And if you could post all of your code and how the configs are set might also be helpful.

MangoKid
- 8th December 2010, 03:49
Thanks again i will check the links and do what I can. as far as the code goes all of the code i have is written above.

mackrackit
- 8th December 2010, 06:29
Lets back up a bit. We normally start with a blinky program. If you have already done this then great, if not....

The configs will need set for the hardware. This can be done int the *.inc file found in the PBP directory or in code space. This link tell about it.
http://www.picbasic.co.uk/forum/showthread.php?t=543
Being that you are starting, start using MPASM for the assembler. It will save you trouble later.

Start with the 16F877A.
By default the OSC in the *.inc is set for XT. This is fine if you have a crystal attached, if you have a resonator change it to HS.
Now for the code,


'16F877A
DEFINE OSC 4 ' This tells PBP how fast the chip is running.

'This line is the configs MPASAM style
'Comment it out if the configs are set in the *.inc
@ __config _HS_OSC & _WDT_OFF & _LVP_OFF & _CP_OFF &_BODEN_OFF

ADCON1=%00000111 ' Turns off the ADC
CMCON=%00000111 'Turns off the Comparators
TRISA = %00000000 ' Makes all pins on PORTA outputs

'Set up some variables.
LED VAR PORTA.1 'Attach a LED to PORTA pin 1

RUN: Make the LED blink at 1 sec on and 1 sec off
HIGH LED
PAUSE 1000
LOW LED
PAUSE 1000
GOTO RUN
END

The above will help you determine if the chip is running correctly.

Do the same for the 12F683.
The PBP default is internal OSC and the chip defaults to 4 MHz. So you should be good as long as PBP is told about it.
DEFINE OSC 4
In you code like we did for the 877A.

Get everything blinking as it should and then tackle the serial part.

Darrel Taylor
- 8th December 2010, 08:50
Your flowpin is on GPIO.3.

SERIN2 GPIO.1\GPIO.3, 3313, [DEC X]

Put the serial data on GPIO.3, and the flowpin on GPIO.1.
flowpin needs to be able to output.

And set GPIO.0 to OUTPUT for the LED.

OUTPUT GPIO.0

Don't forget to disable MCLR in the configuration.

MangoKid
- 12th December 2010, 20:09
I have been working on the code for a couple of days. I have made a small progression if i manually send a "1" to the SERIN it will assume a flash rate of 1. this is done by placing the datapin wire to ground. Hear is the current code i am working with. Any help would be great, Thanks!


Recive:
Clear
define ocs 4
INCLUDE "modedefs.bas"
@_config_HS_osc &_WDT_OFF &_LVP_OFF &_CP_OFF &_BODEN_OFF
ADCON1 = %00000111
CMCON = %00000111
TRISA = %11111111
TRISD = %00000000
X VAR BYTE
SERIN2 PORTA.5\PORTA.1, 3313, [DEC X]
LOOP1:
HIGH PORTD.2
PAUSE X
LOW PORTD.2
PAUSE X
GOTO LOOP1
END

Send:
Clear
define ocs 4
INCLUDE "modedefs.bas"
@_config_HS_osc &_WDT_OFF &_LVP_OFF &_CP_OFF &_BODEN_OFF
ADCON1 = %00000111
CMCON = %00000111
TRISA = %00000000
TRISD = %00000000
PORTA = %00000000
LED VAR PORTA.1
X VAR BYTE
X = 100
LOOP1:
SEROUT2 PORTA.5/PORTA.1, 3313, [100]
PAUSE 5000
GOTO LOOP1
END
Thanks again!

Archangel
- 12th December 2010, 23:18
Hi MangoKid,
I am away from MY Computer and my books, but if I understand this last statement correctly,
" this is done by placing the datapin wire to ground."
I believe you are referring to the ports "Idle State" if so here is the little secret: If you are sending TRUE the port wants a pull up resistor and if you are sending INVERTED the port wants a pull Down resistor.

mackrackit
- 13th December 2010, 01:05
Not sure what you mean by datapin...
But..
On the receive side you are only checking the serial port once,


SERIN2 PORTA.5\PORTA.1, 3313, [DEC X]
LOOP1:
HIGH PORTD.2
PAUSE X
LOW PORTD.2
PAUSE X
GOTO LOOP1

Put the SERIN2 line inside of LOOP1.

cncmachineguy
- 13th December 2010, 01:40
Thats prolly my fault. Way back at the beginning I suggested to take it out of the loop. I was thinking it would be gotten before entering the loop.

MangoKid
- 13th December 2010, 18:18
Thanks again i moved SERIN inside the loop but to no avail. I am assuming there is an issue with my baud rate. If i am sending should the sending and receiving baud rate be identical or should one be inverted?

Thanks for the help

MangoKid
- 13th December 2010, 20:00
So after alot of headaches i have finally got SERIN SEROUT working . The first and biggest problem was the baud rate i was using the initial (3313) is only meant to communicate with a level converter. I am using (27889) which is 7bit with even parity. Here is the code i hope it helps anyone else who is stuck with the SERIN SEROUT.

Send:

clear
DEFINE OCS 4
ADCON1 = %00000111
CMCON = %00000111
TRISA = %11111111
TRISD = %00000000
X VAR BYTE
X = 555

LOOP1:
SEROUT2 PORTA.5, 27889 , [244]
PAUSE 500
GOTO LOOP1
END

Recive:

Clear
define ocs 4
ADCON1 = %00000111
CMCON = %00000111
TRISA = %11111111
TRISD = %00000000
X VAR BYTE

LOOP1:
LOW PORTD.3
SERIN2 PORTA.5, 27889, [X]
HIGH PORTD.3
HIGH PORTD.2
PAUSE (X * 10)
LOW PORTD.2
PAUSE (X * 10)

GOTO LOOP1

END

Thanks Again to all those who helped solve this.