zoukankan      html  css  js  c++  java
  • PIC32MZ tutorial -- Blinky LED

      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.

    /**
      <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 = 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

    /**
      <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 = 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 <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 (1)
        {
            LED_SETON();
            Delay_1S();
            LED_SETOFF();
            Delay_1S();
        }
    }
  • 相关阅读:
    【部分原创】标准C语言的优先级、结合性、求值顺序、未定义行为和非确定行为浅析
    &&、||、?:、,四个运算符的求值顺序
    [转]说说C语言运算符的“优先级”与“结合性”
    初识Nginx,简单配置实现负载均衡(ubuntu + Nginx + tomcat)
    Hadoop伪分布式模式安装
    HBase hbase-site.xml 参数
    HBase单机环境搭建
    Hadoop单机模式安装
    JAVA笔记整理(一),JAVA介绍
    JAVA笔记整理(十),JAVA中的File
  • 原文地址:https://www.cnblogs.com/geekygeek/p/pic32mz_blinkyled.html
Copyright © 2011-2022 走看看