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 support@ccsinfo.com

PIC18F47K42 UART overflow?
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Rikste



Joined: 18 Feb 2022
Posts: 6

View user's profile Send private message

PIC18F47K42 UART overflow?
PostPosted: Fri Feb 18, 2022 8:47 am     Reply with quote

Hi!

I have problem with the pic18F47K42 and the HC-05 Bluetooth module. They both are in same voltage lines. I made voltage divider on the HC-05 RX pin that change PIC's signal 5V to 3,3V. When I power system up, it will work about 1 times of 20 after startup. When it works, I'm able to send and receive characters. Sometimes data is corrupted and I get some weird characters. Baud rate is set to 9600 which is default baud rate of the HC-05 on data line.

I think that what is happening, must be an UART 1 buffer overflow on startup. PIC is not ready on startup to execute INT_RDA interrupt and buffer fill overflow and UART freezes. I think that HC-05 is sending garbage on startup to pic.

It's seems that adding ERRORS on RS232 line not working to clear buffer overflow. I've read that "Clearing UART overflow flag PIC18F47K42" on this forum, but i don't know how to do manual that CREN clear. I try to copy that code on my own code and I was getting errors after errors. I will send my code later on this topic when i get on home.

I purchased and download workshop compiler with pic18f47k42 support on the CCS site last week so it is up to date.

Sorry for bad English.


Last edited by Rikste on Fri Feb 18, 2022 10:23 am; edited 1 time in total
Rikste



Joined: 18 Feb 2022
Posts: 6

View user's profile Send private message

PostPosted: Fri Feb 18, 2022 10:14 am     Reply with quote

Compiler version 5.107

Here is program, its not ready...:

Code:

#include <main.h>
#if defined(__PCH__)
#include <18F47K42.h>
#include <stdlib.h>
#include <string.h>


#fuses NOWDT, BROWNOUT
#FUSES NOMCLR                   //Master Clear pin used for I/O
#use delay(internal=64MHZ)



#pin_select U1TX=PIN_C6
#pin_select U1RX=PIN_C7
#use rs232(UART1, baud=9600, bits=8,parity=N, stop=1, ERRORS ) /// HC-05 Bluetooth default speed 9600, voi mennä kuulemma 1382400... vakio pic18f47k42 on 115200 eli NOSTA NYT HELVETISSÄ TÄTÄ TUOTANTOVERSIOON KUN OPIT OHJELMOIMAAN SEN HC-05




#define VREF_HIGH 0x80
#define VSS_VDD               0x00              // Range 0-Vdd
   
#define OFF output_low  // Lähtöjen tilojen nimet
#define ON  output_high

#define LAHTO1    PIN_B5 // lähtöjen alustus
#define LAHTO2    PIN_B4
#define LAHTO3    PIN_B3
#define LAHTO4    PIN_B2
#define LAHTO5    PIN_D7
#define LAHTO6    PIN_D6
#define LAHTO7    PIN_D5
#define LAHTO8    PIN_D4
#define LAHTO9    PIN_C5

#define TULO1() input(PIN_E1)   //tulojen alustus
#define TULO2() input(PIN_E2) // #define TULO3() input(PIN_E3) MCLR EI PELAA INPUTTINA!!!
#define TULO3() input(PIN_A6)
#define TULO4() input(PIN_C0)
#define TULO5() input(PIN_D0)
#define TULO6() input(PIN_D1)
#define TULO7() input(PIN_D2)
#define TULO8() input(PIN_D3)
char saapuvatieto1; // hyväksytty saapuva tieto HC05


int mode; //suorakäyttö 1 ohjelma 2





#int_rda
 void rda_isr() // HC-5 bluetooth
 {
  saapuvatieto1 = getc();
  putc (saapuvatieto1);
 
  if (saapuvatieto1 == 's' )
   mode = 1; // suorakäyttö
 
  if (saapuvatieto1 == 'o')
  mode = 2; // ohjelma
 
   if (saapuvatieto1 == 'S' )
   mode = 1; // suorakäyttö
 
  if (saapuvatieto1 == 'O')
  mode = 2; // ohjelma

   }





void main(void)
{

enable_interrupts(GLOBAL);
enable_interrupts( INT_RDA );



while(1)
{
 ON(LAHTO1);
 delay_ms(500);
 OFF(LAHTO1);
 delay_ms(500);

 
 if(mode == 1) // suorakäyttö
 {
   if (saapuvatieto1== '1')
   ON(LAHTO1);
     
 if (saapuvatieto1== '2')
  ON(LAHTO2);
 
 if (saapuvatieto1== '3')
  ON(LAHTO3);
 
 if (saapuvatieto1== '4')
  ON(LAHTO4);
     
 if (saapuvatieto1== '5')
  ON(LAHTO5);
 
 if (saapuvatieto1== '6')
  ON(LAHTO6);
 
 if (saapuvatieto1== '7')
  ON(LAHTO7);
     
 if (saapuvatieto1== '8')
  ON(LAHTO8);
 
 if (saapuvatieto1== '9')
  ON(LAHTO9);
 
 if (saapuvatieto1== '0')
{
 OFF(LAHTO1);
 OFF(LAHTO2);
 OFF(LAHTO3);
 OFF(LAHTO4);
 OFF(LAHTO5);
 OFF(LAHTO6);
 OFF(LAHTO7);
 OFF(LAHTO8);
 OFF(LAHTO9);
}     
  }

}
}
 

oscillator is ok... i measured 10 blinks on 10 seconds...

I need to know how i can clear that Uart overflow flag...

sorry for ugly code Sad

EDIT: I cleanup code... still same isue..
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Fri Feb 18, 2022 11:51 am     Reply with quote

Add 'NOINIT' to the #use RS232 declaration.

Then in your code, have a little delay at the start, and then, read the
UART input line, and wait for this to go high. Once it goes high, enable the
UART, with:
Code:

setup_uart(TRUE);


The UART should still call the interrupt if it is overflowed, and reading a
character in here should unlock the UART (this is what ERRORS adds).
However the characters read will be garbage at this point. Problem is that
as written, you send this garbage character. So, wait for the HC05 to
have actually waken up before you enable the UART.
temtronic



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

View user's profile Send private message

PostPosted: Fri Feb 18, 2022 12:07 pm     Reply with quote

I wonder if it's the 5 volt PIC 3 volt peripheral problem ??
I read 'voltage divider' in the post...

Haven't checked IF the PIC will see the 3 volts tx from peripheral as a valid '1'.

hmm.. just downloaded the 850 page 'datasheet'... curious, I'm wondering HOW 'PPS' pins are handled for logic '1' and '0' ?

Old PICs had TTL and Schimdt and possibly SMB levels...
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Fri Feb 18, 2022 12:55 pm     Reply with quote

Have to agree. The best way would be to run the PIC at the same voltage
as the HC-05. He talks about a divider (which suggests the PIC is running
at a higher voltage than the HC-05), but if so, would be borderline going
the other way. He can program the input voltage required using the input
voltage control settings, but is not doing that.
Rikste



Joined: 18 Feb 2022
Posts: 6

View user's profile Send private message

PostPosted: Fri Feb 18, 2022 1:22 pm     Reply with quote

Thanks for quick answers!

Yes indeed pic is driven 5v line and so is HC-05 because it is also 5v device, but that HC-05 rx line can take only 3,3v... same configuration works earlier good on pic18f4550... only difference was that power on HC-05 was controlled with switch. Now when powering both on same time, this issue occurs.

In datasheet page 746 suggests that TTL pins low is 0 - 0,8v and high is 2v or over that. So that will maybe work?

Anyway i did changes :
Code:
#use rs232(UART1, baud=9600, bits=8,parity=N, stop=1, ERRORS,NOINIT)


and:

Code:
void main(void)
{
 delay_ms(5000);
setup_uart(TRUE);
saapuvatieto1 = getc(); // [b]hangs here so there is no any charcters[/b]
enable_interrupts(GLOBAL);
enable_interrupts( INT_RDA );



while(1)
{
}

program hangs on getc() maybe because interrupts are not enabled

and i try this:
Code:
 delay_ms(5000);
saapuvatieto1 = getc(); // [b]Cannot go forward because UART is FALSE[/b]
setup_uart(TRUE);
enable_interrupts(GLOBAL);
enable_interrupts( INT_RDA );
 


and i cant get any characters if:


Code:
void main(void)
{
 delay_ms(5000);
setup_uart(TRUE);
enable_interrupts(GLOBAL);
enable_interrupts( INT_RDA );


So NOINIT and setup_uart(TRUE) not work. I wonder because this chip has two hardware UARTS, is that setup_uart OK?

or hardware issue on my project?

Edit: I remove NOINIT + setup_uart(TRUE) + delay_Ms(5000), no effect / no characters. I remove ERRORS and then i get characters. i try different baud rates (4800, 9600, 19200, 38400, 76800) and 9600 is correct but i still get about 1/5 startups right characters. Why removing ERRORS give best results?


Last edited by Rikste on Fri Feb 18, 2022 2:57 pm; edited 1 time in total
temtronic



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

View user's profile Send private message

PostPosted: Fri Feb 18, 2022 2:51 pm     Reply with quote

while I don't use that PIC....

CCS provides an example program ex_sisr.c which is a basic interrupt driven serial code 'snippet'. it's worked for years with 18F46K22.

You should show a small, compilable program so others can test, maybe 'see' something obvious that you can't.
As for the logic levels, again I don't know about PPS pins...but if 'TTL' then it should work. However you say the HC05 IS 5 volts, so you shouldn't need the dividers...maybe VDD is 5, but logic pins are 3V ??
Rikste



Joined: 18 Feb 2022
Posts: 6

View user's profile Send private message

PostPosted: Fri Feb 18, 2022 3:05 pm     Reply with quote

Yes. Vdd 5v and logic pins are 3,3v. So if PIC's TX-pin drives on direct 5v HC-05 RX-pin, that not last very long. Maybe i must use logic converter, but that voltage divider make job for PIC18F4550... HC-05 TX-pin drives pics RX-pin at 3,3v. I must look that ex_sisr.c and try. I don't wanna bother you guys too much. You have helped me lot for now...Thanks

EDIT: I must ask, is that old ERROR- error (overflow) with PIC18F47K42 fixed on this version (5.107) of CCS compiler or is that manual CREN clear still way to go?
temtronic



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

View user's profile Send private message

PostPosted: Fri Feb 18, 2022 5:33 pm     Reply with quote

...well I went to page 746 read the chart...
But still don't know for sure WHAT decides if a pin is TTL, ST ,I2C or SMB2, or 3.
Maybe there's a chart or table, I didn't see anything obvious in the PPS section.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Fri Feb 18, 2022 10:49 pm     Reply with quote

Whoa.

The HC-05, is a 3.3v chip.
If you have a 5v module, these will normally have built in dividers
lowering a potentially 5v incoming signal to 3.3v (to avoid damage),
but normally just feed their outputs out directly, which will be a 3.3v
signal. This is because the are designed for devices like the Arduino,
that have TTL input thresholds.
A quick test with a meter or scope is called for. However if
the unit has built in dividers, if you are running the PIC at 3.3v, it
won't be driving the GC-05 inputs properly.
Rikste



Joined: 18 Feb 2022
Posts: 6

View user's profile Send private message

PostPosted: Sat Feb 19, 2022 6:43 am     Reply with quote

This is indeed voltage issue, pic sends every time on every startup, every second Ok to bluetooth terminal (so that self made voltage divider works) but when i try to send something on the pic, that won't work. I have plans to use 5 ADC channels and i'm not sure that Vdd 3,3v will cause something other problems on that... is there any change to do with program code something that will help... if not then i try use fast FET to raise that input voltage to pic...
temtronic



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

View user's profile Send private message

PostPosted: Sat Feb 19, 2022 8:01 am     Reply with quote

Two simple hardware solutions...

1) use a 3v rated PIC. Usually has 'L' in the part number.

2) use a 'logic level' conversion module between the PIC and HC05.

You can't use software to fix the hardware problem, unless you see IF you can change PPS pins 'structrure' (TTL, ST, etc).

Edit. I looked at the electrical specs...that PIC can run at 3 volts, do that, problem solved.
Rikste



Joined: 18 Feb 2022
Posts: 6

View user's profile Send private message

PostPosted: Sat Feb 19, 2022 10:46 am     Reply with quote

Ok. I will try that, maybe 3,3v 0,8A regulator will do that job. Thanks for help.
temtronic



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

View user's profile Send private message

PostPosted: Sat Feb 19, 2022 11:24 am     Reply with quote

Better look at the power needed for the HC05. Most wireless units need well over an amp, not for long, but if you don't have capacity, VDD goes low, PIC gets reset....
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Tue Feb 22, 2022 6:19 am     Reply with quote

It's not that bad Jay.
The HC05, is a Bluetooth module, not something like WiFi or cellular.
It can draw a peak of about 100mA, but typically only 30mA.
Like most wireless modules a reasonably good decoupling capacitor
is needed close to the module. The instantaneous change in current
draw when it keys up, can cause problems otherwise....
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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