lab 7
定时器并行
#include <ti/devices/msp432p4xx/driverlib/driverlib.h>
#include <stdint.h>
#include <stdbool.h>
int main(void)
{
MAP_WDT_A_holdTimer();
MAP_FPU_enableModule();
MAP_CS_setDCOFrequency(1000000);
MAP_CS_initClockSignal(CS_MCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_1);
MAP_GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN0);
MAP_GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN0);
MAP_GPIO_setAsOutputPin(GPIO_PORT_P2, GPIO_PIN1);
MAP_GPIO_setOutputHighOnPin(GPIO_PORT_P2, GPIO_PIN1);
MAP_Timer32_initModule(TIMER32_0_BASE, TIMER32_PRESCALER_1, TIMER32_32BIT,TIMER32_PERIODIC_MODE);
MAP_Timer32_initModule(TIMER32_1_BASE, TIMER32_PRESCALER_1, TIMER32_32BIT,TIMER32_PERIODIC_MODE);
MAP_Interrupt_enableInterrupt(INT_T32_INT1);
MAP_Timer32_enableInterrupt(TIMER32_0_BASE);
MAP_Interrupt_enableInterrupt(INT_T32_INT2);
MAP_Timer32_enableInterrupt(TIMER32_1_BASE);
MAP_Interrupt_enableMaster();
MAP_Timer32_setCount(TIMER32_0_BASE,4000000);
MAP_Timer32_setCount(TIMER32_1_BASE,4000000);
MAP_Timer32_startTimer(TIMER32_0_BASE, false);
MAP_Timer32_startTimer(TIMER32_1_BASE, false);
while(1);
}
void T32_INT1_IRQHandler(void)
{
MAP_Timer32_clearInterruptFlag(TIMER32_0_BASE);
MAP_GPIO_toggleOutputOnPin(GPIO_PORT_P1,GPIO_PIN0);
}
void T32_INT2_IRQHandler(void)
{
MAP_Timer32_clearInterruptFlag(TIMER32_1_BASE);
MAP_GPIO_toggleOutputOnPin(GPIO_PORT_P2,GPIO_PIN1);
}
交通信号灯模拟
#include <string.h>
#include <ti/devices/msp432p4xx/driverlib/driverlib.h>
#define COUNT (25000)
typedef enum
{
DETECT = 0,
S1,
S2,
S1S2
} KEY_STATUS;
KEY_STATUS key = DETECT;
int time = COUNT;
int cnt = 0;
int choose = 0;
int flag = 0;
int main(void)
{
MAP_WDT_A_holdTimer();
MAP_FPU_enableModule();
MAP_CS_setDCOFrequency(1000000);
GPIO_setOutputHighOnPin(GPIO_PORT_P2, GPIO_PIN1);
MAP_Timer32_initModule(TIMER32_0_BASE, TIMER32_PRESCALER_1, TIMER32_32BIT, TIMER32_PERIODIC_MODE);
MAP_Interrupt_enableInterrupt(INT_T32_INT1);
MAP_Timer32_enableInterrupt(TIMER32_0_BASE);
MAP_GPIO_setAsInputPinWithPullUpResistor(GPIO_PORT_P1, GPIO_PIN1);
MAP_GPIO_enableInterrupt(GPIO_PORT_P1, GPIO_PIN1);
MAP_GPIO_clearInterruptFlag(GPIO_PORT_P1, GPIO_PIN1);
MAP_GPIO_setAsInputPinWithPullUpResistor(GPIO_PORT_P1, GPIO_PIN4);
MAP_GPIO_enableInterrupt(GPIO_PORT_P1, GPIO_PIN4);
MAP_GPIO_clearInterruptFlag(GPIO_PORT_P1, GPIO_PIN4);
MAP_Interrupt_enableInterrupt(INT_PORT1);
GPIO_setAsOutputPin(GPIO_PORT_P2, GPIO_PIN0);
GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN0);
GPIO_setAsOutputPin(GPIO_PORT_P2, GPIO_PIN0);
GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN0);
GPIO_setAsOutputPin(GPIO_PORT_P2, GPIO_PIN1);
GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN1);
GPIO_setAsOutputPin(GPIO_PORT_P2, GPIO_PIN1);
GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN1);
GPIO_setOutputHighOnPin(GPIO_PORT_P2, GPIO_PIN1);
MAP_Timer32_setCount(TIMER32_0_BASE, 5000000);
MAP_Timer32_startTimer(TIMER32_0_BASE, false);
MAP_Interrupt_enableMaster();
while (1)
{
if (key == DETECT)
{
while (time--)
{
if (key == S1S2)
break;
}
time = COUNT;
}
else
{
if (key == S1S2)
{
choose = 0;
MAP_Timer32_clearInterruptFlag(TIMER32_0_BASE);
GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN0);
GPIO_setOutputHighOnPin(GPIO_PORT_P2, GPIO_PIN1);
MAP_Timer32_setCount(TIMER32_0_BASE, 5000000);
MAP_Timer32_startTimer(TIMER32_0_BASE, false);
MAP_Timer32_enableInterrupt(TIMER32_0_BASE);
}
else if (key == S2)
{
Timer32_disableInterrupt(TIMER32_0_BASE);
GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN0|GPIO_PIN1|GPIO_PIN2);
GPIO_setOutputHighOnPin(GPIO_PORT_P2, GPIO_PIN1);
}
else if (key == S1)
{
Timer32_disableInterrupt(TIMER32_0_BASE);
GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN0|GPIO_PIN1|GPIO_PIN2);
GPIO_setOutputHighOnPin(GPIO_PORT_P2, GPIO_PIN0);
}
key = DETECT;
}
}
}
void T32_INT1_IRQHandler(void)
{
MAP_Timer32_clearInterruptFlag(TIMER32_0_BASE);
if (choose == 0)
{
MAP_Timer32_setCount(TIMER32_0_BASE, 1500000);
MAP_Timer32_startTimer(TIMER32_0_BASE, false);
GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN0|GPIO_PIN1|GPIO_PIN2);
GPIO_setOutputHighOnPin(GPIO_PORT_P2, GPIO_PIN0|GPIO_PIN1);
}
if (choose == 1)
{
MAP_Timer32_setCount(TIMER32_0_BASE, 2500000);
MAP_Timer32_startTimer(TIMER32_0_BASE, false);
GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN0|GPIO_PIN1|GPIO_PIN2);
GPIO_setOutputHighOnPin(GPIO_PORT_P2, GPIO_PIN0);
}
if (choose == 2)
{
MAP_Timer32_setCount(TIMER32_0_BASE, 5000000);
MAP_Timer32_startTimer(TIMER32_0_BASE, false);
GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN0|GPIO_PIN1|GPIO_PIN2);
GPIO_setOutputHighOnPin(GPIO_PORT_P2, GPIO_PIN1);
}
choose = (choose + 1) % 3;
}
void PORT1_IRQHandler(void)
{
uint32_t status;
status = MAP_GPIO_getEnabledInterruptStatus(GPIO_PORT_P1);
MAP_GPIO_clearInterruptFlag(GPIO_PORT_P1, status);
if ((status & GPIO_PIN1) && (status & GPIO_PIN4))
{
key = S1S2;
}
else if (status & GPIO_PIN1)
{
if (key == DETECT)
{
key = S1;
time = COUNT;
}
else if (key == S2)
key = S1S2;
}
else if (status & GPIO_PIN4)
{
if (key == DETECT)
{
key = S2;
time = COUNT;
}
else if (key == S1)
key = S1S2;
}
}