Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Complete Sensor Guide
      • Engineering Deep Dives
      • AI
      • ARDUINO Compatible Coding
      • Audio Electronics
      • Battery Management
      • Beginners Electronics Series
      • Brainwave
      • Digital electronics (DE)
      • Electric Vehicles
      • EMI/EMC/RFI
      • EVs
      • Hardware Filters
      • IoT tutorials
      • LoRa/LoRaWAN
      • Power Tutorials
      • Protocol
      • Python
      • RPI Python Programming
      • Sensors
      • USB
      • Thermal management
      • Verilog
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey 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
  • Learn
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Tech Terms
    • Webinars & Digital Events
  • Resources
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • White Papers
  • Guest Post Guidelines
  • Advertisers
    • Advertise
    • PR Submissions
    • Eblast Submissions
  • Subscribe

One Second Delay Generation by using internal Timers of Microchip Pic Microcontroller, xc8 compiler with Mplabx Ide

By EG Projects August 4, 2019

While working with microchip pic microcontrollers i came across a situation where custom delay is required in seconds. I was working with pic18f4580 microcontroller, Mplabx ide and xc8 compiler. I know that __delay_ms() and __delay_us() macros do exists for generating delays. But i want to see how the timer configuration is done for one second delay. I want to go through the whole process by my self. I read out the data sheet of pic18f4580 microcontroller and found some relevant information that i am going to share with the microcontroller project community.  

How to generate specific delay’s using Timers: The internal structure of Pic Microcontroller’s

​Each pic microcontroller divides the input clock frequency from external crystal by 4, before supplying the clock pulse to internal peripherals. After dividing, the clock signal is supplied to internal timers, ADC’s, GPIO’s, Processor and Uart etc. Suppose you attached an external 20MHz crystal at XTAL pins. The input frequency/clock signal supplied to peripherals of pic will be 20/4= 5MHz.

This 5Mhz frequency can further be divided to operate a particular peripheral on different or desired frequency, by activating internal prescaler’s. 

Pic microcontroller internal clock timer structure

Pic microcontroller internal clock timer structure

Pic Microcontroller Delay Calculation formula

Pic Microcontroller Delay Calculation formula

Pic Microcontroller Delay Calculation formula

Tick/Counter frequency
Tick/Counter frequency is the final frequency on which the timer is working.
Timer Count
Timer Count is the number of counts required to generate a particular delay/frequency at output of microcontroller pin. Timer count 
is the final value to be loaded in TMRxH and TMRxL Registers.
Register Value 
Register Value must not be negative or grater than 65535. Since timer is a 16-bit and max 16-bit value is 65535 

Lets calculate 1 second delay

Suppose we want to generate 1 second delay using timer-1 of pic16f877a microcontroller. An external crystal with frequency Fosc = 20Mhz is used as clock source to microcontroller. We also want to activate prescaler for reducing the clock speed supplied to microcontroller. We selected 1:4 Prescaler. Putting the given values in formula given above yields out. 
Note: I am calculating value for 10 ms and then running the 10 ms delay 100 times to produce 1 second delay. 

Pic input frequency= Fosc / 4= 20 Mhz / 4 = 5 Mhz
Prescaler = 1:4
Tick Counter frequency = Prescaler/5 Mhz = 4 / 5 Mhz =  0.8 u s
Delay required = 10 ms
Delay required = Timer Count * 
Tick Counter frequency
Timer Count = Delay required / Tick Counter frequency = 10 m s/ 0.8 u s = 12.5 k = 12500 (Hexadecimal = 0x30D4)
Register value = 65535 – 12500 = 53035 Value not negative and under 65535, its safe we can use 12500.

Timer counter value for 10 ms delay comes out to be 12500 which is in 16-bit range and we can load it in our timer registers. If i directly calculate 1 second value for timer count it comes out to be greater than 65535 hence 1 second delay can not be directly generated with the above given information. We have to adjust prescaler or input frequency for directly generating 1 second delay. For this tutorial i am only going with the 10 ms and running it for 100 times for 1 second delay.

Below is a test program in which i uploaded the above settings. Pic16f877a microcontroller is used in the project. Xc8 compiler and MPLABx is used for code compilation. An external 20 Mhz crystal is used as clock source. An led is connected at output of port-b pin#4. Timer-1 of pic16f877a is used to generate 1 second delay. Led toggles after every 1 second.

More advanced tutorials on toggling/event triggering/blinking an led on specific time delay with pic microcontroller. Click the below button to visit the tutorial/project.

Toggle led on specific delay with pic microcontroller

Download the project code. Folder contains the pic microcontroller MPLABx ide Project files. Please give us your feed back on the project. In case you have any queries please write them below in the comments section.
One second delay code/files


Filed Under: Microcontroller Projects, PIC Microcontroller

 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.



Tell Us What You Think!! Cancel reply

Log in to leave a comment:

Lost your password?

Don't have an account? Register here

Submit a Guest Post

submit a guest post

EE TECH TOOLBOX

“ee
Tech Toolbox: Wide Bandgap Semiconductors
Moving from silicon to GaN or SiC feels like a whole new ballgame, doesn’t it? It isn’t just about faster switching; it’s about managing the layout parasitics and thermal realities that come with that speed. This month’s Tech Toolbox features a curated eBook that tackles these design hurdles head-on.

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS feed: EDABOARD.com Discussions. EDABOARD.com Discussions.

  • Component footprint "centre" to pin 1 ?
  • Leaving your C as C, but Rust-Grade Safety w/C++
  • SOT23-6 with thermal pads
  • ECAL cable position strategy usage
  • DRC in Altium: Regions of application

RSS feed: Electro-Tech-Online.com Discussions Electro-Tech-Online.com Discussions

  • PIC Assembler Coding
  • Ways of handle battery when reaching charge
  • I enjoyed this math puzzle
  • VNA calibration linked causality questions
  • A market plan for a affordable website without advertising (kinky as it might sound)

Featured Tutorials

LoRa and LoRaWAN: A Complete Guide to Wireless Communication

  • Best practices for securing LoRa networks
    Best practices for securing LoRa networks
  • Understanding LoRa architecture: nodes, gateways, and servers
    Understanding LoRa architecture: nodes, gateways, and servers
  • Revolutionizing RF: LoRa applications and advantages
    Revolutionizing RF: LoRa applications and advantages
  • How to build a LoRa gateway using Raspberry Pi
    How to build a LoRa gateway using Raspberry Pi
  • How LoRa enables long-range communication
    How LoRa enables long-range communication
  • How communication works between two LoRa end-node devices
    How communication works between two LoRa end-node devices
More Tutorials >

Recent Articles

  • Emerson adds 1.5 GHz oscilloscopes and 6½-digit DMM
  • TASKING introduces unified toolchain for automotive multicore processors
  • Harwin launches custom cable assembly and testing service
  • Renesas introduces 64-bit MPU for four-axis motor control
  • STMicroelectronics GaN drivers handle 60 A in compact LGA

EE ENGINEERING TRAINING DAYS

engineering
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2026 Arrowfly 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 Arrowfly
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Complete Sensor Guide
      • Engineering Deep Dives
      • AI
      • ARDUINO Compatible Coding
      • Audio Electronics
      • Battery Management
      • Beginners Electronics Series
      • Brainwave
      • Digital electronics (DE)
      • Electric Vehicles
      • EMI/EMC/RFI
      • EVs
      • Hardware Filters
      • IoT tutorials
      • LoRa/LoRaWAN
      • Power Tutorials
      • Protocol
      • Python
      • RPI Python Programming
      • Sensors
      • USB
      • Thermal management
      • Verilog
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey 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
  • Learn
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Tech Terms
    • Webinars & Digital Events
  • Resources
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • White Papers
  • Guest Post Guidelines
  • Advertisers
    • Advertise
    • PR Submissions
    • Eblast Submissions
  • Subscribe