[SOLVED] LCD is not displaying anything

Status
Not open for further replies.

Mrunal Ahirrao

Full Member level 2
Joined
Nov 26, 2012
Messages
133
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,298
Location
India
Visit site
Activity points
2,213
Hi,
I am trying to display Current and voltage values on LCD but its not working. I could see only "black squares" in first row.But if I remove any of the code part(current_read or voltage read) then LCD works!Here is my code:
Code:
// LCD module connections
sbit LCD_RS at RB7_bit;
sbit LCD_EN at RB6_bit;
sbit LCD_D4 at RB5_bit;
sbit LCD_D5 at RB4_bit;
sbit LCD_D6 at RB3_bit;
sbit LCD_D7 at RB2_bit;
sbit LCD_RS_Direction at TRISB7_bit;
sbit LCD_EN_Direction at TRISB6_bit;
sbit LCD_D4_Direction at TRISB5_bit;
sbit LCD_D5_Direction at TRISB4_bit;
sbit LCD_D6_Direction at TRISB3_bit;
sbit LCD_D7_Direction at TRISB2_bit;
// End LCD module connections


float I;    //to store Current value
char Current[14];
float v;   //to store voltage value
char voltage[5];



void Voltage_Read()   //Vrms calculation
{
           float max;
           int i;
           int t[40];
           ADCON0=0b01000001;
           delay_us(20);
           ADCON0.ADON=1;
           for(i=0; i<=39; i++)
           {
           v= ADC_Read(0);
           v=v*(10.0/1023.0);
           v=(v-5.0);
           t[i]=v*110.1909091;
           }
           ADCON0.ADON=0;
           max=t[0];
           for(i=0; i<=39; i++)
           {
           if(max<t[i])
            max=t[i];
           }
           max=max*.757106781;
           ByteToStr(max,voltage);
           Lcd_out(1,9,voltage);
           delay_ms(1000);
 }

void Current_Read()
{
 ADCON0=0b01000101;
 delay_us(20);
 ADCON0.B2=1;
 I=ADC_read(1); //reading ADC value
 I=I*0.0048875;      //finding voltage from ADC reading
 I=I/4.4;           //dividing by amplifying factor
 I=I/120;          //dividing by burden resistor value to get secondary current
 I=I*2500;         //multiplying secondary Current by transformer turns ratio
 FloatToStr(I,Current);
 delay_us(100);
 Lcd_out(2,9,Current);
 delay_ms(1000);
 }

void main()
{
 TRISA = 0xFF; // set all pins of PORT A as input
 TRISB =0x00;
 ADCON1=0b00000000;
 ADCON0=0b01000000;
 ANSEL =0b0000011;
 ANSELH=0x00;
 CM1CON0.B7=0;
 CM1CON0.B5=0;
 CM2CON0.B7=0;
 CM2CON0.B5=0;
 Delay_ms(100);
 ADCON0.B0=1;
 delay_us(20);
 ADCON0.B2=1;

 //OPTION_REG = 0b00101000; // set TOCKI as clock counter

 Lcd_Init();
 delay_us(100);
 Lcd_Cmd(_LCD_CLEAR);
 delay_us(100);
 Lcd_Cmd(_LCD_CURSOR_OFF);
 delay_us(100);
  while(1)
  {
   Lcd_out(1,1,"Voltage:");
  delay_us(100);
   Lcd_Chr(1,12,'V');
   Lcd_out(2,1,"Current:");
   delay_us(100);
   Lcd_Chr(2,16,'A');
   Voltage_Read();
   delay_ms(100);
    Current_Read();



  }// while
}// void main

I am using MikroC for PIC and using PIC16F886@ 4Mhz HS oscillator. Please guide me.
 

Are you trying to measure AC ? If yes, show your circuit. Try attached code. Add necessary calculations in Voltage_Read() function.

If you are using PIC18F device then change RBx_bit to LATBx_bit in LCD defines.


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
// LCD module connections
sbit LCD_RS at RB7_bit;
sbit LCD_EN at RB6_bit;
sbit LCD_D4 at RB5_bit;
sbit LCD_D5 at RB4_bit;
sbit LCD_D6 at RB3_bit;
sbit LCD_D7 at RB2_bit;
 
sbit LCD_RS_Direction at TRISB7_bit;
sbit LCD_EN_Direction at TRISB6_bit;
sbit LCD_D4_Direction at TRISB5_bit;
sbit LCD_D5_Direction at TRISB4_bit;
sbit LCD_D6_Direction at TRISB3_bit;
sbit LCD_D7_Direction at TRISB2_bit;
// End LCD module connections
 
double i;    
char Current[17];
double v;   
char voltage[17];
 
void Voltage_Read()  
{           
    int i;
 
        for(i=0; i<=20; i++)
        {
           v += ADC_Read(0);
           Delay_ms(20);           
        }
        
    v /= 20;
    
    FloatToStr(v, voltage);
    Lcd_out(1,9,voltage);
    
 }
 
void Current_Read(){    
        
    i = ADC_read(1);
    i = i * 0.0048875;      
    i = i / 4.4;           
    i = i / 120.0;          
    i = i * 2500.0;         
    FloatToStr(i,Current);
    Lcd_out(2,9,Current);
}
 
void main()
{
 TRISA = 0xFF; // set all pins of PORT A as input
 TRISB =0x00;
 ADCON1=0b00000000;
 ADCON0=0b01000000;
 ANSEL =0b0000011;
 ANSELH=0x00;
 CM1CON0 = 0x00;
 CM2CON0 = 0x00;
 
 Delay_ms(100);
 
 Lcd_Init();
 Lcd_Cmd(_LCD_CURSOR_OFF);
 Lcd_Cmd(_LCD_CLEAR);
 Lcd_out(1,1,"Voltage:");
 Lcd_out(2,1,"Current:");
 
  while(1)
  {
     
   Voltage_Read();
   
   delay_ms(20);
   Current_Read();
   Lcd_Chr(1,12,'V');
   Lcd_Chr(2,16,'A');
  }
}

 

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…