Author Topic: Put Display 16*2 with PIC16F  (Read 1397 times)

0 Members and 1 Guest are viewing this topic.

Offline JasonbitTopic starter

  • Regular Contributor
  • *
  • Posts: 105
  • Country: br
Put Display 16*2 with PIC16F
« on: August 18, 2018, 03:52:06 pm »
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.c
Code: [Select]
#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 input

void 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 main

Lcd.c
Code: [Select]
//LCD Functions Developed by electroSome

#ifndef D0
#define D4 RB4
#define D5 RB5
#define D6 RB6
#define D7 RB7
#endif

//LCD 4 Bit Interfacing Functions

void Lcd4_Port(char a)
{
if(a & 1)
D4 = 1;
else
D4 = 0;

if(a & 2)
D5 = 1;
else
D5 = 0;

if(a & 4)
D6 = 1;
else
D6 = 0;

if(a & 8)
D7 = 1;
else
D7 = 0;
}
void Lcd4_Cmd(char a)
{
RS = 0; // => RS = 0
Lcd4_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 = 1
Lcd4_Port(y>>4); //Data transfer
EN = 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[i]!='\0';i++)
Lcd4_Write_Char(a[i]);
}

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 Functions

I can Build my code in MPLab IDE and I have this result in proteus: https://snag.gy/T5izKX.jpg
The message didn't appear on screen! I can't write anything on display.

Another Attempt
I added lcd.c file through right-button mouse in "Source File" -> "Add Files". When I tried Build in Mplab I receive this errors list:

Quote
Error [192] path\lcd.c; 15.1 undefined identifier "RB4" Error [192] path\lcd.c; 20.1 undefined identifier "RB5" Error [192] path\lcd.c; 25.1 undefined identifier "RB6" Error [192] path\lcd.c; 30.1 undefined identifier "RB7" Error [192] path\lcd.c; 36.1 undefined identifier "RS" Error [192] path\lcd.c; 38.1 undefined identifier "EN" Warning [361] path\lcd.c; 39.1 function declared implicit int Error [192] path\lcd.c; 94.1 undefined identifier "RS" Error [192] path\lcd.c; 96.1 undefined identifier "EN"

Thanks
« Last Edit: August 18, 2018, 03:55:24 pm by Jasonbit »
 

Offline cv007

  • Frequent Contributor
  • **
  • Posts: 826
Re: Put Display 16*2 with PIC16F
« Reply #1 on: August 18, 2018, 11:53:50 pm »
Quote
I added lcd.c file
You 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.h
void 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 lcd


two c files- main.c and lcd.c will now be in our project, both compiled separately, both having only the info they need to compile

you 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

 

Offline JPortici

  • Super Contributor
  • ***
  • Posts: 3461
  • Country: it
Re: Put Display 16*2 with PIC16F
« Reply #2 on: August 19, 2018, 09:18:13 am »
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!
 

Offline JasonbitTopic starter

  • Regular Contributor
  • *
  • Posts: 105
  • Country: br
Re: Put Display 16*2 with PIC16F
« Reply #3 on: August 20, 2018, 01:54:23 pm »
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/1HIxBVS

Thanks
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf