|
|
View previous topic :: View next topic |
Author |
Message |
pg1995
Joined: 05 Apr 2014 Posts: 31
|
LCD output formatting |
Posted: Tue Jul 22, 2014 2:08 pm |
|
|
Hi
I'm using scaled integers in the code which I have written to be used in a project. I was experimenting on how to output scaled integers to a LCD.
In the code I'm using millivolts and milliamperes. The current in my project won't exceed 3A (or, 3000mA) but voltage could be anywhere between 5V to 25V (i.e. 5000mV to 25000mV).
These millivolts and milliamperes can be output in volts and amperes respectively using %x.yW format where "x" is minimum field width and "y" is number of digits after the decimal point. Is this really the way to go?
Please have a look here which shows the LCD output for the code below. Let's only focus on the "if" blocks. I believe that I need to use "if" blocks so that voltages lower than 10V and over 10V could be displayed properly. Am I right?
Code: |
#include <16f876.h>
#use delay(Clock=20000000)
#fuses HS, NOWDT, NOPROTECT, NOPUT, NOBROWNOUT,NOLVP
#include <Flex_LCD420.c>
void main()
{
int16 v1, v2;
signed int16 a1, a2, a3, a4;
//I need to output values in volts and amperes
v1 = 7200; //mV
v2 = 14000; //mV
a1 = 300; //mA
a2 = 2000; //mA
a3 = 400; //mA
//a4 = 8000; //mV
a4 = 15000; //mV
lcd_init();
printf(lcd_putc, "\f");
delay_ms(500);
while(TRUE) {
printf(lcd_putc, "\fVi=%Lu", v1);
printf(lcd_putc, "V");
delay_ms(200);
printf(lcd_putc, " Vo=%Lu", v2);
printf(lcd_putc, "V");
delay_ms(200);
printf(lcd_putc, " \nIi=%Ld", a1);
printf(lcd_putc, "A");
delay_ms(200);
printf(lcd_putc, " Io=%Ld", a2);
printf(lcd_putc, "A");
printf(lcd_putc, " \nI=%4.3w", a3);
printf(lcd_putc, "A");
if (a4 < 10000) {
printf(lcd_putc, " \nV=%4.3w", a4);
printf(lcd_putc, "V");
}
if (a4 >= 10000 ) {
printf(lcd_putc, " \nV=%5.3w", a4);
printf(lcd_putc, "V");
}
delay_ms(5000);
}
}
|
Is there any way to display 15.000V as 15.00V? I have tried "printf(lcd_putc, " \nV=%4.3w", a4)" but it still displays 15.000V.
Thank you. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9253 Location: Greensville,Ontario
|
|
Posted: Tue Jul 22, 2014 2:59 pm |
|
|
...... V=%4.3w......
.3w says 3 places after the decimal point
so
.2w should just give 2 places.
If you press F11 while your project is open, the CCS help files magically appear. Find the functions, then printf(). The details are in there.
hth
jay |
|
|
pg1995
Joined: 05 Apr 2014 Posts: 31
|
|
Posted: Tue Jul 22, 2014 3:30 pm |
|
|
Thanks.
I have already checked it. It gives 150.00 for V=%4.2w. So, how do I get around this? Please let me know. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19561
|
|
Posted: Wed Jul 23, 2014 1:57 am |
|
|
There is no standard way to 'throw way' an extra digit at the end of %w.
1) Divide the number by 10..... Rather obvious.
2) Sprintf to a string, and then throw away the last character.
3) Write your own output routine.
For #2, using strings, possibly something like:
Code: |
char temp_val[10];
sprintf(temp_val,"%6.3Lw",a4);
if (a4>=10000)
temp_val[5]='\0'; //truncate the string
printf(lcd_putc,"%s",temp_val);
|
Now, this is 'sneaky'....
For numbers <10000, they will fit in the 5 digits given in the output width used in the %w. Above 10000, and extra digit will be output, which I then throw away. |
|
|
pg1995
Joined: 05 Apr 2014 Posts: 31
|
|
Posted: Wed Jul 23, 2014 8:37 pm |
|
|
Thank you, Ttelmah.
Or, better still just leave it as it is and use an extra character on the LCD!
Could you please also help me with the query below?
I was experimenting with the code below and was expecting the LED (the one on the left in the video) to remain on or off for at least 30 seconds but on average it remains on or off for at least 37 seconds. It would have been okay if it were 32 or 28 seconds but 37 is way too much. Where am I going wrong with it? Thanks.
Code: |
#include <18f4520.h>
#device adc=10
#device *=16
#fuses H4, NOWDT, NOPUT, NOPROTECT, NOBROWNOUT,NOLVP, INTRC_IO
#use delay(Clock=32000000)
#define LED PIN_D2
void main() {
output_high(pin_d3);
while(1) {
delay_ms(30000);
output_toggle(LED);
}
}
|
Regards
PG |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19561
|
|
Posted: Wed Jul 23, 2014 11:39 pm |
|
|
What clock are you actually running?.
You have both the internal RC oscillator, and an external 8MHz crystal selected.
You want one or the other. |
|
|
pg1995
Joined: 05 Apr 2014 Posts: 31
|
|
Posted: Thu Jul 24, 2014 2:12 am |
|
|
Thank you.
Ttelmah wrote: |
You want one or the other. |
Sorry about that. I wanted to use internal oscillator. I have updated the code but I'm getting an error now. I believe the warning can be ignored but I don't get the reason for that error. Could you please help me with it?
Warning: Line 10(1,1): Condition always TRUE
Error: Line 11(1,1): Function used but not defined: ... delay_ms SCR=836
Code: |
#include <18f4520.h>
#fuses H4, NOWDT, NOPUT, NOPROTECT, NOBROWNOUT,NOLVP, INTRC_IO
#define LED pin_d2
void main() {
output_high(pin_d3);
while(1) {
delay_ms(30000);
output_toggle(LED);
}
}
|
PS: Actually I learned how to use the internal oscillator using different past posts on this forum and one of the posts which really helped was by temtronic, the first one from top in this thread. You can see here that he is using both "INTRC_IO" fuse setting used for internal oscillator and "#use delay()" which, in my opinion, is used for external crystal. Am I missing something here?
Last edited by pg1995 on Thu Jul 24, 2014 12:04 pm; edited 2 times in total |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1938 Location: Norman, OK
|
|
Posted: Thu Jul 24, 2014 11:26 am |
|
|
The warning can be ignored.
delay_ms() requires the #use delay line to be added _________________ Google and Forum Search are some of your best tools!!!! |
|
|
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
|
Posted: Thu Jul 24, 2014 11:52 am |
|
|
if you want to get rid of the warning use
while (TRUE) {
instead of
while (1) { |
|
|
|
|
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
|