element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Embedded and Microcontrollers
  • Technologies
  • More
Embedded and Microcontrollers
Embedded Forum PIC32 delay help
  • Blog
  • Forum
  • Documents
  • Quiz
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Embedded and Microcontrollers to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 9 replies
  • Subscribers 472 subscribers
  • Views 2229 views
  • Users 0 members are here
  • programming.
  • timer
  • delay_timers
  • pic32
Related

PIC32 delay help

sanyika95
sanyika95 over 6 years ago

Hello everyone!

 

I just started to learn PIC32 programming with a PIC32 Ethernet Starter kit. I wanted to blink its led, 1 second on - 1 second off.

 

Here is the code for the delay:

#define USHORT unsigned short

void delayMs(USHORT milisec) {
    T1CON = 0x8010;
    TMR1 = 0x00;
    USHORT i = 0;
    PR1 = 0x40;
    while(i != milisec) {
        while(TMR1) {
        }
        i++;
        TMR1 = 0x00;
    }
    T1CON = 0x00;
    return;
}

 

It just works (or it looks like to work, I haven't measured it with an oscilloscope, just with a "stopwatch") and I don't know why. Can someone explain me?

 

 

The datasheet of the kit:

http://ww1.microchip.com/downloads/en/DeviceDoc/61166A.pdf

 

The datasheet of its PIC:

http://ww1.microchip.com/downloads/en/devicedoc/60001156j.pdf

  • Sign in to reply
  • Cancel

Top Replies

  • fmilburn
    fmilburn over 6 years ago +2
    I am not familiar with the PIC32 but that is strange code. You need to know what the clock speed is and how the dividers to the timer are set. A quick look at the datasheet shows that T1CON is the timer…
  • sanyika95
    sanyika95 over 6 years ago in reply to fmilburn +2
    I don't really know the clock speed. In the datasheets there were a 32kHz and a 8MHz clock as internal. I did my calculation with both of them and none of them worked. As you said, the T1CON is the control…
  • rusgray
    rusgray over 6 years ago +2
    Have you done the simple beginner Harmony tutorial project ? It implements a blinking LED, and does a good job of covering the basics of working with Harmony. It also serves as a good starting point for…
Parents
  • fmilburn
    fmilburn over 6 years ago

    I am not familiar with the PIC32 but that is strange code.  You need to know what the clock speed is and how the dividers to the timer are set.  A quick look at the datasheet shows that T1CON is the timer control register for TIMER1.  It looks like the variable milisec holds the value 1000 and that the register TMR1 is incremented once per millisecond and then the while will loop 1000 times using the variable i as a counter.  Of course normally you would have the timer count to the specified number of milliseconds and trigger an interrupt without blocking.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • fmilburn
    fmilburn over 6 years ago

    I am not familiar with the PIC32 but that is strange code.  You need to know what the clock speed is and how the dividers to the timer are set.  A quick look at the datasheet shows that T1CON is the timer control register for TIMER1.  It looks like the variable milisec holds the value 1000 and that the register TMR1 is incremented once per millisecond and then the while will loop 1000 times using the variable i as a counter.  Of course normally you would have the timer count to the specified number of milliseconds and trigger an interrupt without blocking.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
Children
  • sanyika95
    sanyika95 over 6 years ago in reply to fmilburn

    I don't really know the clock speed. In the datasheets there were a 32kHz and a 8MHz clock as internal. I did my calculation with both of them and none of them worked.

     

    As you said, the T1CON is the control register of TIMER1. In the code, the T1CON is set to Enable timer, set its prescaler to 8 and use the internal clock.

     

    You wrote the TMR1 is incremented once per milisecond, but if I understood the timing well, the TMR1 register will increase at every hit of the clock! So it have to reach X bit, depending on the clock and the prescaler to wait 1 milisecond. The PR1 is the value when to reset the TMR1.

    So when the TMR1 reaches 64 (in decimal) is 1 milisecond.

     

    The question is to understand the math behind it to be able to reproduce it on other MCU-s, with different clock speed and prescaler.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
  • fmilburn
    fmilburn over 6 years ago in reply to sanyika95

    I would not want to reproduce that code on another MCU regardless of clock speed and prescalar settings :-).  It is straight forward to track milliseconds on the microcontrollers I am familiar with - set the timer to count up and when it reaches 1 millisecond trigger an interrupt that increments the millisecond counter in an ISR.  Of course you must know how to set the clocks and timers to do this and the code you are working from does not seem to do this in a very clear manner.  Suggest finding a better example as suggested below....

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube