Libstock prefers package manager

Package Manager

We strongly encourage users to use Package manager for sharing their code on Libstock website, because it boosts your efficiency and leaves the end user with no room for error. [more info]

Project Request

Return
Shaheen Muhammed
shaheen544

posted on 2014/04/01 12:17:44 PM CEST

Timers (Real time clock)

Need code for a counter to run between two inputs.. details can be found below

i have two pairs IR sensors A and B respectively.
When sensor A detects its input and sensor B detects its input within 20s after sensorA detects then light a LED.
Please im struggling so please help.
Thanks in advance

USER Comments

Checker
checker

posted on 2014/04/17 10:15:07 AM CEST

here is a simple solution as far as I can gather from your post, assuming setup for MikroC PIC18F45K22 32 MHz HS-PLL so you will need to modify it to suit your needs. Using 50ms timer1.

note: to keep proper formatting I replaced all spaces with '.' just copy into text editor and do 'search and replace' all the .'s

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
sbit LED at LATA2_bit;

unsigned short cnt, trigg, i;

//Timer1
//Prescaler 1:8; TMR1 Preload = 15536; Actual Interrupt Time : 50 ms
void InitTimer1(){
..T1CON.........= 0x31;
..TMR1IF_bit....= 0;
..TMR1H.........= 0x3C;
..TMR1L.........= 0xB0;
..TMR1IE_bit....= 1;
..INTCON........= 0xC0;
}

void Interrupt() {
..if (TMR1IF_bit) {
....TMR1IF_bit = 0;
....TMR1H......= 0x3C;
....TMR1L......= 0xB0;
....cnt++;
..}
}

void main() {
..ANSELA = 0;
..TRISA = 0x03;

..InitTimer1();
..LED = 0;

..do {
....if (Button(&PORTA, 0, 2, 1)) {.............// only used buttons as example, can substitute with any input
......cnt = 0;
......trigg = 1;
....}

....if (cnt >= 401) {
.......trigg = 0;
.....}

....if (trigg == 1 && Button(&PORTA, 1, 2, 1)) {
.......LED = 1;
.......delay_ms(1000);.......................// remove this and next line if you only want it to stay lit
.......LED = 0;
.......trigg = 0;
.......continue;...................................// remove if you do not want it setup as a one-shot
....}
..} while(1);
}

  • Rating:
0
No Abuse Reported

Do you want to report abuse comment ID: 1121 in "Need code for a counter to run between two inputs.. details can be found below" request.