PIC32MZ Tutorial--Blinky LED

Source: Internet
Author: User

Today I finish the "Blinky LED" application on PIC32MZ Starter Kit. This application to LED1 blink with 0.5HZ frequence. The pseudo code is like

    LOOP        :        ledon1  second        led OFF        1 Second

It uses Timer1 to control the delay time. So first I implement the three Timer1 functions.

/** <p><b>Function:TMR1_Open</b></p> <p><b>summary:initialization of Timer & Lt;/b></p> <P><B>DESCRIPTION:TMR1 on; 0.02 microsecond every tick </b></p> <p><b>remarks:pre-scale 1:2; PB 100MHz; PR1 0xffff</b></p>*/voidTmr1_open (void) {T1con=0x8010; PR1=0xFFFF;}//Comment a function definition and leverage automatic documentation/** <p><b>Function:TMR1_Write</b></p> <p><b>summary:write tmr1</b> </p> <p><b>description:write A value to tmr1</b></p> <p><b>remarks:the valu E is range of 0~65535</b></p>*/voidTmr1_write (unsignedintvalue) {TMR1= value &0xFFFF;}/** <p><b>Function:TMR1_Read</b></p> <p><b>summary:read tmr1</b></ P> <p><b>description:read the value from tmr1</b></p> <p><b>remarks:the value I s range of 0~65535</b></p>*/unsignedintTmr1_read (void){    return(TMR1 &0xFFFF);}

Second I Finish the delay function, the implemention is like below

/** <p><b>function:delay_1s </b></p> <p><b>summary:delay using tmr1</b> </p> <p><b>description:delay One second </b></p> <p><b>remarks:call tmr1_ Open First </b></p>*/voidDelay_1s (void) {unsignedintCount = +; unsignedintTicks = -;  while(count--) {Tmr1_write (0);  while(Tmr1_read () <ticks) {            ; // do nothing        }    }}

Actually we can do this like below

/** <p><b>function:delay_1s </b></p> <p><b>summary:delay using tmr1</b> </p> <p><b>description:delay One second </b></p> <p><b>remarks:call tmr1_ Open First </b></p>*/voidDelay_1s (void) {unsignedintCount = -; unsignedintTicks = +;  while(count--) {Tmr1_write (0);  while(Tmr1_read () <ticks) {            ; // do nothing        }    }}

I prefer to the second one. I believe the second one has higher accuracy than the first one.

In the end, I finish the main function. In the last blog, I already show how to implement Led_seton. This time, we'll the same Led_seton funtion, and more, we need to implement Led_setoff. That's easy once you had read my last blog. If you don ' t know, please look at below.

#include <proc/p32mz2048ech144.h>#include"Delay.h"#include"ConfigurationBits.h"#defineLed_ioctl () trishclr = (1<<0)#defineLed_seton () Lathset = (1<<0)#defineLed_setoff () lathclr = (1<<0)#defineLed_open () Anselh &= 0xFFFFFFFEvoidMainvoid) {tmr1_open ();    Led_open ();    Led_ioctl ();  while(1) {Led_seton ();        Delay_1s ();        Led_setoff ();    Delay_1s (); }}

PIC32MZ Tutorial--Blinky LED

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.