Advertisement
Guest User

msp430_freq_divider

a guest
Jun 24th, 2014
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.73 KB | None | 0 0
  1. #include <msp430.h>
  2. #include <legacymsp430.h>
  3.  
  4. // dividers from which one is selected with the dip switch input
  5. const unsigned char dividers[4] = { 12, 13, 14, 15 };
  6.  
  7. volatile unsigned char hi_cycles; // number of input cycles to leave output high
  8. volatile unsigned char lo_cycles; // number of input cycles to leave output low
  9. volatile unsigned char count;     // current count of input cycles
  10. volatile unsigned char dir;       // flag indicating whether the next transition
  11.                                   //  will be low-to-high or high-to-low
  12.  
  13. int main(void)
  14. {
  15.     unsigned char idx = 0;
  16.  
  17.     WDTCTL = WDTPW + WDTHOLD; // disable the watchdog
  18.  
  19.     hi_cycles = dividers[1] / 2;
  20.     lo_cycles = dividers[1] - hi_cycles;
  21.     count = 0;
  22.     dir = 0;
  23.  
  24.     P1DIR |= BIT7;           // pulsed output
  25.  
  26.     P1OUT &= ~(BIT4 + BIT5);
  27.     P1REN |=  (BIT4 + BIT5); // enable internal pull-down for switch input
  28.  
  29.     P1OUT &= ~BIT7; // init P1.7 to the low state
  30.  
  31.     P1IES &= ~BIT3; // set interrupt to low-to-hi transition for P1.3
  32.     P1IFG &= ~BIT3; // clear the interrupt flag
  33.     P1IE  |=  BIT3; // enable the interrupt
  34.  
  35.     __enable_interrupt();
  36.  
  37.     while(1)
  38.     {
  39.         idx = ((P1IN & BIT5) >> 4) | ((P1IN & BIT4) >> 4);
  40.         hi_cycles = dividers[idx] >> 1;
  41.         lo_cycles = dividers[idx] - hi_cycles;
  42.     }
  43. }
  44.  
  45. #pragma vector=PORT1_VECTOR
  46. __interrupt void Port_1(void)
  47. {
  48.     if (dir == 0)
  49.     {
  50.         if (++count >= hi_cycles)
  51.         {
  52.             P1OUT &= ~BIT7;
  53.             dir = 1;
  54.             count = 0;
  55.         }
  56.     }
  57.     else
  58.     {
  59.         if (++count >= lo_cycles)
  60.         {
  61.             P1OUT |= BIT7;
  62.             dir = 0;
  63.             count = 0;
  64.         }
  65.     }
  66.  
  67.     P1IFG &= ~BIT3;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement