Put Display 16*2 with PIC16F - Page 1
Forum section: electronics / general

author: Jasonbit
author_rank: Regular Contributor
timestamp: August 18, 2018, 03:52:06 pm
content: Hello folks,I can't understand and find way to notice how can I create a simple project with library lcd and simulate this in Proteus. I have this:Main.cCode:#include <htc.h>#include <pic16f690.h>#define RS RC0#define EN RC1#define D4 RB4#define D5 RB5#define D6 RB6#define D7 RB7#define _XTAL_FREQ 4000000#include "lcd.c"//CONFIG__CONFIG(FOSC_EC & WDTE_OFF & PWRTE_ON & MCLRE_OFF);// INTOSCIO oscillator: I/O function on RA4/OSC2/CLKOUT pin AND RA5/OSC1/CLKIN// WDT disabled and can be enabled by SWDTEN bit off the WDTCON register// PWRT enabled// MCLRE off - pin 4 (RA3) will be digital inputvoid main(){int i;TRISB = 0x00;TRISC = 0x00;Lcd4_Init();//Lcd4_Set_Cursor(1,1);Lcd4_Write_String("electroSome");while(1){RC2 = 1;__delay_ms(500);RC2 = 0;__delay_ms(500);} // end while} // end mainLcd.cCode://LCD Functions Developed by electroSome#ifndef D0#define D4 RB4#define D5 RB5#define D6 RB6#define D7 RB7#endif//LCD 4 Bit Interfacing Functionsvoid Lcd4_Port(char a){if(a & 1)D4 = 1;elseD4 = 0;if(a & 2)D5 = 1;elseD5 = 0;if(a & 4)D6 = 1;elseD6 = 0;if(a & 8)D7 = 1;elseD7 = 0;}void Lcd4_Cmd(char a){RS = 0; // => RS = 0Lcd4_Port(a);EN = 1; // => E = 1__delay_ms(4);EN = 0; // => E = 0}Lcd4_Clear(){Lcd4_Cmd(0);Lcd4_Cmd(1);}void Lcd4_Set_Cursor(char a, char b){char temp,z,y;if(a == 1){temp = 0x80 + b;z = temp>>4;y = (0x80+b) & 0x0F;Lcd4_Cmd(z);Lcd4_Cmd(y);}else if(a == 2){temp = 0xC0 + b;z = temp>>4;y = (0xC0+b) & 0x0F;Lcd4_Cmd(z);Lcd4_Cmd(y);}}void Lcd4_Init(){Lcd4_Port(0x00);__delay_ms(20);Lcd4_Cmd(0x03);__delay_ms(5);Lcd4_Cmd(0x03);__delay_ms(11);Lcd4_Cmd(0x03);/////////////////////////////////////////////////////Lcd4_Cmd(0x02);Lcd4_Cmd(0x02);Lcd4_Cmd(0x08);Lcd4_Cmd(0x00);Lcd4_Cmd(0x0C);Lcd4_Cmd(0x00);Lcd4_Cmd(0x06);}void Lcd4_Write_Char(char a){char temp,y;temp = a&0x0F;y = a&0xF0;RS = 1; // => RS = 1Lcd4_Port(y>>4); //Data transferEN = 1;__delay_ms(5);EN = 0;Lcd4_Port(temp);EN = 1;__delay_ms(5);EN = 0;}void Lcd4_Write_String(char *a){int i;for(i=0;a!='\0';i++)Lcd4_Write_Char(a);}void Lcd4_Shift_Right(){Lcd4_Cmd(0x01);Lcd4_Cmd(0x0C);}void Lcd4_Shift_Left(){Lcd4_Cmd(0x01);Lcd4_Cmd(0x08);}//End LCD 4 Bit Interfacing FunctionsI can Build my code in MPLab IDE and I have this result in proteus:https://snag.gy/T5izKX.jpgThe message didn't appear on screen! I can't write anything on display.Another AttemptI added lcd.c file through right-button mouse in "Source File" -> "Add Files". When I tried Build in Mplab I receive this errors list:QuoteThanks

author: cv007
author_rank: Frequent Contributor
timestamp: August 18, 2018, 11:53:50 pm
content: QuoteYou already 'include' lcd.c in main.c which is not a good idea as you just found out. You effectively did a cut/paste of lcd.c into main.c where you include it, which means the lcd code knows about the pin defines.Adding lcd.c to the project means the compiler now also compiles lcd.c- keep in mind every c file is compiled as a separate 'unit' and knows nothing about other c files. In this case lcd.c knows nothing about the pin defines so you get the errors. You are also duplicating all lcd.c code, so will also get other errors even if you had the defines info available.You can probably find many tutorials on how to organize code. Or read other good code for examples.There are many ways to go about it, but one way to do it (simplified version)-//global.h#define _XTAL_FREQ 4000000//anyone needing to know the cpu freq can just include this file//any other global info here which doesn't really fit anywhere else//lcd.hvoid Lcd4_Init();void Lcd4_Write_String(char *a);//any other info others may need to use the lcd//but nothing more- no one needs to know how we put the data out//or what pins we use//lcd.c#include global.h //we need cpu freq for delays#include lcd.h#define RS RC0#define EN RC1//etc.//we can set pins as output in our init, no need for others to do this//lcd functions here//main.c#include "global.h"#include "lcd.h"//we can now init and use the lcdtwo c files- main.c and lcd.c will now be in our project, both compiled separately, both having only the info they need to compileyou will also have to know about include guards to prevent unintended multiple inclusion of header files- if using mplabx/xc8, when creating a new header in the ide it will create the guards for you

author: JPortici
author_rank: Super Contributor
timestamp: August 19, 2018, 09:18:13 am
content: global.h must also include "htc.h"htc.h is the include file which in turns include the library functions (in your case, delays) and the specific PIC include file with the register and pin definitions.the error was that you didn't include "htc.h" in lcd.c, too. One solution is a global include file, such as cv007 suggested, but the global include file needs to include htc.h!

author: Jasonbit
author_rank: Regular Contributor
timestamp: August 20, 2018, 01:54:23 pm
content: Thanks to yours reply. I create a need project following yours advice but I still message errors. What I do wrong?How should I search (keywords) to learn more about how organize de files and code in project Mplab?Note: The version I use is V8.83 of the Mplab. Please download entire project through this link:http://www.rapidshare.com.cn/1HIxBVSThanks