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.

[PIC] Problem sending data to LCD PIC16f877a via mikroC

Status
Not open for further replies.

ChrisTV

Newbie level 2
Joined
Jan 30, 2015
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
23
Hi all

I am struggling to figure out why I am not able to send data to my 16x2 LCD display from my pic16f877a. I am a newbie and still learning, but I tried to compile my program many times, and had noticed that as I approached the flash memory of the pic (hex file size) the LCD behaved differently. I am using Proteus 8 for simulation as well. I have view many forums and examples.
Any help would be much appreciated.


My code is below..


Code dot - [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
// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
 
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections
 
     unsigned int ADCValue;
     unsigned int pwmValue;
     char x[4];
     char pwm[4];
 
void main() {
 
     UART1_Init(115200);
     ADC_Init();
     PWM1_Init(5000);
     PORTA=0;
     PORTB=0;
     TRISA=0x1;
     TRISC=0x39;
     PWM1_Start();
 
     Lcd_Init();                        // Initialize LCD
     Lcd_Cmd(_LCD_CLEAR);               // Clear display
     Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
 
 
     do
     {
     //                     Calculations
     unsigned int input;
     unsigned int pwm;
     ADCValue=ADC_Read(0);
     pwm=2.55E2*ADCValue/1024;
     pwmValue=1E2*ADCValue/1024;
     WordToStr(pwmValue,pwm);
     WordToStr(ADCValue,x);
     delay_ms(20);
          //                       LCD Display
     Lcd_Out(1,1,"Analog V:");
     Lcd_Out(1,11,x);
     delay_ms(20);
     Lcd_Out(2,1,"PWM % :");
     Lcd_Out(2,11,pwmValue);
 
     PWM1_Set_Duty(pwm);
     //                       Serial port
    UART1_Write_Text("Analog Value=");
    UART1_Write_Text(x);
     UART1_Write(13);
     Delay_ms(20);
 
    }while(1);
}

 
Last edited by a moderator:

Try this code.


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
// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
 
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections
 
     unsigned int ADCValue;
     unsigned int pwmValue;
     char x[17];
     char pwm[17];
 
void main() {
 
     UART1_Init(115200);
     ADC_Init();
     PWM1_Init(5000);
     PORTA=0;
     PORTB=0;
     TRISA=0x1;
     TRISB = 0x00;
     TRISC=0b10111001;
     PWM1_Start();
 
     Lcd_Init();                        // Initialize LCD
     Lcd_Cmd(_LCD_CLEAR);               // Clear display
     Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
 
 
     do
     {
     //                     Calculations
     unsigned int input;
     unsigned int pwm;
     ADCValue=ADC_Read(0);
     pwm=2.55E2*ADCValue/1024;
     pwmValue=1E2*ADCValue/1024;
     WordToStr(pwmValue,pwm);
     WordToStr(ADCValue,x);
     delay_ms(20);
          //                       LCD Display
     Lcd_Out(1,1,"Analog V:");
     Ltrim(x);
     Lcd_Out(1,11,x);
     delay_ms(20);
     Lcd_Out(2,1,"PWM % :");
     Ltrim(pwmValue);
     Lcd_Out(2,11,pwmValue);
 
     PWM1_Set_Duty(pwm);
     //                       Serial port
    UART1_Write_Text("Analog Value=");
    UART1_Write_Text(x);
     UART1_Write(13);
     Delay_ms(20);
 
    }while(1);
}



If it still displays garbage data on LCD then google for "mikroe CopyConst2Ram" and use CopyConst2Ram() function to display constant text. Save RAM. Memory bank switching is a problem with PIC16F and mikroC PRO PIC Compiler. You will get "IRP bit must be set manually to access xyz variable" warning message.
 

Try this code.


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
// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
 
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections
 
     unsigned int ADCValue;
     unsigned int pwmValue;
     char x[17];
     [COLOR="#FF0000"]char pwm1[17];[/COLOR] 
void main() {
 
     UART1_Init(115200);
     ADC_Init();
     PWM1_Init(5000);
     PORTA=0;
     PORTB=0;
     TRISA=0x1;
     TRISB = 0x00;
     TRISC=0b10111001;
     PWM1_Start();
 
     Lcd_Init();                        // Initialize LCD
     Lcd_Cmd(_LCD_CLEAR);               // Clear display
     Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
 
 
     do
     {
     //                     Calculations
     unsigned int input;
     unsigned int pwm;
     ADCValue=ADC_Read(0);
     pwm=2.55E2*ADCValue/1024;
     pwmValue=1E2*ADCValue/1024;
     WordToStr(pwmValue,[COLOR="#FF0000"]pwm1[/COLOR]);
     WordToStr(ADCValue,x);
     delay_ms(20);
          //                       LCD Display
     Lcd_Out(1,1,"Analog V:");
     Ltrim(x);
     Lcd_Out(1,11,x);
     delay_ms(20);
     Lcd_Out(2,1,"PWM % :");
     Ltrim([COLOR="#FF0000"]pwmValue[/COLOR]);
     Lcd_Out(2,11,pwmValue);
 
     PWM1_Set_Duty(pwm);
     //                       Serial port
    UART1_Write_Text("Analog Value=");
    UART1_Write_Text(x);
     UART1_Write(13);
     Delay_ms(20);
 
    }while(1);
}



Thanks for the reply. I made changes highlighted in red, and it now works. The only problem is that once I get above 4 significant figures in the LCD, and reduce the PWM down again, the 4th Significant figure does not disappear. I will attach a pic to illustrate.

<a title="Proteus 8 Pic.png" href="http://obrazki.elektroda.pl/9641453400_1422608492.png"><img src="http://obrazki.elektroda.pl/9641453400_1422608492_thumb.jpg" alt="Proteus 8 Pic.png" /></a>
 

There was a mistake. You were sending pwmValue to LCD_Out(). It is wrong because it is unsigned int. You have to send pwm and x. Also you were sending pwm to PWM1-Set_Duty() and again it is wrong. You can't send string to it you have to send unsigned char.

what is the purpose of these


Code C - [expand]
1
2
pwm=2.55E2*ADCValue/1024;
     pwmValue=1E2*ADCValue/1024;



when pwm and pwmValue are unsigned int ?

If you need floating point value then use double type and FloatToStr()

Remember if FloatToStr() is used array used should have 23 bytes or you can use float2ascii.h by paulfjujo. Ask him if you want.


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
// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
 
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections
 
     unsigned int ADCValue, prevADCVal;
     unsigned int pwmValue, prevPwmVal;
     char x[17];
     char pwm[17];
 
void main() {
 
     UART1_Init(115200);
     ADC_Init();
     PWM1_Init(5000);
     PORTA=0;
     PORTB=0;
     TRISA=0x1;
     TRISB = 0x00;
     TRISC=0b10111001;
     PWM1_Start();
 
     Lcd_Init();                        // Initialize LCD
     Lcd_Cmd(_LCD_CLEAR);               // Clear display
     Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
 
 
     do
     {
     //                     Calculations
     unsigned int input;
     unsigned int pwm;
     ADCValue=ADC_Read(0);
     pwm=2.55E2*ADCValue/1024;
     pwmValue=1E2*ADCValue/1024;
     WordToStr(pwmValue,pwm);
     WordToStr(ADCValue,x);
     delay_ms(20);
          //                       LCD Display
     Lcd_Out(1,1,"Analog V:");
     Ltrim(x);
     if(ADCValue != prevADCVal) {
     LCD_Out(1,11,"      ");
     Lcd_Out(1,11,x);
     prevADCVal = ADCValue;
     }
     
     delay_ms(20);
     Lcd_Out(2,1,"PWM % :");
     
     Ltrim(pwmValue);
     if(pwmValue != prevPwmVal) {
     LCD_Out(2,11,"      ");
     Lcd_Out(2,11,pwm);
     prevPwmVal = pwmValue;
     }
 
     PWM1_Set_Duty(pwmValue);
     //                       Serial port
    UART1_Write_Text("Analog Value=");
    UART1_Write_Text(x);
     UART1_Write(13);
     Delay_ms(20);
 
    }while(1);
}

 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top