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. idea中如何配置tomcat

    这几天想通过JDBC驱动使用MySQL数据库,但老是运行不成功,但是写成java就没有问题,于是想到是不是服务器没配置好 idea中配置tomcat的步骤如下 1:File->Settings. ...

  2. BZOJ 2595 斯坦那树

    很久以前就想做,后来弃坑了. 最近又在群里有人问了类似的问题,艾老师说是斯坦纳树(%%%) 就是状压DP,然后用Spfa对状态进行转移. #include <iostream> #incl ...

  3. 【LeetCode】Binary Tree Preorder Traversal

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  4. xml 解析的四种方式

    =========================================xml文件<?xml version="1.0" encoding="GB2312 ...

  5. 不让padding影响元素的宽度

    CSS3 新增了 box-sizing 属性. 以前,如果指定 div 的宽度为 div { width: 100px; height: 100px; padding: 10px; } 则包含 pad ...

  6. [Spring] - Property注入

    使用Spring注入Properies文件方法: 1.src中新建一个settings.properties文件,内容如下: db_driverClassName=com.mysql.jdbc.Dri ...

  7. 552 you must authentication

    配置邮箱到outlook时 出现以下错误: 发送测试电子邮件消息: 无法发送此邮件.请在帐户属性中验证电子邮件地址.  响应服务器: 552 you must authentication 需要在”其 ...

  8. web移动端input获得光标Fixed定位失效解决方案

    移动端业务开发,iOS 下经常会有 fixed 元素和输入框(input 元素)同时存在的情况. 但是 fixed元素在有软键盘唤起的情况下,会出现许多莫名其妙的问题. 这篇文章里就提供一个简单的有输 ...

  9. 数论 UVA 10780

    数论题目.有关内容:整数质因数分解,N的阶乘质因数分解,整除的判断. 这道题的题意是给你两个数n.m,要求你求出n!所能整除的m^k的最大值的k是多少. 由于数据范围:1<m<5000,1 ...

  10. jquery 之ajax获取数据

    $.ajax({    url: "http://www.hzhuti.com",    //请求的url地址    dataType: "json",   / ...