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.

microcontroller code error in C

Status
Not open for further replies.

mentoz

Junior Member level 1
Joined
Nov 25, 2012
Messages
15
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,366
i little bit confuse with my problem, because the microcontroller is went wrong but in proteus everything is alright. is there someone check for this C code? especially inside main body and while 1, because the time cannot be decrease less than three seconds. let me know if ther is something I should revised.
Code:
#include<stdio.h>
#include<reg52.h>										   

#define port P3
#define dataport P2
#define key P1

//sfr Port0=0x80;

sbit rs=port^7;
sbit rw=port^5;
sbit en=port^6;

sbit col1=key^4;
sbit col2=key^5;
sbit col3=key^6;
sbit row1=key^0;
sbit row2=key^1;
sbit row3=key^2;
sbit row4=key^3;
sbit out=port^0;
sbit pir1=port^2;
sbit pir2=port^3;
sbit lampsig=P0^3;

static unsigned int time;
static unsigned int count;
static unsigned timer;
//static unsigned int bcounter=1;
unsigned char digit[3]={0,0,0};

int i,k;

void delay(unsigned int msec)
{
	int i,j;
	for(i=0;i<msec;i++);
	for(j=0;j<1275;j++);
}

void lcd_cmd(unsigned char item) // Function to send command to LCD
{
dataport = item;
rs= 0;
rw=0;
en=1;
delay(1);
en=0;
return;
}

void lcd_data(unsigned char item) // Function to send data to LCD
{
dataport = item;
rs= 1;
rw=0;
en=1;
delay(1);
en=0;
return;
}

void lcd_data_string(unsigned char *str) // Function to send data to string
{
while(*str)
{
  lcd_data(*str++);
  delay(1);
}
return;
}

void lcd(unsigned char str[10])
{
lcd_cmd(0x38);
lcd_cmd(0x0e);
lcd_data_string(str);
}

void lcd_data_int(int time_val)			// Function to send three digit number
{
	int int_amt;
	int_amt = time_val/100;
	lcd_data(int_amt+48);
	time_val = time_val%100;
	int_amt = time_val/10;
	lcd_data(int_amt+48);
	int_amt = time_val%10;	 
	lcd_data(int_amt+48);
}

void digit_set(char num)			
{
	if(count==3)
	{
		lcd_cmd(0x01);
		lcd_cmd(0x83);
		lcd_data_string("INPUT DONE!");
		lcd_cmd(0xc0);
		lcd_data_int(time);
		lcd_cmd(0xc3);
		lcd_data_string("second");
		lcd_cmd(0x08);
	}
	else
	{
		if(count==0)
			{
			digit[2]=num;
			//digit[1]=gak ada
			//digit[0]=gak ada
			}
		else
			if(count==1)
			{
				//digit[2]=digit[2];
				digit[1]=num;
				//digit[0]=gak ada 
			}
			else
				if(count==2)
				{
					//digit[2]=digit[2];
					//digit[1]=digit[1];
					digit[0]=num;
				}
		lcd_data((num+48));
	}
}

void check_col1()		 // Col1 check
{
	row1=row2=row3=row4=1;
	row1=0;
	if(col1==0)				 	// Key 1
	{  
		delay(10);
		digit_set(1);
	}
	row1=1;
	row2=0;
	if(col1==0)					// Key 4
	{
		delay(10);
		digit_set(4);
	}
	row2=1;
	row3=0;
	if(col1==0)			  		// Key 7
	{ 
		delay(10);
		digit_set(7);
	}
	row3=1;
	row4=0;
	if(col1==0)					// Key *
	{ 
		delay(10);
		if(count==4)
		{
			digit_set(10);
		}
		else
		{
			count=4;
			digit_set(10);
		}
	}
	row4=1;
}

void check_col2()     //Col2 check
{
	row1=row2=row3=row4=1;
	row1=0;
	if(col2==0)					// Key 2
	{
		delay(10);
		digit_set(2);
	}
	row1=1;
	row2=0;
	if(col2==0)			 		// Key 5
	{
		delay(10);
		digit_set(5);
	}
	row2=1;
	row3=0;
	if(col2==0)	       				// Key 8
	{
		delay(10);
		digit_set(8);
	}
	row3=1;
	row4=0;
	if(col2==0)		 			// Key 0
	{
		delay(10);
		digit_set(0);
	}
	row4=1;
}

void check_col3()	 // Col3 check
{
	row1=row2=row3=row4=1;
	row1=0;
	if(col3==0)					// Key 3
	{
		delay(10);
		digit_set(3);
	}
	row1=1;
	row2=0;
	if(col3==0)			   		// Key 6
	{
		delay(10);
		digit_set(6);
	}
	row2=1;
	row3=0;
	if(col3==0)				  	// Key 9
	{
		delay(10);
		digit_set(9);
	}
	row3=1;
	row4=0;
	if(col3==0)					// Key #
	{
		delay(1);
	}
	row4=1;
}

void keypad()
{ 
  row1=row2=row3=row4=0;
  while(col1==1 && col2==1 && col3==1);
  for(i=0;i<3;i++)
  {
   delay(100);
   lcd_cmd(0x0F);
   lcd_cmd(0xc6+i);
   row1=row2=row3=row4=0;
   while(col1==1 && col2==1 && col3==1);
   row1=row2=row3=row4=0;
	if(col1==0)
		check_col1();
	else
		if(col2==0)
			check_col2();
		else
			if(col3==0)
				check_col3();
	delay(50);
}
}
void main()
{
out=0;
pir1=0;
pir2=0;
lampsig=1;

TMOD=0x01;
EA=0;
TH0=29300/256;TL0=29300%256;   //19456(4c00)
ET0=1;
EX0=0;
IT0=0;
PX0=0;
TF0=0;	
EA=1;

count=0;			//initial count value

 lcd_cmd(0x38);
 delay(2); 
 lcd_cmd(0x01);  // Clear Screen
 lcd_cmd(0x84);
 lcd_cmd(0x1c);
 lcd("WELCOME");
 delay(20000);

	lcd_cmd(0x01);
	lcd_cmd(0x80);
	lcd("Enter time interval(second):");
	lcd_cmd(0x80);
	delay(10);
	lcd("nter time interval(second):");
	lcd_cmd(0x80);
	delay(10);
	lcd("ter time interval(second):");
	lcd_cmd(0x80);
	delay(10);
	lcd("er time interval(second):");
	lcd_cmd(0x80);
	delay(10);
	lcd("r time interval(second):");
	lcd_cmd(0x80);
	delay(10);
	lcd(" time interval(second):");
	lcd_cmd(0x80);
	delay(10);
	lcd("time interval(second):");
	lcd_cmd(0x80);
	delay(10);
	lcd("ime interval(second):");
	lcd_cmd(0x80);
	delay(10);
	lcd("me interval(second):");
	lcd_cmd(0x80);
	delay(10);
	lcd("e interval(second):");
	lcd_cmd(0x80);
	delay(10);
	lcd(" interval(second):");
	lcd_cmd(0x80);
	delay(10);
	lcd("interval(second):");
	lcd_cmd(0x80);
	delay(10);
	lcd("nterval(second):");
	lcd_cmd(0x80);
	delay(10);
while(count<4)
{					
		
		delay(50);
		keypad();
		time=(digit[0]+(digit[1]*10)+(digit[2]*100));
		count++;		
		delay(1);
}

lcd_cmd(0x01);
	lcd_cmd(0x82);
	lcd("DEVICE READY");
	delay(50000);
lcd_cmd(0x01);
	lcd_cmd(0x82);
	lcd("TIME DISPLAY");
	
while(1)
{
   lcd_cmd(0xc6);
   lcd_data_int(time);

if(pir1==1 && pir2==1)
  out=1;
if(pir1==1 && pir2==0)
  out=1;
if(pir1==0 && pir2==1)
  out=1;

if(pir1||pir2==1)
{ 
TR0=1;
}			

while(lampsig==0 && (pir1||pir2)==1)
{
time=(digit[0]+(digit[1]*10)+(digit[2]*100));
}
						 
if(time==0)
	{
	out=0;
	TR0=0;
	}		
}
}

void isr_t0()interrupt 1 using 2
{
 TR0=0;
 TF0=0;
 TH0=29300/256;TL0=29300%256;
 if(++timer==20)
 {--time;//bcounter^=1;
  timer=0;
   }
  //if(++timer1==5){(bcounter^=1);timer1=0;}
 TR0=1;
 }

thank you
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top