Jump to content

basic demo: clocks,timers,interrupts

Recommended Posts

This is probably redundant to some/most, this is just some of my test code on interpreting ti's docs on clocks, timers & interrupts into c, all their example code was in asm which wasn't too helpful for me as I'm pretty much starting from scratch, its been a long time since I've done any programming.

Its mostly just setting up the clocks,timers, interrupts, and not much of a demo: it uses 3 clocks and blinks. Cpu/software@15.25mhz, wdt, & timer_a1. Wdt & timer_a1 are sourced from aclk/vlo@12khz. software clock runs somewhere from 600ms-700ms, wdt@ ~667ms , timer_a1 @ ~5.46seconds(65545@12khz). The software clock goes in/out of sync with the wdt clock. I didn't put a stop watch to the wdt, or timera1 to check for accuracy, if ti's docs &my math are correct wdt/timera1 should be good enough for my purposes

Anyways here's my code I hope it helps someone:

#include "msp430.h" 
int main(void)
   P1DIR = 1|2|4|8|16|32|64|128;  P1OUT=32|64; // p1.x dir=output
/*CPU, CLOCK SETTINGS*/    //SET CPU @ ~15.25MHZ  refer to chip datasheet for speed settings
   DCOCTL  = DCO1|DCO0 ;                  //DCOx = 3, MODx=0
   BCSCTL1 = RSEL3|RSEL2|RSEL1|RSEL0;      //RSELx =15
/*CLK Dividers, _3:/8 _2:/4 _1:/2 _0:/1 */
   BCSCTL3 = LFXT1S_2;                     //ACLK=VLO:12kHz
   _BIS_SR(GIE);                           // enable interrupts
   WDTCTL  = WDT_ADLY_250;                // ~667ms@12khz  using aclock:vlo
   IE1    |= WDTIE;                           //enable wdt interrupt
   TACTL   = TASSEL_1|ID_0|MC_2|TAIE;      //aclk:vlo|/0| mode:cont.| TimerA int
   for(;{  //infinite loop
         int ms, us; //time scale
         for (ms=0;ms<34;ms++)  //software delay
            for (us=0;us<0xffff;us++); 
         P1OUT ^= 1|2; //flashes p1.0,p1.1  
#pragma vector = WDT_VECTOR,TIMERA1_VECTOR   
__interrupt void interrupts (void) {
   switch(TAIV){     //check timer_a1 interrupt cause
     case TAIV_NONE:  //check other interrupts?
     case TAIV_TAIFG:
      P1OUT ^=64|128;   // flashes p1.6,1.7 @ 65545/12khz =~5.46seconds
  if (IFG1|=WDTIFG){              //check wdt interrupt flag
       P1OUT ^=4|8|16|32;  //flashes p1.3-1.5 @ 250ms*32khz/12khz=~667ms
  /*clear interrupts*/
       TACTL &= ~TAIFG;
       IFG1 &= ~WDTIFG;    

Link to post
Share on other sites
  • 5 months later...

Sean, I would clear interrupt flags for the given interrupt inside conditionals. For example timer's IF could be set while servicing WDT interrupt, then it would be cleared, and in turn that interrupt would not be handled.

Alternatively, you could use 2 separate ISRs.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...