PDA

View Full Version : 12F683 Serial Comms



ross246
- 17th November 2010, 14:31
Hi Guys,

A while ago I got code working for my 16F690 that allowed me to communicate back and forth with my PC Serial Port. I Picked up some 12F683's however I am struggling to convert the code to work with the 12F683. The purpose of the project is to provide some sort of Software Protection for a program I have written in vb.net so I figured an 8 pin would be perfect I can even get power off the serial port.

Here is the existing code I have for my 16F690, if anyone has any time to point me in the right direction to use it on a 12F683


Include "modedefs.bas"

DEFINE OSC 4
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 20h
DEFINE HSER_SPBRG 25
DEFINE HSER_CLROERR 1

ANSEL = 0
ANSELH = 0
CM1CON0 = 0
CM2CON0 = 0

stat VAR byte

stat = " "

Main:
HSERIN [stat]
if stat = "0" Then goto reqSerial
GOTO Main

reqSerial:
HSEROUT ["51384529"]
GOTO MAin
END

I am using a max232 with the circuit. I have taken out some of the original code to make it easier to post here, should compile and it works on a 16F690 :)

Thanks,
Ross.

ScaleRobotics
- 17th November 2010, 15:00
Hi Ross,

The 12F683 does not have a hardware serial port, so you would need to use debugin and debug, or serin and serout, instead of hserin and hserout.

If you need a hardware serial port, then there will be the 12F1822 (pretty soon).

ross246
- 17th November 2010, 15:06
Ah I see. I don't know much hehe I tried to put some code together just to test but it seems not to work. Could this be close?


Include "modedefs.bas"

OSCCON = %01100000 ' Internal 4MHz osc
ADCON0 = 0
CMCON0 = 7
ANSEL = 0
WPU = 0
GPIO = %00000000

rcx var GPIO.4 'input pin
tcx var GPIO.2 'output pin

stat VAR byte

Main:
serin rcx,N2400,STAT
pause 100
SEROUT tcx,n2400,[stat]
GOTO Main
END

ScaleRobotics
- 17th November 2010, 15:16
Looks very close.

If you are still using a rs232, then you want True, if you are not, then just as you did, with inverted.

ross246
- 17th November 2010, 15:26
Ah thanks for pointing that out. I was using a max232 with 'n' and not 't' :P

Working code:


Include "modedefs.bas"

OSCCON = %01100000 'set for 4mhz internal
CMCON0 = 7 'TURN COMPARITORS OFF
ANSEL = %00000000 'Set A/D OFF
ADCON0 = %00000000 'Analog converter OFF

rcx var GPIO.4 'input pin
tcx var GPIO.1 'output pin

stat VAR byte

Main:
serin rcx,t2400,STAT
pause 100
SEROUT tcx,t2400,[stat]
GOTO Main
END

Is a max232 needed? I haven't had much success without it in the past but I might consider trying to lose it since space is a bit tight in the dongle case.

ScaleRobotics
- 17th November 2010, 15:36
Is a max232 needed? I haven't had much success without it in the past but I might consider trying to lose it since space is a bit tight in the dongle case.

When you use a hardware serial port on a pic16 device, you need an RS232. If you use hardware serial on a PIC18, then you can invert the bits to get away without one.

For degug or serin/out you don't need one. You can follow the directions in the manual for the resistor size to protect your pic.

ross246
- 17th November 2010, 15:51
Awesome thanks so much for your help. Just got to search through the manual now. 10k sticks in my head for some reason but I will check.

Thanks again.

ScaleRobotics
- 17th November 2010, 16:38
Awesome thanks so much for your help. Just got to search through the manual now. 10k sticks in my head for some reason but I will check.

Not a problem, great to see you got it going.

It's 1K resistor for serout, and 22K (or so) for serin pins.