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.

Temperature Sensing Using Thermocouple, PIC microcontroller and LCD

Status
Not open for further replies.

haronraziq

Newbie level 6
Joined
Apr 3, 2011
Messages
14
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
1,449
I am trying to program a microcontroller for temperature sensing and output to an lcd. I have programs both in Fed C and Hi-tech C included below.

The FED C program has voltages at the outputs but the LCD remains with a black lined display (all black boxes)

The Hi-tech C program does not have output voltage and the LCD remains black lined.


Can anyone see what is causing this problem in my programs? I would really appreciate the help.


#ifndef _XTAL_FREQ
// Unless specified elsewhere, 4MHz system frequency is assumed
#define _XTAL_FREQ 4000000
#endif


#include <htc.h>
#include "lcd.h"
#include <string.h>
#include <math.h>
#include <stdio.h>

#define LCD_RS RC3
#define LCD_RW RC2
#define LCD_EN RC1

#define LCD_DATA PORTD

#define LCD_STROBE() ((LCD_EN = 1),(LCD_EN=0))

/* write a byte to the LCD in 4 bit mode */

void
lcd_write(unsigned char c)
{
__delay_us(40);
LCD_DATA = ( ( c >> 4 ) & 0x0F );
LCD_STROBE();
LCD_DATA = ( c & 0x0F );
LCD_STROBE();
}

/*
* Clear and home the LCD
*/

void
lcd_clear(void)
{
LCD_RS = 0;
lcd_write(0x1);
__delay_ms(2);
}

/* write a string of chars to the LCD */

void
lcd_puts(const char * s)
{
LCD_RS = 1; // write characters
while(*s)
lcd_write(*s++);
}

/* write one character to the LCD */

void
lcd_putch(char c)
{
LCD_RS = 1; // write characters
lcd_write( c );
}


/*
* Go to the specified position
*/

void
lcd_goto(unsigned char pos)
{
LCD_RS = 0;
lcd_write(0x80+pos);
}

/* initialise the LCD - put into 4 bit mode */
void
lcd_init()
{
char init_value;

//ADCON1 = 0x06; // Disable analog pins on PORTA//

init_value = 0x3;
TRISC=0;
TRISD=0;
LCD_RS = 0;
LCD_EN = 0;
LCD_RW = 0;

__delay_ms(15); // wait 15mSec after power applied,
LCD_DATA = init_value;
LCD_STROBE();
__delay_ms(5);
LCD_STROBE();
__delay_us(200);
LCD_STROBE();
__delay_us(200);
LCD_DATA = 2; // Four bit mode
LCD_STROBE();

lcd_write(0x28); // Set interface length
lcd_write(0xF); // Display On, Cursor On, Cursor Blink
lcd_clear(); // Clear screen
lcd_write(0x6); // Set entry Mode
}
void main()
{
float temp,LSB,lsd1;
int msd,lsd2;
unsigned char temperature[];

LSB = 5000.0/1024.0; //LSB in mV
TRISA = 1;

//Initialize the LCD//
lcd_init();
lcd_goto(0);

while(1) //DO FOREVER
{

/* Configure the A/D */
ADCON1 = 0x80; //Select 10 bits
ADCON0 = 0x41;

/* Start A/D conversion */
ADCON0=0x43; //Start A/D conversion

while((ADCON0 & 2) != 0); //Wait for conversion
temp=256.0*(float)ADRESH+(float)ADRESL; //Get temperature

/* Convert reading to temperature */
temp = temp*LSB; //temp in mV
temp = temp/10.0; //temp in degrees C

/* Format for the display */
//lcd_clear(); //Clear LCD and home//

msd=(int)temp; //msd digit
lsd1=10.0*(temp-msd); //lsd digit
lsd2=(int)lsd1;
sprintf(temperature,"%d",msd);
temperature[2]= '.';
sprintf(temperature+3,"%d",lsd2);
lcd_puts(temperature);

__delay_ms(1000);
}

}
 

Okay so I tried checking with a sample program on two different lcds with the same result, black boxes despite a voltage reading on the pins.

I think it highly unlikely that both lcds are broken. Anything is possible though.


Any suggestions?
 
I am using a 16x2 lcd. I could not find a simulator that has all the parts necessary to simulate my circuit.

The circuit and code is extracted from a textbook written by Dogan Ibrahim on Programming Microcontrollers for Temperature Sensing. I have attached the page with the relevant schematic. I am however, using the pic16f887 instead of the 16f877 and I made the requisite adjustments to the code.

Let me know if you figure anything out.
 

Attachments

  • Programming Microcontrollers femperature Sensing and Control .pdf
    53.8 KB · Views: 177

you should use proteus simulator to simulate your code.. your schematic is okay i think your code is the problem.

I did not put my hands on your code :) so you'd beter read the datasheet of 16x2 LCD how to interface it to controller..

this is working example of 16x2 LCD code (4bit mode):

use this code as reference.. this is written in keil compiler


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
// 4 bit example
 
#include<reg51.h>
 
#define LCD P1    //port 1
#define LCD_EN      0x80
#define LCD_RS      0x20
                     
//LCD Commands        
 
#define LCD_SETMODE        0x04
#define LCD_SETVISIBLE     0x08
#define LCD_SETFUNCTION    0x28
#define LCD_SETDDADDR      0x80
 
void delayms(unsigned char);
void delayus(unsigned char);
void lcd_init();
void lcd_reset();
void lcd_cmd (char);
void lcd_data(unsigned char);
void lcd_str (unsigned char*);
 
 
void  main (void)
{
lcd_init();
lcd_cmd(0x80);            // ist line address
lcd_str("     i love  ");
lcd_cmd(0xC0);            // 2nd line address
lcd_str("romel");
while(1);
}
 
void delayus(unsigned char delay){
    while(delay--);
}
 
void delayms(unsigned char delay){
    while(delay--)
        delayus(149);
}
 
void lcd_reset()   //reset LCD
{
    LCD = 0xFF;
    delayms(40);
    LCD = 0x03+LCD_EN;
    LCD = 0x03;
    delayms(40);
    LCD = 0x03+LCD_EN;
    LCD = 0x03;
    delayms(5);
    LCD = 0x03+LCD_EN;
    LCD = 0x03;
    delayms(5);
    LCD = 0x02+LCD_EN;
    LCD = 0x02;
    delayms(5);
}
 
 
void lcd_init ()   //initialize LCD
{
    lcd_reset();
    lcd_cmd(LCD_SETFUNCTION);                    // 4-bit mode - 1 line - 5x7 font. 
    lcd_cmd(LCD_SETVISIBLE+0x04);                // Display no cursor - no blink.
    lcd_cmd(LCD_SETMODE+0x02);                   // Automatic Increment - No Display shift.
    lcd_cmd(LCD_SETDDADDR);                      // Address DDRAM with 0 offset 80h.
 }
 
 void lcd_cmd (char cmd)
{ 
    LCD = ((cmd >> 4) & 0x0F)|LCD_EN;
    LCD = ((cmd >> 4) & 0x0F);
 
    LCD = (cmd & 0x0F)|LCD_EN;
    LCD = (cmd & 0x0F);
 
    delayus(250);
    delayus(250);
}
 
void lcd_data (unsigned char dat)  
{ 
    LCD = (((dat >> 4) & 0x0F)|LCD_EN|LCD_RS);
    LCD = (((dat >> 4) & 0x0F)|LCD_RS);
    
    LCD = ((dat & 0x0F)|LCD_EN|LCD_RS);
    LCD = ((dat & 0x0F)|LCD_RS);
 
    delayus(250);
    delayus(250);
}
 
void lcd_str (unsigned char *str)   //display string to LCD
{
    while(*str){
        lcd_data(*str++);
    }
}



---------- Post added at 05:04 ---------- Previous post was at 05:03 ----------

use this simulator for... everything you need is here for your application

**broken link removed**
 
I really appreciate your helping me on this project.

Is the C language for the Keill similar to Hi-tech C?

---------- Post added at 23:47 ---------- Previous post was at 23:45 ----------

Also, the above code is generated for what kind of microcontroller?
 

it seems that you are not initializing the lCD correctly...
void
lcd_init()
{
char init_value;

//ADCON1 = 0x06; // Disable analog pins on PORTA//

init_value = 0x3;
TRISC=0;
TRISD=0;
LCD_RS = 0;
LCD_EN = 0;
LCD_RW = 0;

__delay_ms(15); // wait 15mSec after power applied,
LCD_DATA = init_value;
LCD_STROBE();
__delay_ms(5);
LCD_STROBE();
__delay_us(200);
LCD_STROBE();
__delay_us(200);
LCD_DATA = 2; // Four bit mode
LCD_STROBE();

lcd_write(0x2; // Set interface length
lcd_write(0xF); // Display On, Cursor On, Cursor Blink
lcd_clear(); // Clear screen
lcd_write(0x6); // Set entry Mode
}

you are sending the initial value = 0x3;
which is for 8 bit mode
you should send 0x2; //intial value for 4 bit mode
it should work then...
 
I really appreciate your helping me on this project.

Is the C language for the Keill similar to Hi-tech C?

---------- Post added at 23:47 ---------- Previous post was at 23:45 ----------

Also, the above code is generated for what kind of microcontroller?

that code is example only,, that is for 8051 microcontroller..but how to interface the 16x2 lcd is the same.. you may refer to the datasheet of LCD for you to understand something you need to know.. and also check the code i posted how to initialize the LCD..
 
I am going to try some new implementations on Wednesday, so we will see if those work.

Thanks for the suggestions!

I'll post whether or not some of those adjustments work.
 

yeah.. i suggest you to get the LCD work first then add another function to your work.. do it step by step..
 

Romel, you could clarify which ports you are using on the microcontroller as output ports? Which line of code indicates that?

---------- Post added at 21:45 ---------- Previous post was at 21:30 ----------

I'm sorry let me rephrase my question. Are you still connecting the first four ports P1[0-3] to data ports[4-7] on the lcd?
 

I understand that he is not programming for pic. However, my question is just that since that LCD is 4 bit, it can take 4 input data. Which pins of P1 is he using for that?
 

he is using the lower 4 bits of P1 as he is shifting the data 4 times towards LSB and ANDing with 0x0f. so he uses P1.0 to P1.3...................
 

So what about for the RS and EN bits? Also, is the RW not at all necessary?
 

if you ground rw then also it is ok or you can use it as busy flag check bit....

rs is p0.0 and i guess enable or en is connected to p0.1( i guess).
 

sorry hussain your answer is wrong.....
EN = P0.7
RS = P0.5 P0.7 has address 0x87 and p0.5 has address 0x85

though i have not touched 8051 from past 6 years i am dam sure you are wrong

the correct answer is

0x80 address corresponds to p0.0 and 0x20 address corresponds to p3.0

so the connected port is P0.0 and P3.0 for rs and en
 

sorry hussain your answer is wrong.....


though i have not touched 8051 from past 6 years i am dam sure you are wrong

the correct answer is

0x80 address corresponds to p0.0 and 0x20 address corresponds to p3.0

so the connected port is P0.0 and P3.0 for rs and en

have a look at the following lines
LCD = 0x03+LCD_EN;
LCD = 0x03;
LCD = (((dat >> 4) & 0x0F)|LCD_EN|LCD_RS);
what the code is doing is performing the operation on port1 which is defined "LCD"
hence the EN and RS are P1.7 and P1.5 respectively....
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top