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.

[51] Programming in C language for 8051 Micro Controller

Status
Not open for further replies.
You can do like this.

Code:
unsigned int myInt = 32;

unsigned int myFunction1(unsigned int myLoclInt) {
	return myLocalInt * 2;
}

unsigned int myFunction2(unsigned int myLoclInt) {
	return myLocalInt * 2;
}

void main() {

	
	while(1) {
		myInt = myFunction1(myFunction2(myInt));
	}

}

After returning from myFunction(), myInt will hold 128 as the value that is before second loop.
 

Hi, I solved problem with displaying number per second.
Program checking numbers in string which are dividing with 3 then print number on display
But now I have problem with displaying numbers with four string...
He always recognizing only one string in interrupt rutin. Probably I need something between strings in interrupt rutine ..
But I dont know what..
So program check only one string, another doesnt exist. He must check all string but I dont know how

NEED HELP

Code is
Code:
#include <reg51.h>
#include <math.h>
typedef unsigned char byte;

byte a[16]={1,22,31,4,5,6,7,8,23,3,10,30,73,40,55,93};
byte b[16]={3,14,7,13,17,19,97,1,12,128,127,124,123,122,33,21};
byte c[16]={9,15,4,13,41,49,57,18,74,3,39,59,112,100,91};
byte d[16]={33,58,68,66,21,19,97,1,14,222,56,12,3,9,15,11};

sbit switcher=P0^0;
byte i=0;
byte counter;


byte xdata display _at_ 0x8001;
void Inic(void) {
	EA=1;
	ET0=1;
	TMOD=0x01;
	TH0=0xC3;
	TL0=0x50;
	TR0=1;
	counter=0;
	i=0;

}

void timer0 (void) interrupt 1 using 2 {
	
	TH0=0xC3;
	TL0=0x50;
	
if((++counter==0)) {	 counter=20;
	if(switcher) {
	
	if(!(a[i]%3) ) 
		display = a[i];

		i++;
		if(i==16) i=0;
		  if(!(b[i]%3) ) 
			display = b[i];

		i++;
		if(i==16) i=0;
			if(!(c[i]%3) ) 
			display = c[i];

		i++;
		if(i==16) i=0;
			if(!(d[i]%3) ) 
			display = d[i];

		i++;
		if(i==16) i=0;
	}}
	TR0=0;
}

void main(void) {
	Inic();
	while(1){TR0=1;}
	}
 

Please find attach Photo and tell me code is right or wrong?
IMG-20151229-WA0003.jpg

- - - Updated - - -

if i will write like as below then it is right?

Code:
void timer0 (void) interrupt 1
		{
                      timer0_int_call();
                 }

         void timer0_int_call()
                {
                }
 

Hi jignesh,
Depends on your coding style & requirement you can pass it in different ways...

If it is an array (or) variable (or) any data , you can pass address of that from your
calling function and collect like a pointer in the called function

for ex:

return_type sub_routine1(parameters)
{
/* some implementation*/

sub_routine(&data); //passing the address of the data

/* some implementation */

}


return_type sub_routine2(data_type *ptr)
{
/* we can perform the operations on it */
return ptr;
}
 

Re: Programming in C language for 8051 Micro Controller

please explain with some code example with main function

I want some brief example...
 

You are asking for code examples without telling what you want exactly to achieve. The purpose of your latest code in post #23 is apparently different from previous tries, but it comes without any explanation.
 

I have give my code in below section.

In this please verify section of Interrupt and please tell me is it ok?

Code:
#include <AT89X55.H>
#include <MATH.H>
//#include <lcd.h>

sfr lcd_data_pin=0xA0;  // data port P2
sbit rs=P3^7; // Register select pin
sbit rw=P3^6; // Read write pin
sbit en=P3^5; // Enable pin
sbit power_hz=P1^0;  // Frequency Count

	unsigned long c,d,frequency,rpm_count,rpm;
	unsigned char d1,d2,d3,d4,d5,pulse_ov,pulse_count,lo_count;

	void delay(unsigned int msec)    //delay function
		{
			int i,j;
			for(i=0;i<msec;i++)
			for(j=0;j<1275;j++);
		}
	void lcd_command(unsigned char comm) // function to send command to LCD
		{
			lcd_data_pin=comm;
			en=1;
			rs=0;
			rw=0;
			delay(1);
			en=0;
		}
	void lcd_data(unsigned char disp)    // function to send data on LCD
		{
			lcd_data_pin=disp;
			en=1;
			rs=1;
			rw=0;
			delay(1);
			en=0;
		}

	lcd_dataa(unsigned char *disp)    // function to send string to LCD
		{
			int x;
			for(x=0;disp[x]!=0;x++)
				{
					lcd_data(disp[x]);	

				}
		}

	void lcd_ini()     //Function to inisialize the LCD
		{
			lcd_command(0x38);   
			delay(5);
			lcd_command(0x0C);       
			delay(5);

		}
	
void timer0 (void) interrupt 1
		{
		
			TF0 = 0x00;
			pulse_count = pulse_count++;

			if (pulse_count == 1)
				{
				P1 = 0x00;
				delay(100);
				
				}

			else
				{
				P1 = 0xff;
				delay(100);
				
			return;
				}		
			
		}

	void ISR_timer1 (void) interrupt 3
		{
			TF1 = 0;
			pulse_ov = ++pulse_ov;
		}

	void rpm_start()
		{
			pulse_ov = 0x00;
			pulse_count = 0x00;
			TH1 = 0x00;
			TL1 = 0x00;
			ET1 = 1;
			ET0 = 1;
			EA  = 1;
			TR0 = 1;
		}

	void main()
		{
			P1 = 0xff;
			lcd_ini();
			TMOD = 0x16;
			T2CON = 0x01;	

			IT0 = 1;
			IE0 = 1;
			EX0 = 1;
			EA = 1;			
		
			rpm_start();
		
		}
 

What is the meaning of using 2 in below code?

is it compulsory to write down this in interrupt routine?

Code:
void timer0 (void) interrupt 1 using 2  {
  if (++interruptcnt == 4000)  {    /* count to 4000 */
    second++;                       /* second counter    */
    interruptcnt = 0;               /* clear int counter */
  }
 

Not strictly compulsory but a straightforward way to implement a one second tic counter.
 

Why should we have to write?

- - - Updated - - -

Can u please tell me in below program my interrupt subroutine is right?

if it is wrong then please tell me where i have occur fault?

Code:
#include <AT89X55.H>
#include <MATH.H>

void timer0 (void) interrupt 1
		{
		
		P1 = 0x00;
		delay(100);
		
		P1 = 0xff;
		delay(100);
				
		return;
		}		

	void rpm_start()
		{
			pulse_ov = 0x00;
			pulse_count = 0x00;
			TH1 = 0x00;
			TL1 = 0x00;
			ET1 = 1;
			ET0 = 1;
			EA  = 1;
			TR0 = 1;
		}

	void main()
		{
			P1 = 0xff;
			
			TMOD = 0x16;
			T2CON = 0x01;	

			IT0 = 1;
			IE0 = 1;
			EX0 = 1;
			EA = 1;			
		
			rpm_start();
		
		}
 

It's generally unwanted to have delays (or other time consuming actions) inside an interrupt function. What do you want to achieve?
 

Actually in my project, when i will get pulse on timer 0 at that time i will get timer 0's Interrupt and at that time i will start timer 1 as counter mode and in other pulse i will stop counter.
but i will not get proper result so i have to check if my routine of interrupt is working or not?
so i wrote this small program to check my concept of interrupt.
if u have any material related to interrupt routine or sample program then provide so i can learn more deeply...
 

Are you trying to make a frequency counter ?

You have to use external interrupt pin like INT0 and when it gets triggered you have to start timer in timer mode. When next pulse is detected you have to stop the timer and calculate the frequency of pulse by using the TMRx value.
 

something like that, i don't want to measure line frequency but i am going to measure motor speed.

My project is already run using assembly language but i want to move on C platform so that i try to write.

but i want some detail knowledge of interrupt, what is the basic concept to write program for interrupt using C language.
 

Subroutine or function 1 returns some data and you pass it as argument for second subroutine or function.
 

Case 1: In first code when i disable EA and see port 1 on proteus then it works properly as logic.
Case 2: In second code when i just enable EA nothing to do other and see port 1 on proteus then it blinks.
so my question is what is the logic behind this occur.
Code:
void timer0 (void) interrupt 1
		{
		
		P1 = 0x00;
		delay(100);
		
		P1 = 0xff;
		delay(100);
				
		return;
		}		


	void main()
		{
			P1 = 0xff;
			lcd_ini();
			TMOD = 0x16;
			T2CON = 0x01;	

			IT0 = 1;
			IE0 = 1;
			EX0 = 1;
		//	EA = 1;			
		}
Code:
void timer0 (void) interrupt 1
		{
		
		P1 = 0x00;
		delay(100);
		
		P1 = 0xff;
		delay(100);
				
		return;
		}		


	void main()
		{
			P1 = 0xff;
			lcd_ini();
			TMOD = 0x16;
			T2CON = 0x01;	

			IT0 = 1;
			IE0 = 1;
			EX0 = 1;
			EA = 1;			
		}
 

Hi jignesh,

Basically interrupt is a signal which is caused by the external hardware device (or) internal software. The necessity of interrupt is should do some time critical task i.e at some other memory location.

When ever interrupt is invoked program counter is switched from present task address location to the interrupted task location & perform interrupted task operation.

Now Let you explain about what do you want to achieve, then i can explain about it.
 

Timer 0's external interrupt can't run in Following Edge Trigger mode?

Can i call some subroutine from interrupt's routine?
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top