|
|
View previous topic :: View next topic |
Author |
Message |
demedeiros
Joined: 27 Dec 2013 Posts: 71
|
Issues with multiple RS232 |
Posted: Tue Aug 23, 2022 6:09 am |
|
|
Hi all,
I am working on a program for communicating with a RFID module over UART using a PIC16F1829 and am running into an issue when using multiple UART streams. It seems that the stream functionality isn't actually working.
For example, in the code below the fputc function should be writing 0x55 ('U') to the RFID module. However I see it (the 'U') in the "PC" stream on the terminal (through a USB adapter). It seems that the fputc function is ignoring the stream parameter.
Any thoughts as to what I am doing wrong here?
EDIT: Compiler Version 5.109
Thanks!
16F1829Testing.h
Code: |
#include <16F1829.h>
#device ICD=TRUE
#fuses HS,NOWDT,NOPROTECT,NOLVP,BROWNOUT
#use delay(crystal=20000000)
#define XTAL_FREQUENCY 20000000
#define BUFFER_SIZE 32
#use rs232(baud=57600, xmit=Pin_B7,RCV=Pin_B5, bits=8, parity=N, receive_buffer=BUFFER_SIZE, errors, stream=RFID)
#use rs232(baud=9600, xmit=Pin_C4,RCV=Pin_C5, bits=8, parity=N, errors, stream=PC)
#define ECHO 0x55
|
16F1829Testing.c
Code: |
#include <16F1829Testing.h>
//Poll the CR95HF for an echo response
char EchoResponse()
{
char response = 0;
fputc(ECHO,RFID); //Write the echo command to the RFID module, 0x55
if(kbhit() > 0) //Check and see if serial data is available, if so read it
{
response = fgetc(RFID);
}
if(response == 0x55) //Check the response value
{
fprintf(PC, "GOT IT!\n");
return 1;
}else
{
fprintf(PC, "NADA!\n");
return 0;
}
}
void main()
{
//Enable RS232 RX interrupt
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
fprintf(PC, "Waiting for Echo!\n");
//Check to see if the RFID module is ready
while(EchoResponse() == 0)
{
output_low(PIN_C7);
}
fprintf(PC,"Got the ECHO!\n");
output_high(PIN_C7);
while(TRUE)
{
//Do stuff
}
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19559
|
|
Posted: Tue Aug 23, 2022 10:39 am |
|
|
Key thing is your kbhit, also needs the stream name. Currently it is
looking for a character arriving on the PC stream (the actual UART). |
|
|
demedeiros
Joined: 27 Dec 2013 Posts: 71
|
|
Posted: Tue Aug 23, 2022 10:50 am |
|
|
Thank you for catching that! Still seem to be having the same issue though.
One interesting thing I noticed is that the order of the #use rs232 statements seems to impact how it functions. For example, if the RFID rs232 statement is first, then I get the following output on the PC terminal "UNADA!" which leads me to believe that the fputc command is being sent over the PC stream when it should be sent over the RFID stream. If the PC stream statement is listed first I get no output to the PC terminal. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19559
|
|
Posted: Wed Aug 24, 2022 12:26 am |
|
|
The order of the #USE statements, affects which is the 'default' #use
used when you have missed a stream name. Means something you are
doing does not have a stream name involved.
When using streams, these need to be used _everywhere_.
You also need to handle kbhit differently. Understand a software RS232,
has no buffering. You need to wait _till_ a character starts to arrive.
Code: |
fputc(ECHO,RFID); //Write the echo command to the RFID module, 0x55
while(kbhit() == FALSE) ; //wait for the character to start
//now it can be read.
response = fgetc(RFID);
|
You have to wait for the falling edge to arrive, and then immediately
go to the receive. No buffering. No character 'waiting' to be read in the
UART. |
|
|
demedeiros
Joined: 27 Dec 2013 Posts: 71
|
|
Posted: Wed Aug 24, 2022 5:11 am |
|
|
Thank you for the information. I am thinking I am misunderstanding the datasheet which is causing my troubles. From reading the datasheet I had assumed that the 1829 has two hardware UARTS, one at RB5 & RB7 and the other at RC4 & RC5, is that incorrect?
[/img] |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19559
|
|
Posted: Wed Aug 24, 2022 5:35 am |
|
|
No, if you look at the start of the data sheet, at
"PIC12(L)F1822/1840/PIC16(L)F182x/1847 Family Types"
You have a table showing the available peripherals. It has a column
for UART, and '1' in this column.
Your chip has only one UART. It can be connected to two different pairs
of pins using the APFCON register. So you can have one hardware UART on
B5/B7 _or_ C4/C5
Not both....
As a 'add on comment' to this, where a chip has got multiple UART's,
the connections are called RX1/TX1 RX2/TX2 etc.. |
|
|
demedeiros
Joined: 27 Dec 2013 Posts: 71
|
|
Posted: Mon Aug 29, 2022 7:47 am |
|
|
AHHH!!! Thank you for clarifying that!
I assume that the first #use RS232 statement using either pair of pins will default to hardware and the second will default to software uart?
EDIT: Figured it out with support, need to use the FORCE_SW switch. My assumption (may be incorrect) is that because I was using a second #use RS232 statement using the other hardware pins, it was defaulting to using the second statement and not the first.
Thanks again for all of your help! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19559
|
|
Posted: Mon Aug 29, 2022 8:21 am |
|
|
No, you have to explicitly set the APFCON register. It defaults to one
particular pair of pins.
Better to set the register yourself, and use UART1 instead of the pin
names, then you kmow which port is using he hardware, |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|