Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to start counter when sensor is activate

Status
Not open for further replies.

Daljeet12

Member level 4
Member level 4
Joined
Jun 16, 2018
Messages
78
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
648
I need help in program I want a counter to count 10 pulses when sensor is activate and then flash LED

Code:
// PIC16F877A Configuration Bit Settings

// CONFIG
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF        // Watchdog Timer Enable bit (WDT disable)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF        // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

#define _XTAL_FREQ 20000000 //Specify the XTAL crystal FREQ

#include<xc.h>

void main(void) 
{
    PORTD = 0b00000000;
    
    TRISC0 = 1;    //PORTC R0  pins are used as Input encoder
    TRISD4 = 0;    //PORTD R4  pins are used as output.LED
    TRISB1 = 1;    //PORTC R0  pins are used as Input. sensor
    
     //Loading starting value of counter 65486
    TMR1H = 0xFF ;    // High byte FF
    TMR1L = 0xF6 ;    // Low Byte F6
    
    //Set T1CON Register 
    
    TMR1ON  = 0; // Stops Timer1
    TMR1CS =  1; // Oscillator is enabled
    T1SYNC =  0; // Synchronize external clock input
    T1OSCEN = 0; // Oscillator is shut-off
    T1CKPS0 = 0; // 1:8 prescale value
    T1CKPS1 = 0;
    
    TMR1IF = 0 ;  // Clear interrupt flag
    
    TMR1ON = 1; //Start Timer1
       
    while(1)
    {
       if(TMR1IF ==1)
      { 

          RD4 = 1;  // LED ON
          __delay_ms(500); // 
          RD4 = 0;  // LED OFF
         
          TMR1IF=0; // Clear timer interrupt flag
          
          TMR1ON = 0; //Stop Timer1
          
          //reload counter
          TMR1H = 0xFF ;    // High byte FF
          TMR1L = 0xF6 ;    // Low Byte F6
      }  
    } 
}

How to start counter when sensor is activate ?
 

Hi,

Where is the part that's suppose to increment the counter?

Something like: countr = countr + 1;
 

Not necessary. It's a hardware timer/counter.

How to start a counter if the sensor is high else stop counter and start the counter if the sensor is high again
 

You can configure Timer1 in Counter mode. Pulse input has to be connected to T1CK1 pin.

What is your Pulse input frequency or its frequency range?

Are you using Rotary Encoder switch or encoder connected to motor?

Give full details about the project so that suitable code can be advised.

You need to enable PEIE and GIE bits of INTCON register to enable global interrupts to get TMR1IF set.
 
Last edited:

What is your Pulse input frequency or its frequency range?
I don't know the frequency of the pulses, I just want to count them when they come in.

You need to enable PEIE and GIE bits of INTCON register to enable global interrupts to get TMR1IF set.
I don't want interrupt I want to set timer flag when counter overflow timer flag will set so I would check if flag is set, triggered LED
 
Last edited:

I have counter that goes from 65426 to 65536 and later it would overflow when it overflow one timer flag would be set and when flag set triggered LED

Let's make it simple

check the sensor

if it goes high start counter

if it goes low stop counter

if it goes high again start counter


Can you tell me How would you start counter when sensor is high in my program ?
 

I want to start counter when sensor triggress. I was expecting program should supposed to but it doesn't work as I expect

can someone point me what could be wrong ?

Code:
// PIC16F877A Configuration Bit Settings

// CONFIG
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF        // Watchdog Timer Enable bit (WDT disable)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF        // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

#define _XTAL_FREQ 20000000 //Specify the XTAL crystal FREQ

#include <xc.h>

void main(void) 
{
    PORTD = 0b00000000;
    
    TRISC0 = 1;    //PORTC R0  pins are used as Input encoder
    TRISD4 = 0;    //PORTD R4  pins are used as output.LED
    TRISB1 = 1;    //PORTC R0  pins are used as Input. sensor
    
     //Loading starting value of counter 65486
    TMR1H = 0xFF ;    // High byte FF
    TMR1L = 0xF6 ;    // Low Byte F6
    
    //Set T1CON Register 
    
    TMR1ON  = 0; // Stops Timer1
    TMR1CS =  1; // Oscillator is enabled
    T1SYNC =  0; // Synchronize external clock input
    T1OSCEN = 0; // Oscillator is shut-off
    T1CKPS0 = 0; // 1:8 prescale value
    T1CKPS1 = 0;
    
    TMR1IF = 0 ;  // Clear interrupt flag
    
    TMR1ON = 1; //Start Timer1
       
    while(1)
	
	
    {
	
	 if (RB1 == 0)
	 {
	   TMR1ON = 0; //Stop Timer1
	 }
	   
	
      else if(TMR1IF ==1)
      { 

          RD4 = 1;  // LED ON
          __delay_ms(500); // 
          RD4 = 0;  // LED OFF
         
          TMR1IF=0; // Clear timer interrupt flag
          
          TMR1ON = 0; //Stop Timer1
          
          //reload counter
          TMR1H = 0xFF ;    // High byte FF
          TMR1L = 0xF6 ;    // Low Byte F6
      }  
    } 
}
 

Still not clear. Which input is expected to advance the counter, which input to start and stop the counter?
 
Still not clear. Which input is expected to advance the counter, which input to start and stop the counter?

The sensor is connected to RB1, an encoder is connected to RC0 and LED is connected to RD4

When the sensor is low don't run counter. when a sensor trigger, starts counter to count pulse coming on RC0 pin. the timer will start incrementing. the timer will overflow after 10 counts from the encoder, and LED1 will turn ON for 500 mSec. If the SENSOR triggers again, the process will repeat.
 

The code looks basically right, I'm only missing TMR1ON set to '1' following RB1 input.
 

The code looks basically right, I'm only missing TMR1ON set to '1' following RB1 input.

Where to set TMR1ON = 1 ?

Code:
while(1)
	
	
    {
	
	 if (RB1 == 0)
	 {
	   TMR1ON = 0; //Stop Timer1
	 }
	   
	
      else if(TMR1IF ==1)
      { 

          RD4 = 1;  // LED ON
          __delay_ms(500); // 
          RD4 = 0;  // LED OFF
         
          TMR1IF=0; // Clear timer interrupt flag
          
          TMR1ON = 0; //Stop Timer1
          
          //reload counter
          TMR1H = 0xFF ;    // High byte FF
          TMR1L = 0xF6 ;    // Low Byte F6
      }  
    } 
}
 

Only if Timer is On the counter will work and so set it before the while(1) loop.

Code:
void main() {

    TMR1ON = 1;

    while(1) {


    }
}
 

Only if Timer is On the counter will work and so set it before the while(1) loop.

Code:
void main() {

    TMR1ON = 1;

    while(1) {


    }
}

But the code is not working as I want to do

The codes in the post #1 is working but when I add code for sensor triggers code #13 is not working

What could be wrong ?
 

Try this.

Code:
if(RB1 == 0) {
   __delay_ms(50);
   if(RB1 == 0) {
	TMR1ON = 1; //Stop Timer1
   }
}
else if(RB1 == 1) {
   __delay_ms(50);
   if(RB1 == 1) {
	TMR1ON = 0; //Stop Timer1
   }
}

Still better method is to setup External Interrupt (RB0/INT) pin and toggle INTEDG bit and TMR1ON bit as required in External Interrupt ISR and for this you need to change sensor connection to RB0/INT pin. This will work when there are blocking delays in main loop.
 

In this situation you have to test your code in various location

For me your #1 post code is correct except baileychic is pointed out with delay

Fist experiment

Code:
 TMR1CS =  0; // Internal clock (FOSC/4)

Once you change the above code Timer1 run with internal clock , then LED should ON and off at least one time

Then check that your encoder feeds the pulse for RC0 or you can manually feed the pulse to RC0

don't forget to re change for counter mode

Code:
 TMR1CS =  1; // External clock from pin RC0/T1OSO/T1CKI (on the rising edge)

Hope this help
 
Last edited:

In this situation you have to test your code in various location

For me your #1 post code is correct except baileychic is pointed out with delay

Fist experiment

Code:
 TMR1CS =  0; // Internal clock (FOSC/4)

Once you change the above code Timer1 run with internal clock , then LED should ON and off at least one time

Then check that your encoder feeds the pulse for RC0 or you can manually feed the pulse to RC0

don't forget to re change for counter mode

Code:
 TMR1CS =  1; // External clock from pin RC0/T1OSO/T1CKI (on the rising edge)

My be help

Thanks for the helpfull reply


I have tested with TMR1CS = 1; // External clock from pin RC0/T1OSO/T1CKI (on the rising edge)

so the problem is LED should Flash on the sensor triggers that's not happening
 

Is LED blinking with internal clock ?

check and confirm us

Code:
TMR1CS =  0; // Internal clock (FOSC/4)
 
Last edited:

Have you analyzed what happens with your code (below mentioned) when say __delay_ms(500) is executing and sensor activates again?

Code:
else if(TMR1IF ==1)
      { 

          RD4 = 1;  // LED ON
          __delay_ms(500); // 
          RD4 = 0;  // LED OFF
         
          TMR1IF=0; // Clear timer interrupt flag
          
          TMR1ON = 0; //Stop Timer1
          
          //reload counter
          TMR1H = 0xFF ;    // High byte FF
          TMR1L = 0xF6 ;    // Low Byte F6
      }

Let's say you get your code working but due to the above code lets say __delay_ms(500) just started executing and there is another sensor trigger and counter overflows counting 10 pulses in 10ms say (Op doesn't know the pulse frequency), then TMR1IF gets cleared after executing __delay_ms(500) and then you will miss one sensor triggered operation.
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top