Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] LCD to Display Voltage and temperature simultaniuosly

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
Activity points
137
Hi all,

I have been working on the following code and running the simulation on Proteus:

Code:
// ***************************************************************************
//  File Name        : newproject.c
//  Version          : 1.0
//  Description      : Pulse Width Modulation (PWM)
//                     ADC
//  Author(s)        : Tamim Neak
//  Target(s)        : PIC16F887
//  Programmer       : PICKit
//  Last Updated     : 14 Feb 2015
// ***************************************************************************

// LCD module connections
sbit LCD_RS at RC4_bit;
sbit LCD_EN at RC5_bit;
sbit LCD_D4 at RD1_bit;
sbit LCD_D5 at RD2_bit;
sbit LCD_D6 at RD3_bit;
sbit LCD_D7 at RD4_bit;

sbit LCD_RS_Direction at TRISC4_bit;
sbit LCD_EN_Direction at TRISC5_bit;
sbit LCD_D4_Direction at TRISD1_bit;
sbit LCD_D5_Direction at TRISD2_bit;
sbit LCD_D6_Direction at TRISD3_bit;
sbit LCD_D7_Direction at TRISD4_bit;
// End LCD module connections

unsigned int battery_voltage;
unsigned int output_voltage;
unsigned int output_current;
unsigned int temp;
float temp1;
float battery_voltage1;
char temper[7];
char battery_voltage2[7];
float x = 15.15;
void temp_read(void){
     temp = ADC_read(0);            // Get 10-bit results of AD conversion
     temp1 = (temp* 500./1023);
     if (temp > 56) PORTB.F1 = 1;   // cooling fan on
       else PORTB.F1 = 0;           // cooling fan off
     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);                       // Get 10-bit results of AD conversion
     battery_voltage1 = (battery_voltage* 15.15/1023);
     if (battery_voltage < 676) PORTB.F0 = 1;             // battery LED off
       else PORTB.F0 = 0;                                 // battery too low LED on
     IntToStr(battery_voltage1,battery_voltage2);
     lcd_out(1,6, Ltrim(battery_voltage2));
     Lcd_Chr_Cp('V');
     Lcd_Chr_Cp(' ');
                    }

void main() {
 ADCON1 = 0x00; // All channels are config as analog I/p.
 TRISA=0b00001111;
 TRISD.F0 = 1;       //Configure 1st bit of PORTD as input for power on
 TRISB = 0;          //Configure PORTB as output
 PORTB = 0;
 PORTD.F0 = 1;       //Switch off

adc_init();        // Initialize ADC module with default settings
Lcd_Init();                        // Initialize LCD
Lcd_Cmd(_LCD_CURSOR_OFF);          // LCD Cursor Off
Lcd_Cmd(_LCD_CLEAR);               // Clear display
lcd_out(1,1,"TEMP=");
lcd_out(1,11,"Bat=");

do {
   //output_voltage = ADC_read(3);
   //output_current = ADC_read(4);

   temp_read();
   battery_read();

battery_voltage = ADC_read(1);
   if ((PORTD.F0 == 0) && (battery_voltage > 676))  //If the switch is pressed
      {
       Delay_ms(100);    //Switch Debounce
       if ((PORTD.F0 == 0) && (battery_voltage > 676))        //If the switch is still pressed
          {
           PORTB.F2 = 1; //PWM LED ON
           Delay_ms(100);
           temp_read();
           }
       }else {
              PORTB.F2 = 0;    //PWM LED Off
              Delay_ms(100);
              }
    } while(1);
}

I have two problems:
1- I don't get any value for the Battery_voltage in the LCD.
2- When i run the code and make the battery_voltage less than 10V the led at PORTB.F0 keeps blinking instead of staying on till the battery_voltage > 10V.
any help regarding these issues are appreciated
 

I fixed the code here it is for those interested.

Code:
// ***************************************************************************
//  File Name        : newproject.c
//  Version          : 1.0
//  Description      : Pulse Width Modulation (PWM)
//                     ADC
//  Author(s)        : Tamim Neak
//  Target(s)        : PIC16F887
//  Programmer       : PICKit
//  Last Updated     : 15 Feb 2015
// ***************************************************************************

// LCD module connections
sbit LCD_RS at RC4_bit;
sbit LCD_EN at RC5_bit;
sbit LCD_D4 at RD1_bit;
sbit LCD_D5 at RD2_bit;
sbit LCD_D6 at RD3_bit;
sbit LCD_D7 at RD4_bit;

sbit LCD_RS_Direction at TRISC4_bit;
sbit LCD_EN_Direction at TRISC5_bit;
sbit LCD_D4_Direction at TRISD1_bit;
sbit LCD_D5_Direction at TRISD2_bit;
sbit LCD_D6_Direction at TRISD3_bit;
sbit LCD_D7_Direction at TRISD4_bit;
// End LCD module connections

unsigned int battery_voltage;
unsigned int output_voltage;
unsigned int output_current;
unsigned int temp;
float temp1;
float battery_voltage1;
char temper[7];
char battery_voltage2[3];
float x = 15.15;
void temp_read(void){
     temp = ADC_read(0);            // Get 10-bit results of AD conversion
     temp1 = (temp* 500./1023);
     /*if (temp > 56) PORTB.F1 = 1;   // cooling fan on
       else PORTB.F1 = 0;           // cooling fan off*/
     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);                       // Get 10-bit results of AD conversion
     battery_voltage1 = (battery_voltage* 15.15/1023);
     //if (battery_voltage < 676) PORTB.F0 = 1;             // battery LED off
       //else PORTB.F0 = 0;                               // battery too low LED on
     FloatToStr(battery_voltage1,battery_voltage2);
     battery_voltage2[5] =0;
     lcd_out(1,15, battery_voltage2);
     Lcd_Chr_Cp('V');
                    }

void main() {
 ADCON1 = 0x00; // All channels are config as analog I/p.
 TRISA=0b00001111;
 TRISD.F0 = 1;       //Configure 1st bit of PORTD as input for power on
 TRISB = 0;          //Configure PORTB as output
 PORTB = 0;
 PORTD.F0 = 1;       //Switch off

adc_init();        // Initialize ADC module with default settings
Lcd_Init();                        // Initialize LCD
Lcd_Cmd(_LCD_CURSOR_OFF);          // LCD Cursor Off
Lcd_Cmd(_LCD_CLEAR);               // Clear display
lcd_out(1,1,"TEMP=");
lcd_out(1,11,"Bat=");

do {
   //output_voltage = ADC_read(3);
   //output_current = ADC_read(4);

   temp_read();
   battery_read();
   
    if ((temp > 56)&&(battery_voltage < 676)) {
       if (PORTD.F0 == 1)             PORTB = 0b00000011;   // cooling fan on and battery LED on
       else PORTB = 0b00000011;
    }else if ((temp < 56)&&(battery_voltage < 676)) {
       if (PORTD.F0 == 1)             PORTB = 0b00000001;   // cooling fan off and battery LED on
       else PORTB = 0b00000001;
    }else if ((temp < 56)&&(battery_voltage > 676)) {
       if (PORTD.F0 == 1)             PORTB = 0b00000000;   // cooling fan off and battery LED off
       else PORTB = 0b00000100;
    }else if ((temp > 56)&&(battery_voltage > 676)) {
       if (PORTD.F0 == 1)             PORTB = 0b00000010;   // cooling fan on and battery LED on
       else PORTB = 0b00000110;
    }

    } while(1);
}
 

Configure ADCON0 properly.


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
// LCD module connections
sbit LCD_RS at RC4_bit;
sbit LCD_EN at RC5_bit;
sbit LCD_D4 at RD1_bit;
sbit LCD_D5 at RD2_bit;
sbit LCD_D6 at RD3_bit;
sbit LCD_D7 at RD4_bit;
 
sbit LCD_RS_Direction at TRISC4_bit;
sbit LCD_EN_Direction at TRISC5_bit;
sbit LCD_D4_Direction at TRISD1_bit;
sbit LCD_D5_Direction at TRISD2_bit;
sbit LCD_D6_Direction at TRISD3_bit;
sbit LCD_D7_Direction at TRISD4_bit;
// End LCD module connections
 
unsigned int battery_voltage;
unsigned int output_voltage;
unsigned int output_current;
unsigned int temp;
float temp1;
float battery_voltage1;
char temper[23];
char battery_voltage2[23];
float x = 15.15;
 
void temp_read(void) {
 
     temp = ADC_read(0);            // Get 10-bit results of AD conversion
     temp1 = (temp * 500 / 1023);
     if(temp > 56) PORTB.F1 = 1;   // cooling fan on
     else PORTB.F1 = 0;             // cooling fan off
 
     FloatToStr(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);                       // Get 10-bit results of AD conversion
     battery_voltage1 = (battery_voltage * 15.15 / 1023.0);
     if(battery_voltage < 676) PORTB.F0 = 1;             // battery LED off
     else PORTB.F0 = 0;
                                 // battery too low LED on
     FloatToStr(battery_voltage1,battery_voltage2);
     LCD_Out(2,6,Ltrim(battery_voltage2));
     Lcd_Chr_Cp('V');
     Lcd_Chr_Cp(' ');
                    }
 
void main() {
 
 CM1CON0 = 0x00;
 CM2CON0 = 0x00;
 ANSEL = 0x0F;
 ANSELH = 0x00;
 ADCON1 = 0x80;
 
 TRISA = 0x0F;
 TRISB = 0x00;
 TRISC = 0x00;
 TRISD = 0x01;
 
 PORTD.F0 = 1;       //Switch off
 
LCD_Init();                        // Initialize LCD
LCD_Cmd(_LCD_CURSOR_OFF);          // LCD Cursor Off
LCD_Cmd(_LCD_CLEAR);               // Clear display
LCD_out(1,1,"TEMP=");
LCD_out(1,11,"Bat=");
 
do {
 
   temp_read();
   battery_read();
 
   if ((PORTD.F0 == 0) && (battery_voltage > 676)) {  //If the switch is pressed
       Delay_ms(100);    //Switch Debounce
       while((PORTD.F0 == 0) && (battery_voltage > 676));        //while the switch is pressed wait here
          {
           PORTB.F2 = 1; //PWM LED ON           
          }
    }
    else {
           PORTB.F2 = 0;    //PWM LED Off
    }
 
    } while(1);
}

 
Last edited:
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top