Tamim Neak
Junior Member level 1
- Joined
- Feb 15, 2015
- Messages
- 18
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 137
Hi all i have the following code
As long as i use any two of the three ADC( one using inttostr the second using floattostr) everything is fine but using the third ADC makes the values on LCD blink and the program after that with all the if, else if doesn't get executed. I have tried using all other conversions such as longtostr, shorttostr, etc which give me even more problems. Any suggestions?
Code:
// Author(s) : Tamim Neak
// Target(s) : PIC16F887
sbit LCD_RS at RD0_bit;
sbit LCD_EN at RD1_bit;
sbit LCD_D4 at RD2_bit;
sbit LCD_D5 at RD3_bit;
sbit LCD_D6 at RD4_bit;
sbit LCD_D7 at RD5_bit;
sbit LCD_RS_Direction at TRISD0_bit;
sbit LCD_EN_Direction at TRISD1_bit;
sbit LCD_D4_Direction at TRISD2_bit;
sbit LCD_D5_Direction at TRISD3_bit;
sbit LCD_D6_Direction at TRISD4_bit;
sbit LCD_D7_Direction at TRISD5_bit;
// End LCD module connections
unsigned int battery_voltage;
unsigned int output_voltage;
unsigned int current;
unsigned int temp;
float temp1;
float battery_voltage1;
float current1;
char temper[7];
char battery_voltage2[3];
char current2[7];
float x = 15.15;
char *str;
void temp_read(void){
temp = ADC_read(0); // Get 10-bit results of AD conversion
temp1 = (temp* 500./1023);
IntToStr(temp1,temper);
lcd_out(1,6, Ltrim(temper));
Lcd_Chr_Cp(0xdf);
Lcd_Chr_Cp('C');
Lcd_Chr_Cp(' ');
}
void battery_read(){
battery_voltage = ADC_read(1);
battery_voltage1 = (battery_voltage* 15.15/1023);
FloatToStr(battery_voltage1,battery_voltage2);
battery_voltage2[5] =0;
lcd_out(1,15, battery_voltage2);
Lcd_Chr_Cp('V');
}
void Current_read(){
current = ADC_read(2);
current1 = (current* 150./1023);
inttostr(current1,current2);
current2[7] =0;
lcd_out(2,9, current2);
Lcd_Chr_Cp('A');
}
void main() {
CM1CON0 = 0x00;
CM2CON0 = 0x00;
ANSEL = 0x0F;
ANSELH = 0x00;
ADCON1 = 0x80;
TRISA=0b00001111;
TRISC = 1; //Configure PORTC as input
TRISB = 0; //Configure PORTB as output
PORTB = 0;
PORTC = 0b00000001;
adc_init();
Lcd_Init();
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Cmd(_LCD_CLEAR);
lcd_out(1,1,"TEMP=");
lcd_out(1,11,"Bat=");
lcd_out(2,1,"CURRENT=");
do {
temp_read();
battery_read();
Current_read();
if ((temp > 56)&&(battery_voltage < 676)) {
if (PORTC.F0 == 1) PORTB = 0b00000011; // cooling fan on and battery LED on
else PORTB = 0b00000011;
}else if ((temp < 56)&&(battery_voltage < 676)) {
if (PORTC.F0 == 1) PORTB = 0b00000001; // cooling fan off and battery LED on
else PORTB = 0b00000001;
}else if ((temp < 56)&&(battery_voltage > 676)) {
if (PORTC.F0 == 1) PORTB = 0b00000000; // cooling fan off and battery LED off
else PORTB = 0b00000100;
}else if ((temp > 56)&&(battery_voltage > 676)) {
if (PORTC.F0 == 1) PORTB = 0b00000010; // cooling fan on and battery LED on
else PORTB = 0b00000110;
}
} while(1);
}
As long as i use any two of the three ADC( one using inttostr the second using floattostr) everything is fine but using the third ADC makes the values on LCD blink and the program after that with all the if, else if doesn't get executed. I have tried using all other conversions such as longtostr, shorttostr, etc which give me even more problems. Any suggestions?