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
  • mike-0rc
    mike-0rc over 6 years ago

    I would suspect that the WDT is kicking in causing a reset, I have seen this before.

    If you want a good timer then use the call back feature from the Harmony and use the DRV_Handle call back feature, this then allows you to pass a value to the timer function that runs in the background and interrupts after the elapsed time. As there are so many different approaches to this, If you have a Core timer available you can calculate the time in ms from this although you will not be able to exit the loop until it ends, this is the simplest way but does stop the rest of the code from running and if using communication or other important tasks you run the risk of a WDT reset or data being missed.

     

    I hope this helps.

     

    As a point Microchip changed the function __delay_   And you have to create this yourself. I will try finding some of my code and post it image

     

    Mike Clapham.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • mike-0rc
    mike-0rc over 6 years ago

    I would suspect that the WDT is kicking in causing a reset, I have seen this before.

    If you want a good timer then use the call back feature from the Harmony and use the DRV_Handle call back feature, this then allows you to pass a value to the timer function that runs in the background and interrupts after the elapsed time. As there are so many different approaches to this, If you have a Core timer available you can calculate the time in ms from this although you will not be able to exit the loop until it ends, this is the simplest way but does stop the rest of the code from running and if using communication or other important tasks you run the risk of a WDT reset or data being missed.

     

    I hope this helps.

     

    As a point Microchip changed the function __delay_   And you have to create this yourself. I will try finding some of my code and post it image

     

    Mike Clapham.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Children
  • mike-0rc
    mike-0rc over 6 years ago in reply to mike-0rc

    I have scoured my home PC and I have no examples as I have not done one on this PC yet, Instead you can look at the following as there are too many steps to write down here (You have to edit a lot of headers and c files to implement the timer call back (I started here while learning how to implement them in to my code).

    https://microchipdeveloper.com/harmony:example-tmr-sys-mz-ef-sk  As I am using a high end PIC32MZ2048EFH250 you may need to modify the code or have a look at the code (They should be cross device compatible) to get an idea on how this works.

     

    Regards,

    Mike Clapham.

    • 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