PDA

View Full Version : Serial coms with a pc



Andre_Pretorius
- 16th June 2007, 11:05
Hi there

I have built a circuit with a pic16f627 and a so232acp chip. The idea is to read an input and display it on hyper terminal and toggle two leds if I send a command from the pc, I am using the on board usart.
The transmitting from the pic work 100% but I can not seem to get it responding to the commands from the pc. Can anyone please help; here is the testing code I use.

INCLUDE "ANSI.INC"

CMCON = %00000111 ;Disable Comparator module's
OPTION_REG = %11010111 ;Set PIC options
INTCON = 0 ;Disable interrupts
TRISB = %00000000 ;RB7...RB0 are outputs.
TRISA = 255 ;SET PORTA AS INPUTS

DEFINE HSER_RCSTA 90h ;Set receive register to receiver enabled
DEFINE HSER_TXSTA 20h ;Set transmit regester to transmitter enabled
DEFINE HSER_BAUD 2400 ;Set baud rate
DEFINE HSER_SPBRG 25 ;Set spbrg regester




I VAR BYTE
I = 0
PORTB.6 = 1

loop:
I =0
@ ClearScr ; Clear Screen
HSEROUT ["Value of input 1:",DEC PORTA.0,10,13]

HSEROUT ["Please select a command",10,13]
HSEROUT [9,"1 = Flash leds",10,13]


HSERIN 5000,LOOP,[I]

IF I = 1 THEN
PORTB.7 = 1
PORTB.6 = 0
PAUSE 1000
PORTB.7 = 0
PORTB.6 = 1
GOTO LOOP

ELSE

GOTO LOOP

ENDIF

End

Jerson
- 16th June 2007, 19:26
Try checking for the condition

If i = "1" then

since the keypressed from the pc is a printable ascii character.

Jerson

skimask
- 16th June 2007, 19:36
INCLUDE "ANSI.INC"
cmcon=7:option_reg=$d7:intcon=0:trisb=0:trisa=$ff
DEFINE HSER_RCSTA 90h ;Set receive register to receiver enabled
DEFINE HSER_TXSTA 20h ;Set transmit regester to transmitter enabled
DEFINE HSER_BAUD 2400 ;Set baud rate
DEFINE HSER_SPBRG 25 ;Set spbrg regester
temp var byte:I VAR BYTE:I = 0
loop:
I =0
@ ClearScr ; Clear Screen
hserout ["Value of input 1:",dec porta.0,10,13]
hserout ["Please select a command",10,13]
hserout [9,"1 = Flash 1 LED",10,13]:hserout [9,"2 = Flash 2 LED",10,13]
hserout [9,"3 = Flash Both LED",10,13]:hserin 5000,loop,[I]
IF I = "1" THEN
for temp = 1 to 10:PORTB.7 = 1:pause 1000:portb.7=0:pause 1000:next temp
endif
if i = "2" then
for temp = 1 to 10:portb.6=1:pause 1000:portb.6=0:pause 1000:next temp
endif
if i="3" then
for temp = 1 to 10:portb.6=1:portb.7=1:pause 1000:portb.6=0
portb.7=0:pause 1000:next temp
endif
GOTO LOOP
End
Try that out...

EDIT: DOH! Jerson beat me to it! :)

Andre_Pretorius
- 17th June 2007, 20:10
Thank you for the reply, but stil nothing.
I am starting to think my hardware is faulty. I have written a program that acording to me could ecco what is sended to the pic. do you agree with the program, if it is correct i definitly need to start looking at my hardware.
Thank you again for al the help

INCLUDE "ANSI.INC"

CMCON = %00000111 ;Disable Comparator module's
OPTION_REG = %11010111 ;Set PIC options
INTCON = 0 ;Disable interrupts
TRISB = %00000000 ;RB7...RB0 are outputs.
TRISA = 255 ;SET PORTA AS INPUTS

DEFINE HSER_RCSTA 90h ;Set receive register to receiver enabled
DEFINE HSER_TXSTA 20h ;Set transmit regester to transmitter enabled
DEFINE HSER_BAUD 2400 ;Set baud rate
DEFINE HSER_SPBRG 25 ;Set spbrg regester

I VAR BYTE
I = 20

loop:
@ ClearScr ; Clear Screen
HSEROUT ["Value of I:",I,10,13]
HSERIN 5000,LOOP,[I]
GOTO loop
End

skimask
- 17th June 2007, 20:26
Loopback with lower case to upper case translation (shows the PIC is actually working and that the PC isn't just receiving the same thing it sent out, i.e. local echo):


include "modedefs.bas"
cmcon=7:option_reg=$d7:trisb=0:trisa=$ff:i var byte
main:
serin portb.1,n2400,I:if (i > 96) and (i < 123) then i = i - 32
serout portb.2,n2400,[I]:goto main


And I just noticed something in the code in your last post. Take out the clear screen and if anything happens...

mister_e
- 17th June 2007, 21:12
i just copy/paste your code and, yes indeed there's few mistake.

first..

DEFINE HSER_RCSTA 90h ;Set receive register to receiver enabled
DEFINE HSER_TXSTA 20h ;Set transmit regester to transmitter enabled
DEFINE HSER_BAUD 2400 ;Set baud rate
DEFINE HSER_SPBRG 25 ;Set spbrg regester

In red, you overwrite PBP calcs. This result of a bad SPBRG setting for a TXSTA=20. Also, as you're using HSERIN, i would suggest you to use HSER_CLROERR feature to clear all possible overflow error. The correct declaration should be

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

Even with this, you have configure the RX pin as output :eek: so change TRISB to

TRISB = %00000010 ' RB1(RX) as input, others to Output

that sort few error, but unless you want to have a full-filled of "Value of I:" screen, i'll suggest you to remove the timeout delay and it's jump label

HSERIN [i]
This way, the program will sit there and wait for a user entry.
<hr>
EVEN with the above correction done, you may have some problem if you're using the internal OSC, if your "Value of I" message is messy, use an external one OR try with a lower baudrate. Here, it doesn't work @9600 baud with the internal OSC. The output looks like

VX• ˁ'‘
i don't know for others... but i can't understand what the above ask me ;)
<hr>
the full corrected code, with config fuse setting for MPASM and a external 4MHz crystal, but without the ANSI feature looks like

<font color="#000000"> <font color="#008000">;INCLUDE &quot;ANSI.INC&quot;
</font>@ __CONFIG _XT_OSC &amp; _MCLRE_ON &amp; _LVP_OFF &amp; _WDT_OFF &amp; _PWRTE_ON &amp; _BODEN_ON
CMCON = %00000111 <font color="#008000">;Disable Comparator module's
</font>OPTION_REG = %11010111 <font color="#008000">;Set PIC options
</font>INTCON = 0 <font color="#008000">;Disable interrupts
</font>TRISB = %00000010 <font color="#008000">' RB1(RX) as input, others to Output
</font>TRISA = 255 <font color="#008000">;SET PORTA AS INPUTS

</font><font color="#000080">DEFINE </font>HSER_RCSTA 90h <font color="#008000">' Enable serial port &amp; continuous receive
</font><font color="#000080">DEFINE </font>HSER_TXSTA 24h <font color="#008000">' Enable transmit, BRGH = 1
</font><font color="#000080">DEFINE </font>HSER_SPBRG 25 <font color="#008000">' 9600 Baud @ 4MHz, 0.16%
</font><font color="#000080">DEFINE </font>HSER_CLROERR 1 <font color="#008000">' Clear overflow automatically


</font>I <font color="#000080">VAR BYTE
</font>I = 20

loop:
<font color="#008000">;@ ClearScr ; Clear Screen
</font><font color="#000080">HSEROUT </font>[&quot;Value of I:&quot;,I,10,13]
<font color="#000080">HSERIN </font>[I]
<font color="#000080">GOTO </font>loop
<font color="#000080">END
</font>

More information on config fuse and possible compilation warning messages at the following link
http://www.picbasic.co.uk/forum/showthread.php?t=543

hth

Darrel Taylor
- 17th June 2007, 23:47
Well, I'm always looking for "comments" on my programs.

I guess "commenting" them out qualifies. :)

ANSI.INC

.

mister_e
- 17th June 2007, 23:49
:D don't take it personal.. i hate Hyperterminal, sure it has to work with.

Darrel Taylor
- 17th June 2007, 23:57
And the best part is ... you don't have to use Hyperterminal.

Any terminal program that can emulate ANSI will do.

Even a DOS window with ANSI.SYS loaded. Yuck!

.

mister_e
- 18th June 2007, 00:18
Deuh... but MisteEZCOM one don't do it :D

Anyhow, proof it's working...
<IMG SRC="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1754&stc=1&d=1182118643">
I prefer RealTerm... but... erm, MisterEZCOM is not bad to...


@ ForColor yellow ; Set foreground (text) color to yellow
@ BackColor blue ; Set background color to blue

loop:
@ ClearScr ; Clear Screen
HSEROUT ["Value of I:",I,10,13]
HSERIN [i]
GOTO loop
END

Feel better now ? http://www.mister-e.org/Pics/ROFL

Darrel Taylor
- 18th June 2007, 01:50
Feel better now ?

Well yes, actually, I do. :)

Thank you!

.

Darrel Taylor
- 18th June 2007, 02:05
P.S.

Where do I get this MisterEZCOM?

Must have missed that thread.
.

mister_e
- 18th June 2007, 02:10
It's not released yet, so you didn't missed it ;)

Andre_Pretorius
- 18th June 2007, 15:41
Thank you all