PDA

View Full Version : Serial com PIC <> PC - what is best?



flotulopex
- 1st January 2007, 22:28
Hello,

I'm struggeling to get an error free serial communication between my 16F88/4MHz and the PC's terminal (using the MCS's one).

The aim is to send data to the PIC's DATA memory and read it back in the PC.

Actually, I'm transferring data kind of "one by one" memroy location basis and it works more or less fine. If I want to transfer to more than one memory location at the time, the trouble begins.

I have read lots of threads about SERIN/SEROUT and the usage of USART. To be honest, the more I read, the more I get lost...

My PIC is equipped with AUSART. Is this a "hardware" USART?

What is the best and most reliable way to transmit data to and from the PC?

mister_e
- 1st January 2007, 22:54
Serial comm handling is an endless story, there's tons of different ways, with check sum or not, with header/footer or not, split packets, use FlowControl pins etc etc.

First of all, you must have a reliable PIC frequency. So, if it's not already the case, forgive the idea to use the internal OSC.

Your PIC have a USART indeed, so forget the SERIN/SEROUT stuff, use HSERIN/HSEROUT instead. It have few advantage over SERIN/SEROUT.

Now, depending what else your PIC do, you may miss some incomming data or not if you spend too much time here and there before the HSERIN... be carefull.

You may decide to use a lower baudrate first.

Post the code you have, it will be easier to figure this out together.

skimask
- 1st January 2007, 22:56
Hello,

I'm struggeling to get an error free serial communication between my 16F88/4MHz and the PC's terminal (using the MCS's one).

The aim is to send data to the PIC's DATA memory and read it back in the PC.

Actually, I'm transferring data kind of "one by one" memroy location basis and it works more or less fine. If I want to transfer to more than one memory location at the time, the trouble begins.

I have read lots of threads about SERIN/SEROUT and the usage of USART. To be honest, the more I read, the more I get lost...

My PIC is equipped with AUSART. Is this a "hardware" USART?

What is the best and most reliable way to transmit data to and from the PC?



Those example circuits in the PBP manual (without the RS232 converters) have always worked well for me.
What kind of data are you trying to transfer? Bulk data, lots of info, single bytes, words, what?
You'll probably end up writing your own version of FTP (flotulopex transfer protocol) :)

flotulopex
- 2nd January 2007, 11:04
I'll try with a 4MHz crystal.

At the end, I want to send WORDs to the PIC's program memory. I tried this internally with WRITECODE and it works as long as 4 WORDs are stored in a row.

I can't figure out how to send more than 1 data from the terminal to the PIC. So now, I have to press "Send" a couple of times until the LCD shows up some results. I send the datas comma separated, it this so?

Since I'm testing step-by-step, I didn't made an endless loop.

Here is the SERIN version:
@ DEVICE PIC16F88,INTRC_OSC_NOCLKOUT,PROTECT_OFF,WDT_ON,PWR T_ON,MCLR_ON
@ DEVICE PIC16F88,BOD_ON,LVP_OFF,CPD_OFF,DEBUG_OFF,CCPMX_OF F

' Register settings
OSCCON = %01100000 'Internal RC set to 4MHZ
ANSEL = %00000000 'Disable Analogue Inputs

' LCDOUT command (if defaults are not changed, DEFINEs can be omitted)
DEFINE LCD_DREG PORTB 'LCD data port
DEFINE LCD_DBIT 0 'LCD data starting bit 0 or 4 on µC
DEFINE LCD_RSREG PORTA 'LCD register select port
DEFINE LCD_RSBIT 3 'LCD register select bit
DEFINE LCD_EREG PORTA 'LCD enable port
DEFINE LCD_EBIT 2 'LCD enable bit
DEFINE LCD_BITS 4 'LCD bus size 4 or 8
DEFINE LCD_LINES 2 'Number lines on LCD
DEFINE LCD_COMMANDUS 2000 'Command delay time in us
DEFINE LCD_DATAUS 50 'Data delay time in us

Value1 var WORD
Value2 var WORD
Value3 var WORD
Value4 var WORD

'-------------------------------------------------------------------------------
' Init LCD
lcdout $FE, 1
pause 1000

'-------------------------------------------------------------------------------
' Input data from PC's terminal and show it on LCD
serin2 PORTA.0, %100000001010100, [dec3 Value1] '9600, inverted
serin2 PORTA.0, %100000001010100, [dec3 Value2] '9600, inverted
serin2 PORTA.0, %100000001010100, [dec3 Value3] '9600, inverted
serin2 PORTA.0, %100000001010100, [dec3 Value4] '9600, inverted
lcdout dec3 Value1, " ",dec3 Value2, " ",dec3 Value3, " ",dec3 Value4
END

This is the HSERIN version, same problem as before:
@ DEVICE PIC16F88,INTRC_OSC_NOCLKOUT,PROTECT_OFF,WDT_ON,PWR T_ON,MCLR_ON
@ DEVICE PIC16F88,BOD_ON,LVP_OFF,CPD_OFF,DEBUG_OFF,CCPMX_OF F

OSCCON = %01100000 'Internal RC set to 4MHZ
ANSEL = %00000000 'Disable Analogue Inputs
RCSTA = %10010000 'Enable serial port (RB2=RX & RB5=TX) and continuous receive
TXSTA = %00100000 'Enable Transmit Bit
SPBRG = 6 'Set baudrate to 9600

DEFINE LCD_DREG PORTA 'LCD data port
DEFINE LCD_DBIT 0 'LCD data starting bit 0 or 4 on µC
DEFINE LCD_RSREG PORTA 'LCD "RS" register select port
DEFINE LCD_RSBIT 7 'LCD "RS" register select bit
DEFINE LCD_EREG PORTA 'LCD "E" enable port
DEFINE LCD_EBIT 6 'LCD "E" enable bit
DEFINE LCD_BITS 4 'LCD bus size 4 or 8
DEFINE LCD_LINES 2 'Number lines on LCD
DEFINE LCD_COMMANDUS 2000 'Command delay time in us
DEFINE LCD_DATAUS 50 'Data delay time in us

'-------------------------------------------------------------------------------
' Init
Value1 var WORD
Value2 var WORD
Value3 var WORD
Value4 var WORD
lcdout $FE, 1
PAUSE 500

'-------------------------------------------------------------------------------
' Input data from PC's terminal and show it on LCD
Hserin [dec3 Value1] '9600, inverted
Hserin [dec3 Value2] '9600, inverted
Hserin [dec3 Value3] '9600, inverted
Hserin [dec3 Value4] '9600, inverted
lcdout dec3 Value1, " ",dec3 Value2, " ",dec3 Value3, " ",dec3 Value4
END

skimask
- 2nd January 2007, 13:37
[QUOTE=flotulopex;30449]I'll try with a 4MHz crystal.

Check the datasheet for the 16F88, page 100. You are using a 4mhz at 9600 baud. If you do the math, just like the datasheet says, you'll be 6.99% off at 9600 baud using the regular 'lo-speed' baud rate generator. Switch over to the high speed baud rate generator and you'll only be .16% off at 9600. Either that or switch down to 2400 baud and see what happens (.17% error at that speed with the lo-speed baud rate generator).
If the 2400 works with lo-speed, do like I said (switch to the hi-speed BRG) and see what happens..

Jumper
- 2nd January 2007, 13:39
If you plan to use WRITECODE you should be aware that some series require you to erase a block at the time and then write a block at the time. The size of these blocks can be found in the datasheet for the PIC. ERASECODE will erase the entire block that the address you have pointed to is inside.

I just saw you said you wrote 4 words and that is not a good way later....

/me

mister_e
- 2nd January 2007, 19:38
How many Bytes/Words you need to store to the PIC memory?

Be aware, if you plan to use WRITECODE often, your PIC's life will be short...

I also agree with the BRG setting, so i guess you wasn't aware of the PICMultiCalc?


<img SRC="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1310&stc=1&d=1167766974">

Just click on the Copy PBP DEFINEs to clipboard button and paste the settings in your code. This software choose the best setting for you. Don't have to worry about using your calc or the datasheet Tables.

The software is always available Here (http://www.mister-e.org/pages/utilitiespag.html) and it's free.

flotulopex
- 3rd January 2007, 08:37
I've got PICMultiCalc already (thank you for that, Mister_e).

I plan to fill the PIC's program memory only once and store datas defenitively.

The idea is to use it just like DATA memory.

I imagine, the better and easier way would be to use a "smaller" PIC and add external serial memory...

Actually, it is more about to learn how to use a PIC.