Connect with us

How to set counter's in queue

Discussion in 'Microcontrollers, Programming and IoT' started by Daljeet12, Apr 28, 2019.

  1. Daljeet12

    Daljeet12

    10
    0
    Jun 16, 2018
    I have pic16f877a microcontroller and I am writing my code on MPLABX XC8. The sensor is connected to RB1, an encoder is connected to RC0 and LED is connected to RD4

    I have written a program for counter that can count 10 events

    Code:
    // 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;    //PORTB R1  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
          } 
        }
    }
    I want to set multiple counters ( 5) in the queue whenever the sensor triggers set the counter in FIFO

    When the sensor triggers the first-time set counter 1 and when timer overflow flash led
    When the sensor triggers the second-time set counter 2 and when it timer overflow flash led
    When the sensor triggers the third-time set counter 3 and when it timer overflow flash led
    When the sensor triggers the forth-time set counter 4 and when it timer overflow flash led
    When the sensor triggers the fifth-time set counter 5 and when it timer overflow flash led

    How to accomplish in programming? any suggestion would be appreciated
     
  2. Harald Kapp

    Harald Kapp Moderator Moderator

    8,994
    1,785
    Nov 17, 2011
    You use a variable to count how many times the sensor has triggered. Set this variable to 0 at the start of the program. Whenever a trigger occurs you check the value of this variable (e.g. in a switch statement) and perform the required action. Then increment the variable to be ready for the next trigger.
     
    Daljeet12 likes this.
  3. Daljeet12

    Daljeet12

    10
    0
    Jun 16, 2018
    Done
    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;
      
        int Trigger = 0;
      
        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
          }
        }
    }
    example code for switch case

    Code:
    switch (Trigger) {
        case 1 : "counter1"
           break;
        case 2 : "counter 2"
           break;
        case 3 : "counter 3"
           break;
        case 4 : "counter 4"
           break;
        case 5 : "counter 5"
           break;
     }
    I have mentioned counter 1 counter 2 up to counter 5 for example

    How to perform the required option in place of a counter name?
     
    Last edited: Apr 28, 2019
  4. Harald Kapp

    Harald Kapp Moderator Moderator

    8,994
    1,785
    Nov 17, 2011
    Look up the switch statement in any of the many c-manuals online.
     
  5. Daljeet12

    Daljeet12

    10
    0
    Jun 16, 2018
    My code is not working as expected When sensor triggers LED doesn't flash

    Do you see something wrong in the code ?

    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;
     
        int Trigger = 0;
     
        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
          }
        }
    }
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-