Hi-Tech #define

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

Hi-Tech #define

Postby shahrul » Fri Mar 02, 2012 12:53 pm

Hi, I write code with Hi-Tech. I have main.c and LibraryHD44780.h
This is example the part of
main.c
CODE: SELECT_ALL_CODE
#include <htc.h>
#include "LibraryHD44780.h"
__CONFIG(FOSC_HS &         //External Crystal at High Speed
      WDTE_OFF &         //Disable Watchdog Timer
      PWRTE_ON  &         //Enable Power Up Timer
      BOREN_OFF &         //Disable Brown Out Reset
      MCLRE_ON &         //MCLR function is enabled
      LVP_OFF);            //Disable Low Voltage Programming

#define _XTAL_FREQ    20000000

void pic_init(void);

main()
{
pic_init();                  //initialize PIC
lcd_init();                  //initialize LCD
for(;;){
   lcd_goto(0,0);            //select first line
   lcd_string("   PIC IS FUN   ");   //display string
   lcd_goto(0,1);            //select second line
   lcd_string("                ");      //display string
}}

and LibraryHD44780.h
CODE: SELECT_ALL_CODE
#define LCD_RS    RD0
#define LCD_RW   RD1
#define LCD_EN    RD2
#define LCD_LIGHT   RD3
#define LCD_DATA    PORTD      //D7-D4
#define LCD_PULSE() ((LCD_EN=1),(LCD_EN=0))
#define _XTAL_FREQ    20000000

void lcd_init(void);
void lcd_write(unsigned char c);
void lcd_goto(unsigned char pos, unsigned char row);
void lcd_string(const char *s);
void lcd_number(unsigned int no, char base, char digit);
void lcd_scroll(int speed, unsigned char row, unsigned char pos, const char *s);

How I want remove the #define LCD port in the header file and insert into main fail, so that I no need to change on header file in the future?
User avatar
shahrul
Professional
 
Posts: 812
Joined: Sat May 16, 2009 9:54 pm
Location: Selangor

Re: Hi-Tech #define

Postby robosang » Fri Mar 02, 2012 10:46 pm

Normally, there is a hardware.h or IO.h or interface.h where it define all the label interface with device. And those file that uses that label include that header file. So you modify that header file only for all the library.
robosang
Expert
 
Posts: 1239
Joined: Wed Jun 10, 2009 5:37 pm

Re: Hi-Tech #define

Postby shahrul » Fri Mar 02, 2012 11:29 pm

robosang WROTE:Normally, there is a hardware.h or IO.h or interface.h where it define all the label interface with device. And those file that uses that label include that header file. So you modify that header file only for all the library.

If the I/O pin declare in other .h file, how the LCD header file want recognize that name? I mean, we declare that name and can be use in all .c and .h file. Because, I use #define, that is only can use in that file only.
User avatar
shahrul
Professional
 
Posts: 812
Joined: Sat May 16, 2009 9:54 pm
Location: Selangor

Re: Hi-Tech #define

Postby sich » Mon Mar 05, 2012 11:32 am

You can include "hardware.h" at the beginning of your LCD header file and any file that requires it.
~> How to ask QUESTIONS the SMART way in FORUM? <~
User avatar
sich
Moderator
 
Posts: 604
Joined: Tue Apr 21, 2009 2:15 pm

Re: Hi-Tech #define

Postby shahrul » Mon Mar 05, 2012 11:55 am

sich WROTE:You can include "hardware.h" at the beginning of your LCD header file and any file that requires it.

Ok, I get it. I create file hardware.h and I paste the port like this
CODE: SELECT_ALL_CODE
#define LCD_RS    RD0
#define LCD_RW   RD1
#define LCD_EN    RD2
#define LCD_LIGHT   RD3
#define LCD_DATA    PORTD      //D7-D4
#define LCD_PULSE() ((LCD_EN=1),(LCD_EN=0))

#define _XTAL_FREQ    20000000

There is no error.
User avatar
shahrul
Professional
 
Posts: 812
Joined: Sat May 16, 2009 9:54 pm
Location: Selangor

Re: Hi-Tech #define

Postby robosang » Mon Mar 05, 2012 2:51 pm

Yup, you got it right, thanks to sich for the help!
robosang
Expert
 
Posts: 1239
Joined: Wed Jun 10, 2009 5:37 pm


Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 4 guests