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 second
LED OFF
Delay 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 </b></p> <p><b>Description: TMR1 on; 0.08 microsecond every tick </b></p> <p><b>Remarks: Pre-scale 1:8; PB 100MHz; PR1 0xFFFF</b></p>
*/
void TMR1_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 value is range of 0~65535</b></p>
*/
void TMR1_Write(unsigned int value)
{
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 is range of 0~65535</b></p>
*/
unsigned int TMR1_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>
*/
void Delay_1S(void)
{
unsigned int count = ;
unsigned int ticks = ;
while (count--)
{
TMR1_Write();
while (TMR1_Read() < ticks)
{
; // do nothing
}
}
}

  Actually we are also able to do that 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>
*/
void Delay_1S(void)
{
unsigned int count = ;
unsigned int ticks = ;
while (count--)
{
TMR1_Write();
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 <proc/p32mz2048ech144.h>
#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 ()
{
LED_SETON();
Delay_1S();
LED_SETOFF();
Delay_1S();
}
}

PIC32MZ tutorial -- Blinky LED的更多相关文章

  1. PIC32MZ tutorial -- External Interrupt

    In my older blog "PIC32MZ tutorial -- Key Debounce", I shows how to acheive key debounce w ...

  2. PIC32MZ tutorial -- OC Interrupt

    In my previous blog "PIC32MZ tutorial -- Output Compare", I shows how to apply Output Comp ...

  3. PIC32MZ tutorial -- Output Compare

    Output Compare is a powerful feature of embedded world. The PIC32 Output Compare module compares the ...

  4. PIC32MZ tutorial -- Change Notification

    In my last post I implement "Key Debounce" with port polling, port polling is not very eff ...

  5. PIC32MZ tutorial -- Key Debounce

    Today I accomplish a application on PIC32MZ EC Starter Kit. The feature of application is to light u ...

  6. PIC32MZ tutorial -- Timer Interrupt

    An interrupt is an internal or external event that requires quick attention from the controller. The ...

  7. PIC32MZ tutorial -- Hello World

    Today I implement "Hello World" on PIC32MZ EC starter kit. The application of "Hello ...

  8. PIC32MZ tutorial -- UART Communication

    At this moment, I accomplish the interface of UART communication for PIC32MZ EC Starter Kit. This in ...

  9. PIC32MZ tutorial -- Watchdog Timer

    Watchdog is a very necessary module for embedded system. Someone said that embedded system operates ...

随机推荐

  1. C# 不同版本切版时,方法不支持,加载对应dll, 相关Dll的资源

    不过,有些高版本有的DLL,低版本运行时,需要引用相关DLL.我们不用在网上去下载 下面的路径,查找对应版本下的DLL,可能会给你意想不到的收获哦 C:\Program Files\Reference ...

  2. Swift基础语法 、 元组(Tuple)

    字符串的使用 1.1 问题 Swift的String和Character类型提供了一个快速的,兼容Unicode的方式来处理代码中的文本信息.创建和操作字符串的语法与C语言中字符串类似.本案例将学习如 ...

  3. NPOI设置Excel保护

    有时,我们可能需要某些单元格只读,如在做模板时,模板中的数据是不能随意让别人改的.在Excel中,可以通过“审阅->保护工作表”来完成,如下图:  那么,在NPOI中有没有办法通过编码的方式达到 ...

  4. 使用Object.observe 实现数据绑定

    Object.observe API概述 最近,JavaScript的MVC框架在Web开发届非常盛行.在实现MVC框架的时候,一个非常重要的技术就是数据绑定技术.如果要实现模型与视图的分离,就必须要 ...

  5. DAO接口及实现类

    DAO接口中定义了所有的用户操作,如添加记录.删除记录及查询记录. package chapter13; import java.util.*; public interface UserDAO { ...

  6. 基于redis的排行榜设计和实现

    前言: 最近想实现一个网页闯关游戏的排行榜设计, 相对而言需求比较简单. 秉承前厂长的训导: “做一件事之前, 先看看别人是怎么做的”. 于是乎网上搜索并参考了不少排行榜的实现机制, 很多人都推荐了r ...

  7. 递归输出文件夹下的所有文件的名称(转自 MSDN)

    问题:如何输出给定文件夹目录下面的所有文件的名称? C#代码: using System; using System.IO; namespace MyTest { public class Progr ...

  8. (转)Markov Chain Monte Carlo

    Nice R Code Punning code better since 2013 RSS Blog Archives Guides Modules About Markov Chain Monte ...

  9. ZFS(一):ZFS在Debian GNU/Linux上的安装

    以下内容翻译自https://pthree.org/2012/04/17/install-zfs-on-debian-gnulinux/,并附有原文,由于是第一次翻译,如有任何翻译不恰当之处,欢迎指出 ...

  10. CentOS6.8 MySQL 5.6实现主从复制

    主库操作 1.将mysqldump命令添加到/usr/bin中 ln -s /application/mysql/bin/mysqldump /usr/bin/ 2.开启master上的log-bin ...