Please help me to correct the code(I am using PIC16F688)

Status
Not open for further replies.

pradeep_k_b

Full Member level 3
Joined
Dec 21, 2011
Messages
160
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,306
Location
Nilambur, India
Visit site
Activity points
2,509
Code:
// LCD module connections
sbit LCD_RS at RC4_bit;
sbit LCD_EN at RC5_bit;
sbit LCD_D4 at RC0_bit;
sbit LCD_D5 at RC1_bit;
sbit LCD_D6 at RC2_bit;
sbit LCD_D7 at RC3_bit;
sbit LCD_RS_Direction at TRISC4_bit;
sbit LCD_EN_Direction at TRISC5_bit;
sbit LCD_D4_Direction at TRISC0_bit;
sbit LCD_D5_Direction at TRISC1_bit;
sbit LCD_D6_Direction at TRISC2_bit;
sbit LCD_D7_Direction at TRISC3_bit;
// End LCD module connections
 char txt2[] = {4,4,4,4,31,14,4,0};
 const char character[] = {0,0,4,2,31,2,4,0};

void CustomChar(char pos_row, char pos_char) {
  char i;
    Lcd_Cmd(64);
    for (i = 0; i<=7; i++) Lcd_Chr_CP(character[i]);
    Lcd_Cmd(_LCD_RETURN_HOME);
    Lcd_Chr(pos_row, pos_char, 0);
}

 char txt1[] = "  CHR OFF ";
 char txt3[] = "  CHR ON  ";
 char txt4[] = "BAD LIGHT";

//Function Prototypes
void floattostring(double FP_NUM);

double FP_NUM, volt;
char string[6];

//Sub Function
void floattostring(double FP_NUM) {
        double fpnumber;
        long int befdec, aftdec;

        fpnumber = FP_NUM;

        befdec = fpnumber;                      // Fractional part is truncated
                                                // 12.163456 becomes 12
        aftdec = fpnumber * 100;            // 12.163456 becomes 1216
        aftdec = aftdec - (befdec * 100);   // 1216 - 1200 = 16


        if (fpnumber < 1) {
                string[0] = '0';
                string[1] = '.';
                string[2] = (aftdec/10) + 48;
                string[3] = (befdec/1)%10 + 48;
                string[4] = ' ';
                string[5] = '\0';

        }

        else if ((fpnumber >= 1) && (fpnumber < 10)) {
                string[0] = (befdec/1)%10 + 48;
                string[1] = '.';
                string[2] = (aftdec/10) + 48;
                string[3] = (befdec/1)%10 + 48;
                string[4] = ' ';
                string[5] = '\0';


        }

        else if ((fpnumber >= 10) && (fpnumber < 100)) {
                string[0] = (befdec/10) + 48;
                string[1] = (befdec/1)%10 + 48;
                string[2] = '.';
                string[3] = (aftdec/10) + 48;
                string[4] = (befdec/1)%10 + 48;
                string[5] = '\0';


        }
}
void main(){ANSEL = 0b00000100; // RA2/AN2 is analog input
  ADCON0 = 0b00001000; // Analog channel select @ AN2
  ADCON1 = 0x00;
  //CMCON0 = 0x07 ; // Disbale comparators
  TRISC = 0b00000000; // PORTC All Outputs
  //TRISA = 0b00001100; // PORTA All Outputs, Except RA3 and RA2
  TRISA = 0b00000011; // PORTA All Outputs, Except RA3 and RA2
  //TRISA = 0b00001111; // PORTA All Outputs, Except RA3 and RA2

 LCD_Init();
       Lcd_Cmd(_LCD_CURSOR_OFF);
       Lcd_Cmd(_LCD_CLEAR);
       Lcd_Out(2,1,"Hello...!");
       Delay_ms(1000);                          // Waits 2 sec. for splash screen to be shown :)
       Lcd_Cmd(_LCD_CURSOR_OFF);
       Lcd_Cmd(_LCD_CLEAR);
       Lcd_Out(1,1,"Welcom To");
       Lcd_Out(2,8,"SOLAR CHARGER");
       Delay_ms(1000);                          // Waits 2 sec. for splash screen to be shown :)
       LCD_Init();
       Lcd_Cmd(_LCD_CURSOR_OFF);
       Lcd_Cmd(_LCD_CLEAR);
       Lcd_Out(1,1,"SOLAR");
       Lcd_Out(2,1,"BATTERY");


       while(1) {
            volt = ADC_Read(0);
            volt = volt * 0.0305498981670061;
            volt = volt * 0.9612303748798462;
            floattostring(volt);
            Lcd_Out(2,11,string);
            Lcd_Out(2,16,"V");
            
        if(PORTA.RA1 = 0) {
           Lcd_Out(1,8,txt4);
           PORTA.RA2 = 0;
           //PORTA.RA4 = 1;

           }


            else if(volt < 13.8) {  // Change value here
                   PORTA.RA2 = 1;

            Lcd_Out(1,8,txt3);


            }


            if(volt > 14.8) {      // Change value here
                   PORTA.RA2 = 0;



            Lcd_Out(1,8,txt1);


}
if(volt>10.8)
{

    PORTA.RA4 = 1;  }


} 
                        }
Hi,I am trying to design a solar charge controller using PIC16F688.And the code is working fine and there is no error when I execute it,but when I try to use this code in Proteus simulator the circuit is not working.And when I remove the last part of the program
if(volt>10.8)
{

PORTA.RA4 = 1; }
it working fine with simulator,but that part is very important and I can't remove that from the program .Please help me to solve this
 

is your code complies successfully first?

Code:
if(volt>10.8)
{

    PORTA.RA4 = 1;  }

}

Brackets are extra in your code.
 
Last edited by a moderator:

Yeah, the code compileed successfully and obtained the HEX file.which brackets you meant?

there are 3 brackets at the end 1-closing 'if' function,2-closing 'while',3-closing main function
 

if(PORTA.RA1 = 0) {

Try this...

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// LCD module connections
sbit LCD_RS at RC4_bit;
sbit LCD_EN at RC5_bit;
sbit LCD_D4 at RC0_bit;
sbit LCD_D5 at RC1_bit;
sbit LCD_D6 at RC2_bit;
sbit LCD_D7 at RC3_bit;
sbit LCD_RS_Direction at TRISC4_bit;
sbit LCD_EN_Direction at TRISC5_bit;
sbit LCD_D4_Direction at TRISC0_bit;
sbit LCD_D5_Direction at TRISC1_bit;
sbit LCD_D6_Direction at TRISC2_bit;
sbit LCD_D7_Direction at TRISC3_bit;
// End LCD module connections
 
#define lower_limit 13.8
#define upper_limit 14.8
 
char            txt2[] = {4, 4, 4, 4, 31, 14, 4, 0};
const char character[] = {0, 0, 4, 2, 31, 2, 4, 0};
 
void CustomChar(char pos_row, char pos_char) {
  char i;
    Lcd_Cmd(64);
    for (i = 0; i<=7; i++) Lcd_Chr_CP(character[i]);
    Lcd_Cmd(_LCD_RETURN_HOME);
    Lcd_Chr(pos_row, pos_char, 0);
}
 
const char lcdMsg1[] = "Charger OFF     ";
const char lcdMsg2[] = "Charger ON      ";
const char lcdMsg3[] = "Bad Light       ";
const char lcdMsg4[] = "Hello!...       ";
const char lcdMsg5[] = "Welcome to      ";
const char lcdMsg6[] = "Solar Charger   ";
const char lcdMsg7[] = "Solar           ";
const char lcdMsg8[] = "Battery         ";
 
char  lcdData[23];
 
double volt = 0.0, voltBackup = 0.0;
 
void Delay2Sec() {
    Delay_ms(2000);
}
 
char *CopyConst2Ram(char *dest, const char *src) {
    char *d ;
 
    d = dest;
 
    for(;*dest++ = *src++;);
 
    return d;
}
 
void main() {
 
    ANSEL = 0x04; // RA2/AN2 is analog input
    ACON0 = 0x88;
    ADCON1 = 0x00;
 
    TRISA = 0x06; // PORTA All Outputs, Except RA3 and RA2 
    TRISC = 0x00; // PORTC All Outputs
    PORTA = 0x00;
    PORTC = 0x00;   
 
    LCD_Init();
        Lcd_Cmd(_LCD_CURSOR_OFF);
        Lcd_Cmd(_LCD_CLEAR);
        Lcd_Out(2,1,CopyConst2Ram(lcdData, lcdMsg4));
        Delay2Sec()();
                          
        Lcd_Cmd(_LCD_CLEAR);
        Lcd_Out(1,1,CopyConst2Ram(lcdData, lcdMsg5));
        Lcd_Out(2,1,CopyConst2Ram(lcdData, lcdMsg6));
        Delay2Sec()();
                            
    Lcd_Cmd(_LCD_CLEAR);
        Lcd_Out(1,1,CopyConst2Ram(lcdData, lcdMsg7));
        Lcd_Out(2,1,CopyConst2Ram(lcdData, lcdMsg8));
 
 
        while(1) {
 
                volt = ADC_Read(2);
    
        if(voltBackup != volt) {
 
            volt /= 34.0536;
            FloatToStr(volt, lcdData);
 
            lcdData[7] = '\0';
            strcat(lcdData, " V");
            Lcd_Out(2,1,CopyConst2Ram(lcdData, lcdData));
 
            if(volt < lower_limit) {  
                PORTA.F2 = 1;
                Lcd_Out(1,1,CopyConst2Ram(lcdData, lcdMsg2));
                }
            else if(volt > 10.8)
            {
                    PORTA.RA4 = 1;
 
                if(volt > upper_limit) {            
                    PORTA.F2 = 0;
                    Lcd_Out(1,1,CopyConst2Ram(lcdData, lcdMsg1));
                }       
            }
 
            
            voltBackup = volt;
            
        }
 
        if(PORTA.F1 == 0) { 
            Lcd_Out(1,1,CopyConst2Ram(lcdData, lcdMsg3));
                PORTA.F2 = 0;
            }
    }
}

 
Last edited:

Why did you comment out the line that disables the comparator? Comparator disable is needed.

RA4 is permanently on because nowhere do you initialise PORT outputs to any special state. Put a line at the start of you program:

Code:
PORTA = 0;         // initialise all outputs to low


The reason it looks as if you have the number of braces wrong is because your code is formatted so badly. You should fix the formatting and make it consistent, otherwise your code becomes unreadable.
 



@Milan.rajik..when I try to compile it ,it shows some errors

- - - Updated - - -

@hexreader,I am not an expert in coding,so I am collecting the codes from internet and trying to edit it for my circuits.But I have tried it with RA5,but then also it shows the same error
 

Zip and post the mikroC project files.

Try this...

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// LCD module connections
sbit LCD_RS at RC4_bit;
sbit LCD_EN at RC5_bit;
sbit LCD_D4 at RC0_bit;
sbit LCD_D5 at RC1_bit;
sbit LCD_D6 at RC2_bit;
sbit LCD_D7 at RC3_bit;
sbit LCD_RS_Direction at TRISC4_bit;
sbit LCD_EN_Direction at TRISC5_bit;
sbit LCD_D4_Direction at TRISC0_bit;
sbit LCD_D5_Direction at TRISC1_bit;
sbit LCD_D6_Direction at TRISC2_bit;
sbit LCD_D7_Direction at TRISC3_bit;
// End LCD module connections
 
#define lower_limit 13.8
#define upper_limit 14.8
 
char            txt2[] = {4, 4, 4, 4, 31, 14, 4, 0};
const char character[] = {0, 0, 4, 2, 31, 2, 4, 0};
 
void CustomChar(char pos_row, char pos_char) {
  char i;
    Lcd_Cmd(64);
    for (i = 0; i<=7; i++) Lcd_Chr_CP(character[i]);
    Lcd_Cmd(_LCD_RETURN_HOME);
    Lcd_Chr(pos_row, pos_char, 0);
}
 
const char lcdMsg1[] = "Charger OFF     ";
const char lcdMsg2[] = "Charger ON      ";
const char lcdMsg3[] = "Bad Light       ";
const char lcdMsg4[] = "Hello!...       ";
const char lcdMsg5[] = "Welcome to      ";
const char lcdMsg6[] = "Solar Charger   ";
const char lcdMsg7[] = "Solar           ";
const char lcdMsg8[] = "Battery         ";
 
char  lcdData[23];
 
double volt = 0.0, voltBackup = 0.0;
 
void Delay2Sec() {
    Delay_ms(2000);
}
 
char *CopyConst2Ram(char *dest, const char *src) {
    char *d ;
 
    d = dest;
 
    for(;*dest++ = *src++;);
 
    return d;
}
 
void main() {
 
    ANSEL = 0x04; // RA2/AN2 is analog input
    ADCON0 = 0x88;
    ADCON1 = 0x00;
 
    TRISA = 0x06; // PORTA All Outputs, Except RA3 and RA2 
    TRISC = 0x00; // PORTC All Outputs
    PORTA = 0x00;
    PORTC = 0x00;   
 
    LCD_Init();
        Lcd_Cmd(_LCD_CURSOR_OFF);
        Lcd_Cmd(_LCD_CLEAR);
        Lcd_Out(2,1,CopyConst2Ram(lcdData, lcdMsg4));
        Delay2Sec()();
                          
        Lcd_Cmd(_LCD_CLEAR);
        Lcd_Out(1,1,CopyConst2Ram(lcdData, lcdMsg5));
        Lcd_Out(2,1,CopyConst2Ram(lcdData, lcdMsg6));
        Delay2Sec()();
                            
    Lcd_Cmd(_LCD_CLEAR);
        Lcd_Out(1,1,CopyConst2Ram(lcdData, lcdMsg7));
        Lcd_Out(2,1,CopyConst2Ram(lcdData, lcdMsg8));
 
 
        while(1) {
 
                volt = ADC_Read(2);
    
        if(voltBackup != volt) {
 
            volt /= 34.0536;
            FloatToStr(volt, lcdData);
 
            lcdData[7] = '\0';
            strcat(lcdData, " V");
            Lcd_Out(2,1,lcdData);
 
            if(volt < lower_limit) {  
                PORTA.F2 = 1;
                Lcd_Out(1,1,CopyConst2Ram(lcdData, lcdMsg2));
                }
            else if(volt > 10.8)
            {
                    PORTA.RA4 = 1;
 
                if(volt > upper_limit) {            
                    PORTA.F2 = 0;
                    Lcd_Out(1,1,CopyConst2Ram(lcdData, lcdMsg1));
                }       
            }
 
            
            voltBackup = volt;
            
        }
 
        if(PORTA.F1 == 0) { 
            Lcd_Out(1,1,CopyConst2Ram(lcdData, lcdMsg3));
                PORTA.F2 = 0;
            }
    }
}

 
i saw your code and problem is declare in Delay2Sec()();

put only Delay2Sec(); . in your code and run bro!!!!!
 

are u test in real hardware or Proteus.

if real hardware then post your schematic and if u using Proteus then post your Proteus project file.

as milan described in post#7 code u put in your code or not?


ok sorry i think u tried milan code already .....
 

In future zip and post your project files when asking for help.
 

Attachments

  • mikroC PRO PIC 6 Code + Proteus Sim.rar
    58.4 KB · Views: 113
Explain what you are measuring (and also range of measurement) and when the different LCD messages should be displayed.
 

I am designing a charge solar charge controller,with upper cut off voltage 14.2V and lower cut off voltage 11.2V.And I want to connect a push button switch to turn on/off the output(load)

and the LCD should display the following things
>14.2V-"solar charger off &Load ON"
11.2V-14.2-"solar charger on&Load ON" -if the voltage is between 11.6-14.2&"solar charger On and load OFF"-if the voltage level is between 11.2-11.6
<11.2V-"Battery low&Load off"

and if there is no output from solar panel,then the LCD should display 'bad light'.pls find the attched circuit digram

- - - Updated - - -

On another note when I try to transfer the hex file of this program to PIC,the programmer window shows 'fuse error',if you don't mind could you please let me know what does it mean?
 

Attachments

  • 688-1.rar
    18.4 KB · Views: 74

Export Proteus circuit to PDF and send it. I don't have Proteus 8.

What is this?

11.2V-14.2-"solar charger on&Load ON" -if the voltage is between 11.6-14.2&"solar charger On and load OFF"

if voltage is 11.7 to 14.2 then you want to display both "Solar Charger ON & Load ON" and "Solar Charger ON & Load OFF" messages?

You mean load will be ON and also OFF at the same time?
 
Last edited:

I am sorry,if the voltage is 11.6 to 14.2 then I want to show 'solar charger ON(in first first row of LCD) and LOAD ON(in second row) and if the voltage is 11.2-11.6 the solar charger ON(1st row),Load OFF(2nd row).and if the voltage is less than 10.8 solar charger on(1st row) and battery low(2nd row)
 

Attachments

  • 688-1.pdf
    25.7 KB · Views: 91

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…