# Counting Negative Pulses with PIC16F877 Timer0

Status
Not open for further replies.

#### alibataineh

##### Newbie level 2
Hello everyone,
Hope you're doing well .
I study power engineering and this is my third year. To be able to graduate I have to pass micro-controller course, even though it's not related to my field of study.
Anyway, I was searching online and I'm really thrilled to find this site.
I need your help regarding programming TMR0 as counter. Does anyone know how to write a program to count the number of negative pulses received on one of the PIC 6F877 I/O pins ?

This code actually counts the positive pulses :

main:

; 1. configure tmr0
Code:
movlw b’10111000’
movwf option_reg
; 2. Set up ports
Code:
movlw 0x00 ;
Movwf trisd
movlw B’00011111’ ;
Movwf trisa ;
; 3. Mask 2nd digit of TMR0
Code:
loop:movf trm0,w ;
Andlw 0x0f;
call segment
movwf portd ;
goto loop ;

What about the negative pulses ?

You'd really save my life if you know the code
Peace

Last edited by a moderator:

##### Super Moderator
Staff member
Can't help much with the code for your pic.

However in case it must not be exposed to negative volt levels...

you will need to convert the negative pulses to a positive polarity.

A simple circuit below can deliver a positive pulse at the end of the negative pulse.

Screenshot:

The simulation shows the pot adjusted to 32k ohms.

The upper resistance must be sufficient ohms so that the output never goes above or below a safe volt level for your device.

The 5.1V zener is optional although a good idea.

V
Points: 2

#### alibataineh

##### Newbie level 2
Although, My teacher insists that there is a way to solve the circuit with a code without converting the negative pulses .
But really thank you so much

#### betwixt

##### Super Moderator
Staff member
As stated, if you put a negative voltage on the PIC input it will be damaged. The silicon structure of the PIC is not rated to withstand negative voltages beyond what it's static protection diodes can handle and that isn't very much.

Does your teacher really mean the pulses are of negative voltage, going below ground potential, or do they mean negative going edges? A negative going edge is one where a high logic level drops to a low level, the PIC does have a feature for detecting that and using it to control TMR0, otherwise you need extra components.

Brian.

#### wp100

Hi,

Think you need to clarify what you mean by 'Negative pulses'.

Brad has shown a circuit for converting pulses from a negative voltage source which, if fed directly without the circuit Brad shows, will damage the Pic.

I suspect you actually mean the Negative transistion of a pulse, the fall from high to low ?

Your current code increases timer0 on the Positve transision of the pulses, if you see the datasheet for the Option Register you will see what you need to do to make it count on the Negative transisions

Status
Not open for further replies.