Las luces del coche fantástico con 4 leds. El bucle principal del programa avanza un led en cada ciclo y entra en LPM4. La interrupción del Timer se encarga de reanudar la ejecución.
src/grace/GPIO_init.c
P1.0 tiene que configurarse como TA0CLK.
/*
* ======== GPIO_graceInit ========
* Initialize MSP430 General Purpose Input Output Ports
*
* The GPIO registers should be set in a specific order:
* PxOUT
* PxSEL or PxSELx
* PxDIR
* PxREN
* PxIES
* PxIFG
* PxIE
*
* This will ensure that:
* - IFG doesn't get set by manipulating the pin function, edge
* select, and pull-up/down resistor functionalities (see
* Section 8.2.6 of the MSP430 User's manual)
* - Glitch-free setup (configuring the OUT register _before_
* setting the pin direction)
* - Pull-up/pull-down resistor enable with the correct direction
* (.up. vs. .down.)
*/
void GPIO_graceInit(void)
{
/* USER CODE START (section: GPIO_graceInit_prologue) */
/* User initialization code */
/* USER CODE END (section: GPIO_graceInit_prologue) */
/* Port 1 Output Register */
P1OUT = 0;
/* Port 1 Port Select Register */
P1SEL = BIT0;
/* Port 1 Direction Register */
P1DIR = BIT1 + BIT2 + BIT3 + BIT4 + BIT5 + BIT6 + BIT7;
/* Port 1 Interrupt Edge Select Register */
P1IES = 0;
/* Port 1 Interrupt Flag Register */
P1IFG = 0;
/* Port 2 Output Register */
P2OUT = 0;
/* Port 2 Port Select Register */
P2SEL &= ~(BIT6 + BIT7);
/* Port 2 Direction Register */
P2DIR = BIT0 + BIT1 + BIT2 + BIT3 + BIT4 + BIT5 + BIT6 + BIT7;
/* Port 2 Interrupt Edge Select Register */
P2IES = 0;
/* Port 2 Interrupt Flag Register */
P2IFG = 0;
}
src/grace/Timer0_A3_init.c
/*
* ======== Timer0_A3_graceInit ========
* Initialize MSP430 Timer0_A3 timer
*/
void Timer0_A3_graceInit(void)
{
/*
* TA0CCTL0, Capture/Compare Control Register 0
*
* CM_0 -- No Capture
* CCIS_0 -- CCIxA
* ~SCS -- Asynchronous Capture
* ~SCCI -- Latched capture signal (read)
* ~CAP -- Compare mode
* OUTMOD_0 -- PWM output mode: 0 - OUT bit value
*
* Note: ~<BIT> indicates that <BIT> has value zero
*/
TA0CCTL0 = CM_0 + CCIS_0 + OUTMOD_0 + CCIE;
/* TA0CCR0, Timer_A Capture/Compare Register 0 */
TA0CCR0 = 3199;
/*
* TA0CTL, Timer_A3 Control Register
*
* TASSEL_0 -- TACLK
* ID_0 -- Divider - /1
* MC_1 -- Up Mode
*/
TA0CTL = TASSEL_0 + ID_0 + MC_1;
}
src/grace/InterruptVectors_init.c
La interrupción despierta al MCU. El cuerpo del programa está en main().
/*
* ======== Timer0_A3 Interrupt Service Routine ========
*/
#pragma vector=TIMER0_A0_VECTOR
__interrupt void TIMER0_A0_ISR_HOOK(void)
{
/* USER CODE START (section: TIMER0_A0_ISR_HOOK) */
// Continua con el programa principal
__bic_SR_register_on_exit(CPUOFF);
/* USER CODE END (section: TIMER0_A0_ISR_HOOK) */
}
main.c
unsigned int contador = 0;
int dir=1;
/*
* ======== main ========
*/
int main(void)
{
Grace_init(); // Activate Grace-generated configuration
while (1) {
contador = contador + dir;
if (contador==4){
dir = -1;
}
if (contador == 1){
dir = 1;
}
P1OUT = (1 << contador);
LPM4;
}
}