CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Problem with UART RXD isr on 18F47K42 [SOLVED]

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Lagaffe



Joined: 14 Sep 2010
Posts: 14
Location: Portugal, TNV

View user's profile Send private message

Problem with UART RXD isr on 18F47K42 [SOLVED]
PostPosted: Fri Feb 04, 2022 12:07 pm     Reply with quote

Hello everyone,

I'm using CCS PCH C Compiler, Version 5.080, with a PIC18F47K42, and I can't figure out how to make the UART1 RX interrupt to work.

The pic is connected to a GSM module, though a serial connection. I'm able to send a string to the GSM module, and the GSM module replies. I can check and decode it with the osciloscope, and everything seem fine on the hardware side, apart from a little overshoot on the rising edge ... but it might be due to the ground lead of my osciloscope probe being too long.

I'll post my code, maybe anyone can spot something wrong, and help me figure out why I'm not catching nothing with my RX isr.

main.h
Code:

#include <18F47K42.h>
#device ADC=12

#fuses MCLR                //Master Clear pin enabled
#fuses NOWDT               //No Watch Dog Timer
#fuses NOPUT               //No Power Up Timer
#fuses BROWNOUT            //Reset when brownout detected
#fuses BORV28              //Brownout reset at 2.8V
#fuses STVREN              //Stack full/underflow will cause reset
#fuses NODEBUG             //No Debug mode for ICD
#fuses NOLVP               //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#fuses NOWDT               //No Watch Dog Timer
#fuses FCMEN               //Fail-safe clock monitor enabled
#fuses NOXINST             //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#fuses PROTECT             //Code protected from reading
#fuses NOWRTD              //Data EEPROM not write protected


#use delay(internal=32MHz)
#use rs232(baud=115200,parity=N,xmit=PIN_D0,rcv=PIN_C7,bits=8, stream=GSM)


main.c
Code:

#include <main.h>
#include <stdlib.h>
#include "my_flex_lcd20x4.c"

#define BUFFER_SIZE  256

char myString[BUFFER_SIZE];

BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;

BOOLEAN IncomingMsg=FALSE;

#INT_RDA
void serial_isr()
{
   int t;
   
   buffer[next_in]=fgetc(GSM);
   t=next_in;
   next_in=(next_in+1) % BUFFER_SIZE;
   if(next_in==next_out) next_in=t;           // Buffer full !!
   
   IncomingMsg=TRUE;
}

#define bkbhit (next_in!=next_out)

BYTE bgetc()
{
   BYTE c;

   while(!bkbhit) ;
   c=buffer[next_out];
   next_out=(next_out+1) % BUFFER_SIZE;
   return(c);
}

// Remove special chars from the string received from the GSM module
// 0x0A = Line feed "\n"    0x0C = Form feed "\f"    0x0D = Carriage Return "\r"
void CleanReply(void)
{
  int i;
  char temp;
 
  i=0;
  while(bkbhit)
   {
     temp=bgetc();                  // Copy buffer to myString 
     if(!(temp==0x0A || temp==0x0C|| temp==0x0D))
     {
      myString[i]=temp;
      i++;
     }
     if(i > (BUFFER_SIZE-2)) break;
   }
   myString[i]=NULL;                // NULL terminate string
   
   IncomingMsg=FALSE;
}


void main()
{
   
   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);
   
   lcd_init();

   printf(lcd_putc,"\f  UART RX-TX test!");

   while(TRUE)
   {
      fprintf(GSM,"AT\r\n");
      lcd_gotoxy(1,2);printf(lcd_putc,"Sent: AT  isrFlag:%u",IncomingMsg);
      delay_ms(1000);
     
      CleanReply();
      lcd_gotoxy(1,3);printf(lcd_putc,"> %s",myString);
      delay_ms(500);
   }

}


I'm not showing the LCD driver, as it is a bit long, and it is working without any problems, I'll just show the pins used for configuration:

Code:

// my_flex_lcd20x4.c

#define LCD_RS    PIN_C0
#define LCD_RW    PIN_C1
#define LCD_E     PIN_C2

#define LCD_DB4   PIN_C3
#define LCD_DB5   PIN_C4
#define LCD_DB6   PIN_C5
#define LCD_DB7   PIN_C6


On the main.c code, one can see that I even used a flag inside the #INT_RDA isr, just to "flag" if the isr is called, and this flag is always 0 ... and needless to say, I cannot catch the GSM serial reply (which by way it replies "AT OK"

I even started to question if the RX1 pin was working, but if I change the rsr232 initialization line to this:
Code:

#use rs232(baud=115200,parity=N,xmit=PIN_C7,rcv=PIN_D0,bits=8, stream=GSM)

I can see the PIC starts to transmit on the PIN_C7 instead of the previous PIN_D0 ... so, I guess this is not the problem (?)

Oh well, I'm stuck, can someone please check my code, and see what I'm missing ? Thanks in advance


Last edited by Lagaffe on Mon Feb 07, 2022 10:58 am; edited 1 time in total
jeremiah



Joined: 20 Jul 2010
Posts: 1349

View user's profile Send private message

PostPosted: Fri Feb 04, 2022 1:56 pm     Reply with quote

I would start by making sure you are specifying a hardware UART. Some versions of the compiler can interpret your #use rs232 as a software UART and then the ISR wouldn't work

Code:

#use rs232(UART1,baud=115200,parity=N,bits=8, ERRORS,stream=GSM)


I don't know which UART you are using, but I just set the example to 1. Feel free to change it to 2 or 3 or whatever you use.
dyeatman



Joined: 06 Sep 2003
Posts: 1934
Location: Norman, OK

View user's profile Send private message

PostPosted: Fri Feb 04, 2022 2:23 pm     Reply with quote

Since you are using a non-standard XMIT pin and this version is a PPS chip you can try adding Pin-Select lines for the TX/RX pins. Without that I expect it is
generating a software UART which will not generate interrupts.
You also need to add ERRORS to your #use rs232 line.
_________________
Google and Forum Search are some of your best tools!!!!
Lagaffe



Joined: 14 Sep 2010
Posts: 14
Location: Portugal, TNV

View user's profile Send private message

PostPosted: Fri Feb 04, 2022 3:26 pm     Reply with quote

jeremiah wrote:
I would start by making sure you are specifying a hardware UART. Some versions of the compiler can interpret your #use rs232 as a software UART and then the ISR wouldn't work

Code:

#use rs232(UART1,baud=115200,parity=N,bits=8, ERRORS,stream=GSM)


I don't know which UART you are using, but I just set the example to 1. Feel free to change it to 2 or 3 or whatever you use.


Hi jeremiah,

Thanks for your help.

I'm using UART1 for RS232, and I want to use UART2 for RS485 (haven't got there, yet)

According to the datasheet, the hardware pin for RX1 is C7, and TX1 would be a PPS pin, so I have already tried something like this:

Code:

#pin_select U1TX=PIN_D0
#pin_select U1RX=PIN_C7
#use rs232(baud=115200,parity=N,UART1,bits=8,stream=GSM,ERRORS)


problem is, compiler don't let me choose D0 as a U1TX. Compiler only lets me choose some Port B or Port C pins ... which is odd, IMHO, because I failed to see that mentioned on the datasheet. And as I'm using smd components, I designed my PCB to use PIN_D0 as a TX pin. Oh well, I'll have to figure out a way to change my PCB
Lagaffe



Joined: 14 Sep 2010
Posts: 14
Location: Portugal, TNV

View user's profile Send private message

PostPosted: Fri Feb 04, 2022 3:31 pm     Reply with quote

dyeatman wrote:
Since you are using a non-standard XMIT pin and this version is a PPS chip you can try adding Pin-Select lines for the TX/RX pins. Without that I expect it is
generating a software UART which will not generate interrupts.
You also need to add ERRORS to your #use rs232 line.


Hi dyeatman,

Thanks for your help.

I have already tried something like this:

Code:

#pin_select U1TX=PIN_D0
#pin_select U1RX=PIN_C7
#use rs232(baud=115200,parity=N,UART1,bits=8,stream=GSM,ERRORS)


just the compiler don't let me use PIN_D0 for TX1. (please see my previous reply) ... but I'll try to find a way, and I'll post the results Smile
temtronic



Joined: 01 Jul 2010
Posts: 9226
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Feb 04, 2022 3:55 pm     Reply with quote

don't use them fancy PICs with PPS but..
..does THAT PIC allow PPS to configure the pins the way you want ?
It might be that certain pins cannot be used for certain peripherals ??
Buried 3-400 page down in the datasheet there must be a 'legal/illegal use of pins chart' ??
dyeatman



Joined: 06 Sep 2003
Posts: 1934
Location: Norman, OK

View user's profile Send private message

PostPosted: Fri Feb 04, 2022 5:30 pm     Reply with quote

Looking at the datasheet (tables 17.1 and 17.2) that combination of pins is not
available for PPS on the hardware UARTs. Either one or the other (U1 or U2)
but not both.
_________________
Google and Forum Search are some of your best tools!!!!
Ttelmah



Joined: 11 Mar 2010
Posts: 19513

View user's profile Send private message

PostPosted: Fri Feb 04, 2022 10:53 pm     Reply with quote

Some of the latest chips have pretty much universal PPS. Any device,
to any pin. However the older PPS chips have restrictions on what can
be done. Usually particular devices can only go to particular pins from
a group of pins. This chip is one of these. This is why there are the tables
of which devices can go to which pins. The compiler can't change these
limits.
temtronic



Joined: 01 Jul 2010
Posts: 9226
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Feb 05, 2022 7:14 am     Reply with quote

re:
Quote:
Some of the latest chips have pretty much universal PPS

aw, that takes all the fun out of designing ! Very Happy
Lagaffe



Joined: 14 Sep 2010
Posts: 14
Location: Portugal, TNV

View user's profile Send private message

PostPosted: Sat Feb 05, 2022 10:56 am     Reply with quote

temtronic wrote:
don't use them fancy PICs with PPS but..
..does THAT PIC allow PPS to configure the pins the way you want ?
It might be that certain pins cannot be used for certain peripherals ??
Buried 3-400 page down in the datasheet there must be a 'legal/illegal use of pins chart' ??


I confess I didn't read all those 833 pages, but at least I was thinking I could redirect the TX1 output to any other pin.

On table 2 it even mentions that:
For RX1: "This is a PPS remappable input signal. The input function may be moved from the default location shown to one of several other PORTx pins."

For TX1: "All output signals shown in this row are PPS remappable"

I've failed to check tables 17.1 and 17.2, as dyeatman mentioned. ...


Last edited by Lagaffe on Sat Feb 05, 2022 11:12 am; edited 1 time in total
Lagaffe



Joined: 14 Sep 2010
Posts: 14
Location: Portugal, TNV

View user's profile Send private message

PostPosted: Sat Feb 05, 2022 11:04 am     Reply with quote

dyeatman wrote:
Looking at the datasheet (tables 17.1 and 17.2) that combination of pins is not
available for PPS on the hardware UARTs. Either one or the other (U1 or U2)
but not both.


You've nailed my friend !

Indeed, contrary to my first assumption, I can only use Port B or Port C to remap the UARTs I/O. Even though I left RX1 on RC7 and RX2 on RB7, as defined at POR

My mistake was assuming I could remap TX1 to RD0. (TX2 I left on RB5, so, should be OK)
Lagaffe



Joined: 14 Sep 2010
Posts: 14
Location: Portugal, TNV

View user's profile Send private message

PostPosted: Sat Feb 05, 2022 11:11 am     Reply with quote

Ttelmah wrote:
Some of the latest chips have pretty much universal PPS. Any device,
to any pin. However the older PPS chips have restrictions on what can
be done. Usually particular devices can only go to particular pins from
a group of pins. This chip is one of these. This is why there are the tables
of which devices can go to which pins. The compiler can't change these
limits.


Absolutely right my friend !

This is not one of those chips that I can remap pins without restrictions ... unfortunately.
Lagaffe



Joined: 14 Sep 2010
Posts: 14
Location: Portugal, TNV

View user's profile Send private message

PostPosted: Mon Feb 07, 2022 11:02 am     Reply with quote

I want to thank you all for your help ! "My" UART is now working, and I have already marked this post as "SOLVED".


Very Happy
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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