Soft_UART with PIC18F4550

mikroC, mikroBasic and mikroPascal PRO for Microchip’s 8-bit PIC MCUs.
Post Reply
Author
Message
ennng
Posts: 61
Joined: 25 Sep 2012 19:48

Soft_UART with PIC18F4550

#1 Post by ennng » 12 Jun 2019 15:50

Hello All;
I am working with PIC18F4550 connected to LCD through SPI and all work fine, but I want also to send the data to UART and I guess these two pins (UART and SPI) are shared in this MCU, so I can use one at a time.
I tried to use soft_uart and I assigned it to portD, E all sending to terminal but I got strange characters! even though I sent string after conversion (floattostr).
Could you please give a simple example of that: (My requirements)
Read ADC value and converted to volt and then converted to string and then send to LCD (SPI)->all these work fine for me, what I want now is to send the volt value through soft_uart !
Regards

User avatar
stefan.filipovic
mikroElektronika team
Posts: 1135
Joined: 18 Dec 2018 10:30

Re: Soft_UART with PIC18F4550

#2 Post by stefan.filipovic » 13 Jun 2019 13:19

Hi,

Please find the simple example of Software UART library for PIC18F4550 in the attachment.

Kind regards,
Attachments
Soft_UART_PIC18F4550.zip
(40.67 KiB) Downloaded 83 times
Stefan Filipović

ennng
Posts: 61
Joined: 25 Sep 2012 19:48

Re: Soft_UART with PIC18F4550

#3 Post by ennng » 13 Jun 2019 14:44

Dear stefan.filipovic

Thank you for your replay,
I tried this example but doesn't work for me since I want to send ADC (value) or Volt after convert it to string!

Regards

User avatar
stefan.filipovic
mikroElektronika team
Posts: 1135
Joined: 18 Dec 2018 10:30

Re: Soft_UART with PIC18F4550

#4 Post by stefan.filipovic » 13 Jun 2019 15:22

Hi,

Could you please tell me step by step how have you been trying to test this example?

What development board are you using?

If you want to display ADC value, then you need to use IntToStr function to convert an integer to string.
Since there is no Soft_Uart_Write_Text function, you need to write this value character per character using Soft_Uart_Write function as shown below:

Code: Select all

char ADC_text[5];
char i = 0;

while(ADC_text[i]) {
   Soft_UART_Write(ADC_text[i]);
   i++;
}
Kind regards,
Stefan Filipović

ennng
Posts: 61
Joined: 25 Sep 2012 19:48

Re: Soft_UART with PIC18F4550

#5 Post by ennng » 14 Jun 2019 11:08

Hi;

Thank you,
Yes, I used attached code as it is copy and past, but didn't work, nothing happen in the terminal!

I used simple breadboard with PIC18F4550 or PIC18F4553 (20MHz crystal) and I test it with hardware UART and just works fine, I got what I send on the terminal but nothing happen with soft_UART, please see code below and modify accordingly,
Thank you,

Code: Select all

float adc;
float volt;
char Volt_text[5];
char i = 0;


void main() {

/*TRISC.B6=0;
  LATC=0;
  PORTC=0;
  UART1_Init(19200);               // Initialize UART module at 9600 bps
  Delay_ms(100);                  // Wait for UART module to stabilize*/
  
    TRISE.B2=0;
    LATE.B2=0;
    PORTE.B2=0;
    Soft_UART_Init(&PORTE, 1, 2, 19200, 0);
    delay_ms(100);


  while (1) {                     // Endless loop

       /*UART1_Write_Text("A");
        UART1_Write(10);
        UART1_Write(13);
        delay_ms(1000);*/
        
        adc = ADC_Read(0);
        volt = (adc*5000.0/4096.0);
        floattostr(volt,Volt_text);
        while(Volt_text[i]) {
        Soft_UART_Write(Volt_text[i]);
        i++;
         }
    }
  }

User avatar
stefan.filipovic
mikroElektronika team
Posts: 1135
Joined: 18 Dec 2018 10:30

Re: Soft_UART with PIC18F4550

#6 Post by stefan.filipovic » 14 Jun 2019 11:56

Hi,

You must configure RE1, RE2 pins as digital and disable comparators. The ADC_Read function returns the unsigned type value so you need to declare "adc" variable as unsigned. Also, you need to clear 'i' iterator after while(Volt_text) loop.
Also, your ADC to voltage calculation is not correct, this MCU has an ADC of 10-bit resolution so it should be divided by 1024.
See the code below.

Code: Select all

unsigned adc;
float volt;
char Volt_text[5];
char i = 0;

void main() {
    ADCON1 |= 0x09;                         // Configure ports with analog function as digital
    CMCON  |= 7;                            // Disable comparators

    TRISE.B2=0;
    LATE.B2=0;
    PORTE.B2=0;
    
    Soft_UART_Init(&PORTE, 1, 2, 19200, 0);
    delay_ms(100);
    while (1) {
        adc = ADC_Read(0);
        volt = (adc*5000.0/1024.0);
        floattostr(volt,Volt_text);
        Delay_ms(10);
        while(Volt_text[i]) {
            Soft_UART_Write(Volt_text[i]);
            i++;
            Delay_ms(10);
        }
        Soft_UART_Write(13);
        Soft_UART_Write(10);
        i = 0;
    }
}
Kind regards,
Stefan Filipović

ennng
Posts: 61
Joined: 25 Sep 2012 19:48

Re: Soft_UART with PIC18F4550

#7 Post by ennng » 14 Jun 2019 15:44

Thank you very much
now it gives something not like previous but still the value is not correct !
I used PIC18F4553 which is 12bit ADC.
The volt is wrong and keep changing quite a lot please see attached photo.


[img]
Untitled.png
Untitled.png (140.03 KiB) Viewed 3342 times
[/img]

User avatar
stefan.filipovic
mikroElektronika team
Posts: 1135
Joined: 18 Dec 2018 10:30

Re: Soft_UART with PIC18F4550

#8 Post by stefan.filipovic » 14 Jun 2019 16:33

Hi,

We were talking about PIC18F4550, unfortunately, I do not have F4553 to test it, but I tested it with F4550 without issues.

What voltage do you measure on RA0 with a multimeter?
How much differs multimeter and ADC calculation values?

Kind regards,
Stefan Filipović

ennng
Posts: 61
Joined: 25 Sep 2012 19:48

Re: Soft_UART with PIC18F4550

#9 Post by ennng » 16 Jun 2019 11:07

Hello Dear;

Thank you for your replay,
I changed the MCU to PIC18F4550 but still has the same issue, it gave wrong value (volt),
The volt on ADC channel is stable on RA0 is like potentiometer voltage divider. I also tried to change the baud rate but still the same!
see attached photo.

[img]
Untitled.png
Untitled.png (127.59 KiB) Viewed 3327 times
[/img]

ennng
Posts: 61
Joined: 25 Sep 2012 19:48

Re: Soft_UART with PIC18F4550

#10 Post by ennng » 16 Jun 2019 11:50

Hi again;

Thank you for your help,

What I have done is that I changed the baud rate to 9600 and divided the output by 1000, and now got the volt, now I will try with MUC 4553 12bits and see!

please see attached photo.

[img]
Untitled1.png
Untitled1.png (130.02 KiB) Viewed 3326 times
[/img]

Kind Regards

User avatar
stefan.filipovic
mikroElektronika team
Posts: 1135
Joined: 18 Dec 2018 10:30

Re: Soft_UART with PIC18F4550

#11 Post by stefan.filipovic » 17 Jun 2019 11:39

Hi,

The previous results were in millivolts due to such calculation: adc*5000.0/1024.0, in this case, the multiplier is in mV (5000mV=5V).
The baud rate was not a problem, the baud rate in the range 9600-19200 is recommended for software UART for this specific MCU.

Kind regards,
Stefan Filipović

ennng
Posts: 61
Joined: 25 Sep 2012 19:48

Re: Soft_UART with PIC18F4550

#12 Post by ennng » 17 Jun 2019 13:53

Thank you for clarification, yes it also does work with PIC18F4553.

User avatar
stefan.filipovic
mikroElektronika team
Posts: 1135
Joined: 18 Dec 2018 10:30

Re: Soft_UART with PIC18F4550

#13 Post by stefan.filipovic » 17 Jun 2019 14:14

Hi,

You're welcome.

I'm glad it works.

Kind regards,
Stefan Filipović

Post Reply

Return to “PIC PRO Compilers”