Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

8051 stepper motor interfacin

Status
Not open for further replies.
Hi kvtal,
I saw a header file : #include"lcd.h"
in your program !!!

Can you tell me is this a mistake or really any lcd.h file is there ????
and is this the correct way to load a header file by double quotes !?

I need to know how to use this header file with at89c51 applications.
 

no error in this code i made this header my own
i will post it here


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <reg52.h>
#define LCD P2
 
void delay_ms(unsigned int i)
{
unsigned int j;
    while(i-->0)
    {
        for(j=0;j<500;j++)
        {
            ;
        }
    }
}
 
 
void cmd_lcd(unsigned char c)
{          
unsigned char temp; 
temp=c>>4;  
LCD=temp<<4|0x02; //logical or with 0x02 since rs(rs=0) & en(en=1) are 
LCD=0;      //connected to p2.0 & p2.1 respectively 
//transmit low byte
LCD=c<<4|0x02;
LCD=0;
delay_ms(2); //delay 2 milliseconds
}
 
void init_lcd(void)
{                            
delay_ms(10); //delay 10 milliseconds
cmd_lcd(0x28); //4 bit initialize, 5x7 character font, 16x2 display
cmd_lcd(0x0c); //lcd on, cursor on
cmd_lcd(0x06); //right shift cursor automatically after each character is displayed
cmd_lcd(0x01); //clear lcd
}
 
 
void write_lcd(unsigned char c)
{
unsigned char temp;
temp=c>>4;
LCD=temp<<4|0x03; //logical or with 0x03 since rs(rs=1) & en(en=1) are 
LCD=0;
LCD=c<<4|0x03;
LCD=0;
delay_ms(2); 
}
 
void display_lcd(unsigned char *s)
{
while(*s)
write_lcd(*s++);
}
void integer_lcd(int n)
{
unsigned char c[6];
unsigned int i=0;
  if(n<0)
  {
    write_lcd('-');
    n=-n;
  }
  if(n==0)
    write_lcd('0');
  while(n>0)
  {
    c[i++]=(n%10)+48;
    n/=10;
  }
  while(i-->=1)
    write_lcd(c[i]);
}



save this file as lcd.h
in the 'c' folder then u can add like #include "lcd.h"
no error in that
 
Last edited by a moderator:
Thanks kvtal,

Excellent. I've never thought before to make header file by my own.
I've implement it and got a great result, it keeps the program clean and less complex as my program was too long.
But it doesn't make any difference in size !
I mean I've thought it may reduce the size, but the good news is it doesn't increase it also...
The size are exactly same whether I use header file or paste it in original program.

Many thanks.
 

Code:
#include <at89x52.h>
#define stepper P1

void main
{ 
  while(1)
     {
         stepper=0x01;
         delay(10);
         stepper=0x02;
         delay(10);
         stepper=0x04;
         delay(10);
         stepper=0x08;
         delay(10); 
        }
     }
 
Last edited by a moderator:

Hello!

save this file as lcd.h

You should avoid having implementation in a header file. This should be in lcd.c.
lcd.h should contain only the functions' prototypes.

Excellent. I've never thought before to make header file by my own.

You should get used to it. Here is a quick tutorial: for example write a function
that calculates a sum.

The h file (to be saved for instance in "adder.h"):
Code:
#ifndef _ADDER_H_
#define _ADDER_H_

uint32 add(uint32 x, uint32 y);

#endif

The .c file (to be saved for instance in "adder.c") :
Code:
#include "adder.h"

uint32 add(uint32 x, uint32 y) {
    uint32 result = x+y;
    return resukt;
}

Note that if you put everything in the h file, it will work.
But there is a big inconvenience to this way of proceeding:

Suppose that your main.c calls functions from lcd.h. If you modify main.c, then the
compiler will also recompile what is implemented in lcd.h. Therefore, if your program
gets large, you will recompile everything everytime, and this can really take time.
The program I'm working on right now includes about 50 modules (maybe 100)...
It takes about 3 minutes for a full compile. So if I have to wait 3 minutes at every
tiny modification, my productivity would really drop.

By separating into modules (lcd.c / lcd.h), the compiler will never recompile lcd.c
if you don't modify it.

Your LCD.h file should look like this:

Code:
#ifndef _LCD_H_
#define _LCD_H_

void delay_ms(unsigned int i);
void cmd_lcd(unsigned char c);
void init_lcd(void);
void write_lcd(unsigned char c);
void display_lcd(unsigned char *s);
void integer_lcd(int n);

#endif

Now some comments about this header:
1. You should choose function names wisely.
If you read your program next year, do you think you will understand the difference
between display_lcd and write_lcd? Why not naming them for instance:

display_string(char * s);
display_char(char c);
display_int(int n);

2. You should use #define statements. Well, this LCD is simple and its initialization is
pretty standard. But one of these days if you use a graphic LCD, you will have to tune
dozens of parameters, and it would be a good idea to get used to this.
Example :
#define MODE_4_5x7_16x2 0x28
#define LCD_CURSOR_ON 0x0C
#define CURSOR_AUTO_SHIFT 0x06
#define CLEAR_LCD 0x01

then in your code:

cmd_lcd(MODE_4_5x7_16x2);
cmd_lcd(LCD_CURSOR_ON);
cmd_lcd(CURSOR_AUTO_SHIFT);
cmd_lcd(CLEAR_LCD);

And you don't even need to write comments, the functionality of these commands is
self-explanatory.



Dora.
 
Your LCD.h file should look like this:

Code:
#ifndef _LCD_H_
#define _LCD_H_

void delay_ms(unsigned int i);
void cmd_lcd(unsigned char c);
void init_lcd(void);
void write_lcd(unsigned char c);
void display_lcd(unsigned char *s);
void integer_lcd(int n);

#endif


First of all, Thanks for the tutorial Dora.

Here you've gave an example how the header file should look like.
But here you've just defined the functions, where should we write function's body ?
and what is the reason for using #ifndef here ?
Thanks in advance.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top