LED blinking sequence using timers

Home Forums MPLAB XC8 Project Discussions LED blinking sequence using timers

Viewing 0 reply threads
  • Author
    Posts
    • #5060
      Italo Balbo
      Participant

      Hi

      I’m using a PIC18F4580 and I want to blink 8 LED one after the other, using a timer delay. I’m using the 8 Mhz internal oscillator.

      As shown in the Proteus schematic, there are two push-buttons acting as two inputs and 8 LED as 8 outputs. The first button RB0 will be used to set the time delay (500ms for every push) and the second button (RB1) will be used to start the timer sequence blinking. For example, if the first button is pressed thrice (500*3 = 1500ms) the delay will be set for 1.5sec and when the button two is pressed each LED will turn ON and OFF with the predefined time delay. It’s an example I’ve found online that I’ve tried to adapt to my PIC (and I failed…).The behaviour is totally unexpected. I have blinking sequencences from time to time without any button pressed. I really don’t know what’s going on here. Please help me.

      <hr />

      // CONFIG1H
      #pragma config OSC = IRCIO67 // Oscillator Selection bits (Internal oscillator block, port function on RA6 and RA7)
      #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
      #pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)

      // CONFIG2L
      #pragma config PWRT = OFF // Power-up Timer Enable bit (PWRT disabled)
      #pragma config BOREN = OFF // Brown-out Reset Enable bits (Brown-out Reset disabled in hardware and software)
      #pragma config BORV = 3 // Brown-out Reset Voltage bits (VBOR set to 2.1V)

      // CONFIG2H
      #pragma config WDT = OFF // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
      #pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)

      // CONFIG3H
      #pragma config PBADEN = ON // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
      #pragma config LPT1OSC = OFF // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
      #pragma config MCLRE = OFF // MCLR Pin Enable bit (RE3 input pin enabled; MCLR disabled)

      // CONFIG4L
      #pragma config STVREN = OFF // Stack Full/Underflow Reset Enable bit (Stack full/underflow will not cause Reset)
      #pragma config LVP = OFF // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
      #pragma config BBSIZ = 1024 // Boot Block Size Select bit (1K words (2K bytes) boot block)
      #pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))

      // CONFIG5L
      #pragma config CP0 = OFF // Code Protection bit (Block 0 (000800-001FFFh) not code-protected)
      #pragma config CP1 = OFF // Code Protection bit (Block 1 (002000-003FFFh) not code-protected)
      #pragma config CP2 = OFF // Code Protection bit (Block 2 (004000-005FFFh) not code-protected)
      #pragma config CP3 = OFF // Code Protection bit (Block 3 (006000-007FFFh) not code-protected)

      // CONFIG5H
      #pragma config CPB = OFF // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
      #pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM not code-protected)

      // CONFIG6L
      #pragma config WRT0 = OFF // Write Protection bit (Block 0 (000800-001FFFh) not write-protected)
      #pragma config WRT1 = OFF // Write Protection bit (Block 1 (002000-003FFFh) not write-protected)
      #pragma config WRT2 = OFF // Write Protection bit (Block 2 (004000-005FFFh) not write-protected)
      #pragma config WRT3 = OFF // Write Protection bit (Block 3 (006000-007FFFh) not write-protected)

      // CONFIG6H
      #pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
      #pragma config WRTB = OFF // Boot Block Write Protection bit (Boot block (000000-0007FFh) not write-protected)
      #pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM not write-protected)

      // CONFIG7L
      #pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (000800-001FFFh) not protected from table reads executed in other blocks)
      #pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (002000-003FFFh) not protected from table reads executed in other blocks)
      #pragma config EBTR2 = OFF // Table Read Protection bit (Block 2 (004000-005FFFh) not protected from table reads executed in other blocks)
      #pragma config EBTR3 = OFF // Table Read Protection bit (Block 3 (006000-007FFFh) not protected from table reads executed in other blocks)

      // CONFIG7H
      #pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) not protected from table reads executed in other blocks)

      // #pragma config statements should precede project file includes.
      // Use project enums instead of #define for ON and OFF.

      #define _XTAL_FREQ 8000000

      <hr />

       

       

      #include <xc.h>
      #include <stdio.h>
      #include <stdlib.h>
      #include “Timers_1.h”

      //TIMER0 8-bit $$RegValue = 256-((Delay * Fosc)/(Prescalar*4)) delay in sec and Fosc in Hz
      //FORMULA to calculate Delay
      //Delay = ((256-REG_val)*(Prescal*4))/Fosc

      char hscnd = 0; // number of 0.5s delayed passed
      int count = 0;
      char get_scnds =0; // number of 0.5s delays wanted: 1 each time the push-button is pressed
      char flag =0;
      char i=0;

      void __interrupt() timer_isr()
      {
      if(INTCONbits.TMR0IF == 1) // Timer flag has been triggered due to timer overflow: 0.001s has passed from the last isr
      {
      WRITETIMER0(131); //Load the time value for 0.001s; delayValue can be between 0-256 only valore 131 =0x83
      INTCONbits.TMR0IF=0; // Clear timer interrupt flag
      count++;
      }

      if (count == 500)
      {
      hscnd+=1; // hscnd will get incremented for every half second
      count=0;
      }
      }

      void main()
      {

      OSCCON = 0x76; //Configure OSCCON register to use internal oscillator at 8 MHz

      /***** Configuration for Timer ******/
      //Set the prescaler to 1:16
      T0CONbits.T0PS0 = 1;
      T0CONbits.T0PS1 = 1;
      T0CONbits.T0PS2 = 0;

      T0CONbits.T08BIT = 1; //8-bit timer
      T0CONbits.T0CS = 0;
      T0CONbits.T0SE = 0;
      T0CONbits.PSA = 0;
      T0CONbits.TMR0ON = 1;
      WRITETIMER0(131); // Load the time value for 0.001s; delayValue can be between 0-256 only
      INTCONbits.TMR0IE = 1; //Enable timer interrupt bit in PIE1 register
      INTCONbits.TMR0IF = 0; //Enable timer interrupt bit in PIE1 register
      INTCONbits.GIE = 1; //Enable Global Interrupt
      INTCONbits.PEIE = 1; //Enable the Peripheral Interrupt
      /***********______***********/

      /*****Port Configuration for I/O ******/
      TRISBbits.RB0 = 1; //Instruct the MCU that the PORTB pin 0 is used as input for button 1.
      TRISBbits.RB1 = 1; //Instruct the MCU that the PORTB pin 1 is used as input for button 2.
      TRISC = 0x00; //Instruct the MCU that all pins on PORT C are output
      LATC = 0x00; //Initialize all pins on PORT C to 0
      /***********______***********/

      ei();

      while(1)
      {
      count = 0; //Do not run timer while in main loop

      //*******Get the number of 0.5s delays from user****//
      if (PORTBbits.RB0 == 0 && flag == 0) //When the input is given
      {
      get_scnds = get_scnds+1; //Increment 0.5s delay 1 time
      flag = 1;
      }
      if (PORTBbits.RB0==1) //To prevent continuous incrementation
      flag = 0;
      /***********______***********/

      //*******Execute sequence with delay****//////
      while (PORTBbits.RB1==0) //Until button RB1 is pressed
      {
      LATC = 0b00000001<<i; //Left shift LED by i
      if(hscnd==get_scnds) //If the required time is reached
      {
      i = i+1; //Move to next LED after the defined Delay
      hscnd = 0;
      }
      flag = 2;
      }
      if (flag == 2 && PORTBbits.RB1 == 1) //Reset timer if button is high again
      {
      get_scnds = 0;
      hscnd = 0;
      i = 0;
      LATC = 0x00; //Turn off all LEDs
      }
      /***********______***********/
      }
      }

      <hr />

       

      Attachments:
      You must be logged in to view attached files.
Viewing 0 reply threads
  • You must be logged in to reply to this topic.
Select your currency
EUR Euro
USD United States (US) dollar