EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Topics / Flash LED on and off every other second using TMR0. (Beginner coding question)

Flash LED on and off every other second using TMR0. (Beginner coding question)

|

Microcontroller › PIC › Flash LED on and off every other second using TMR0. (Beginner coding question)

  • This topic has 1 reply, 2 voices, and was last updated 11 years, 3 months ago by Ashutosh Bhatt.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • November 21, 2014 at 8:35 pm #3321
    Craig
    Participant

    Hello, 

    I am learning how to use TMR0 on a pic16f690.  I am trying to flash an led on and off every other second.  I am using the microchip low pin count board, a pickit 2 to program and MPLAB X to code and build the program with the MPLAB X compiler.  My code is below.  When i run the code on the board, the led lights up after about 5 seconds and just stays on.  No flashing or anything else.  Could anyone please tell me what i am doing wrong.  

    Thank you for your help ahead of time!

     

    #include <xc.h>
     #include <stdint.h>
     
    // #pragma config statements should precede project file includes.
    #pragma config MCLRE = OFF, CP = OFF, CPD = OFF, BOREN = OFF, WDTE = OFF
    #pragma config PWRTE = OFF, FOSC = INTRCIO
     
    #define _XTAL_FREQ 8000000  //pic16F690 OSC Frequency
     
    void main()
    {
        //ANSEL = 0b00000000;  //turn off analog, all ports are now digital output
        TRISC = ~(1<<1);  //Make RC1 an output
        PORTC = 0x00;  //Turn off all of the leds on PORTC
     
        OPTION_REGbits.T0CS = 0;  //select timer mode, not counter mode
        OPTION_REGbits.PSA = 0;  //assign prescalar to timer0
     
        //we will need to use a loop that goes around 250X to get 8ms.  (250x around at 32us will give us 8ms.)(.008/32us = 250)
        //then if we loop again 125X, we can get to 1 second (.008ms X 125 = 1 second, if we wanted a 1X every other second led flash)
        OPTION_REGbits.PS0 = 0b101;  //prescale = 64  (64/2,000,000 instruction cycle frequency = 32us, so the timer will tick every 32us)
     
        uint8_t a;
        
        for (;;)  //loop forever
        {
           for (a=0; a<125; a++)
             {
                TMR0 = 0 ; //reset TIMER 0
     
                while (TMR0 < 250)
                 {
                    ;;
                 }
             }
     
           PORTCbits.RC1 = ~PORTCbits.RC1;
        }
    }
    November 23, 2014 at 5:41 pm #12365
    Ashutosh Bhatt
    Participant

    first you go through our PIC tutorials given on

    EG labs / micro controller / PIC

     

    surely you will get idea whts going wrong in your application

     

  • Author
    Posts
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • My Advanced Realistic Humanoid Robots Project March 12, 2026
  • Very Curious Issue With DS3231 RTC Clock March 12, 2026
  • Motor Getting Hot March 11, 2026
  • LM741 alternatives? March 11, 2026
  • H bridge current direction logic March 11, 2026

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2026 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise