Continue to Site

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.

cont pulse C18

Status
Not open for further replies.

Mitt

New Member
Sorry, my language maybe wrong becuase I'm Thai'student.
I use pic18f4620. I want to count pulse form input(motion sensor).I want count pulse by count pulse chang 1 to 0 becuase I use pull up at sensor.
Give me app note or example note for guild me please. I use mplab c18.
I begin practice use controller by mplab c18.
Thank you
 
Just read the input at regular intervals (let's say in the main loop of your program or during timed interrupt) and store the value in a variable.
When you re-read the value, if the new value is 0 and the old is 1, then increment counter.

Code:
...
if (new_value==0) && (old_value==1) {
    counter++ ;
}
old_value = new_value ;
...
 
Just read the input at regular intervals (let's say in the main loop of your program or during timed interrupt) and store the value in a variable.
When you re-read the value, if the new value is 0 and the old is 1, then increment counter.

Code:
...
if (new_value==0) && (old_value==1) {
    counter++ ;
}
old_value = new_value ;
...
Thank you for your answer.
Assume, I want use port RC0 connect to sensor for count pulse.
I want to read pulse for keep in new_value. What do I have to write the code or make any functions? I try to read MPLAB_C18_Users_Guide but my language have a problem,so show me the example code.
thank you
 
#include <p18f4620.h>
#include <timers.h>
#include <portb.h>


#pragma romdata CONFIG1H = 0x300001
const rom unsigned char config1H = 0b00000110; // HSPLL oscillator

#pragma romdata CONFIG2L = 0x300002
const rom unsigned char config2L = 0b00011111; // Brown-out Reset Enabled in hardware @ 2.0V, PWRTEN disabled

#pragma romdata CONFIG2H = 0x300003
const rom unsigned char config2H = 0b00010010; // HW WD disabled, 1:512 prescaler

#pragma romdata CONFIG3H = 0x300005
const rom unsigned char config3H = 0b10000010; // PORTB digital on RESET

#pragma romdata CONFIG4L = 0x300006
const rom unsigned char config4L = 0b10000001; // DEBUG disabled,
// XINST disabled
// LVP disabled
// STVREN enabled







// prototype section
void int0_isr (void); //Interrupts sevice rounting for External interrupts 0//
void EnableLowInterrupts (void);

/*
* For PIC18xxxx devices, the low interrupt vector is found at 000000018h.
* Change the default code section to the absolute code section named
* low_vector located at address 0x18.
*/
#pragma code low_vector=0x18
void low_interrupt (void)
{
/*
* Inline assembly that will jump to the ISR.
*/

if(INTCONbits.INT0IF == 1)
{
_asm GOTO int0_isr _endasm
}

}

/*
* Returns the compiler to the default code section.
*/
#pragma code

/*
* Specifies the function timer_isr as a low-priority interrupt service
* routine. This is required in order for the compiler to generate a
* RETFIE instruction instead of a RETURN instruction for the timer_isr
* function.
*/
#pragma interruptlow int0_isr

/*
* Define the timer_isr function. Notice that it does not take any
* parameters, and does not return anything (as required by ISRs).
*/
void
int0_isr (void)
{
INTCONbits.INT0IF = 0;

}

int counter;
void main (void)
{

RCONbits.IPEN=1;
INTCONbits.GIEL=1;

OpenRB0INT( PORTB_CHANGE_INT_ON & FALLING_EDGE_INT & PORTB_PULLUPS_ON);

while (1)
{

}
}

void EnableLowInterrupts (void)
{

} At interupts, I want cout pulse keep at Counter.How should I create the code?
 
#include <p18f4620.h>
#include <timers.h>
#include <portb.h>
int counter;


#pragma romdata CONFIG1H = 0x300001
const rom unsigned char config1H = 0b00000110; // HSPLL oscillator

#pragma romdata CONFIG2L = 0x300002
const rom unsigned char config2L = 0b00011111; // Brown-out Reset Enabled in hardware @ 2.0V, PWRTEN disabled

#pragma romdata CONFIG2H = 0x300003
const rom unsigned char config2H = 0b00010010; // HW WD disabled, 1:512 prescaler

#pragma romdata CONFIG3H = 0x300005
const rom unsigned char config3H = 0b10000010; // PORTB digital on RESET

#pragma romdata CONFIG4L = 0x300006
const rom unsigned char config4L = 0b10000001; // DEBUG disabled,
// XINST disabled
// LVP disabled
// STVREN enabled







// prototype section
void int0_isr (void); //Interrupts sevice rounting for External interrupts 0//
void EnableLowInterrupts (void);

/*
* For PIC18xxxx devices, the low interrupt vector is found at 000000018h.
* Change the default code section to the absolute code section named
* low_vector located at address 0x18.
*/
#pragma code low_vector=0x18
void low_interrupt (void)
{
/*
* Inline assembly that will jump to the ISR.
*/

if(INTCONbits.INT0IF == 1)
{
_asm GOTO int0_isr _endasm
}

}

/*
* Returns the compiler to the default code section.
*/
#pragma code

/*
* Specifies the function timer_isr as a low-priority interrupt service
* routine. This is required in order for the compiler to generate a
* RETFIE instruction instead of a RETURN instruction for the timer_isr
* function.
*/
#pragma interruptlow int0_isr

/*
* Define the timer_isr function. Notice that it does not take any
* parameters, and does not return anything (as required by ISRs).
*/

void int0_isr () // Interrupts sevrvice routine for Interrupts RB0(FALLING_EDGE_INT)//
{
INTCONbits.INT0IF = 0;

counter++;
}


void main (void)
{

RCONbits.IPEN=1;
INTCONbits.GIEL=1;

OpenRB0INT( PORTB_CHANGE_INT_ON & FALLING_EDGE_INT & PORTB_PULLUPS_ON);

while (1)
{

}
}

void EnableLowInterrupts (void)
{

}
I want to simulate simulate this program but i have a problem about simulation process.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top