.

Dependencies:   mbed EthernetInterface mbed-rtos

Fork of Bootloader_K64F by Erik -

main.cpp

Committer:
Sissors
Date:
2014-08-22
Revision:
0:9396d3376435
Child:
1:782a3ddc329e

File content as of revision 0:9396d3376435:

#include "mbed.h"
#include "FastPWM.h"
#include "FastIO.h"

#define TIME_REG    LPC_TIM2->TC
FastPWM output(p21);
FastIn<p22> input;
void initTimer2();

 
int main()
{
    //Ticks to be measured
    unsigned int tick_width = 100;
    
    unsigned int measured_timed[3];
    
    initTimer2(); 
 
    while (true) {
        //Setup PWM
        output.period_ticks(tick_width);
        output.pulsewidth_ticks(tick_width/2);
        wait_us(10);
        
        //Start measuring at first rising edge
        while(!input);
        while(input);
        measured_timed[0] = TIME_REG;
        while(!input);
        measured_timed[1] = TIME_REG;
        while(input);
        measured_timed[2] = TIME_REG;
        
        printf("\r\nPulsewidth = %d ticks, which is %2.1fMHz\r\n", tick_width, (float)SystemCoreClock / (float)tick_width / 1000000.0f);
        printf("Measured period = %d ticks, pulsewidth = %d ticks\r\n", measured_timed[2]-measured_timed[0], measured_timed[1]-measured_timed[0]);
        
        //If more than 20% too large, consider it a fail
        if ((measured_timed[2] - measured_timed[0]) > 1.2 * tick_width) {
            printf("Period setting failed\r\n");
            while(1);
        }
        
        //Setup next tick
        tick_width--;
 
    }
}

//Semi copy-pasted from: https://mbed.org/users/garyr/code/Counter/file/e619b6823668/Timer2.cpp
//We init timer at full clock speed, don't do anything else
void initTimer2() {
    LPC_SC->PCONP |= (1<<22);           // Power on the Timer2
    LPC_SC->PCLKSEL1 &= ~(3<<12);
    LPC_SC->PCLKSEL1 |= (1<<12);        // Select  CCLK for Timer2
    LPC_TIM2->TCR = 2;
    LPC_TIM2->TCR = 1;                  // Enable Timer2
}