Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

LCD temperature display using XC8 and PIC16F877A

Status
Not open for further replies.

HanNhuThien

New Member
Hi there, I'm Thien
My homework is to display temperature (celcius) on LCD 16*2 but I do not know how to get it done
Require: LCD 16*2 , PIC16F877A, XC8 C (Mplab 8 ), frequency: 1 MHz

Could you help me with the outline ?
Thank you so much

UPDATE: I tried it again, but still did not work but I think it's more readable

Below is my code and my proteus project screen shot
 

Attachments

  • Screenshot (10).png
    Screenshot (10).png
    114.5 KB · Views: 342
  • week5.txt
    1.4 KB · Views: 276
Last edited:
For a start I cant see a LCD initiate function, this needs to be called once at the start on main. There are other problems but its a start for you.

You have no power on the LCD and the contrast (lcd pin 3) needs a pot or tying low.

MCLR pin needs tying hi via 10K resistor or it will sit in reset, also not sure what the start default is but I would declare if the MCLR pin is on or off (recommend on).

You have a HS OSC selected in code but no OSC on the drawing, this is full of **** you need to sort the simple things out first, I get the feeling you have done little study in this.

Go through the drawing and match it to the code, you clearly have not done this.
 
Last edited:
Sorry if all that sounded grumpy, it wasnt intended to be grumpy. Have another go and repost your results people will help you get it done if you do the work :D
 
Is that all your code? you dont seem to have a LCD library called and no lcd functions. As your using 4 pins for lcd data you will need functions to write nibbles (half bytes) it might be easier to use 8 lcd data lines and an inbuilt library for the LCD. I use C18 so not upto speed on xc18 yet.
 
this is not for your chip but the functions should give you an idea for 4 dataline in CD.

C:
/*
* Build in the MPLAB IDE using C18 and device type 181320
* .
*/
#pragma config OSC = IRCIO67, PWRT = OFF, WDT = OFF, MCLRE = ON, DEBUG = ON, LVP = OFF //..............Fuse stup
#include <p18f4685.h>

#define LCDPORT LATA
#define LCDTRIS TRISA
#define LCD_RS LATBbits.LATB4
#define LCD_TRIS_RS TRISBbits.TRISB4
#define LCD_E LATBbits.LATB1
#define LCD_TRIS_E TRISBbits.TRISB1

void Delay(int);
void WriteCommand(unsigned char);
void WriteChar(unsigned char);
void WriteNibble(unsigned char);
void WaitLCDBusy(void);
void WriteLCD(unsigned char);
void InitLCD(void);
void PutMessage(rom char*);
void PutMessageAt(unsigned char,unsigned char,rom char*);
void PutHex(unsigned char);
void PutNibble(unsigned char);
void main (void)
{
  OSCCONbits.IRCF0=1;
  OSCCONbits.IRCF1=1;
  OSCCONbits.IRCF2=1;
  ADCON1=0x0A; //set up ADC
  CMCON=0x07;
InitLCD();

while(1);
}

//Write a string to the LCD
void PutMessage(rom char *Message){
  while(*Message!=0)
  WriteChar(*Message++);
}

//Set position and write string.
void PutMessageAt(unsigned char X,unsigned char Y,rom char *Message){
  WriteCommand(0x80+(--Y<<6)+--X);
  while(*Message!=0)
  WriteChar(*Message++);
}

void PutHex(unsigned char Num){
  PutNibble(Num>>4);
  PutNibble(Num&0x0f);
}

void PutNibble(unsigned char num){
  if (num>9){
  WriteChar(num+'A'-10);
  }
  else{
  WriteChar(num+'0');
  }
}

//Initialises the LCD
void InitLCD(void){
  LCDTRIS &= 0xE1;  //ensure data bits are output
  LCD_E=0;  //clear enable
  LCD_RS = 0;  //going to write command
  LCD_TRIS_E=0;  //Set enable to output
  LCD_TRIS_RS=0;  //set RS to output
  Delay(30);  //delay for LCD to initialise.
  WriteNibble(0x30);  //Required for initialisation
  Delay(5);  //required delay
  WriteNibble(0x30);  //Required for initialisation
  Delay(1);  //required delay
  WriteCommand(0x20);  //set to 4 bit interface
  WriteCommand(0x2c);  //set to 4 bit interface, 2 line and 5*10 font
  WriteCommand(0x01);  //clear display
  WriteCommand(0x06);  //move cursor right after write
  WriteCommand(0x0C);  //turn on display
}

//wait for the LCD to finish last command.
void WaitLCDBusy(void){
  Delay(5);
}

//Send a command to the LCD
void WriteCommand(unsigned char command){
  WaitLCDBusy();  //wait until not busy
  LCD_RS = 0;  //setup to send command
  WriteNibble(command>>4);  //write the high nibble
  WriteNibble(command);  //then the low nibble
}

//Send a character to the LCD
void WriteChar(unsigned char chr){
  WaitLCDBusy();  //wait until not busy
  LCD_RS=1;  //Setup to send character
  WriteNibble(chr>>4);  //write the high nibble
  WriteNibble(chr);  //then the low nibble
}

//Send any 4 bits to the LCD
void WriteNibble(unsigned char command){
  LCDPORT &= 0xE1;  //clear the data bits
  LCDPORT |= ((command<<1) & 0x1E);  //or in the new data
  LCD_E = 1;  //enable the LCD interface
  LCD_E = 0;  //disable it
}

// delay mS at 20MHz
void Delay(int Time){
int i,j;
  for(j=0;j<Time;j++)
  for(i=0;i<275;i++);
}

Also note this is really old code and in C18, but should give you some help
 
I would put it back to HS but put a crystal in the schematic, I dont use 16F chips but assumed it had a internal OSC, might be easier to just put a crystal in the schematic, dont use the PLL.
 
Thank you
I set FOSC back to HS but my teacher said it was not necessary to put a crystal
Updated !
But still wrong

Error screen below
 

Attachments

  • Screenshot (13).png
    Screenshot (13).png
    154.2 KB · Views: 257
Ok go with what your teacher says, I dont use proteus much or 16F chips, the code i gave you sets the internal OSC for a 18f chip, if the 16f dosnt have internal OSC it will need the code changing. I gave you the code for the LCD routines not the setup. Look at page 145 of the data sheet, you still dont have the MCLR pin tied so you might be sitting in reset. put a message early in the code to display on the LCD, that way you know when you have that bit working, then you can sort out the ADC for the temp sensor.

Looks like the codes have something to do with problems to the ADC, respost the code you have in full. I am on and off today so might be a while until I can answer, dosnt help that I dont use the same compiler.
 
Thank Little Ghostman, I have MCLR pin tied as recommended
Thank Nigel Goodwin, I'll read all

But deadline is coming in some hours, so what I need right now is lcd.h header file for 8 bits mode LCD, I have trouble with the header files but I can not write it

I have 4 bit LCD header file below, could anybody change it to 8 bit for me ?

Thank you so much
 

Attachments

  • lcd.h
    1.4 KB · Views: 337
Last edited:
I'm writing an XLCD c file to go with your program!!! So far I have found two functions nested in the main (Big No No )... function calls outside of the program flow..( Big No No..)

The LM335 maths isn't right... I'll show you why soon!

I'll soon have a valid example for you..
 
Okay!!
XLCD.H and XLCD.C

They are commented... OpenXLCD takes 1 argument Function set.. ie.. 0x28 for 4 bit ( Set the defines in the header file..)

Now your file...
C:
#include <xc.h> //CONFIG
#include <stdio.h>
#include <stdlib.h>
#include "XLCD.h"
#define _XTAL_FREQ 1000000 // if 8Mhz is HS

#pragma config FOSC = XT // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)

unsigned int hienled (void);
unsigned char buffer[20];
unsigned long ADCvalue = 0;
unsigned int voltage;
int x;

void ADCinit (void)// khoi tao adc
   { // chon tan so clock cho bo adc
   ADCS2 = ADCS1 = 0;
   ADCS0 = 1; // chon kenh adc la kenh an0
   CHS2 = CHS1 = CHS0 = 0; // chon cach luu data
   ADFM = 1; // cau hinh cong vao
   PCFG3 = PCFG2 = PCFG1 = 1;
   PCFG0 = 0; // cap nguon cho khoi adc
   }

unsigned int hienled (void)// doc led 7 doan
   {
   ADON = 1;
   GO = 1;
   delayUs(100);
   while(GO);
   return (int)ADRESH*256 + ADRESL;
   }
// main is cleaned up so it will work
void main()
   {
   TRISA = 0xff;
   LCD_PORT = 0;
   TRIS_LCD_PORT = 0x00;
   TRIS_RW = TRIS_RS = TRIS_E = 0;
   ADCinit();
   OpenXLCD(0x28);
   while(1)
     {
     ADCvalue = hienled();
     voltage = ADCvalue * 5000 / 1023 ; // Yours was the wrong way round also ADCvalue  HAS to be a long!!
     x = voltage / 10 - 273;
     sprintf(buffer," TEMP = %d%c",x,0xDF);
     SetCGRamAddr(0);
     putsXLCD(buffer);
     } 
   }
And circuit (Running)
upload_2016-2-28_20-15-38.jpeg


EDIT!!!! Forgot the XLCD.C file...
 

Attachments

  • XLCD.h
    1.9 KB · Views: 260
  • XLCD.c
    2.2 KB · Views: 220
Last edited:
So in proteus you can leave power pins unconnected?? For a sim that seems a bit odd. Does the MCLR have to be tied in proteus?
I notice its the same with the OSC pins.

I didnt realize you were so short on time! do they normally only give you a couple of days to do it?
 
For proteus to sim as fast as possible... You leave anything analogue out of the equation.. MCLR is tied to 5v on chip that need it... resistors are only needed for pullup purposes... There is a special pullup model..
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top