Rising edge counter

General discussion on mikroBasic.
Post Reply
Author
Message
Drako
Posts: 108
Joined: 05 Sep 2006 20:41

Rising edge counter

#1 Post by Drako » 25 Aug 2007 12:28

Hello,
Im trying to make a couter with a 16F628A to count the rising edge pulses in portb, and show the count in a LDC.
Im trying to make a kind of data logger to capture two lines of data and clock.
So my first problem is how to detect the rising edge and then be able to count them.

Thanks in advance.

xor
Posts: 5465
Joined: 18 May 2005 00:59
Location: NYC
Contact:

#2 Post by xor » 25 Aug 2007 14:33

There are 2 methods to catch an edge, the RB0/INT pin, and the CCP module coupled with Timer1. Both of these PIC functions have options to catch either the rising or falling edge of a signal. You can find both referenced in the datasheet.
[color=darkred][b]xor[/b][/color]
[url=http://circuit-ed.com]CircuitED -[/url]

dhouston
Posts: 64
Joined: 29 Apr 2006 13:44

#3 Post by dhouston » 25 Aug 2007 16:45

Also, TMR0 and TMR1 can be used in Counter mode (search datasheet for TMR0 & TMR1) but these require the inputs on specific pins.

If the pins you are using have Interrupt On Change, you can set up your own counter variable and increment it on each interrupt.

Drako
Posts: 108
Joined: 05 Sep 2006 20:41

#4 Post by Drako » 25 Aug 2007 18:38

I took your advice xor, and looked for some info about RB0/INT.
It is the basic program Im doing with pic 16F628 A and EP3 , but.. I think when I enable interrupts the LCD gets a bit crazy.
I use RB0 to capture the pulses. Also I dont know how to disable the interrupts after the capture is done.

Code: Select all

program Test

sub procedure Interrupt
' other lines

    clearbit(INTCON,INTF)
end sub

main:

OPTION_REG=0
INTCON=%11010000

 portb=0
 trisb=%00000011           'all output, except RB0/INT and RB1 as inputs

while true
' the program
' showing LCD 4bit mode in port B


wend

end.
So, Im thinking of use other pic and other port to connect the display using the custom LCD library... at the moment I can't find other solution....

Thanks for your help

trust issues
Posts: 231
Joined: 14 Nov 2004 19:43

#5 Post by trust issues » 25 Aug 2007 23:15

You can disable your interrupts at any point by either assigning GIE or INT0IE equal to 0. You can find these within the INTCON register.

xor
Posts: 5465
Joined: 18 May 2005 00:59
Location: NYC
Contact:

#6 Post by xor » 25 Aug 2007 23:35

If you wish to disable the interrupt you can simply not reset the INTF interrupt flag, or clear INTE interrupt enable, or clear GIE global interrupt enable. Any one or more of these will disable it.

Example of setting up RB0 Edge Interrupt:

Code: Select all

dim counter as longint

sub procedure interrupt
   If INTCON.INTF = 1 Then  ' check type of interrupt
      inc(counter)          ' increment pulse counter
   End If
   INTCON.INTF = 0          ' clear interrupt flag to catch next pulse
end sub

sub procedure INT_Setup
   counter = 0              ' clear pulse counter
   OPTION_REG = 0
   INTCON = 0
   OPTION_REG.RBPU = 0      ' PortB pullups active
   OPTION_REG.INTEDG = 1    ' interrupt on RB0 rising edge
   INTCON.INTE = 1          ' enable RB0 interrupt
   INTCON.INTF = 0          ' clear interrupt flag ... enables jump to ISR
   INTCON.GIE = 1           ' enable global interrupts
end sub

main:
   PORTB = 0
   TRISB = 1                ' RB0 as input
   INT_Setup                ' setup registers
   
   While true
      ' any running code
   Wend
end.
[color=darkred][b]xor[/b][/color]
[url=http://circuit-ed.com]CircuitED -[/url]

Drako
Posts: 108
Joined: 05 Sep 2006 20:41

#7 Post by Drako » 29 Aug 2007 21:37

Thank you, now it works perfect!!!

abdo2007451aa
Posts: 2
Joined: 11 Mar 2016 14:26

Re: Rising edge counter

#8 Post by abdo2007451aa » 11 Mar 2016 14:41

you can make positive edge by this code


sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections

char x1[7];
char x;
void main() {
trisd=255;
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR);
lcd_cmd(_lcd_cursor_off);
lcd_out(1,1,"my counter");
while(1)
{
if(PORTD.B1==1)
{ x+=1;
inttostr(x,x1) ;
// LCD_CMD( _LCD_MOVE_CURSOR_RIGHT);
ltrim(x1);
lcd_out(2,1,x1);
while(1){
if(portd.b1==0) break;
}
}
}
//Lcd_Cmd(_LCD_CURSOR_OFF);
//Lcd_Cmd(_LCD_TURN_OFF); // Clear display
//Lcd_Cmd(_LCD_UNDERLINE_ON); // Cursor off
//Lcd_Out(1,1,"h"); // Write text in first row
Delay_ms(2000);
//Lcd_Cmd(_LCD_CLEAR); // Clear display


}

Post Reply

Return to “mikroBasic General”