Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Fix __delay_cycles explanation

...

I've already explained the PxOUT and PxDIR registers, so we'll skip over them and look at the for loop. In the for loop, we are toggling bit 0 of P1OUT using the XOR operator (See Clearing, Toggling, Setting, and Checking a Bit) and then we call a special function, __delay_cycles.

Code Block
languagecpp
themeConfluence
    __delay_cycles(100001000000);


This function delays the processor for x cycles. In our case, we chose 1,000,000 because MCLK runs at 1Mhz by default so it should be around 1 second of delay. So, in our loop, we toggle the state of the LED (if it's on, it turns off, and if it's off, it turns on) and then we wait for a little bit. This creates the effect of the LED blinking.

...

At the beginning of the main function, we can see the IO and timer setup code. The IO configuration is the same as before, but the timer setup is new.

First, we setup the Timer A control register (TA0CTL) to use SMCLK  SMCLK  (TASSEL_2), up-down mode (MC_2), and a frequency divider of 8 (ID_3). Next, we set the Timer A capture/compare control register (TA0CCTL0) to enable interrupts locally for the timer. After that, we set the value we will be counting to by setting TA0CCR0 to 65535 (the maximum value it can store). This will cause the timer to start counting. Lastly, we globally enable interrupts by calling __enable_interrupt and loop forever.

Here at the bottom in the #pragma section, we can see the ISR we created for Timer A interrupts. All this service routine does is toggle the state of the LED to make it blink.

...