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.

Frequency calculation in 89c51 controller

Status
Not open for further replies.

sujeethaa

Junior Member level 3
Joined
Nov 18, 2012
Messages
26
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
bangalore
Activity points
1,464
hi all

i m trying to get input pulse in 89c51 through external interrupt.that pulse is unknown !!
then i could i calculate the frequency of that pulse!!
 

hi thank u !!!

but actually i want to measure the frequency of the pulse coming from the heart beat sensor!!
that ll be given through the external interrupt!!! then i cannot use this method !!!

what my idea is frequency = no. of cycles /sec ...
so i want to measure no. of cycles in that input pulse in second !!

could u help me how to go ahead with this!!
 

You want to measure heartbeat pulses, (e.g=70-80) but in second??? Not in minute????
 

hi sujeetha. the best approach will be to use the timers in the 89C51 as a counter. for example u can make use of two timers for this purpose. the first timer should act as a gating device to the second timer just like the conventional frequency counter. it can be made to enable the second timer at an interval of say one second. while the second timer which will be configured as a counter is enabled, it will count the number of pulses present on its pin, when disabled via the first timer which is a free runing gater, the number of counts that it has made will be copied to a register which will act as a buffer for further processing....hope this helps

sequel...
 

hi sujeetha. the best approach will be to use the timers in the 89C51 as a counter. for example u can make use of two timers for this purpose. the first timer should act as a gating device to the second timer just like the conventional frequency counter. it can be made to enable the second timer at an interval of say one second. while the second timer which will be configured as a counter is enabled, it will count the number of pulses present on its pin, when disabled via the first timer which is a free runing gater, the number of counts that it has made will be copied to a register which will act as a buffer for further processing....hope this helps

sequel...

hi thank u so much !!
i got the point!!!

one doubt is that if i m using 11.859Mhz crystal the maximum count i count is 460,800 events !
am i correct i.e i can count only this much H-L transitions in 1 sec !!
is that correct
 

yes because the MCU will still of two machine cycle...which is 11.0592MHz/24
 

yeah... you are welcome.....

sequel




if i helped you just click on helped me!...:p
 
You want to measure heartbeat pulses, (e.g=70-80) but in second??? Not in minute????

hi
no i should measure the heart beat rate i.e in minute

- - - Updated - - -

hi sujeetha. the best approach will be to use the timers in the 89C51 as a counter. for example u can make use of two timers for this purpose. the first timer should act as a gating device to the second timer just like the conventional frequency counter. it can be made to enable the second timer at an interval of say one second. while the second timer which will be configured as a counter is enabled, it will count the number of pulses present on its pin, when disabled via the first timer which is a free runing gater, the number of counts that it has made will be copied to a register which will act as a buffer for further processing....hope this helps

sequel...

hi
then i should connect the input signal into the timer pin!!!
but i want to use it as an external interrupt because i have to measure body temperature also so when someone touches the heart beat sensor it produces an interrupt signal and i need to measure count the pulse!!!
 

i dont think am getting you clear.....is it that the interrupt will be used to initiate the count?... or the count can be made regardless of the interrupt?
....please clearify
 

i dont think am getting you clear.....is it that the interrupt will be used to initiate the count?... or the count can be made regardless of the interrupt?
....please clearify


when interrupt occurs i need to count the pulse.

the input signal is given the external interrupt .
when any signal is produced at the external interrupt i need to count pulse.

i think this code ll count the pulse when interrupt occurs!!

void extrint (void) interrupt 0
{
count++;
}


because interrupt occurs when H-L transition occurs so i can able to count no of pulse i think!

am i correct!
 
Last edited:

now the input signal will also be the sensor to the heart beat?...

- - - Updated - - -

if so then all you need to the will be to short the external interrupt pin to the timer/counter so that once the interrupt is invoked the gater will be enabled, while the counter should also be cleared so that new count can commence. at this point you also have to disable the external interrupt so that you dont end up hanging in the interrupt routine....

1) if interrupt is true then stop it for now
2) clear your counter
3) enable the gater for one second timing and also the counter using the TMOD register
4) once a second is complete, harvest your count
5) clear your counter and reset your interrupt so that you can refresh your system
....hope this helps

sequel....
 

void extrint (void) interrupt 0 // external Interrupt to detect the heart pulse
{
bt=tick; // number of ticks are picked
tick=0; // reset for next counting
}
void timer0 (void) interrupt 1 using 1 // Timer 0 for one second time
{
TH0 = 0xdc; //The value is taken for Ssc/100 at crystal 11.0592MHz
sec100++; // It is incremented every Ssc/100 at crystal 11.0592MHz
tick++; // This variable counts the time period of incoming pulse in Sec/100
if(tick>=3500){tick=0;} // tick are limited to less trhan 255 for valid calculation
if(sec100 >=100) // 1 sec = sec100 * 100
{
sec++;
sec100=0;
}
}




this is the code used to count the pulse !!!

i dnt wat he has done actully in that calculation part
 

@ sujeethaa am not that flexible in C- coding ...if you dont mind i will post in assembly language
 

ok check out this quick code. here i used timer0 and timer2....the ext0 should be tied to timer0 so that the falling edge of the pulse will trigger an interrupt and also form the basis for counting....feel free to ask your questions...sequel

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; RUNS @ 12MHZ
; TARGET CHIP 89S52 OR 89C52
; BY VINCENT "SEQUEL"
; DATE 19-11-2012
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;
T2CON DATA 0C8H
RCAP2L DATA 0CAH
RCAP2H DATA 0CBH
;;;;;;;;;;;;;;;;;;;;;;;;;

OUTPUT DATA 080H ; PORT0 SERVES AS THE OUTPUT FOR SEVEN SEGMENT DISPLAY A-G
SCAN DATA 0A0H ;

SEG_1 BIT SCAN.0 ; USED FOR MUX LSB
SEG_2 BIT SCAN.3 ; USED FOR MUX
SEG_3 BIT SCAN.6 ; USED FOR MUX MSB

COUNTER DATA 000H
COUNTER2 DATA 001H
STORE_1 DATA 002H
STORE_2 DATA 003H
STORE_3 DATA 004H
DELAY_REG DATA 005H
DELAY_REG1 DATA 006H
DELAY_REG2 DATA 007H
RESTORE DATA 008H
SAMPLER DATA 009H

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;END OF VARIABLE DECLARATION;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


ORG 000H
AJMP BOOTSTRAP

ORG 003H
AJMP GET_PULSES

ORG 02BH
AJMP SAMPLE


ORG 02FH




BOOTSTRAP:

MOV SP,#020H
MOV OUTPUT,#000
MOV COUNTER,#000
MOV COUNTER2,#000
MOV STORE_1,#000
MOV STORE_2,#000
MOV STORE_3,#000
MOV SCAN,#000
MOV RCAP2L,#0F0H ; THE RCAP2 IS A 16BIT REGISTER AND IT IS CONFIGURED IN AUTORELOAD MODE BY LOADING IT WITH THE VALUE OF 55536
MOV RCAP2H,#0D8H ; THIS MEANS THAT IT WILL COUNT UPWARDS TO 65535...THAT IS TO SAY AN EXTRA 10000
; THUS IT WILL DIVIDE THE SYSTEM FREQUENCY DOWN TO 100HZ...THAT IS 12MH/12 = 1000000
; 1000000/10000 = 100
MOV SAMPLER,#100 ; THIS REGISTER IS FURTHER USED TO BRING THE 100HZ DOWN TO A SECOND
MOV TMOD,#005H ; CONFIGURE TIMER0 FOR COUNTER MODE
MOV IE,#10100001B ; INTERRUPT VECTORS FOR EXT0 AND FOR TIMER2 OVERFLOW
AJMP MAIN



MAIN: MOV A,STORE_1
ACALL DISP_MASK
MOV OUTPUT,A
SETB SEG_1
ACALL DELAY_150_12
CLR SEG_1
;;;;;;;;;;;;;;;;;;;;;
MOV A,STORE_2
ACALL DISP_MASK
MOV OUTPUT,A
SETB SEG_2
ACALL DELAY_150_12
CLR SEG_2
;;;;;;;;;;;;;;;;;;;;;
MOV A,STORE_3
ACALL DISP_MASK
MOV OUTPUT,A
SETB SEG_3
ACALL DELAY_150_12
CLR SEG_3
AJMP MAIN

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;DISP_MASKLAY MASK SUBROUTINE FOR COMMON ANODE ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

DISP_MASK: INC A
MOVC A,@A+PC
RET ; SWAP WITH THESE FOR COMMON CATHODE
DB 03FH ; Digit 0 mask 0C0H
DB 006H ; Digit 1 mask 0F9H
DB 05BH ; Digit 2 mask 0A4H
DB 04FH ; Digit 3 mask 0B0H
DB 066H ; Digit 4 mask 099H
DB 06DH ; Digit 5 mask 092H
DB 07DH ; Digit 6 mask 082H
DB 007H ; Digit 7 mask 0F8H
DB 07FH ; Digit 8 mask 080H
DB 06FH ; Digit 9 mask 090H

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;START PULSE COUNTING;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

GET_PULSES: MOV IE,#10100000B ; DISABLE EXT0 INT.
MOV T2CON,#004
SETB TR0
RETI
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;SAMPLE SUBROUTINE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

SAMPLE: MOV T2CON,#004
DJNZ SAMPLER,RETII
CLR TR0
MOV T2CON,#000
MOV SAMPLER,#100
MOV COUNTER,TL0
MOV COUNTER2,TH0
MOV TL0,#000
MOV TH0,#000
MOV A,COUNTER ; YOUR COUNT IS COPIED TO THIS REGISTER "LOW BYTE" THATS 8BIT...YOU CAN USE THE TH0 FOR THE HIGH BYTE. THUS GIVING U 16BIT
BINARY_2BCD: MOV B,#010 ; THIS ROUTINE CONVERTS YOUR BINARY DATA TO DECIMAL
DIV AB
MOV STORE_1,B
MOV B,#010
DIV AB
MOV STORE_2,B
MOV STORE_3,A
MOV IE,#10100001B ; ENAABLE EXT0 INT....NOTE THAT YOU CAN INTRODUCE A DELAY HERE INCASE YOUR DISPLAY FLICKERS
RETII: RETI

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;DELAY SUBROUTINE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

DELAY_150_12: MOV DELAY_REG1,#14
MOV DELAY_REG2,#168
_150_12: DJNZ DELAY_REG2,_150_12
DJNZ DELAY_REG1,_150_12
RET








END
 
ok check out this quick code. here i used timer0 and timer2....the ext0 should be tied to timer0 so that the falling edge of the pulse will trigger an interrupt and also form the basis for counting....feel free to ask your questions...sequel

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; RUNS @ 12MHZ
; TARGET CHIP 89S52 OR 89C52
; BY VINCENT "SEQUEL"
; DATE 19-11-2012
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;
T2CON DATA 0C8H
RCAP2L DATA 0CAH
RCAP2H DATA 0CBH
;;;;;;;;;;;;;;;;;;;;;;;;;

OUTPUT DATA 080H ; PORT0 SERVES AS THE OUTPUT FOR SEVEN SEGMENT DISPLAY A-G
SCAN DATA 0A0H ;

SEG_1 BIT SCAN.0 ; USED FOR MUX LSB
SEG_2 BIT SCAN.3 ; USED FOR MUX
SEG_3 BIT SCAN.6 ; USED FOR MUX MSB

COUNTER DATA 000H
COUNTER2 DATA 001H
STORE_1 DATA 002H
STORE_2 DATA 003H
STORE_3 DATA 004H
DELAY_REG DATA 005H
DELAY_REG1 DATA 006H
DELAY_REG2 DATA 007H
RESTORE DATA 008H
SAMPLER DATA 009H

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;END OF VARIABLE DECLARATION;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


ORG 000H
AJMP BOOTSTRAP

ORG 003H
AJMP GET_PULSES

ORG 02BH
AJMP SAMPLE


ORG 02FH




BOOTSTRAP:

MOV SP,#020H
MOV OUTPUT,#000
MOV COUNTER,#000
MOV COUNTER2,#000
MOV STORE_1,#000
MOV STORE_2,#000
MOV STORE_3,#000
MOV SCAN,#000
MOV RCAP2L,#0F0H ; THE RCAP2 IS A 16BIT REGISTER AND IT IS CONFIGURED IN AUTORELOAD MODE BY LOADING IT WITH THE VALUE OF 55536
MOV RCAP2H,#0D8H ; THIS MEANS THAT IT WILL COUNT UPWARDS TO 65535...THAT IS TO SAY AN EXTRA 10000
; THUS IT WILL DIVIDE THE SYSTEM FREQUENCY DOWN TO 100HZ...THAT IS 12MH/12 = 1000000
; 1000000/10000 = 100
MOV SAMPLER,#100 ; THIS REGISTER IS FURTHER USED TO BRING THE 100HZ DOWN TO A SECOND
MOV TMOD,#005H ; CONFIGURE TIMER0 FOR COUNTER MODE
MOV IE,#10100001B ; INTERRUPT VECTORS FOR EXT0 AND FOR TIMER2 OVERFLOW
AJMP MAIN



MAIN: MOV A,STORE_1
ACALL DISP_MASK
MOV OUTPUT,A
SETB SEG_1
ACALL DELAY_150_12
CLR SEG_1
;;;;;;;;;;;;;;;;;;;;;
MOV A,STORE_2
ACALL DISP_MASK
MOV OUTPUT,A
SETB SEG_2
ACALL DELAY_150_12
CLR SEG_2
;;;;;;;;;;;;;;;;;;;;;
MOV A,STORE_3
ACALL DISP_MASK
MOV OUTPUT,A
SETB SEG_3
ACALL DELAY_150_12
CLR SEG_3
AJMP MAIN

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;DISP_MASKLAY MASK SUBROUTINE FOR COMMON ANODE ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

DISP_MASK: INC A
MOVC A,@A+PC
RET ; SWAP WITH THESE FOR COMMON CATHODE
DB 03FH ; Digit 0 mask 0C0H
DB 006H ; Digit 1 mask 0F9H
DB 05BH ; Digit 2 mask 0A4H
DB 04FH ; Digit 3 mask 0B0H
DB 066H ; Digit 4 mask 099H
DB 06DH ; Digit 5 mask 092H
DB 07DH ; Digit 6 mask 082H
DB 007H ; Digit 7 mask 0F8H
DB 07FH ; Digit 8 mask 080H
DB 06FH ; Digit 9 mask 090H

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;START PULSE COUNTING;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

GET_PULSES: MOV IE,#10100000B ; DISABLE EXT0 INT.
MOV T2CON,#004
SETB TR0
RETI
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;SAMPLE SUBROUTINE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

SAMPLE: MOV T2CON,#004
DJNZ SAMPLER,RETII
CLR TR0
MOV T2CON,#000
MOV SAMPLER,#100
MOV COUNTER,TL0
MOV COUNTER2,TH0
MOV TL0,#000
MOV TH0,#000
MOV A,COUNTER ; YOUR COUNT IS COPIED TO THIS REGISTER "LOW BYTE" THATS 8BIT...YOU CAN USE THE TH0 FOR THE HIGH BYTE. THUS GIVING U 16BIT
BINARY_2BCD: MOV B,#010 ; THIS ROUTINE CONVERTS YOUR BINARY DATA TO DECIMAL
DIV AB
MOV STORE_1,B
MOV B,#010
DIV AB
MOV STORE_2,B
MOV STORE_3,A
MOV IE,#10100001B ; ENAABLE EXT0 INT....NOTE THAT YOU CAN INTRODUCE A DELAY HERE INCASE YOUR DISPLAY FLICKERS
RETII: RETI

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;DELAY SUBROUTINE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

DELAY_150_12: MOV DELAY_REG1,#14
MOV DELAY_REG2,#168
_150_12: DJNZ DELAY_REG2,_150_12
DJNZ DELAY_REG1,_150_12
RET








END

Thank u sequel

i just used timer as a counter and i got the output!!!

heartly thanks for your kind effort
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top