.

Dependencies:   mbed EthernetInterface mbed-rtos

Fork of Bootloader_K64F by Erik -

Committer:
Sissors
Date:
Fri Aug 22 15:14:52 2014 +0000
Revision:
0:9396d3376435
Child:
1:782a3ddc329e
versie 1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sissors 0:9396d3376435 1 #include "mbed.h"
Sissors 0:9396d3376435 2 #include "FastPWM.h"
Sissors 0:9396d3376435 3 #include "FastIO.h"
Sissors 0:9396d3376435 4
Sissors 0:9396d3376435 5 #define TIME_REG LPC_TIM2->TC
Sissors 0:9396d3376435 6 FastPWM output(p21);
Sissors 0:9396d3376435 7 FastIn<p22> input;
Sissors 0:9396d3376435 8 void initTimer2();
Sissors 0:9396d3376435 9
Sissors 0:9396d3376435 10
Sissors 0:9396d3376435 11 int main()
Sissors 0:9396d3376435 12 {
Sissors 0:9396d3376435 13 //Ticks to be measured
Sissors 0:9396d3376435 14 unsigned int tick_width = 100;
Sissors 0:9396d3376435 15
Sissors 0:9396d3376435 16 unsigned int measured_timed[3];
Sissors 0:9396d3376435 17
Sissors 0:9396d3376435 18 initTimer2();
Sissors 0:9396d3376435 19
Sissors 0:9396d3376435 20 while (true) {
Sissors 0:9396d3376435 21 //Setup PWM
Sissors 0:9396d3376435 22 output.period_ticks(tick_width);
Sissors 0:9396d3376435 23 output.pulsewidth_ticks(tick_width/2);
Sissors 0:9396d3376435 24 wait_us(10);
Sissors 0:9396d3376435 25
Sissors 0:9396d3376435 26 //Start measuring at first rising edge
Sissors 0:9396d3376435 27 while(!input);
Sissors 0:9396d3376435 28 while(input);
Sissors 0:9396d3376435 29 measured_timed[0] = TIME_REG;
Sissors 0:9396d3376435 30 while(!input);
Sissors 0:9396d3376435 31 measured_timed[1] = TIME_REG;
Sissors 0:9396d3376435 32 while(input);
Sissors 0:9396d3376435 33 measured_timed[2] = TIME_REG;
Sissors 0:9396d3376435 34
Sissors 0:9396d3376435 35 printf("\r\nPulsewidth = %d ticks, which is %2.1fMHz\r\n", tick_width, (float)SystemCoreClock / (float)tick_width / 1000000.0f);
Sissors 0:9396d3376435 36 printf("Measured period = %d ticks, pulsewidth = %d ticks\r\n", measured_timed[2]-measured_timed[0], measured_timed[1]-measured_timed[0]);
Sissors 0:9396d3376435 37
Sissors 0:9396d3376435 38 //If more than 20% too large, consider it a fail
Sissors 0:9396d3376435 39 if ((measured_timed[2] - measured_timed[0]) > 1.2 * tick_width) {
Sissors 0:9396d3376435 40 printf("Period setting failed\r\n");
Sissors 0:9396d3376435 41 while(1);
Sissors 0:9396d3376435 42 }
Sissors 0:9396d3376435 43
Sissors 0:9396d3376435 44 //Setup next tick
Sissors 0:9396d3376435 45 tick_width--;
Sissors 0:9396d3376435 46
Sissors 0:9396d3376435 47 }
Sissors 0:9396d3376435 48 }
Sissors 0:9396d3376435 49
Sissors 0:9396d3376435 50 //Semi copy-pasted from: https://mbed.org/users/garyr/code/Counter/file/e619b6823668/Timer2.cpp
Sissors 0:9396d3376435 51 //We init timer at full clock speed, don't do anything else
Sissors 0:9396d3376435 52 void initTimer2() {
Sissors 0:9396d3376435 53 LPC_SC->PCONP |= (1<<22); // Power on the Timer2
Sissors 0:9396d3376435 54 LPC_SC->PCLKSEL1 &= ~(3<<12);
Sissors 0:9396d3376435 55 LPC_SC->PCLKSEL1 |= (1<<12); // Select CCLK for Timer2
Sissors 0:9396d3376435 56 LPC_TIM2->TCR = 2;
Sissors 0:9396d3376435 57 LPC_TIM2->TCR = 1; // Enable Timer2
Sissors 0:9396d3376435 58 }