Maker Pro
Maker Pro

Basic PIC18F4520 TX/RX question

Graham Nelsen

May 13, 2017
5
Joined
May 13, 2017
Messages
5
I have programmed a PIC18F4520 to send some hex values (A,B, and C) to PUTTY every time I press a button. That works great. In my program, I press the button to branch to the transmit subroutine. It loads A, sends it out, loads B, sends it out, then loads C, and sends it out, all out of pin 25, the TX pin.

This is going to be a problem later. It is literally sending "start bit, data, stop bit" for A, and then "start bit, data stop bit" for B, and same for C. However, I would rather it send "start bit, ABC, stop bit," like a string. This is because I would like the PIC to send a full command to something. In order to do this, it needs to be a data string, but I can only figure out on my own how to make the PIC send individual, isolated 8 bit words, each with their own start/stop bit. How can I make the PIC send strings of data?

Also, is there a way to do this without C? I learned how to do PICs in school, and we learned the RISC way (MOVLW 0xF4 kind of stuff).
 

hevans1944

Hop - AC8NS
Jun 21, 2012
4,878
Joined
Jun 21, 2012
Messages
4,878
How can I make the PIC send strings of data?
If you plan to use the PIC's EUSART to do this, I suggest that you read and understand why asynchronous characters begin with a start bit, end with a stop bit, and have only eight or nine NRZ (non-return to zero) data bits in between. Extending the number of data bits as you suggest requires either using a self-clocking code, such as Manchester encoding, to avoid errors in recovering the data, or using a separate clock line to clock the transmit and receive data in a synchronous fashion. See section 18.3 EUSART Synchronous Master Mode on the PIC18F2420/2520/4420/4520 datasheet.

Since controlling the PIC EUSART functions requires the ability to read and write particular bits in the control registers in a timely fashion, this is not a task easily performed in C. Use your assembly language skills to program the PIC. You claimed "I learned how to do PICs in school, and we learned the RISC way (MOVLW 0xF4 kind of stuff)" does not convey that you learned much of anything about programming PICs. A refresher course might be in order. Also check out the Microchip forums, where you will be expected to post code snippets and explain exactly what you are trying to DO before receiving any useful advice. Same should apply here, too, but this is an open forum and folks will chime in with their opinions even if they don't have facts supplied by you.

So, post a copy of the code you are using now to do whatever it is you are doing. And please tell us in some detail exactly what it is you are doing. For example, what device (or devices) receives the serial data stream produced at the TX port? How is this data stream processed by the receiving device (or devices)? If you implement a UART function in the device (or devices) that receive data from your PIC, why is it a problem to collect as many bytes of command "data" as you need to perform command functions? Other than saving the time overhead of one start bit and one stop bit per data byte, what is the advantage of transmitting three consecutive data bytes without wrapping each byte between a start bit and stop bit?

Again, we can give better advice if you would tell us what you are trying to DO.
 

Graham Nelsen

May 13, 2017
5
Joined
May 13, 2017
Messages
5
Thank you for your reply. This is my first time on an electronics forum, I wasn't sure how much info was required. We never learned serial comms for PICs in school, just the basics (how to use a PIC18). Everything outside the basics (a/d conversion, serial comms, etc.) I've been teaching myself, which is why my knowledge is fractured and incomplete. To be more clear, I want my PIC to send "ATZ" to an ELM 327 (RS232/OBDII translator), which is expecting a string. I can send the command "ATZ" from PUTTY on a laptop to the ELM 327 at the correct baud rate, and it works, I can communicate with my car's on board diagnostics computer and everything. But, when I send it from the PIC, the ELM 327 doesn't hear "ATZ", but instead hears "A (enter), T (enter), Z (enter)", which isn't what it's asking for, and sends me back question marks. This is why I want to send it as a whole.
 

Graham Nelsen

May 13, 2017
5
Joined
May 13, 2017
Messages
5
Beneath is the code for my PIC. The receive function, as I am still working on this, is only expecting ANY form of data to satisfy the code so it can move on. Once I can make it transmit the command ATZ to my car in such a way that it accepts it, I'll start working on reception.

;--------------------------------------

Program
;------------------------------------------------------------------------------
MOVLW 0x7C ;Do NOT deletes these commands! They set the
MOVWF OSCCON ;internal clock to 8 MHz.
;------------------------------------------------------------------------------

;***************************************************************

Start

;--------------set up XMTR-----------

MOVLW 0x20 ; enable transmit and choose low baud
MOVWF TXSTA
MOVLW 0x0C ; 9600 bps
MOVWF SPBRG
BCF TRISC, TX ; make TX pin an output
BSF RCSTA, SPEN ; enable serial port

;--------------set up RCVR-----------

MOVLW 0x90 ;enable receive and serial port
MOVWF RCSTA
MOVLW 0x0C ;9600 bps
MOVWF SPBRG
BSF TRISC, RX ;make RX pin an input
CLRF REGA

;--------------Initialize Carputer------

Repeat

MOVLW 0x41
CALL TRANS
MOVLW 0x54
CALL TRANS
MOVLW 0x5A
CALL TRANS
MOVLW 0x00 ; null to purge the buffer
CALL TRANS



R1

BTFSS PIR1, RCIF ;check for ready
BRA R1
MOVFF RCREG, REGA

BRA Repeat




;-----------Subroutines------------

TRANS

S1

BTFSS PIR1, TXIF ; wait until the last bit is gone
BRA S1
MOVWF TXREG
RETURN

;******************************************************************************
;End of program

END
 
Last edited:

Minder

Apr 24, 2015
3,478
Joined
Apr 24, 2015
Messages
3,478
You may want to look at the Usart code in an app note on the Picmicro site, it gives examples in Assembly.
I can look up the app No if you cannot find it.
A couple I believe are AN774 and AN777
For testing communication there is a free pgm out there RS232HEXCOM. which makes it very easy to trouble shoot.
M.
 
Last edited:

Graham Nelsen

May 13, 2017
5
Joined
May 13, 2017
Messages
5
I found the notes, thank you for that. Unfortunately it doesn't solve my problem, but it was helpful.
 

Graham Nelsen

May 13, 2017
5
Joined
May 13, 2017
Messages
5
Oh wait, I was way off. Now I get it. It's not that the ELM327 is seeing A (enter) T (enter) Z (enter), I have to put a CR at the end of my code. I didn't realize that. I got it working, I'm still learning the fundamentals of serial comms, thanks for your help, very much appreciated.
 

hevans1944

Hop - AC8NS
Jun 21, 2012
4,878
Joined
Jun 21, 2012
Messages
4,878
Oh wait, I was way off. Now I get it. It's not that the ELM327 is seeing A (enter) T (enter) Z (enter), I have to put a CR at the end of my code. I didn't realize that. I got it working, I'm still learning the fundamentals of serial comms, thanks for your help, very much appreciated.
Glad to see you figured it out. From your code listing, it appears you have "hard coded" the ASCII string "ATZ<null>" when it should have been "ATZ<cr>" Note that so-called "AT Commands" are left over from telephone modem technology. The "AT" stands for "ATtention" and signifies the characters that follow (one or more) are a command sequence, always ending in a <cr> character. Lots of consumer electronics still use this serial protocol, even when it has nothing to do with a modem.
 

CDRIVE

Hauling 10' pipe on a Trek Shift3
May 8, 2012
4,960
Joined
May 8, 2012
Messages
4,960
Forgive me Hop but I'm having an anal retentive moment. ... That would be "Hayes Commands". "AT" is only one command of the many in the Hayes Command Set.

Anally yours, :D
Chris
 

hevans1944

Hop - AC8NS
Jun 21, 2012
4,878
Joined
Jun 21, 2012
Messages
4,878
Forgive me Hop but I'm having an anal retentive moment. ... That would be "Hayes Commands". "AT" is only one command of the many in the Hayes Command Set.

Anally yours, :D
Chris
The Hayes Command Set was unique to Hayes modems, IIRC, but widely copied by other modem manufacturers who advertised their products as being "Hayes compatible" when often there existed differences in how these "Hayes compatible" modems interpreted and acted upon AT commands.

I used both Hayes modems and U.S. Robotics modems interchangeably as far as my MS-DOS, and later Windows, software was concerned. My last modem was retired when I decided I no longer needed a landline telephone for FAX capability. IIRC, we were up to a blazing 9600 baud dial-up by then, but I was connected to the Internet with ADSL over copper and later via cable modem. Here in Florida we are connected to the Internet via Frontier Communications FiOS, allegedly with a symmetrical 50 Mbps up and 50 Mbps down. Too bad many of the servers I connect to aren't provisioned that way, although Amazon Prime and Netflix seem to do okay streaming two video streams simultaneously to my house. Modems are now just a bad dream from the late 20th century.

Hayes Commands originally always began with the character string "AT" followed by one or more characters that completed the command, but this was later expanded by the inclusion of prefix characters before the AT characters for increased functionality. The character string "AT<cr>" is a "do nothing" command all by itself.
 

CDRIVE

Hauling 10' pipe on a Trek Shift3
May 8, 2012
4,960
Joined
May 8, 2012
Messages
4,960
Hop, I stand corrected. You're more anal than me! :p Seriously though, thanks for the history.

Psss ... I still have about 4 modems squirreled away somewhere. Just in case they come back into fashion. I've been hopelessly waiting for steam locomotives to come back too. My wife is glad I don't have any of them squirreled away!

Chris
 

hevans1944

Hop - AC8NS
Jun 21, 2012
4,878
Joined
Jun 21, 2012
Messages
4,878
As I was picking through my fine collection of "electronics junque," trying to decide what to take to Florida and what to leave behind, perhaps saving for a future trip, I ran across several modems. But it never crossed my mind that these would in any way be useful in Venice, FL, so they stayed behind. Their "wall wart" power supplies were another matter. Who doesn't need an extra "wall wart" power supply?

As I was looking through all these "treasures" I had to sadly leave behind my collection of high-voltage capacitors, in their hermetically-sealed steel cans with ceramic-insulated terminals, carefully shorted with fine copper wire, lest they (somehow) acquire a charge and deliver a shocking experience while sorting through them. They simply took up too much space and weighed too much. Maybe I will bring some back this month though. Or not. This is just a short visit to check on the house, maybe shut off the water heater and close some water valves, find someone to mow the yard... yada, yada, yada... and attend Granddaughter's high school graduation. If I can squeeze it in, maybe get the tandem particle accelerator working again, since apparently my previous boss landed another Phase I SBIR contract to make a few more PCSS devices. Why he would accept such a contract, having "retired" the only person at UES qualified to maintain the accelerator, is beyond my understanding.

My wife is glad I don't have any of them squirreled away!
I like steam-powered engines too, but cannot afford to indulge in the hobby of restoring and operating steam locomotives. There are some people who do, even putting in short runs of track to operate on. Don't know if any of these souls operate in Florida though. The hobby is sort of like pipe-organ collecting. Massive investment of time, money, and property required to do that, but I knew at least two people in Dayton who could afford to do so. Well, one of them could afford it; the other was clearly a wannabe who happened upon a church organ just as the church was about to be demolished to make room for a downtown parking lot. So he bought the thing at a "fire sale" price and carted the organ off in pieces, but AFAIK didn't have an appropriate facility in which to re-assemble it.

Oh, gee... have we hijacked this thread?
 
Top