• FreqT5A1.js Measuring Frequency Using Timer 5 and Pin A1
    Uses Pin B0 as a frequency source to test.

    /* 
    FreqT5A1.js
    Frequency measurement using
    Pin A1 on Timer 5
    Use Pin B0 of  Timer3 as a source of square waves to measure
    Connect Pin B0 to Pin A1
    
    zero counter,
    start counter,
    count for timebase ms,
    stop counter,
    display count as frequency in Hz as adjusted by timebase
    repeat
    
    //Now displays timers 1 thru 14
    This was run on an Espruino board
    
    To clear the timer requires the board to be powered down or the reset button
    
    */
    /*
    http://www.keil.com/dd/docs/datashts/st/­stm32f10xxx.pdf
    */
    var TIM5=0x40000C00;
    //Timer register offsets
    var CR1=0x0;
    var CR2=0x04;
    var SMCR=0x08;
    var DIER=0x0c;
    var SR=0x10;
    var EGR=0x14;
    var CCMR1=0x18;
    var CCMR2=0x1c;
    var CCER=0x20;
    var CNT=0x24;
    var PSC=0x28;
    var ARR=0x2c;
    var CCR1=0x34;
    var CCR2=0x38;
    var CCR3=0x3c;
    var CCR4=0x40;
    var DCR=0x48;
    var DMAR=0x4c;
    
    // use B0 as a frequency source to measure
    analogWrite(B0,0.5,{ freq : 1000 });
    
    //setup pin A1 as the counter input
    A1.mode('input');
    
    //timer 5 clock
    var RCCbase=0x40021000;
    var APB1ENR=RCCbase+0x1c;
    var APB1RST=RCCbase+0x10;
    var APB2ENR=RCCbase+0x18;
    
    //turn on timer 5 clock
     poke8(APB1ENR,(peek8(APB1ENR)&0xf7)|0x08­);
    
    //reset timer 5
     poke8(APB1RST,(peek8(APB1RST)&0xf7)|0x08­);
     poke8(APB1RST,(peek8(APB1RST)^0x08));
    
    //disable timer 5
      poke32(TIM5+CR1,0x80);
    
    //setup timer 5
    poke16(TIM5+CCMR1,0x0100);
    poke16(TIM5+CCER,0x0000); 
    poke16(TIM5+SMCR,0x0067);
    poke32(TIM5+CNT,0);
    //Preload timer
    poke32(TIM5+EGR,1);
    //Enable timer 5
    //poke32(TIM5+CR1,0x81); 
    
    var timebase=2000;
    setInterval(function () {
      poke32(TIM5+CR1,0x80);
      console.log("CNT="+peek32(TIM5+CNT)*1000­/timebase +" Hz.");
      poke32(TIM5+CNT,1);
      poke32(TIM5+CR1,0x81);
    }, timebase);
    
    

    1 Attachment

About