Main Page   Modules   Data Structures   File List   Data Fields   Globals   Related Pages  

Period Meter (max 42s, 10ns resolution).

// Built with BFTC Rev. 1.0.0, mar 26 agosto 2014 13:58:59
/*
    Copyright 2014 Digital Technology Art SRL
    This file is part of Blackfin Toolchain (BFTC) project.

    BFTC is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    BFTC is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Name-Program.  If not, see <http://www.gnu.org/licenses/>.
*/

// Simple program that show how to implement a period meter on PG0 port
// it use a 32 bit counter with a time base of 10ns
// Maximum period length is approx 42s
// Period measure start on the rising edge
//
#include "main.h"

#define PG_BIT  PG0 // Bit used as input

#define ST st = Get_TimerCounter(TIMER_0)
#define ET et = Get_TimerCounter(TIMER_0)

#define TIMEBASE 0xFFFFFFFF // Maximum timer count

volatile bool edge = true, done = false;
unsigned int st, et;

void display()
{
    static  unsigned measureCounter = 0;
    unsigned tx;
    // What you can see below is to prevent timer overflow.
    // A 32 bit timer running at 100MHz 
    // reach the overflow after 43 s
    if(st > et) 
        tx = TIMEBASE - st + et;
    else 
        tx = et - st;
    printf("[%4d]: Period: %u us\n", measureCounter++, tx / 100);
}

// Interrupt service procedure
//
void PortCallback()
{
    // Clear the interrupt request
    PPG_CLR = PG_BIT;
    // Prevent measure overrun
    if(done) return;
    if(edge)    // Rising edge
    {
        ST;
        // Set for falling
        PPG_POL |= PG_BIT;
        edge = false;
    }
    else        // Falling edge
    {
        ET;
        // Set for rising
        PPG_POL &= (~PG_BIT);
        edge = true;
        done = true;    // Measurement done! Signal this event
    }
}

int main(void)
{
    Set_PLL(16, 4);     // CORE: 25MHz * 16 = 400MHz, SCLK: 400MHz / 4 = 100MHz
    Set_Port();         // Set the port according to project set
    Set_Uart0(115200);  // printf is redirected to UART0
    
    GetChar_Uart0();    // Wait a key before starting
    
    // Set an interrupt on the rising edge of PG_BIT on PG port
    Set_PortGInterruptService(PG_BIT, PortCallback);
    Set_PortGInterruptEnable(PG_BIT, true);
    
    Set_TimerCounter(TIMER_0);  // Timer used for the measure, timebase is 10ns
    Set_TimerEnable(TIMER_0, true);
    
    printf("Started....\n");
    
    while(1)                // Main loop
    {
        if(done)            // Display if measure is ready
        {
            display();
            done = false;   // Start a new measure
        }
    }
    return 0;
}

Generated on Tue Apr 7 20:07:44 2015 for BF592A Library by doxygen 1.3.1