'Must be called once before any others
Sub InitUSART
SPBRG = SPBRG_Val
Set TXEN On 'enable transmission
Set BRGH On 'BRGH=1
Set SPEN On 'enable serial port
Set CREN On 'enable receive
End Sub
'Send a byte
Sub HSerSend(In SerData)
Do Until TXIF 'check if transmitter is busy
Loop
TXREG = SerData 'transmit data
End Sub
'Send a string
sub HserPrint(In PrintData$)
PrintLen = PrintData(0)
If PrintLen = 0 Then Exit Sub
For SysPrintTemp = 1 To PrintLen
HSerSend(PrintData(SysPrintTemp))
Next
End Sub
'Send value of byte as characters
sub HserPrintByte(In PrintValue)
ValueTemp = 0
If PrintValue >= 100 Then
ValueTemp = PrintValue / 100
HserSend(ValueTemp + 48)
PrintValue = PrintValue - ValueTemp * 100
End If
If ValueTemp > 0 Or PrintValue >= 10 Then
ValueTemp = PrintValue / 10
HserSend(ValueTemp + 48)
PrintValue = PrintValue - ValueTemp * 10
End If
HSerSend(PrintValue + 48)
End Sub
'Send value of byte as characters with CR and LF
Sub HserPrintByteCRLF(In PrintValue)
HserPrintByte(PrintValue)
HserSend(13)
HserSend(11)
End Sub
'Send CR and LF
Sub HserPrintCRLF
HserSend(13)
HserSend(11)
End Sub
'Receive a byte if avialable, otherwise return zero
Function HSerReceive
HSerReceive = 0
If RCIF Off Then HSerReceive = RCREG
End Function
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
When I try sending a byte using HSerSend, the pic locks up. I have an LCD hooked up that prints out "sending", then the program calls HSerSend(Data1) where Data1 = 87. After the HSerSend(Data1) line, the program prints out "Sent". However, the program never gets to the print "Sent" line. There is a loop that appears to be locking up my PIC for a reason??? in the usart library (HSerSend):
Do Until TXIF 'check if transmitter is busy
Loop
I put a voltmeter to the TX and RX pins on the PIC16F687, the TX pin is putting out 5.0 volts, and the RX pin is putting out 0.0 volts. Is the normal behavior? Should the TX pin be at 5.0 volts even when nothing is being sent?
I am using a MAX232 chip (actually, a PCB from ebay with the chip) to interface my PC COM1 port with the PIC. The RX line from the MAX232 is 3.30 volts (when not plugged into the computer). The TX line from the MAX232 is at 5.0 volts (when not plugged into the computer). The MAX232 is getting its 5.0 volts from the 5v reg that is also powering the PIC and LCD. Is the normal behavior?
I am unable to receive or send any data through the serial port between my PIC and computer.
I have tied the RX and TX lines on the PIC side of the MAX232, and was able to send a receieve data from my computer. This (I believe) verifies that the COM1 port on my computer is functioning, and so is the MAX232 chip. Am I correct to assume this?
Why can't I send or receive any Data between my PIC and computer? I am using a VB.NET program with a serialport control to send/receive data.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
After much work I have gotten it to work. I had to change the HserSend to:
Sub HSerSend(In SerData)
wait until TXIF = 1 'check if transmitter is busy
TXREG = SerData 'transmit data
End Sub
The do...loop would lock up my PIC, but what wait instruction works just fine.
and to receive a byte:
'Receive a byte if avialable, otherwise return zero
Function HSerReceive
HSerReceive = 0
If RCIF on Then HSerReceive = RCREG 'I have changed it to IF RCIF ON instead of OFF
End Function
from the datasheet:
"The RCIF interrupt flag bit of the PIR1 register is set
whenever the EUSART receiver is enabled and there is
an unread character in the receive FIFO."
Is there a way to receive a string with this library, or would I have to manually read each byte coming in?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I did not need extended receive capability when I created the library, a simple byte in worked at the time. If you have updated code to allow for similiar receive functions, that would be good. Please post. I will look at the definition of the RCIF bit when I get a chance. I can't remember if I verified receive, but I believe I did. I believe I followed the GCBasic routine for that.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
First of all, thanks for this code; it will help me a lot when I get started on my project.
I think however there is an error in the HserPrintByteCRLF and HserPrintCRLF sub routines. The line feed code should I believe be 10 not 11 ( I always remember CRLF as 0D0A in hex ). 11 is vertical tab.
I'm new to BASIC & GCBASIC in particular, but what is the meaning of 'In' in the subroutine parameter list? I've been through the manual and cannot find a reference to it. Thanks.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I believe you are correct. I must have read it wrong. Line feed is a 10, and not 11. The above did do what I wanted in hyperterminal and never caught the mistake. Also, the receive routine will not read a null. It could be modified to return a condition to indicate a receive null.
Anybody want to clean these up and add the baud code and repost? Many thanks.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
' For Microchip PIC MCU's with hardware serial port
'
' Library for use with Great Cow Basic
'
'#define SPBRG_Val 51 '9600 Baud @8Mhz
#define SPBRG_Val 8 '57600 Baud @8Mhz
'Must be called once before any others
Sub InitUSART
SPBRG = SPBRG_Val
Set TXEN On 'enable transmission
Set BRGH On 'BRGH=1
Set SPEN On 'enable serial port
Set CREN On 'enable receive
End Sub
'Send a byte
Sub HSerSend(In SerData)
Do Until TXIF 'check if transmitter is busy
Loop
TXREG = SerData 'transmit data
End Sub
'Send a string
sub HserPrint(In PrintData$)
PrintLen = PrintData(0)
If PrintLen = 0 Then Exit Sub
For SysPrintTemp = 1 To PrintLen
HSerSend(PrintData(SysPrintTemp))
Next
End Sub
'Send value of byte as characters
sub HserPrintByte(In PrintValue)
ValueTemp = 0
If PrintValue >= 100 Then
ValueTemp = PrintValue / 100
HserSend(ValueTemp + 48)
PrintValue = PrintValue - ValueTemp * 100
End If
If ValueTemp > 0 Or PrintValue >= 10 Then
ValueTemp = PrintValue / 10
HserSend(ValueTemp + 48)
PrintValue = PrintValue - ValueTemp * 10
End If
HSerSend(PrintValue + 48)
End Sub
'Send value of byte as characters with CR and LF
Sub HserPrintByteCRLF(In PrintValue)
HserPrintByte(PrintValue)
HserSend(13)
HserSend(11)
End Sub
'Send CR and LF
Sub HserPrintCRLF
HserSend(13)
HserSend(11)
End Sub
'Receive a byte if avialable, otherwise return zero
Function HSerReceive
HSerReceive = 0
If RCIF Off Then HSerReceive = RCREG
End Function
Thank you very much, your code works fine...
I just used this:
#define SPBRG_Val (ChipMHz*1000000/baudrate-16)/16
Instead of:
#define SPBRG_Val 8 '57600 Baud @8Mhz
And in the user program add this line with baudrate definition:
#define baudrate 9600 'or any other
This way is no needed to edit the library file.
Greetings.
Does this require the
#include "usart.h"
at the beginning of the program?
When I try sending a byte using HSerSend, the pic locks up. I have an LCD hooked up that prints out "sending", then the program calls HSerSend(Data1) where Data1 = 87. After the HSerSend(Data1) line, the program prints out "Sent". However, the program never gets to the print "Sent" line. There is a loop that appears to be locking up my PIC for a reason??? in the usart library (HSerSend):
Do Until TXIF 'check if transmitter is busy
Loop
I put a voltmeter to the TX and RX pins on the PIC16F687, the TX pin is putting out 5.0 volts, and the RX pin is putting out 0.0 volts. Is the normal behavior? Should the TX pin be at 5.0 volts even when nothing is being sent?
I am using a MAX232 chip (actually, a PCB from ebay with the chip) to interface my PC COM1 port with the PIC. The RX line from the MAX232 is 3.30 volts (when not plugged into the computer). The TX line from the MAX232 is at 5.0 volts (when not plugged into the computer). The MAX232 is getting its 5.0 volts from the 5v reg that is also powering the PIC and LCD. Is the normal behavior?
I am unable to receive or send any data through the serial port between my PIC and computer.
I have tied the RX and TX lines on the PIC side of the MAX232, and was able to send a receieve data from my computer. This (I believe) verifies that the COM1 port on my computer is functioning, and so is the MAX232 chip. Am I correct to assume this?
Why can't I send or receive any Data between my PIC and computer? I am using a VB.NET program with a serialport control to send/receive data.
After much work I have gotten it to work. I had to change the HserSend to:
Sub HSerSend(In SerData)
wait until TXIF = 1 'check if transmitter is busy
TXREG = SerData 'transmit data
End Sub
The do...loop would lock up my PIC, but what wait instruction works just fine.
and to receive a byte:
'Receive a byte if avialable, otherwise return zero
Function HSerReceive
HSerReceive = 0
If RCIF on Then HSerReceive = RCREG 'I have changed it to IF RCIF ON instead of OFF
End Function
from the datasheet:
"The RCIF interrupt flag bit of the PIR1 register is set
whenever the EUSART receiver is enabled and there is
an unread character in the receive FIFO."
Is there a way to receive a string with this library, or would I have to manually read each byte coming in?
I did not need extended receive capability when I created the library, a simple byte in worked at the time. If you have updated code to allow for similiar receive functions, that would be good. Please post. I will look at the definition of the RCIF bit when I get a chance. I can't remember if I verified receive, but I believe I did. I believe I followed the GCBasic routine for that.
What if i will send 0x00 to PIC?
how I will not is there )x00 or just nothing received from function?
First of all, thanks for this code; it will help me a lot when I get started on my project.
I think however there is an error in the HserPrintByteCRLF and HserPrintCRLF sub routines. The line feed code should I believe be 10 not 11 ( I always remember CRLF as 0D0A in hex ). 11 is vertical tab.
I'm new to BASIC & GCBASIC in particular, but what is the meaning of 'In' in the subroutine parameter list? I've been through the manual and cannot find a reference to it. Thanks.
I believe you are correct. I must have read it wrong. Line feed is a 10, and not 11. The above did do what I wanted in hyperterminal and never caught the mistake. Also, the receive routine will not read a null. It could be modified to return a condition to indicate a receive null.
Anybody want to clean these up and add the baud code and repost? Many thanks.