当前位置:首页 > 单片机 > 单片机
[导读]  Today I finish the "Blinky LED" application on PIC32MZ starter kit. This application let LED1 blink with 0.5HZ frequency. The pseudo code is like LOOP: LED ON Delay 1 second LED OFF D

  Today I finish the "Blinky LED" application on PIC32MZ starter kit. This application let LED1 blink with 0.5HZ frequency. The pseudo code is like


LOOP:

LED ON

Delay 1 second

LED OFF

Delay 1 second

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



/**

Function: TMR1_Open


Summary: Initialization of Timer


Description: TMR1 on; 0.08 microsecond every tick


Remarks: Pre-scale 1:8; PB 100MHz; PR1 0xFFFF

*/

void TMR1_Open(void)

{

T1CON = 0x8010;

PR1 = 0xFFFF;

}

// Comment a function definition and leverage automatic documentation

/**

Function: TMR1_Write


Summary: Write TMR1


Description: Write a value to TMR1


Remarks: the value is range of 0~65535

*/

void TMR1_Write(unsigned int value)

{

TMR1 = value & 0xFFFF;

}

/**

Function: TMR1_Read


Summary: Read TMR1


Description: Read the value from TMR1


Remarks: the value is range of 0~65535

*/

unsigned int TMR1_Read(void)

{

return (TMR1 & 0xFFFF);

}


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



/**

Function: Delay_1S


Summary: Delay using TMR1


Description: Delay one second


Remarks: call TMR1_Open first

*/

void Delay_1S(void)

{

unsigned int count = 12500;

unsigned int ticks = 1000;

while (count--)

{

TMR1_Write(0);

while (TMR1_Read() < ticks)

{

; // do nothing

}

}

}


  Actually we are also able to do that like below



/**

Function: Delay_1S


Summary: Delay using TMR1


Description: Delay one second


Remarks: call TMR1_Open first

*/

void Delay_1S(void)

{

unsigned int count = 1000;

unsigned int ticks = 12500;

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 last blog, I already show how to implement LED_SETON. This time, we will the same LED_SETON funtion, and more, we need to implement LED_SETOFF. That's easy once you have read my last blog. If you don't know yet, please look at below.



#include

#include "Delay.h"

#include "ConfigurationBits.h"


#define LED_IOCTL() TRISHCLR = (1<<0)

#define LED_SETON() LATHSET = (1<<0)

#define LED_SETOFF() LATHCLR = (1<<0)

#define LED_OPEN() ANSELH &= 0xFFFFFFFE


void main(void)

{

TMR1_Open();

LED_OPEN();

LED_IOCTL();

while (1)

{

LED_SETON();

Delay_1S();

LED_SETOFF();

Delay_1S();

}

}



本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

为增进大家对LED显示屏的认识,本文将对节能LED显示屏的设计予以介绍。

关键字: LED 指数 显示屏

为增进大家对LED显示屏的认识,本文将对LED显示屏的部件组成、LED显示屏的选型技巧予以介绍。

关键字: LED 指数 显示屏

LED显示屏的身影。为增进大家对LED显示屏的认识,本文将对LED灯珠对LED显示屏的影响予以介绍。

关键字: LED 指数 显示屏

LED显示屏将是下述内容的主要介绍对象,通过这篇文章,小编希望大家可以对它的相关情况以及信息有所认识和了解,详细内容如下。

关键字: LED 显示屏

今天,小编将在这篇文章中为大家带来led显示屏的有关报道,通过阅读这篇文章,大家可以对它具备清晰的认识,主要内容如下。

关键字: LED 显示屏 LED显示屏

LED(Light Emitting Diode)与LCD(Liquid Crystal Display)是当今显示技术领域的两大重要分支,各自凭借独特的优势在消费电子、广告传媒、工业控制、家用电器等多个领域占据着主导地...

关键字: LED LCD

作为温度依赖性低、广角发射且光线均匀的光源,有助于汽车驾驶辅助技术提升

关键字: VCSEL LED 红外光源

爱德万测试集团 (公司总部:东京都千代田区、代表董事:Douglas Lefever、以下简称为“爱德万测试”) 与东丽工程株式会社 (总公司:东京都中央区、代表董事总经理:岩出卓、以下简称为“东丽工程”) 此番宣布,签...

关键字: LED 显示屏

为增进大家对LED路灯的认识,本文将对LED路灯、LED路灯的选购要点、LED路灯使用成本予以介绍。

关键字: LED 指数 路灯

为增进大家对LED路灯的认识,本文将对LED路灯的两点设计要求予以介绍。

关键字: LED 指数 路灯
关闭
关闭