#include <msp430x14x.h>

#include "basic.h"
#include "config.h"
#include "timerb.h"

// ADC12 sample rate: TBCLK/TBCCR0

int timerbInit(void)
{

  TBCTL = TBSSEL0 + TBCLR;                    // TBCLK = ACLK, clear TBR
  TBCCTL1 = OUTMOD_3;                         // TRCCR1 Set/Reset

#ifdef CONFIG_TIMERB_4HZ
  /* 4 Hz */
  TBCCR0 = 2048; // 32hz timerb --> 5.32 hz sampling
  TBCCR1 = 1536; // TBOUTx is on after this many clocks
#endif

#ifdef CONFIG_TIMERB_8HZ
  /* 8 Hz */
  TBCCR0 = 1024; // 32hz timerb --> 5.32 hz sampling
  TBCCR1 = 768; // TBOUTx is on after this many clocks
#endif


  return 0;
}

void timerbStart(void)
{
  TBCTL |= MC0;                               // Start Timer_B in up mode
}

void timerbStop(void)
{
  TBCTL &= ~(0x0030);                         // Stop Timer_B
}
