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. scanf()读取带空格的字符串

    #include <stdio.h> int main() { char str[128]; scanf( "%[^\n]", str ); printf( " ...

  2. iOS 动画 旋转 移动简单代码

    #import "ViewController.h" @interface ViewController () { UIImageView *imgView; BOOL flag; ...

  3. 细说Javascript之null、undefined和NaN

    首先简单介绍一下Javascript中的数据类型,Javascript中的数据类型有undefined,boolen,number,string和object等5种,前4种是原始类型,第5种是引用类型 ...

  4. 自动机理论、语言和计算导论 by John E. Hopcroft

    计算理论是计算机应用的基础,理论和应用缺一而不可. ---- 目录 ---- C01 自动机 C02 有穷自动机 C03 正则表达式与正则语言 C04 正则语言的性质 C05 上下文无关文法及上下文无 ...

  5. 几种循环语句 ,break,continue语句用法

    Java有非常灵活的三循环机制.可以使用以下三种循环之一: while 循环 do...while 循环 for 循环while循环是一个控制结构,可以重复的特定任务次数.在执行时,如果布尔表达式的结 ...

  6. ADC 电源监测

    我能为别人做点什么?这是我最近在思考的问题. 看了 ADC 电源监测代码,觉得对 ADC 的理解不到位,代码中有很多部分都不懂.如: 1. 为什么初始化的时候管脚设置为输出? 2. ADC 采集到的值 ...

  7. ASP.NET应用程序与页面生命周期

    http://www.cnblogs.com/suizhouqiwei/archive/2012/08/15/2637775.html

  8. 高度平衡的二叉搜索树(AVL树)

    AVL树的基本概念 AVL树是一种高度平衡的(height balanced)二叉搜索树:对每一个结点x,x的左子树与右子树的高度差(平衡因子)至多为1. 有人也许要问:为什么要有AVL树呢?它有什么 ...

  9. C#特性学习笔记一

    元数据,就是C#中封装的一些类,无法修改.类成员的特性被称为元数据中的注释. 1.什么是特性   1)属性与特性的区别 属性(Property):属性是面向对象思想里所说的封装在类里面的数据字段,Ge ...

  10. Java 异步处理简单实践

    Java 异步处理简单实践 http://www.cnblogs.com/fangfan/p/4047932.html 同步与异步 通常同步意味着一个任务的某个处理过程会对多个线程在用串行化处理,而异 ...