Microcontroller › PIC › Temperature Sensor : Interfacing LM35 and LCD Display to PIC18F4550
- This topic has 1 reply, 2 voices, and was last updated 9 years, 1 month ago by Rahul Tungar.
Viewing 2 posts - 1 through 2 (of 2 total)
-
AuthorPosts
-
August 13, 2015 at 6:36 am #3810leonardParticipant
Hey.
I need some help with my programming for Temperature Sensor. I used LM35 and connected it to PORTAbits.RA0 so as to make use of the ADC module. I connected my LCD display to PORTD. However, I can't display the temperature=t on my LCD display. Can someone check my source code? I managed to build it successfully but unable to show the temperature. Below is the result of my Temperature Sensor. As you can see, the black block was supposed to show the temperature. Here's my code :
//Temperature Sensor – Convert the Analog signal to Digital and Display it to the LCD Display Board#include <p18F4550.h>#include <delays.h>#include "lcd.h" // Include file is located in the project directory// Include this when using Bootloader Program ================================#pragma udataextern void _startup (void); // See c018i.c in your C18 compiler dir#pragma code _RESET_INTERRUPT_VECTOR = 0x000800void _reset (void){_asm goto _startup _endasm}#pragma code#pragma code _HIGH_INTERRUPT_VECTOR = 0x000808void _high_ISR (void){;}#pragma code _LOW_INTERRUPT_VECTOR = 0x000818void _low_ISR (void){;}#pragma code#pragma code// additional codes ends here ===============================================================// Your program declarations start here:====#define LCD_RS PORTDbits.RD6 // Register Select on LCD#define LCD_EN PORTDbits.RD4 // Enable on LCD controller#define LCD_WR PORTDbits.RD5 // Write on LCD controller//— Function for writing a command byte to the LCD in 4 bit mode
void lcd_write_cmd(signed char cmd){unsigned char temp2;LCD_RS = 0; // Select LCD for command modeDelay10TCYx(4); // 40us delay for LCD to settle downtemp2 = cmd;temp2 = temp2 >> 4; // Output upper 4 bits, by shifting out lower 4 bitsPORTD = temp2 & 0x0F; // Output to PORTD which is connected to LCDDelay1KTCYx(10); // 10ms – Delay at least 1 ms before strobinglcd_strobe();Delay1KTCYx(10); // 10ms – Delay at least 1 ms after strobingtemp2 = cmd; // Re-initialise temp2PORTD = temp2 & 0x0F; // Mask out upper 4 bitsDelay1KTCYx(10); // 10ms – Delay at least 1 ms before strobinglcd_strobe();Delay1KTCYx(10); // 10ms – Delay at least 1 ms before strobing}//—- Function to write a character data to the LCD
void lcd_write_data(char data){char temp1;LCD_RS = 1; // Select LCD for data modeDelay10TCYx(4); // 40us delay for LCD to settle downtemp1 = data;temp1 = temp1 >> 4;PORTD = temp1 & 0x0F;Delay1KTCYx(10);LCD_RS = 1;Delay1KTCYx(10); //_-_ strobe data inlcd_strobe();Delay1KTCYx(10);temp1 = data;PORTD = temp1 & 0x0F;Delay1KTCYx(10);LCD_RS = 1;Delay1KTCYx(10); //_-_ strobe data inlcd_strobe();Delay1KTCYx(10);}//– Function to generate the strobe signal for command and character
void lcd_strobe(void) // Generate the E pulse{LCD_EN = 1; // E = 0Delay1KTCYx(100); // 10ms delay for LCD_EN to settleLCD_EN = 0; // E = 1Delay1KTCYx(100); // 10ms delay for LCD_EN to settle}//—- Function to initialise LCD module
void lcd_init(void){TRISD = 0x00;PORTD = 0x00; // PORTD is connected to LCD data pinLCD_EN = 0;LCD_RS = 0; // Select LCD for command modeLCD_WR = 0; // Select LCD for write modeDelay10KTCYx(250); // Delay a total of 1 s for LCD module toDelay10KTCYx(250); //Delay10KTCYx(250); //Delay10KTCYx(250); // finish its own internal initialisation/* The data sheets warn that the LCD module may fail to initialise properly whenpower is first applied. This is particularly likely if the Vddsupply does not rise to its correct operating voltage quickly enough.It is recommended that after power is applied, a command sequence of3 bytes of 30h be sent to the module. This will ensure that the module is in8-bit mode and is properly initialised. Following this, the LCD module can beswitched to 4-bit mode.*/lcd_write_cmd(0x33);lcd_write_cmd(0x32);lcd_write_cmd(0x28); // 001010xx – Function Set instruction// DL=0 :4-bit interface,N=1 :2 lines,F=0 :5×7 dotslcd_write_cmd(0x0E); // 00001110 – Display On/Off Control instruction// D=1 :Display on,C=1 :Cursor on,B=0 :Cursor Blink onlcd_write_cmd(0x06); // 00000110 – Entry Mode Set instruction// I/D=1 :Increment Cursor position// S=0 : No display shiftlcd_write_cmd(0x01); // 00000001 Clear Display instructionDelay1KTCYx(20); // 20 ms delay}void main(void){// Do not remove these as well=============ADCON1 = 0x0F; // Configure PORTA to be digital I/OCMCON = 0x07;// ========================================// Your MAIN program Starts here: =========lcd_init(); // Initialise LCD moduleLCD_RS = 1; // Select LCD for character data modeDelay1KTCYx(1); // 1 ms delay/* Initialise analog to digital conversion setting */ADCON0 = 0b00000001; // bit5-2 0000 select channel 0 conversion// bit1 A/D conversion status bit// 1- GO to starts the conversion// 0 – DONE when A/D is completed// bit0 Set to 1 to power up A/DADCON1 = 0b00001100; // bit5 reference is VSS// bit4 reference is VDD// bit3-0 AN2 to AN0 Analog, the rest DigitalADCON2 = 0b00010110; // bit7 : A/D Result Format. 0 Left, 1 Right justified ( Set it to 1, Right Justified)// bit5-3 : 010 acquisition time = 4 TAD// bit2-0 : 110 conversion clock = Tosc / 16for(;{unsigned int t; // variable for temperatureADCON0bits.GO = 1; // This is bit2 of ADCON0, START CONVERSION NOWwhile(ADCON0bits.GO == 1); // Waiting for DONEt=(ADRESH * 0.48876); //Convert to Degree Celcius – ADRES (16bit) is the output of ADCwhile(1){lcd_write_cmd(0x80); // Move cursor to line 1 position 1lcd_write_data('T');lcd_write_data('E');lcd_write_data('M');lcd_write_data('P');lcd_write_data('E');lcd_write_data('R');lcd_write_data('A');lcd_write_data('T');lcd_write_data('U');lcd_write_data('R');lcd_write_data('E');lcd_write_data(':');lcd_write_cmd(0xC0); // Move cursor to line 2 position 1lcd_write_data(t); // Display the temperaturelcd_write_data(0xDF);lcd_write_data('C');while(1); //stop here for now}}}August 13, 2015 at 10:15 am #13160Rahul TungarParticipantseparate out digits of variable t and then convert it to its ascii value to display it on LCD.
-
AuthorPosts
Viewing 2 posts - 1 through 2 (of 2 total)
- You must be logged in to reply to this topic.