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.

Generate 1khz frequency using 8051

Status
Not open for further replies.

Pavithra89

Member level 2
Member level 2
Joined
Mar 18, 2013
Messages
53
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Visit site
Activity points
1,695
Hi all,
I've been asked to generate 1khz frequency using timer 0 of 8051. I've written the code but its not working!!!! :-(8-O Please help me where am I going wrong :D
This is my code
MOV TMOD,#02h
AGAIN:MOV TL0,#033h
MOV TH0,#0FEH
SETB TR0
BACK: JNB TF0,BACK
CLR TR0
CPL P3.2
SJMP AGAIN
end
Regards
 

Why you need two timers? Timers are independent.

See timers/counters section here **broken link removed**


Code ASM - [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
//ASSUME DUTY CYCLE 50%
//ASSUME 12MHZ CLOCK IS CONNECTED TO 
//MICRO-CONTROLLER
//USE TIMERS Timer 0 and Timer 1 used as 16-bit Timers
//CHECK OUT PUT IN P3.2 and P3.3
CODE:
 
 
ORG 0000H
MOV TMOD,#11H      ;TMOD = 0b00010001, M0 of Timer0 and Timer1 is set to 1
TMR0:SETB P3.2
LCALL DELAYT0
CLR P3.2
LCALL DELAYT0
SJMP TMR0
 
TMR1:SETB P3.3
LCALL DELAYT1
CLR P3.3
LCALL DELATT1
SJMP TMR1
 
DELAYT0:
MOV TH0,#0FEH
MOV TL0,#0CH
CLR TF0
SETB TR0
HERE1:JNB TF0,HERE1
RET
 
DELAYT1:
MOV TH1,#0FEH
MOV TL1,#0CH
CLR TF1
SETB TR1
HERE2:JNB TF1,HERE2
RET
 
 
END



If you just want two 1 KHz pulses on two output pins then you can use one timer to toggle the two output pins.

Do you want to use the timers as counters to do some counting?
 
Last edited:

Why you need two timers? Timers are independent.

See timers/counters section here **broken link removed**


Code ASM - [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
//ASSUME DUTY CYCLE 50%
//ASSUME 12MHZ CLOCK IS CONNECTED TO 
//MICRO-CONTROLLER
//USE TIMERS Timer 0 and Timer 1 used as 16-bit Timers
//CHECK OUT PUT IN P3.2 and P3.3
CODE:
 
 
ORG 0000H
MOV TMOD,#11H      ;TMOD = 0b00010001, M0 of Timer0 and Timer1 is set to 1
TMR0:SETB P3.2
LCALL DELAYT0
CLR P3.2
LCALL DELAYT0
SJMP TMR0
 
TMR1:SETB P3.3
LCALL DELAYT1
CLR P3.3
LCALL DELATT1
SJMP TMR1
 
DELAYT0:
MOV TH0,#0FEH
MOV TL0,#0CH
CLR TF0
SETB TR0
HERE1:JNB TF0,HERE1
RET
 
DELAYT1:
MOV TH1,#0FEH
MOV TL1,#0CH
CLR TF1
SETB TR1
HERE2:JNB TF1,HERE2
RET
 
 
END



If you just want two 1 KHz pulses on two output pins then you can use one timer to toggle the two output pins.

Do you want to use the timers as counters to do some counting?

Hi again,
Basically whats happening is timer1 s used for serial communication purpose . I'm left with timer0 so should use them to generate a 1khz frequency. My doubt was how to use both timers simultaneously. Google helped me in clearing this doubt. Anyways that code worked fine so no worries.. But have a doubt regarding those th0 and tl0 assignment... when i calculated i got tl0=033h and th0=0fe0h. But this value doesn't generate desired frequency but loading tl0=0ch generated d freq. :?::?::?::?::?:

Regards
 

Hi good morning :I
According to a text book, this is the way thy have shown how to calculate th0 n tl0 value---->>

Finding values to be loaded into the timer
Assuming that we know the amount of timer delay we need, the question is how to find the values needed for the TH, TL registers. To calculate the values to be loaded into the TL and TH registers look at Example 9-10 where we use crystal frequency of 11.0592 MHz for the 8051 system.
Assuming XTAL = 11.0592 MHz from Example 9-10 we can use the following steps for finding the TH, TL registers’ values.

1.
Divide the desired time delay by 1.085 us.
2.
Perform 65536 – n, where n is the decimal value we got in Step 1.
1.
Convert the result of Step 2 to hex, where yyxx is the initial hex value to be
loaded into the timer’s registers.
3.
Set TL = xx and TH = yy.

As a result I got those values :) N when I loaded same values i generated desired frequency but its not exact. Its .9KHZ not exactly 1khz :I Is this the right waay
 

Show a calculation. 0.9 KHz = 900 Hz. T = 1/900 = 1111.1111 us

1111.1111 us / 1.085 us = 1024.065529953917

65536 - 1024.065529953917 = 64511.93447004608 = approx 64512

hex(64512) = 0xFC00

TH = 0xFC
TL = 0x00

Do the calculation again and see.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top