Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC16F877A TX data to PC via serial cable

Status
Not open for further replies.

derrick826

New Member
hi again guys,

I'm facing another problem regarding on the Transmission of datas from my PIC to PC via the serial cable. Currently I'm using the RS232 model circuit taken accordingly from Nigel_goodwin's tutorial, a serial cable to connect to the PC (male to female converter) and of course the PIC to trasmit the data. I'm sending the data through RC6/tX. And my coding goes like this:

__config _CP_OFF & _PWRTE_ON & _XT_OSC & _WDT_OFF

ERRORLEVEL -302 ;Suppress bank warning


org 0x000
goto start

int org 0x004
goto int


start

CALL INITIAL
START1
CALL SEND
CALL CHECK
GOTO START1



SEND
MOVLW B'10010000'
MOVWF TXREG
RETURN

INITIAL
BCF STATUS,RP1
BSF STATUS,RP0
MOVLW B'00000000'
MOVWF TRISB
MOVLW h'24'
MOVWF TXSTA
MOVLW D'129'
MOVWF SPBRG
movlw B'10000000'
movwf TRISC
BCF STATUS,RP0
BSF RCSTA, SPEN
RETURN




CHECK
MOVLW B'00010000'
MOVWF PORTB
ONEMORETIME
BTFSS PIR1,TXIF
GOTO ONEMORETIME
MOVLW B'10000000'
MOVWF PORTB
RETURN


END

i'm actually relooping so that i'm constantly sending b'10010000' (90 in hexa). Since the start bit starts at the LSB therefore the data that is transmitting will be

(startbit) 0 00001001 1 (stop bit)

Am i sending a data '09' in hexa?

i'm using a MATLAB program to receive these particular data and it doesn't seem to respond even though i send a data from my PIC. this particular data has gone through the rs232 for voltage conversion 12V and to the female serial cable which then goes into the male serial of the pc. When i measure the voltage out from the rs232.. it seems to be 20V.. not 12V.

I'm trying out one of Nigel's tutorial regarding on the rs232 thingy but I don't really know what program to use to check weather or not the data which is being trasmitted from my PIC is being display on the PC. Is there any specific program to display the results i sent?

hope you guys are able to give me some hint and help so that i know where to start. thanks in advance
 
hi Nigel_Goodwin,

i had already select the correct configuration

baud rate 9600
data bits 8
stopbits 1
parity none
flow control "hardware" for Hyper terminal, for normal situation we use none

but still no sign of improvement.. the PC receives 12V.. but when it receives a 20v data.. surely it cannot read.. Is there any configuration needed for PIC to link with RS232 when sending data?

isn't it the purpose of the Rs232 is to convert my signal data voltage into a voltage suitable for the PC to be received
 
derrick826 said:
hi Nigel_Goodwin,

i had already select the correct configuration

baud rate 9600
data bits 8
stopbits 1
parity none
flow control "hardware" for Hyper terminal, for normal situation we use none

It won't work with hardware handshaking, you must set flow control to none.

Also make sure you have TX and RX connected correctly, depending what type of lead you are using (either straight or crossover).
 
derrick826 said:
yea thanks for the program i'm trying to figure out how to use it
hi,
Your program will not run, number or errors.

Tried in Oshonsoft, generates errors in hardware uart sim.

Posted a simple uart program for you to try out, it works in Sim and PIC

If you need any guidance using Oshonsoft, will be pleased to help.
 
Last edited:
ok Nigel... i will try it out and let you know my progress.... thanks for your help. But i was little worried about my coding if it's incorrect. So far i'm able to simulate it.

thanks for your help again!
 
hi eric..

can you tell me briefly what does the ser1x.asm program do?
I have tried my program in the Oshonsoft too using the both the hardware and software of uart sim... but there's nothing happening...

what has gone wrong with my codings if i may ask?
 
derrick826 said:
hi eric..

can you tell me briefly what does the ser1x.asm program do?
I have tried my program in the Oshonsoft too using the both the hardware and software of uart sim... but there's nothing happening...

what has gone wrong with my codings if i may ask?

hi,
The program is set for 9600 baud, 1 start bit, no parity, 1 stop bit.
It transmits "UART" and then <crlf> for a new line.
then a 100mSec delay and repeats.

In the Sim set the xtal for 4MHz, PIC as 16F877.
Use the TOOLS menu to select 'hardware uart'

Assemble the program using 'F9' keyboard key.
Use Sim 'FILE' menu to LOAD the serx1.hex

Start the simulation.



Select 'Rate' as Ultimate
 
hi,
Screen shots of my settings.


EDIT: added some notes for sim.

Sequence:

Start Sim
Set PIC type
Change Clock Frequency
Tools/Hardware UART sim interface
Tools/Assembler
... paste in serx1.asm to assembler Window

From Assembler Menu/Tools/Assemble with MPASMWIN [ or 'F9']
.. wait for asm to finish

From PIC Sim Menu/File/Load Program
... load the serx1.hex

Menu/Rate/Ultimate [top speed]

Menu/Simulation/Start

NOTE: I usually shorten the LOOPMS delays to 0.1 before I assemble and load, else
you could wait ages for a 100mSec delay in the Sim.

Tip:
From PIC/Sim
Menu/Options/Change UART Transmit/Receive time...... SET to '50'

For future ref when using LCD Module,
From LCD Module Menu
Click SetUp
Click 'Setup LCD Module Busy delays'
Change the '1640' default to about 100, you will get a faster update on the LCD.

---------------
Extract from manual: use this to switch your delays On and Off
So, just for the purpose of simulation you should recompile your programs with adjusted delay intervals,
that should not exceed 1-10ms.

But, be sure to recompile your program with original delays before you download it to a real device.

There is an easy way to change arguments of all WAITMS statements in a large basic program with a value
in the range 1-10 for simulation purposes.

With one line of code setting parameter SIMULATION_WAITMS_VALUE with DEFINE directive,
the arguments of all WAITMS statements in the program will be ignored and the specified value will be used instead during compiling.

Setting the value 0 (default) for this parameter (or omitting the whole line) will cancel its effect and the compiled code will be ready again for the real hardware.
-------------------------

DEFINE SIMULATION_WAITMS_VALUE = 1; delays ignored,,,, change to '1' before you program a PIC !

Hope this helps.
 
Last edited:
yea the hardware uart display UART..... but may i ask... in real life what's the 'hardware uart'

does this hardware uart displays these alphabets? what if i were to transmit this character to the PC?

this coding:

MOVLW .13 ;crlf
CALL TXRDY
MOVLW .10
CALL TXRDY

is similar to the other:

MOVLW 'U'
CALL TXRDY

but why doesn't it displays the numbers?


i'm sorry Eric... there's some coding here which i'm not really familiar with:

LOOPMS MACRO MSEC
MOVLW MSEC
MOVWF WAITL
CALL WAITLOOP
ENDM

;
;
;
;


LOOPMS .100 ;100mS delay after each line
;
;
;
;


WAITLOOP:
CALL DEL1MS
DECFSZ WAITL,F
GOTO WAITLOOP
RETURN
DEL1MS: ; 1ms delay loop
MOVLW 0X64;100*10US=1MS
MOVWF CNTR0
DL2:
NOP
NOP
NOP
NOP
NOP
NOP
NOP
DECFSZ CNTR0, F
GOTO DL2
RETURN

i'm not familiar with the LOOPMS MACRO MSEC

the code : LOOPMS .100 (actually means that the LOOPMS having the value of 100?)

and then the code : LOOPMS MACRO MSEC (means that the MSEC contain the value of 100 from LOOPMS?)

so sorry Eric that you have to help me with this.. i'm kinda slow.. and i have ask a lot of nonsense...
 
derrick826 said:
yea the hardware uart display UART..... but may i ask... in real life what's the 'hardware uart'

does this hardware uart displays these alphabets? what if i were to transmit this character to the PC?

this coding:

MOVLW .13 ;crlf
CALL TXRDY
MOVLW .10
CALL TXRDY

is similar to the other:

MOVLW 'U'
CALL TXRDY

but why doesn't it displays the numbers?

Because they are the ASCII characters for CR and LF, so the PC will perform those tasks, and merely move the cursor and scroll the screen (if required).

i'm sorry Eric... there's some coding here which i'm not really familiar with:

LOOPMS MACRO MSEC
MOVLW MSEC
MOVWF WAITL
CALL WAITLOOP
ENDM

It's a MACRO, it generates code during the assembler process and inserts a value (MSEC).
 
derrick826 said:
yea the hardware uart display UART..... but may i ask... in real life what's the 'hardware uart'
Its the UART [RS232] in the receiving PC

does this hardware uart displays these alphabets?
The PC with a 'terminal program' running would display 'UART'

what if i were to transmit this character to the PC?
It would be displayed, IF you were running a RS232 receive program

this coding:

MOVLW .13 ;crlf This is carriage Return
CALL TXRDY
MOVLW .10 ;This is Line Feed
CALL TXRDY

is similar to the other:

MOVLW 'U'
CALL TXRDY

but why doesn't it displays the numbers?
These a CONTROL CODES, normally only ASCII characters are displayed

i'm sorry Eric... there's some coding here which i'm not really familiar with:

This is a MACRO, look up Macro's
LOOPMS MACRO MSEC
MOVLW MSEC
MOVWF WAITL
CALL WAITLOOP
ENDM

;
;
;
;


LOOPMS .100 ;100mS delay after each line
;
;
;
;


WAITLOOP:
CALL DEL1MS
DECFSZ WAITL,F
GOTO WAITLOOP
RETURN
DEL1MS: ; 1ms delay loop
MOVLW 0X64;100*10US=1MS
MOVWF CNTR0
DL2:
NOP
NOP
NOP
NOP
NOP
NOP
NOP
DECFSZ CNTR0, F
GOTO DL2
RETURN

i'm not familiar with the LOOPMS MACRO MSEC
the code : LOOPMS .100 (actually means that the LOOPMS having the value of 100?)
This means 'DELAY [loop around] for 100mSec

and then the code : LOOPMS MACRO MSEC (means that the MSEC contain the value of 100 from LOOPMS?)

so sorry Eric that you have to help me with this.. i'm kinda slow.. and i have ask a lot of nonsense...

Dont worry about being slow, wev'e all been there!:rolleyes:

Have a look at your other thread [ADC] just posted info.
 
thanks guys!

Eric.. if let's says in this case we are actually transmitting in ASCII characters.
i have an ultrasonic circuit which displays a measurement value on the 7 segment LED , example 192 which my register:

display_H = 00000001
display_T = 00001001
display_U = 00000010

has these 8-bit in binary
is there anyway which i can do to trasmit this 8-bit binaries to allow the PC to display 192 on the screen?
 
derrick826 said:
thanks guys!

Eric.. if let's says in this case we are actually transmitting in ASCII characters.
i have an ultrasonic circuit which displays a measurement value on the 7 segment LED , example 192 which my register:

display_H = 00000001
display_T = 00001001
display_U = 00000010
You wouldn't have these binaries in your PIC registers?You would have HiReg = 0x0, LoReg = 0xC0 ... 00,,1100,0000
I use the comma's just for clarity.



has these 8-bit in binary
is there anyway which i can do to trasmit this 8-bit binaries to allow the PC to display 192 on the screen?

hi,
It depends upon the receiving program in the PC, they can written to accept binary data, convert to ASCII then display.

The usual way is to do the binary to ASCII conversion in the PIC program and transmit ASCII characters,
in that way you can use any PC terminal program.

Which one do you use?


EDIT: just seen Nigels post,, are you planning to use ALL 10 bits or just 8??
At this time the program 'right justifies' the value.
 
Last edited:
Convert them to ASCII first, then send them.

It's dead simple to do - just add 0x30 to the value - check my LCD tutorial routines, subroutine 'CharD' which does just that for LCD.
 
hi,
As per our PM's

Simple example of OS Basic with asm conversion routines for your studies.

Code:
AllDigital

Define SIMULATION_WAITMS_VALUE = 1  'ignore sim delays

Define LCD_LINES = 4
Define LCD_CHARS = 16

Define LCD_BITS = 4
Define LCD_DREG = PORTD
Define LCD_DBIT = 0

Define LCD_RSREG = PORTE
Define LCD_RSBIT = 0

Define LCD_EREG = PORTE
Define LCD_EBIT = 2

Define LCD_RWREG = PORTE
Define LCD_RWBIT = 1

Define LCD_READ_BUSY_FLAG = 1


Dim ascbfr0 As Byte
Dim ascbfr1 As Byte
Dim ascbfr2 As Byte
Dim ascbfr3 As Byte
Dim ascbfr4 As Byte
Dim ascbfr5 As Byte
Dim ascbfr6 As Byte
Dim ascbfr7 As Byte

Dim b2avh As Byte
Dim b2avm As Byte
Dim b2avl As Byte

Dim hexval As Byte
Dim temp1 As Byte

Dim cntr1 As Byte
Dim cntr2 As Byte
Dim test0 As Byte
Dim test1 As Byte
Lcdinit

test0 = 0
b2avl = test0
hexval = test0

b2avm = 0
b2avl = test0

main:

test0 = test0 + 1
b2avl = test0
b2avm = 0


Gosub bin2asc5
Lcdcmdout LcdLine1Home
Lcdout ascbfr0, ascbfr1, ascbfr2,
Lcdout ascbfr3, ascbfr4, ascbfr5, ascbfr6, ascbfr7

Goto main

Gosub byte2asc3
Lcdcmdout LcdLine1Home
Gosub showme
Gosub hex2asc
ascbfr5 = 0x20
Lcdcmdout LcdLine2Home
Gosub showme

test0 = test0 + 1
b2avl = test0
hexval = test0
Goto main
End                                               

showme:

Lcdout ascbfr5, ascbfr6, ascbfr7
Return                                            

'convert 24 bit bin to 8 asci in ascbfr0 to 7
'or
'b2avh,b2avm,b2avl
'leading spaces in bfr
bin2asc5:
ascbfr0 = 0
ascbfr1 = 0
ascbfr2 = 0
ascbfr3 = 0
ascbfr4 = 0
ascbfr5 = 0
ascbfr6 = 0
ascbfr7 = 0

ASM:        bcf status,rp0
ASM:        bcf status,irp
ASM:        movlw 16  '24  ' bits
ASM:        movwf cntr1
bitlp:  'shift msb into carry
ASM:        rlf b2avl,f
ASM:        rlf b2avm,f
'ASM:        rlf b2avh,f
ASM:        movlw ascbfr7  '0=msd 1st
ASM:        movwf fsr  'fsr=pointer to digits
ASM:        movlw 5  '0x8  'digits to do
ASM:        movwf cntr2
adjlp:
ASM:        rlf indf,f  'shift digit 1 bit left
ASM:        movlw 0x0a
ASM:        subwf indf,W  'check and adjust for overflow
ASM:        btfsc status,c
ASM:        movwf indf
ASM:        decf fsr,f  'incf=msd 1st next digit
ASM:        decfsz cntr2,f
Goto adjlp
ASM:        decfsz cntr1,f  'next bit
Goto bitlp
ASM:        movlw 0x30  'make asci
ASM:        iorwf ascbfr0,f
ASM:        iorwf ascbfr1,f
ASM:        iorwf ascbfr2,f
ASM:        iorwf ascbfr3,f
ASM:        iorwf ascbfr4,f
ASM:        iorwf ascbfr5,f
ASM:        iorwf ascbfr6,f
ASM:        iorwf ascbfr7,f
ASM:        movf ascbfr0,w  'blank leading zeros
ASM:        andlw 0x0f
ASM:        btfss status,z
Return                                            
Return                                            
ASM:        bcf ascbfr0,4  'make dig spc
ASM:        movf ascbfr1,w
ASM:        andlw 0x0f
ASM:        btfss status,z
Return                                            
ASM:        bcf ascbfr1,4
ASM:        movf ascbfr2,w
ASM:        andlw 0x0f
ASM:        btfss status,z
Return                                            
ASM:        bcf ascbfr2,4
ASM:        movf ascbfr3,w
ASM:        andlw 0x0f
ASM:        btfss status,z
Return                                            
ASM:        bcf ascbfr3,4
ASM:        movf ascbfr4,w
ASM:        andlw 0x0f
ASM:        btfss status,z
Return                                            
ASM:        bcf ascbfr4,4
ASM:        movf ascbfr5,w
ASM:        andlw 0x0f
ASM:        btfss status,z
Return                                            
ASM:        nop
Return                                            



hex2asc:
ASM:        movf hexval,w
Gosub hex1
ASM:        movwf ascbfr7
ASM:        swapf hexval,w
Gosub hex1
ASM:        movwf ascbfr6
Return                                            
hex1:
ASM:        andlw 0x0f
ASM:        addlw 0x30
ASM:        movwf temp1
ASM:        movlw 0x3a
ASM:        subwf temp1,w
ASM:        btfss status,c
Goto hex2
ASM:        movf temp1,W
ASM:        addlw 0x7
Return                                            
hex2:
ASM:        movf temp1,W
Return                                            

'leading zero's displayed
byte2asc3:
ASM:        movlw 0x0
ASM:        movwf ascbfr5
ASM:        movwf ascbfr6
ASM:        movwf ascbfr7
ASM:        bcf status,rp0
ASM:        bcf status,rp1
ASM:        bcf status,irp
ASM:        movlw 0x8
ASM:        movwf cntr1
binlp1:
ASM:        rlf b2avl,f
ASM:        movlw ascbfr7
ASM:        movwf fsr
ASM:        movlw 0x3
ASM:        movwf cntr2
binlp2:
ASM:        rlf indf,f
ASM:        movlw 0x0a
ASM:        subwf indf,w
ASM:        btfsc status,c
ASM:        movwf indf
ASM:        decf fsr,f
ASM:        decfsz cntr2,f
Goto binlp2
ASM:        decfsz cntr1,f
Goto binlp1
ASM:        movlw 0x30
ASM:        iorwf ascbfr7,f
ASM:        iorwf ascbfr6,f
ASM:        iorwf ascbfr5,f
Return
 
Hi there Eric.. i have tried the codes for data transmission to the PC via the serial cable and it works... thanks for your help and the PIC simulator IDE really helps..

may i know what can the tools (from PIC simulatior IDE) software UART simulation interface and the PC's serial port terminal do? When i simulate the sample coding you provided me in the previous post.. there are no results on both the tools


Are the tools (software UART simulation interface and the PC's serial port terminal ) related to the hardware UART simulation?
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top