Hello, im making a digital controlled switchmode power supply, and i want to display the voltage and current on a 16x2 lcd controlled by a pic16f886.
the problem is, that I have in one line displaying voltage, and below i have the current being displayed, but the current value is shown over the voltage value as well, Il show you in a picture
As you can see, voltage should display only "5" but instead, it adds the current value after. And its not a simulation bug, it does the same on phyisical
Here is the code:
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB2_bit;
sbit LCD_D5 at RB3_bit;
sbit LCD_D6 at RB4_bit;
sbit LCD_D7 at RB5_bit;
sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB5_bit;
float duty;
float v;
char volt [2];
char amp [6];
float a;
void main() {
TRISA = 1;
PORTA = 0;
TRISB = 0;
TRISC = 0;
PORTC = 0;
C1ON_bit = 0;
C2ON_bit = 0;
ANSEL = 0b00000011;
ANSELH = 0;
PWM1_init(2000);
Pwm1_start();
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
duty = 255;
while(1){
a = adc_read(1)*7.35;
v = adc_read(0)*0.02945;
delay_ms(100);
floatToStr(v,volt);
floatToStr(a,amp);
lcd_out(1,1,"V:");
lcd_out(1,4,volt);
lcd_out(2,1,"mA:");
lcd_out(2,4,amp);
pwm1_set_duty(duty);
if(ra2_bit == 1){
delay_ms(150);
duty = duty +10.66;
}
if(ra3_bit == 1){
delay_ms(150);
duty = duty -10.66;
}
if(ra4_bit == 1){
delay_ms(150);
duty = duty-1;
}
if(ra5_bit == 1){
delay_ms(100);
duty = duty+1;
}
if(duty >= 255){
duty = 255;
}
if(duty <=0){
duty = 0;
}
}
}
I cant seem to tell whats wrong, hope you can help me
Thanks for your time
Do you know what a zero-terminated string is?
In consequence, your 2-character array for the voltage can only take one character plus the zero terminator -- but floatToStr doesn't care about it.
Now guess what floatToStr is likely doing with respect to your 2-character array. (and probably to your 6-char array for the current as well...)
To fully understand what happens with your display output, think about how your two character arrays for volt and ampere are arranged in memory.
I am not sure (i am not a PIC coder), but i would guess that there is also a floatToStr overload where you can specify the size of the char array that receives the generated string...
Do you know what a zero-terminated string is?
In consequence, your 2-character array for the voltage can only take one character plus the zero terminator -- but floatToStr doesn't care about it.
Now guess what floatToStr is likely doing with respect to your 2-character array. (and probably to your 6-char array for the current as well...)
To fully understand what happens with your display output, think about how your two character arrays for volt and ampere are arranged in memory.
I am not sure (i am not a PIC coder), but i would guess that there is also a floatToStr overload where you can specify the size of the char array that receives the generated string...
I just increased the number on the char from 6 to 12 and the problem is gone XD
I hope you understand what the actual problem was and are not just winging your code
If you read the MikroC help file you will find the following information on FloatToStr function:
Destination string should be at least 14 characters in length.