ADGAN
Full Member level 5
- Joined
- Oct 9, 2013
- Messages
- 295
- Helped
- 4
- Reputation
- 8
- Reaction score
- 4
- Trophy points
- 18
- Activity points
- 1,837
Hi! I have written a code to display date and energy consumption for a project using MPLAB C18. Everything displays perfectly in Proteus but when I tested in real hardware variables such as date, time doesn't display on the LCD. I tried a MikroC example code and it displays everything perfectly. Can somebody explain me what I may have done wrong? I'll post some necessary parts of the code below.
For example to display date on the LCD:
To display the energy consumption:
Code:
#include <p18cxxx.h>
#include "LCD.h"
#include "LCD_Config.h"
//#pragma udata
union LCDv8bit LCD_data; // bitfield variable (bitwise acess)
unsigned short LCD_busy_cnt;
// -------------------- LCD-functions ------------------------------------------
#ifdef LCD_ON
void LCD_On (void)
{
LCD_ON = 1;
LCD_ON_DIR = 0;
#warning DO NOT FORGET TO SWITCH ON THE DISPLAY AND TO ADD A POWER UP DELAY !
}
#endif
void LCD_Init (void)
{
LCD_RW = 0; LCD_RW_DIR = 0;
LCD_RS = 0; LCD_RS_DIR = 0;
LCD_E = 0; LCD_E_DIR = 0;
LCD_D4_OUT = 0; LCD_D4_DIR = 0;
LCD_D5_OUT = 0; LCD_D5_DIR = 0;
LCD_D6_OUT = 0; LCD_D6_DIR = 0;
LCD_D7_OUT = 0; LCD_D7_DIR = 0;
LCD_busy_cnt = 1; // busy_flag time out counter
LCD_DELAY_5MS();LCD_DELAY_5MS();LCD_DELAY_5MS(); // wait for 15ms
// display reset procedure
LCD_Write_Nibble(LCD_RESET); LCD_DELAY_5MS();
LCD_Write_Nibble(LCD_RESET); LCD_DELAY_5MS();
LCD_Write_Nibble(LCD_RESET); LCD_DELAY_5MS();
// set 4-bit mode always !!!
LCD_Write_Nibble(FOUR_BIT); while (LCD_Busy()) {;} // wait
#if defined EXT_LCD_3_3_V
LCD_Command(FOUR_BIT_TWO_LINE + EXT_INSTR_TBL_1);
LCD_Command(EXT1_BIAS_1_5);
LCD_Command(EXT1_CONTRAST + 0x0C);
LCD_Command(EXT1_BOOST_ICON_C + BOOST_ON + 1);
LCD_Command(EXT1_FOLLOWER + FOLLOWER_ON + 5);
#elif defined EXT_LCD_5_V
LCD_Command(FOUR_BIT_TWO_LINE + EXT_INSTR_TBL_1);
LCD_Command(EXT1_BIAS_1_4);
LCD_Command(EXT1_CONTRAST + 0x04);
LCD_Command(EXT1_BOOST_ICON_C + BOOST_OFF + 2);
LCD_Command(EXT1_FOLLOWER + FOLLOWER_ON + 1);
#else
LCD_Command(FOUR_BIT_TWO_LINE);
#endif
LCD_Command(DISPLAY_CTRL + DISPLAY_ON);// + BLINK_ON);
LCD_Command(ENTRY_MODE + CURSOR_INC + DSHIFT_OFF);
LCD_Command(CLEAR_DISPLAY);
LCD_Command(RETURN_HOME);
}
// --------------------------------------------------------------
void LCD_signalsTest()
{
LCD_RS_DIR = 0;
LCD_RW_DIR = 0;
LCD_E_DIR = 0;
LCD_D4_DIR = 0;
LCD_D5_DIR = 0;
LCD_D6_DIR = 0;
LCD_D7_DIR = 0;
LCD_RS = 1;
LCD_RW = 1;
LCD_ENABLE();
LCD_D4_OUT = 1;
LCD_D5_OUT = 1;
LCD_D6_OUT = 1;
LCD_D7_OUT = 1;
LCD_RS = 0;
LCD_RW = 0;
LCD_DISABLE();
LCD_D4_OUT = 0;
LCD_D5_OUT = 0;
LCD_D6_OUT = 0;
LCD_D7_OUT = 0;
}
void LCD_Write_Nibble(unsigned char value)
{
LCD_data.all = value;
LCD_RS = LCD_CMD;
LCD_RW = LCD_WR;
LCD_D4_OUT = LCD_data.bit4;
LCD_D5_OUT = LCD_data.bit5;
LCD_D6_OUT = LCD_data.bit6;
LCD_D7_OUT = LCD_data.bit7;
LCD_STROBE();
}
// --------------------------------------------------------------
void LCD_Write(unsigned char value) // command or data
{
LCD_data.all = value;
LCD_RW = LCD_WR;
LCD_D7_OUT = LCD_data.bit7;
LCD_D6_OUT = LCD_data.bit6;
LCD_D5_OUT = LCD_data.bit5;
LCD_D4_OUT = LCD_data.bit4;
LCD_STROBE();
LCD_D7_OUT = LCD_data.bit3;
LCD_D6_OUT = LCD_data.bit2;
LCD_D5_OUT = LCD_data.bit1;
LCD_D4_OUT = LCD_data.bit0;
LCD_STROBE();
}
// --------------------------------------------------------------
void LCD_ConstTextOut(unsigned char row, unsigned char col, const char *text)
{
unsigned char data;
switch(row){
case 0: data = ROW_0 + col; break;
case 1: data = ROW_1 + col; break;
case 2: data = ROW_2 + col; break;
case 3: data = ROW_3 + col; break;
default: break;}
LCD_Command(CURSOR_ADDR + data);
while (*text) {
LCD_CharOut(*text++);
}
}
// --------------------------------------------------------------
// --------------------------------------------------------------
void LCD_TextOut(unsigned char row, unsigned char col, unsigned char text[])
{
unsigned char data,i = 0;
switch(row){
case 0: data = ROW_0 + col; break;
case 1: data = ROW_1 + col; break;
case 2: data = ROW_2 + col; break;
case 3: data = ROW_3 + col; break;
default: break;}
LCD_Command(CURSOR_ADDR + data);
while (text[i]) {
LCD_CharOut(text[i++]);
}
}
//----------------------------------------------------------------
unsigned char LCD_Busy(void)
{
if (LCD_busy_cnt >= LCD_TIMEOUT){
LCD_busy_cnt = 1;
return 0; // return -1 for time out ???????????????
}
LCD_RW = LCD_RD;
LCD_RS = LCD_CMD;
LCD_D4_DIR = 1; LCD_D5_DIR = 1; LCD_D6_DIR = 1; LCD_D7_DIR = 1;
LCD_ENABLE(); LCD_DELAY_1US();
LCD_data.bit7 = LCD_D7_IN;
LCD_DISABLE();
LCD_ENABLE(); LCD_DELAY_1US();
LCD_data.bit3 = LCD_D7_IN;
LCD_DISABLE();
LCD_D4_DIR = 0; LCD_D5_DIR = 0; LCD_D6_DIR = 0; LCD_D7_DIR = 0;
LCD_RW = LCD_WR;
if (LCD_data.bit7 == LCD_BUSY) {
LCD_busy_cnt++;
return 1;
} else {
LCD_busy_cnt = 1;
return 0;
}
}
// -------------------- END LCD-functions ---------------------------
For example to display date on the LCD:
Code:
tnum[0] = day1/10 + 48;
tnum[1] = day1%10 + 48;
tnum[2] = '\0';
LCD_TextOut(2,7,tnum);
To display the energy consumption:
Code:
ultoa(ENERGY_ACT,txt7);
LCD_TextOut(3,3,txt7);