Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

8051 simple counter

Status
Not open for further replies.

Jon1997

New Member
Hi I am currently using an AT89S52 and I am looking for a code (for keil uVision3) that does the following:
- reads my input voltage
- it must count up each time voltage is 3
- display the count in 4-bit binary on 4 led
 
Last edited:

phalanx

Member
What is your sensor? How is the signal "weak"? Do you realize that an 8051 is not an amplifier so you will most likely have to use some sort of external signal conditioning to bring the signal to a level the 8051 can register.
 

Jon1997

New Member
I would appreciate it if some one can provide a code or hex file to program my AT89s52.
I would like the chip to function as an up counter and have an output display in 4-bit binary code on 4 led.
 

mcs51mc

Member
Do you expect the Atmel to compare for the 3V or do you have an external comparator?
In case you have a comparator, connect it to port P3.2, configure that pin edge sensitive and in the ISR write something like
Code:
inc  MyCounter 
reti

First you talked LCD now 4 led's? What will it be??

In case of four led's
you need some code like
Code:
mov P1,MyCounter

In case of LCD
That will be a lot more code
I wrote a LCD library with subroutines to initialize the LCD, write text and data on the LCD, create user defined characters, ... ...
Search my posts and you may find it :)
 

Jon1997

New Member
thak you mcs51mc
I understand i must increment my count, but how do I make it inc each time input has changed?
 

Jon1997

New Member
is this correct?
MOV TMOD,#01100000B ;mode 2, counter 1

MOV TH1,#0

SETB P3.5 ;make T1 input port

AGAIN:SETB TR1 ;start

BACK: MOV A,TL1

MOV P2,A ;display in P2

JNB TF1,Back ;overflow

CLR TR1 ;stop

CLR TF1 ;make TF=0

SJMP AGAIN ;keep doing it
 
Last edited:

mcs51mc

Member
If you have a comparator to check the 3V then use pin P3.2 setup as edge sensitive. Each time the pin goes low, the ISR for that pin is triggered.
Code:
;-------------------------------------------------------------------------
CSEG AT RESET 
	jmp MainProg            ;Main loop 


CSEG AT EXTI0
        jmp intP32		;ISR for P3.2




;-------------------------------------------------------------------------
;ISR for pin P3.2
;
			RSEG intP32
			inc  MyCounter
			reti



;-------------------------------------------------------------------------
;Main loop
;
			RSEG MainProg
                	mov  sp,#Stack-1		;initialize stack
		
			mov  R0,#0FFh			;clear internal RAM
			clr  a
LBL_ClearRAM:		mov  @R0,a
			djnz R0,LBL_ClearRAM
			
			
			setb IT0			;Interrupt P3.2 edge sensitive
			setb EX0			;Enable interrupt pin INT0 P3.2
			setb EA    			;Enable all interrupts
			
LBL_Loop:		mov  P2,MyCounter		;Value to port 2


			jmp  LBL_Loop

The instructions setb IT0 and setb EX0 makes from P3.2 an edge sensitive input pin.

If you don't have a comparator you need an ADC to digitalize the signal from your sensor and read it in the µP.

Check the attached app note for the AD22100 temperature sensor from Analog Devices. They do what you want to do but with another sensor...
 

Attachments

  • AD22100_AN-395.pdf
    116.3 KB · Views: 339
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top