PDA

View Full Version : PIC16F877 to PC via max232 HELP!!



loredana
- 16th January 2005, 16:42
I hope one of you can help me, I am having lots of problems and not getting anywhere fast.

I am trying to connect a 20Mhz PIC16F877 to a PC using a MAX232 and PicBasic. I need to use the features of hserin and hserout for my project not serin and serin2.

I think from what I read so far its not possible to connect the the PIC to the MAX then to the port directly due to some inverting thing which I really dont understand.

I wonder if anyone could give me some working code to echo data sent from the pc back from the PIC so I can be sure to get started.

Also if you have a diagram of the circuit that works, I would be very greatful.

I am using Pic Basic pro 2.45 and an Epic programmer that both work ok and I have ran plenty tests to check.

Please help.

Loredana

mister_e
- 16th January 2005, 19:58
Hi loredana,

I'll suggest you to download MicroCode Studio there : http://www.rentron.com/mcstudio/mcstudio.exe

One circuit example is already in the HELP TOPIC under ICD>Introduction>>Harware Setup. By the way i attach one example.

With MicroCode studio you'll be able to send and receive to your PIC in the same windows task.


I think from what I read so far its not possible to connect the the PIC to the MAX then to the port directly due to some inverting thing which I really dont understand.


You can connect PIC directly to the PC without MAX232 if you're using SERIN/SEROUT and if you're using Inverted Mode. But if You're using the internal USART... you cannot set it by software. Hardware only, this is why you need an MAX232 or some simples inverter transistor circuit.


I wonder if anyone could give me some working code to echo data sent from the pc back from the PIC so I can be sure to get started.


The following will give you this @9600 baud.



' Serial data echo test
' =====================
'
' File name : Serial_echo_test.bas
' Company : Mister E
' Programmer : Steve Monfette
' Date : 16/01/2005
' Device : PIC16F877

' PIC setting & Programming mode
' -------------------------------
'
@ __config _HS_OSC & _WDT_ON & _PWRTE_ON & _BODEN_ON & _LVP_OFF
' HS(20Mhz) oscillator
' Enable watch dog timer
' Enable power up timer
' Enable brown out detect
' Disable low voltage programming
DEFINE OSC 20 ' Use 20 MHZ oscillator

' Usart Settings
' --------------
'
' refer to datasheet section 10 for the following settings
'
DEFINE HSER_RCSTA 90h ' Enable Serial PORT
' Enable continuous receive
'
DEFINE HSER_TXSTA 24h ' Enable transmit
' High baud rate (BRGH=1)
'
DEFINE HSER_SPBRG 129 ' set USART to 9600 baud (when BRGH=1)
DEFINE HSER_CLROERR 1 ' Enable automatic overrun error

' Variable Definition
' -------------------
SerData var byte

Start:
' Program begining/Main Loop
' ==========================
'
HSERIN [serdata]
hserout [serdata]
goto start

loredana
- 17th January 2005, 18:10
Thanks mister_e,

That worked at 9600,

Lost me why DEFINE HSER_SPBRG 129 I cannot find any reference to that in the picbasic manual.

Also I did get a compiler error about an op code with this:

@ __config _HS_OSC & _WDT_ON & _PWRTE_ON & _BODEN_ON & _LVP_OFF

I guessed, added a rem before it then added this DEFINE OSC 20 and it compiled ok.

All I need now is to get to the 19200 and I will be happy.

Do I need to just change the DEFINE HSER_SPBRG 129 value to reach 19200

Thank you also for the gif with the diagram, that helped a lot aswell.

Lory

mister_e
- 17th January 2005, 18:47
Lost me why DEFINE HSER_SPBRG 129 I cannot find any reference to that in the picbasic manual.

That's for sure... You'll only find DEFINE HSER_SPBRG in the manual. 129 comes from the datasheet. See table 10-4. All the settings are there.


Also I did get a compiler error about an op code with this:

@ __config _HS_OSC & _WDT_ON & _PWRTE_ON & _BODEN_ON & _LVP_OFF


These are the configuration fuse for programming your PIC. This line imply that you must use MPASM to compile your code. If you are using MicroCode Studio to edit your code, go to view menu then PIC basic option. Click on the assembler tab then check the Microchip MPASM option. this will work. OR you can define your configuration fuse in a different way too, see the FAQ under setting configuration fuse or something like that.


All I need now is to get to the 19200 and I will be happy.

Do I need to just change the DEFINE HSER_SPBRG 129 value to reach 19200


For baudrate 19200 you'll change only the DEFINE HSER_SPBRG 129 to DEFINE HSER_SPBRG 64

By the datasheet it suppose to work :0)

loredana
- 17th January 2005, 19:20
Hi Again Steve,

It worked alright, meaning the 64.

19,200 and singing, thanks to you.

I am looking now at another thing, maybe you can help me here too.

I want the program to wait untill I sent it **play# then react to the digital that follow upto a certain ending character like !

This compiles and works
HSERIN [WAIT("**play#")]

Then I wanted to the processor to pic up the next 6 characters after the **play#, so I send to the PIC **play#123456. looking at other examples the code should be like this:

HSERIN [WAIT("**play#")STR data\6]

wait for **play# then then next 6 characters will be present in data, well I wish, I get an error with syntax

ERROR Line 59: Expected ']'.

Also I read in the manual STR ArrayVar\n{\c}
Receive string of n characters optionally ended in character c

Does this mean I dont have to specify the value of n

HSERIN [WAIT("**play#")STR data\n\"!"]

wait for **play# and copy the next characters upto the ! to data.


Or am I getting totallly mixed up.

Have you got a few examples I could try.

Hope I am not imterupting you steve with all these questions.

Loredana.

mister_e
- 17th January 2005, 20:16
HSERIN [WAIT("**play#")STR data\6]

wait for **play# then then next 6 characters will be present in data, well I wish, I get an error with syntax

ERROR Line 59: Expected ']'.


you're really close!!! just a ',' is missing + array variable definition


' Variable Definition
' -------------------
SerData var byte [6]

Start:
' Program begining/Main Loop
' ==========================
'


HSERIN [WAIT("**play#"),STR serdata\6]
hserout [str serdata\6]
goto start

the above will echo you the next 6 character next to your **play#


Also I read in the manual STR ArrayVar\n{\c}
Receive string of n characters optionally ended in character c

Does this mean I dont have to specify the value of n

HSERIN [WAIT("**play#")STR data\n\"!"]

wait for **play# and copy the next characters upto the ! to data.


nop ... wait for **play# and copy the next 'n' character or previous ones if the last one is '!'.

must be write HSERIN [WAIT("**play#"), STR lala\n\"!"]


Hope I am not imterupting you steve with all these questions.

Since i answer to this... no. Don't be afraid. We're all here to learn/help as we can.

loredana
- 17th January 2005, 20:38
lala, that was funny, thanks you made me laugh.

Both these work well steve

HSERIN [WAIT("**play#"), STR lala\10\"!"]
hserout [str lala\10]


Well you said it was ok to ask so I have another.

loop:
HSERIN [WAIT("**play#"), STR lala\10\"!"]

' **play#hockey!

' **play#tennis!

' **play#pool!

' **play#badmington!

lala will be contain one of these, can I make them into one string to allow an if statement to branch to different code.

I see I could do this with simple numbers like 1,2,3,4,5 etc but I want to use strings style names if possible.


I found some code that some guy had done like this:
IF relay = 1 THEN outr1 ' if request is for relay#1 then goto relay#1 routine

I would like to make it like this:


If lala\10 = "badmington" then badmington


badmington:
' the code for badmington
GOTO loop

Is that possible Steve

Thanks again

Lory

mister_e
- 17th January 2005, 21:16
Nothing is impossible but i'll prefer use some simple number and use BRANCH or BRANCHL statement for that. Keep it simple at least you can refer to some constant.

hockey con 0
tennis con 1
pool con 2
badmington con 3


when you do your tests...
if lala=hockey then goto hockey

and others. But i'll still prefer the BRANCH option

serdata var byte
sport var byte

HSERIN [WAIT("**play#"), dec Serdata]
sport=serdata
BRANCH sport,[hockey,tennis,pool,badmington]

For the string request... i'll not using this so... :o(

Demon
- 19th February 2005, 06:51
And another thank you from me to Mister_E for helping me get this darn RS232 PIC-PC interface going at 19,200 BAUD.

Now if I can only get Hilgraeve's HyperTerminal to work properly. That thing looks perfect for sending text files to the PIC for storage on EEPROMs.

Gros bisou mon minou...

Robert
:LOL:

mister_e
- 19th February 2005, 10:01
Hey robert,

Ive never get this damn sh^&^% full of cra%$% bull%$#%$#% plein de marde of this stupid hyperterm work properly... better to build your own software in VB

this is probably the most crap software, after MPLAB i've never ever see and use since 30 years now

for a quick link about VB stuff.
http://www.rentron.com/sending_data.htm

Demon
- 19th February 2005, 21:39
My god, what a piece of crap!

Full of bugs, nothing works right, and worst of all, a support forum full of threads asking for help with most of them unanswered.

Disgusting product...

Robert
:beurk:

Luciano
- 21st February 2005, 08:59
Hi!

Useful link.

Regards

Luciano


* * * * * * *
Free Terminal program for MCU SW development :
(No setup, only one EXE file).

Terminal v1.9b 20040714.
http://bray.velenje.cx/avr/terminal/

* * * * * * *

Terry Blackwell
- 1st March 2005, 23:00
yes the invert? PIC to PIC a 1 is a 1 or a 0 is a 0 but the max chip turns input 1 from your pic in to an output o so you have to invert a 1 to 0 and a 0 to 1. Hock it up to your PC RS232 port you should get something anyway. I have used Pic16f628 and it works fine I only us MPLAB MPASM when I'm programming PIC you can do a lot and have more control of your PIC.

Demon
- 2nd March 2005, 01:59
Hi Terry,

Yeah, I'd love to do Assembler as well. I was 2nd in my class when I graduated in '83. And most importantly, I actually loved writing in Assembler, it came naturally.

But a few, ahem, years have gone by since then and I haven't touched Assembler since. I figure I'll get back into it slowly, once I get this project done. Assembler is the best way to go, total control. But I'm taking WAY longer than originally anticipated when I started this. I didn't even expect to program MCUs when I decided to make my little controller; just buttons and LEDs. Yeah, right...

Ok, so I knew nothing of electronics when I started 18 months ago, but hey, I'm managing. LOL I'm a little slow in some areas (like 99%), but once I get something going, I keep building on it. I'll stick to BASIC Pro for now, since BASIC was the 1st language I learned, and that was, ahem, the day it came out.

Fortran, Pascal, RPG, COBOL, name the oldies, I've done them. It's the new generation languages from C and up that I lack. I never thought I'd say that I wished I knew Assembler on the tip of my fingers again though...

Robert
:D

sundaecone
- 11th May 2005, 07:59
wow, this thread has been extremely useful, but i have a follow-up question..
i noticed that the crystal that loredana used was a 20MHz, i just wanna know what the maximum baud rate is for this crystal and also for the 4MHz crystal?
thanks so much!!! ^_^

mister_e
- 11th May 2005, 17:42
Based on internal USART: all will depend wich BRG setting you're using and % error you want to deal with..

Let's say PIC16F877 @20MHZ, all the answers are in the datasheet section 10 Table 10-4. Like every PIC that have USART.