Soft_UART with PIC18F4550
Soft_UART with PIC18F4550
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
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
- stefan.filipovic
- mikroElektronika team
- Posts: 1135
- Joined: 18 Dec 2018 10:30
Re: Soft_UART with PIC18F4550
Hi,
Please find the simple example of Software UART library for PIC18F4550 in the attachment.
Kind regards,
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 84 times
Stefan Filipović
Re: Soft_UART with PIC18F4550
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
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
- stefan.filipovic
- mikroElektronika team
- Posts: 1135
- Joined: 18 Dec 2018 10:30
Re: Soft_UART with PIC18F4550
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:
Kind regards,
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++;
}
Stefan Filipović
Re: Soft_UART with PIC18F4550
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,
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++;
}
}
}
- stefan.filipovic
- mikroElektronika team
- Posts: 1135
- Joined: 18 Dec 2018 10:30
Re: Soft_UART with PIC18F4550
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.
Kind regards,
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;
}
}
Stefan Filipović
Re: Soft_UART with PIC18F4550
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]
[/img]
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]
[/img]
- stefan.filipovic
- mikroElektronika team
- Posts: 1135
- Joined: 18 Dec 2018 10:30
Re: Soft_UART with PIC18F4550
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,
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ć
Re: Soft_UART with PIC18F4550
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]
[/img]
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]
[/img]
Re: Soft_UART with PIC18F4550
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] [/img]
Kind Regards
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] [/img]
Kind Regards
- stefan.filipovic
- mikroElektronika team
- Posts: 1135
- Joined: 18 Dec 2018 10:30
Re: Soft_UART with PIC18F4550
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,
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ć
Re: Soft_UART with PIC18F4550
Thank you for clarification, yes it also does work with PIC18F4553.
- stefan.filipovic
- mikroElektronika team
- Posts: 1135
- Joined: 18 Dec 2018 10:30