[PIC] Pic16f887 3 ADC problem

Status
Not open for further replies.

Tamim Neak

Junior Member level 1
Joined
Feb 15, 2015
Messages
18
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
137
Hi all i have the following code

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?
 

Does the whole LCD blink or a part of LCD blink ?

Three things can be possible.

1. One value on LCD is being overwritten by another LCD data.
2. If you are getting "IRP_bit must be set manually to access variable xyz" message in messages window then it is time to use CopyCons2Ram() function for LCD data or switch to PIC18F.
3. You might be using Cracked version of Compiler and it might be generating wrong .hex file.

For testing purpose please Zip and post the complete mikroC PRO PIC project files.
 

View attachment test2.zipI am simulating the code using proteus. Eveything printed blinks. I had the ADC for Battery and temp before and it worked fine adding the current one is missing everything up.
 

Fixed code attached. Please zip and post Proteus file. I have not tested the code working. floattoascii.h is made by paulfjujo.

You were passing float value to INtToStr() function.
The arrays used for IntToStr() should have minimum 17 bytes and one for FloatToStr() should have 23 bytes and one for float2ascii should have 30 bytes.
 

Attachments

  • test2 fixed.rar
    54.1 KB · Views: 57

You might be right about the compiler generating wrong .hex because when i load your hex file everything works perfectly but when i compile your code then i get errors. Do you know of any free compilers with no demo limit i can use or any other options other than paying 300$. i have attached the proteus file.
 

Attachments

  • test.zip
    858.6 KB · Views: 73

dear tamim, it may be the problem of mikroe. you might crossed minimum memory handling. try with out using the inbuilt function like 'floattostr' with the manual one.

- - - Updated - - -


use mplab compiler
 

No. I checked the .hex file you gave and the .hex file I got by compiling your code. Both were same. So, your compiler is generating right .hex file.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…