starday
Junior Member level 1
Hi all,
Im new to C++ prog and i have a project which requires me using a 18f4520 with MPLAB IDE 8.80 and C18 compiler.
I have a LM35 which i will need to output the temperature value onto a 2x16 LCD.
Im using 18f4520 and making use of its ADC to do the conversion.
The problem now is that the LCD is not displaying anything. I had done a simple program displaying "Hello World" and it is OK so i assumed the hardware connections are fine.
I hope someone can enlighten me with this.
Below is my coding.
I need some verifications in this part to ensure that im on the right track.
Your help is much appreciated.
Thanks
Im new to C++ prog and i have a project which requires me using a 18f4520 with MPLAB IDE 8.80 and C18 compiler.
I have a LM35 which i will need to output the temperature value onto a 2x16 LCD.
Im using 18f4520 and making use of its ADC to do the conversion.
The problem now is that the LCD is not displaying anything. I had done a simple program displaying "Hello World" and it is OK so i assumed the hardware connections are fine.
I hope someone can enlighten me with this.
Below is my coding.
Code:
#include <p18f4520.h>
#include <delays.h>
#include <stdlib.h>
void Init_LCD(void); // Initialize the LCD
void W_ctr_4bit(char); // 4-bit Control word for LCD
void W_data_4bit(char); // 4-bit Text Data for LCD
void Delay_1kcyc(void); // 1000 cycles delay
#define LCD_DATA PORTD
#define LCD_RW PORTAbits.RA2 // RW signal for LCD
#define LCD_RS PORTAbits.RA3 // RS signal for LCD
#define LCD_E PORTAbits.RA1 // E signal for LCD
unsigned char LCD_TEMP, i;
char Buffer[20]; // Buffer to hold temperature character string
unsigned int i; // Index variable
unsigned int t; // Variable for temperature
void main(void)
{
/* Declaration of variables */
TRISA = 0b11110001; // Set Pin RA0, RA4~RA7 as Input, RA1~RA3 as Output
TRISB = 0b00000000; // Set Port B as Output port (LEDs)
PORTB = 0b00000000; // Port B initialize as OFF (LEDs not lighted)
TRISC = 0b00000000; // Set PORT C as Output port (Buzzer)
ADCON2 = 0b10000100; // Result Right justified, Manual acquisition time, Fosc/4
/* Main program starts */
Init_LCD(); // Init LCD 4-bit interface, multiple line
while(1)
{
/* Measure Temperature */
ADCON0 = 0b00000001; // Select AN0 (channel 0) for Temp sensor, turn on ADC
ADCON1 = 0b00001110; // Select AN0 as ANALOG input, internal voltage
Delay10TCYx(1); // Acquisition time 10us (>=5us)
ADCON0bits.GO = 1; // Start A/D Conversion
while(ADCON0bits.DONE); // ADC completed?
{
t= (ADRES*500) / 255; // ADRES (16bit) is the output of ADC, Convert to Degree Celsius in 8 bits format.
W_ctr_4bit(0xC0) // Move cursor to line 2 position 1
itoa(t, Buffer); // Convert number in variable t to a string and store in variable Buffer
i = 0;
while (Buffer[i] != 0) W_data_4bit (Buffer[i++]);
}
}
}
/* LCD display initialization */
void Init_LCD()
{
// Special Sequence a) to d) required for 4-bit interface
Delay1KTCYx(15); // a) 15ms LCD power-up delay
W_ctr_4bit(0x03); // b) Function Set (DB4-DB7: 8-bit interface)
Delay1KTCYx(5); // c) 5ms delay
W_ctr_4bit(0x02); // d) Function Set (DB4-DB7: 4-bit interface)
W_ctr_4bit(0b00101000); // Function Set - 4-bit, 2 lines, 5X7
W_ctr_4bit(0b00001100); // Display on, cursor off
W_ctr_4bit(0b00000110); // Entry mode - inc addr, no shift
W_ctr_4bit(0b00000001); // Clear display & home position
}
/* Write control word in term of 4-bit at a time to LCD */
void W_ctr_4bit (char x)
{
LCD_RW = 0; // Logic ‘0’
LCD_RS = 0; // Logic ‘0’
LCD_TEMP = x; // Store control word
LCD_TEMP >>= 4; // send upper nibble of control word
LCD_E = 1; // Logic ‘1’
LCD_DATA = LCD_TEMP;
Delay1KTCYx(1); // 1ms delay
LCD_E = 0; // Logic ‘0’
Delay1KTCYx(1); // 1ms delay
LCD_TEMP = x; // Store control word
LCD_TEMP &= 0x0f; // Send lower nibble of control word
LCD_E = 1; // Logic ‘1’
LCD_DATA = LCD_TEMP;
Delay1KTCYx(1); // 1ms delay
LCD_E = 0; // Logic 0’
Delay1KTCYx(1); // 1ms delay
}
/* Write text data in term of 4-bit at a time to LCD */
void W_data_4bit (char x)
{
LCD_RW = 0; // Logic ‘0’
LCD_RS = 1; // Logic ‘1’
LCD_TEMP = x; // Store text data
LCD_TEMP >>= 4; // Send upper nibble of text data
LCD_E = 1; // Logic ‘1’
LCD_DATA = LCD_TEMP;
Delay1KTCYx(1); // 1ms delay
LCD_E = 0; // Logic ‘0’
Delay1KTCYx(1); // 1ms delay
LCD_TEMP = x; // Store text data
LCD_TEMP &= 0x0f; // Send lower nibble of text data
LCD_E = 1; // Logic ‘1’
LCD_DATA = LCD_TEMP;
Delay1KTCYx(1); // 1ms delay
LCD_E = 0; // Logic ‘0’
Delay1KTCYx(1); // 1ms delay
}
I need some verifications in this part to ensure that im on the right track.
Code:
while(1)
{
/* Measure Temperature */
ADCON0 = 0b00000001; // Select AN0 (channel 0) for Temp sensor, turn on ADC
ADCON1 = 0b00001110; // Select AN0 as ANALOG input, internal voltage
Delay10TCYx(1); // Acquisition time 10us (>=5us)
ADCON0bits.GO = 1; // Start A/D Conversion
while(ADCON0bits.DONE); // ADC completed?
{
t= (ADRES*500) / 255; // ADRES (16bit) is the output of ADC, Convert to Degree Celsius in 8 bits format.
W_ctr_4bit(0xC0) // Move cursor to line 2 position 1
itoa(t, Buffer); // Convert number in variable t to a string and store in variable Buffer
i = 0;
while (Buffer[i] != 0) W_data_4bit (Buffer[i++]);
}
}
Your help is much appreciated.
Thanks