Mantra counting machine using 8051 micro-controller

Published Dec 14, 2020
 5 hours to build
 Intermediate

This project is a prototype of mantra counting machine using 8051 micro-controller.

display image

Components Used

LCD16x2 Display
LCD16x2 Display
1
8051 AT89c51
8051 AT89c51
1
Mini Push Button Switch - 5-6mm
Mini Push Button Switch - 5-6mm
1
Description

WORKING 

This project is about Mantra counting machine using 8051. In this project, user will be able to count up to 5000 mantras. Whenever user pressed the respective button after chanting the mantra, counter get incremented. This is how mantras get counted.Here I have used 16x2 alphanumeric lcd display.

Lcd display is used to display the count. Data pins of the lcd display are connected to port 1 and control pins (RS, R/W, E) are connected to p3.7,p3.6,p3.5 respectively.

Three buttons(start, stop ,reset) are used which are connected to p2.0,p2.6,p3.4 respectively.


Working of the buttons are as follows:


START:

Whenever user presses this button after chanting a mantra counter get incremented by one.Thus mantras get counted.

This button needs to be pressed after chanting a mantra otherwise counter will not get incremented and lcd will display the present value of the count.


STOP:

When this button is pressed, counter stops counting and LCD will displayed the total count of mantras.

If the user pressed the 'START' button after pressing this button counter will start counting from the last value of the count.

RESET:

When this button is pressed counter gets reset and it will started counting from the first count.

 

PROGRAM OF THE PROJECT:

LCD header file:

#include<reg51.h>

sbit RS = P3^7;

sbit RW = P3^6;

sbit EN = P3^5;

void delay(int t);

void lcd_init(void);

void lcd_command(char c);

void lcd_data(char d);

void str(char a[]);

void lcd_init(void)     // function for initialization of lcd

{

lcd_command(0x38);     //8 bit,2 line,5x8 dots 

lcd_command(0x01);     // clear display

lcd_command(0x0f);  //display on cursor blinking

lcd_command(0x06); // Entry mode

lcd_command(0x0c); // turn on the display and turn off the cursor

lcd_command(0x80); // force cursor to beginning of first row

}

void lcd_command(char c) // function to execute command

{

P1=c;

RS=0;      // command resister is selected

RW=0;

EN=1;                 

delay(5);

EN=0;

delay(5);

}

void lcd_data(char d)  // function to display data

{

P1=d;

RS=1;    // data register is selected

RW=0;

EN=1;

delay(5);

EN=0;

delay(5);

}

void delay(int t)         // delay function

{

int j;

for(j=0;j<t*1275;j++);

}

void str(char a[]) // function to display string

{

int j;

for(j=0;a[j]!='\0';j++)

{

lcd_data(a[j]);

}

}

一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一

Main Program:

#include<reg51.h>

#include<string.h>

#include"LCD.h"

 

sbit start=P2^0;         // START button

sbit stop=P2^6;        // STOP button

sbit reset=P3^4;      // RESET button

unsigned int q,r,s,t,i=0;

void print(unsigned int p);

void main()

{

lcd_init();             // initialization of lcd

while(1)

{

if((start==0)&&(stop==1)&&(reset==1))  // start button is pressed

{

i++;

if(i<=5000)

{

while(start==0)      

{

print(i);

}

start=1;

}

}

if((reset==0)&&(start==1)&&(stop==1)) // reset button is pressed

{

i=0;

}

if((stop==0)&&(start==1)&&(reset==1)) // stop button is pressed

{

lcd_command(0x80);

str("Total count:");

print(i);

}

}

}

void print(unsigned int p)  // function to display number up to 5000

{

if(p<10)

{

q=p+48;

lcd_data(q);

lcd_command(0x01);

}

else if((p>=10)&&(p<=99))

{

q=p/10;

q=q+48;

r=p%10;

r=r+48;

lcd_data(q);

lcd_data(r);

lcd_command(0x01);

}

else if((p>=100)&&(p<=999))

{

q=p/100;

q=q+48;

r=((p%100)/10);

r=r+48;

s=p%10;

s=s+48;

lcd_data(q);

lcd_data(r);

lcd_data(s);

lcd_command(0x01);

}

else if((p>=1000)&&(p<=5000))

{

q=p/1000;

q=q+48;

r=((p%1000)/100);

r=r+48;

t=(((p%1000)%100)/10);

t=t+48;

s=p%10;

s=s+48;

lcd_data(q);

lcd_data(r);

lcd_data(t);

lcd_data(s);

lcd_command(0x01);

}

 

 

PROGRAM DESCRIPTION:

In LCD header file, first I have initialized the lcd by providing the various lcd commands required for initialization.After that lcd command function and lcd data function are declared.Delay function is used to provide sufficient delay. String function is used to print the string.

In lcd to pass the commands, command register need to be selected and enable pin must be provided with high to low pulse to process that commands. Command register is selected by providing logic 0 to RS(Register select) pin of lcd.

To pass the data to the lcd, data register need to be selected and enable pin must be provided with high to low pulse to process that data.

Data register is selected by providing logic 1 to RS(Register select) pin of lcd.


BASIC PROCEDURE TO DISPLAY NUMBER ON AN LCD:

If we want to display any number which is greater than 9 on an lcd then decompose(break) the number and add 48 to each separated digit of that number and send the resulting value one by one to the lcd.

Here I have taken four digit number as an example. Suppose I want to display 1352 on lcd.

 

First basic step is initialization of lcd. Then the next step is decompose(break) the number.

Procedure to decompose(break) 4 digit number:

1.First divide the number by 1000.So we get first number.

 Ex.1352/1000=1;

2.Divide the number by 1000 and take the remainder of the division .Divide that remainder by 100 so that we can get second number.

Ex.(1352%1000)/100= 3;

Note: [ ( /  ) divide operator returns quotient of the division and

(%) modulus operator returns remainder of the division.]

3.Divide the number by 1000 and take the remainder of the division. Divide that remainder by 100.Again take the remainder of the division and divide it by 10.So that we can get third digit of the number.

Ex.((1352%1000)%100)/10= 5;

4.Divide the number by 10 and take the remainder of the division so that we can get last digit of the number.

Ex. 1352%10=2;


There are two ways to display characters on an LCD:

1.One is sending character by writing it into single inverted commas 

Ex.   a = '0';

2.The other way is sending the ASCII value of the character.

Ex.  a = 48; (ASCII value of '0')

Here for displaying the number I have used ASCII value of the characters. ASCII value of '0' to '9' are ranging from 48 to 57 respectively.

Now after decomposing the number, next step is to add 48 to each separated digit so that we can able to get ASCII value of that digit.

Ex.

1+48=49;( ASCII value of 1)

3+48=51;(ASCII value of 3)

5+48=53;(ASCII value of 5)

2+48=50;(ASCII value of 2)

Send those values to lcd display one by one. So that we will be able to display complete number 1352.This is the basic procedure to display number on lcd.

By using the above method I have printed numbers up to 5000 on an lcd.

 

Video:

 

Codes

Downloads

mantra counting machine Download
Comments
Ad