Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic 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.

Register Log in

8051 c programmin Codes For Delay generating

Status
Not open for further replies.

Hareesh Kumar

Junior Member level 2
Joined
May 17, 2011
Messages
24
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,450
//*program for generate toogle condition in between 950 microsec and 50 microseconds in two pins*//
#include<reg52.h>

void T0M2Delay(void);
P1=0xff;
sbit mybit=P1^7;

void main(void)
{

while(1)
{
P1=0xff; //*making port p1 high*//
T0M2Delay(); //keep it for 1 millisec//
mybit=~mybit; //make 7th bit low//
T0M2Delay2();//keep it for 50 us//

}
}
//*for generating 950us delay*//
void T0M1Delay(void)
{
/Replace x with timer no.
//Configure and Enable timer.
//Timer Clk=12/12*1 = 1.0 MHz
SCON = 0x50; //8-bit,rx enable
TLx = 0x0; //Lower Byte
THx = 0x0; //Upper Byte
TRx = 1; //Run timer
}
//* for generating 50ms delay
void T0M2Delay2(void)
{
//Replace x with timer no.
//Configure and Enable timer.
//Timer Clk=12/12*1 = 1.0 MHz
SCON = 0x50; //8-bit,rx enable
TLx = 0x0; //Lower Byte
THx = 0x0; //Upper Byte
TRx = 1; //Run timer
}


I write this program for generate delay in 950 microseconds(hig pulse) and generate 50 Us(low pulse) using AT89c52. is this correct?
 

Ram Prasadh

Member level 2
Joined
Feb 16, 2011
Messages
51
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Activity points
1,725
No .. It is wrong. The two functions you wrote there for the two time delays will produce only one delay for certain amount of time which depends on your oscillator frequency. what is your oscillator frequency?
 

Hareesh Kumar

Junior Member level 2
Joined
May 17, 2011
Messages
24
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,450
Xtal Frequency is 12 MHz

---------- Post added at 17:48 ---------- Previous post was at 17:41 ----------

Can you send me the correct code??
 

Ram Prasadh

Member level 2
Joined
Feb 16, 2011
Messages
51
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Activity points
1,725
You are running a 8 bit timer according to the code . so it means that the timer will run upto FF(ie 255) ant a roll over(I hope you know what is roll over!) ie 256 cycles. At 12 Mhz , it creates a time delay of 1.085uS . so 1.085*256=277uS.Youtr timer will run for this time only. Moreover you don need to use TL in 8 bit mode. Please refer the book mazidi . Its a perfect book for begineers.

---------- Post added at 17:55 ---------- Previous post was at 17:53 ----------

You do it.. Making mistake and learning will make you stronger in that concept. I will not write the code .
 

Hareesh Kumar

Junior Member level 2
Joined
May 17, 2011
Messages
24
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,450
thanks..i wil try my best to write it....Thank u for spend time for me..
 

KerimF

Advanced Member level 4
Joined
May 17, 2011
Messages
1,461
Helped
374
Reputation
756
Reaction score
370
Trophy points
1,363
Location
Syria
Activity points
12,314
I use to write in assembly so my reply may not help you completely.

If you like that one timer does the job:

(1) First, the output pin is initialized as 1 for example
(2) The timer THx and TLx are initialized as -950 (since the cycle time is 1us)
Note: -950 = 65536 - 950 (which is 2 bytes, high and low)
Note: for precise timing an offset should be added to -950 (to decrease its value, see below)
(3) ETx and TRx are finally set to 1

(4) In the interrupt subroutine of TimerX:
(4a) At its entry, the output pin is toggled
(4b) TRx=0, to halt the counter
(4c) The output pin is checked
(4c1) if 0, THx and TLx are set to -50 (also 2 bytes + its offset, see below)
(4c2) if 1, THx and TLx are set to -950 (+ its offset, see below)
(4d) TRx=1, to reactivate the counter
(4e) RETI

The time offset to be added is the time after TRx=0 up to TRx=1 in each path which may differ a bit for case 0 and case 1.

Hope it helps in a way.

Kerim

Edited: I forgot to say that the timerX mode should also be set in TMOD; M1x=0 and M0x=1 (Mode 1)
 
Last edited:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top