void shortDelay() { Timer_A_initUpModeParam tParams; tParams.clockSource = TIMER_A_CLOCKSOURCE_ACLK; tParams.clockSourceDivider = TIMER_A_CLOCKSOURCE_DIVIDER_1; tParams.timerPeriod = 8196; tParams.timerInterruptEnable_TAIE = TIMER_A_TAIE_INTERRUPT_DISABLE; tParams.captureCompareInterruptEnable_CCR0_CCIE = TIMER_A_CAPTURECOMPARE_INTERRUPT_ENABLE; tParams.timerClear = TIMER_A_DO_CLEAR; tParams.startTimer = 0; //start timer Timer_A_clearTimerInterrupt(TIMER_A1_BASE); Timer_A_initUpMode(TIMER_A1_BASE, &tParams); Timer_A_startCounter(TIMER_A1_BASE, TIMER_A_UP_MODE); bDelayDone = 0; while( bDelayDone == 0) LPM3; Timer_A_stop(TIMER_A1_BASE); }
/* * ======== Timer0_A3_graceInit ======== * Initialize Config for the MSP430 A3 Timer 0 */ void Timer0_A3_graceInit(void) { /* Struct to pass to Timer_A_initUpMode */ Timer_A_initUpModeParam initUpParam = {0}; /* USER CODE START (section: Timer0_A3_graceInit_prologue) */ /* User initialization code */ /* USER CODE END (section: Timer0_A3_graceInit_prologue) */ /* Initialize TimerA in up mode */ initUpParam.clockSource = TIMER_A_CLOCKSOURCE_ACLK; initUpParam.clockSourceDivider = TIMER_A_CLOCKSOURCE_DIVIDER_1; initUpParam.timerPeriod = 7; initUpParam.timerInterruptEnable_TAIE = TIMER_A_TAIE_INTERRUPT_ENABLE; initUpParam.captureCompareInterruptEnable_CCR0_CCIE = TIMER_A_CCIE_CCR0_INTERRUPT_DISABLE; initUpParam.timerClear = TIMER_A_SKIP_CLEAR; initUpParam.startTimer = false; Timer_A_initUpMode(TIMER_A0_BASE, &initUpParam); /* Start TimerA counter */ Timer_A_startCounter(TIMER_A0_BASE, TIMER_A_UP_MODE); /* USER CODE START (section: Timer0_A3_graceInit_epilogue) */ /* User code */ /* USER CODE END (section: Timer0_A3_graceInit_epilogue) */ }
void Setup1msTimer(void) { //Start timer in continuous mode sourced by SMCLK //Timer_A_initContinuousModeParam initUpParam = {0}; Timer_A_initUpModeParam initUpParam = {0}; initUpParam.clockSource = TIMER_A_CLOCKSOURCE_SMCLK; initUpParam.clockSourceDivider = TIMER_A_CLOCKSOURCE_DIVIDER_1; initUpParam.timerInterruptEnable_TAIE = TIMER_A_TAIE_INTERRUPT_DISABLE; initUpParam.timerClear = TIMER_A_DO_CLEAR; initUpParam.startTimer = false; Timer_A_initUpMode(TIMER_A1_BASE, &initUpParam); //Initiaze compare mode Timer_A_clearCaptureCompareInterrupt(TIMER_A1_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_0); Timer_A_initCompareModeParam initCompParam = {0}; initCompParam.compareRegister = TIMER_A_CAPTURECOMPARE_REGISTER_0; initCompParam.compareInterruptEnable = TIMER_A_CAPTURECOMPARE_INTERRUPT_ENABLE; initCompParam.compareOutputMode = TIMER_A_OUTPUTMODE_OUTBITVALUE; initCompParam.compareValue = UCS_getSMCLK()/1e3; Timer_A_initCompareMode(TIMER_A1_BASE, &initCompParam); Timer_A_startCounter(TIMER_A1_BASE,TIMER_A_UP_MODE); }
void initTimer(void) { setTimer_A_Parameters(); //start timer Timer_A_clearTimerInterrupt(TIMER_A0_BASE); Timer_A_initUpMode(TIMER_A0_BASE, &Timer_A_params); Timer_A_startCounter(TIMER_A0_BASE, TIMER_A_UP_MODE); }
int main(void) { WDT_A_holdTimer(); //Configure Timer unsigned int dcoFrequency = 3E+6; MAP_CS_setDCOFrequency(dcoFrequency); MAP_CS_initClockSignal(CS_SMCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_1); // ( 1Mhz / 64 ) / (Period = 15625) = 1 second //////////////////////////////////////////////////////////////////////////////////////////////////// // Buttons init //////////////////////////////////////////////////////////////////////////////////////////////////// // Set switch 1 (S1) as input button (connected to P1.1) MAP_GPIO_setAsInputPinWithPullUpResistor( GPIO_PORT_P1, GPIO_PIN1 ); // Set switch 2 (S2) as input button (connected to P1.4) MAP_GPIO_setAsInputPinWithPullUpResistor( GPIO_PORT_P1, GPIO_PIN4 ); //////////////////////////////////////////////////////////////////////////////////////////////////// // Timer A 0 and PWM on 2.4 //////////////////////////////////////////////////////////////////////////////////////////////////// GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P2, GPIO_PIN4, GPIO_PRIMARY_MODULE_FUNCTION); //writes 0's in PXSEL registers for pins 1.2 and 1.3 // Set Timer A period (PWM signal period) TA0CCR0 = 10000 ; // i think i wrote this wring, his example he showed in class is is 10000 // Set Duty cycle TA0CCR1 = 0 ; //inital DutyCycle of 0% (we don't want the motor to move unless we push button // Set output mode to Reset/Set TA0CCTL1 = OUTMOD_7 ; // Macro which is equal to 0x00e0, defined in msp432p401r.h // Initialize Timer A TA0CTL = TASSEL__SMCLK | MC__UP | TACLR ; // this bitwise or’s multiple settings at the same time (this just sets different bits in the register to set these functionalities/settings) //Timer_A_startCounter(TIMER_A0_MODULE, TIMER_A_UP_MODE); //////////////////////////////////////////////////////////////////////////////////////////////////// // Timer A 2 and PWM on P5.6 //////////////////////////////////////////////////////////////////////////////////////////////////// GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P5, GPIO_PIN6, GPIO_PRIMARY_MODULE_FUNCTION); //writes 0's in PXSEL registers for pins 1.2 and 1.3 // Set Timer A period (PWM signal period) TA2CCR0 = 10000 ; // i think i wrote this wring, his example he showed in class is is 10000 // Set Duty cycle TA2CCR1 = 0 ; //inital DutyCycle of 0% (we don't want the motor to move unless we push button // Set output mode to Reset/Set TA2CCTL1 = OUTMOD_7 ; // Macro which is equal to 0x00e0, defined in msp432p401r.h // Initialize Timer A TA2CTL = TASSEL__SMCLK | MC__UP | TACLR ; // this bitwise or’s multiple settings at the same time (this just sets different bits in the register to set these functionalities/settings) //Does this start the timer?? (is next line necessary?) //Timer_A_startCounter(TIMER_A1_MODULE, TIMER_A_UP_MODE); ///////////////////////////////////////////////////////////////////////////////////////////////////// //Set LED's as outputs and turn off MAP_GPIO_setAsOutputPin(GPIO_PORT_P1,GPIO_PIN0); MAP_GPIO_setAsOutputPin(GPIO_PORT_P2,GPIO_PIN0); MAP_GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN0); MAP_GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN0); int countedges1 = 1; int countedges2 = 1; while(1){ //infinite loop //////////////////////////////////////////////////////////////////////////////////////////////////// // Turn one direction //////////////////////////////////////////////////////////////////////////////////////////////////// if(GPIO_INPUT_PIN_LOW == MAP_GPIO_getInputPinValue ( GPIO_PORT_P1, GPIO_PIN1 )){ //stop timer 2 Timer_A_stopTimer(TIMER_A2_MODULE); MAP_GPIO_setOutputLowOnPin(GPIO_PORT_P5, GPIO_PIN6); //start timer 1 Timer_A_startCounter(TIMER_A0_MODULE, TIMER_A_UP_MODE); TA0CCR1 = 1000 * countedges1; //increse Duty Cycle by 10% (period is 10000) if (TA0CCR1 > TA0CCR0){ //if Dutcy Cycle = 100%, go back to 0% TA0CCR1 = 0; }//end nested if if (countedges1>11){ countedges1 = 0; } countedges1++; while(GPIO_INPUT_PIN_LOW == MAP_GPIO_getInputPinValue ( GPIO_PORT_P1, GPIO_PIN1 )){ MAP_GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN0); MAP_GPIO_setOutputHighOnPin(GPIO_PORT_P2, GPIO_PIN0); }//end nested while MAP_GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN0); MAP_GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN0); }//end switch 1 if else{ //the button is not being pushed so do not spin motor (DutyCycle = 0); TA0CCR1 = 0; } //////////////////////////////////////////////////////////////////////////////////////////////////// // Turn other direction //////////////////////////////////////////////////////////////////////////////////////////////////// if(GPIO_INPUT_PIN_LOW == MAP_GPIO_getInputPinValue ( GPIO_PORT_P1, GPIO_PIN4 )){ //stop timer 1 Timer_A_stopTimer(TIMER_A0_MODULE); MAP_GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN4); //start timer 2 Timer_A_startCounter(TIMER_A2_MODULE, TIMER_A_UP_MODE); TA2CCR1 = countedges2 * 1000; //increse Duty Cycle by 10% (period is 10000) if (TA2CCR1 > TA2CCR0){ //if Dutcy Cycle = 100%, go back to 0% TA2CCR1 = 0; }//end nested if if (countedges2>11){ countedges2 = 0; } countedges2++; while(GPIO_INPUT_PIN_LOW == MAP_GPIO_getInputPinValue ( GPIO_PORT_P1, GPIO_PIN4 )){ MAP_GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN0); MAP_GPIO_setOutputHighOnPin(GPIO_PORT_P2, GPIO_PIN0); }//end nested while MAP_GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN0); MAP_GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN0); }//end switch 2 if else{ //the button is not being pushed so do not spin motor (DutyCycle = 0) TA2CCR1 = 0; } }//end while }//end main
/* * ======== main ======== */ int main (void) { WDT_A_hold(WDT_A_BASE); // Stop watchdog timer // Minumum Vcore setting required for the USB API is PMM_CORE_LEVEL_2 . PMM_setVCore(PMM_CORE_LEVEL_2); USBHAL_initPorts(); // Config GPIOS for low-power (output low) USBHAL_initClocks(MCLK_FREQUENCY); // Config clocks. MCLK=SMCLK=FLL=MCLK_FREQUENCY; ACLK=REFO=32kHz hal_sd_pwr_on(); initTimer(); USB_setup(FALSE, TRUE); // Init USB & events; if a host is present, connect __enable_interrupt(); // Enable interrupts globally // GPS_init(); // state machine while (1) { switch( state ) { case sIDLE: hal_led_a(0); hal_led_b(0); hal_gps_pwr_off(); hal_sd_pwr_off(); UCS_turnOffXT2(); /* USB connected */ if(USB_getConnectionInformation() & USB_VBUS_PRESENT) { hal_led_a(CYAN); //PMM_setVCore(PMM_CORE_LEVEL_2); hal_sd_pwr_on(); shortDelay(); USBMSC_initMSC(); // Initialize MSC API, and report media to the host if (USB_enable() == USB_SUCCEED){ state = sUSB; hal_led_a(GREEN); //hal_sd_pwr_on(); //detectCard(); USB_reset(); USB_connect(); //generate rising edge on DP -> the host enumerates our device as full speed device } break; // don't enter sleep } /* start GPS */ if(hal_button_event()) { /* delay for starting */ hal_led_a(RED); uint8_t timeout = 16; while( hal_button_status() == 1 && --timeout ) { shortDelay(); } hal_led_a(0); if( hal_button_status() == 0 ) break; state = sGPS; hal_led_a(CYAN); hal_sd_pwr_on(); timeout = 8; while( --timeout ) { shortDelay(); } detectCard(); Timer_A_startCounter(TIMER_A0_BASE, TIMER_A_UP_MODE); gps_start(); hal_button_event(); break; // don't enter sleep } USB_disable(); //Disable hal_gps_rtc_on(); // saves around 7uA Timer_A_stop(TIMER_A0_BASE); //UCS_turnOffSMCLK(); //PMM_setVCore(PMM_CORE_LEVEL_0); __bis_SR_register(LPM4_bits + GIE); _NOP(); //UCS_turnOnSMCLK(); //PMM_setVCore(PMM_CORE_LEVEL_2); break; case sGPS: /* stop GPS */ if((USB_getConnectionInformation() & USB_VBUS_PRESENT)) { state = sIDLE; gps_stop(); break; } if(hal_button_event()) { /* delay for stopping */ uint8_t timeout = 16; while( hal_button_status() == 1 && --timeout ) { hal_led_a(RED); shortDelay(); hal_led_a(RED); } hal_led_a(0); if( hal_button_status() == 0 ) break; state = sIDLE; gps_stop(); break; } if (bDetectCard){ USBMSC_checkMSCInsertionRemoval(); // Clear the flag, until the next timer ISR bDetectCard = 0x00; } while( gps_check() ) { gps_do(); } __bis_SR_register(LPM0_bits + GIE); _NOP(); break; case sUSB: if(!(USB_getConnectionInformation() & USB_VBUS_PRESENT)) { state = sIDLE; break; } /* check state of chareger? */ if( hal_charge_status()) hal_led_b(RED); else hal_led_b(GREEN); hal_button_event(); // clear button event switch (USB_getConnectionState()) { case ST_ENUM_ACTIVE: USBMSC_processMSCBuffer(); // Handle READ/WRITE cmds from the host // Every second, the Timer_A ISR sets this flag. The // checking can't be done from within the timer ISR, because it // enables interrupts, and this is not a recommended // practice due to the risk of nested interrupts. if (bDetectCard){ USBMSC_checkMSCInsertionRemoval(); // Clear the flag, until the next timer ISR bDetectCard = 0x00; } break; // These cases are executed while your device is disconnected from // the host (meaning, not enumerated); enumerated but suspended // by the host, or connected to a powered hub without a USB host // present. case ST_PHYS_DISCONNECTED: case ST_ENUM_SUSPENDED: case ST_PHYS_CONNECTED_NOENUM_SUSP: hal_led_a(BLUE); //state = sIDLE; break; // The default is executed for the momentary state // ST_ENUM_IN_PROGRESS. Usually, this state only last a few // seconds. Be sure not to enter LPM3 in this state; USB // communication is taking place here, and therefore the mode must // be LPM0 or active-CPU. case ST_ENUM_IN_PROGRESS: default:; } break; } } }
void tempSensor() { //Initialize the ADC Module /* * Base Address for the ADC Module * Use Timer trigger 1 as sample/hold signal to start conversion * USE MODOSC 5MHZ Digital Oscillator as clock source * Use default clock divider of 1 */ ADC_init(ADC_BASE, ADC_SAMPLEHOLDSOURCE_2, ADC_CLOCKSOURCE_ADCOSC, ADC_CLOCKDIVIDER_1); ADC_enable(ADC_BASE); //Configure Memory Buffer /* * Base Address for the ADC Module * Use input A12 Temp Sensor * Use positive reference of Internally generated Vref * Use negative reference of AVss */ ADC_configureMemory(ADC_BASE, ADC_INPUT_TEMPSENSOR, ADC_VREFPOS_INT, ADC_VREFNEG_AVSS); ADC_clearInterrupt(ADC_BASE, ADC_COMPLETED_INTERRUPT); // Enable the Memory Buffer Interrupt ADC_enableInterrupt(ADC_BASE, ADC_COMPLETED_INTERRUPT); ADC_startConversion(ADC_BASE, ADC_REPEATED_SINGLECHANNEL); // Enable internal reference and temperature sensor PMM_enableInternalReference(); PMM_enableTempSensor(); // TimerA1.1 (125ms ON-period) - ADC conversion trigger signal Timer_A_initUpMode(TIMER_A1_BASE, &initUpParam_A1); //Initialize compare mode to generate PWM1 Timer_A_initCompareMode(TIMER_A1_BASE, &initCompParam); // Start timer A1 in up mode Timer_A_startCounter(TIMER_A1_BASE, TIMER_A_UP_MODE ); // Delay for reference settling __delay_cycles(300000); //Enter LPM3.5 mode with interrupts enabled while(*tempSensorRunning) { __bis_SR_register(LPM3_bits | GIE); // LPM3 with interrupts enabled __no_operation(); // Only for debugger if (*tempSensorRunning) { // Turn LED1 on when waking up to calculate temperature and update display P1OUT |= BIT0; // Calculate Temperature in degree C and F signed short temp = (ADCMEM0 - CALADC_15V_30C); *degC = ((long)temp * 10 * (85-30) * 10)/((CALADC_15V_85C-CALADC_15V_30C)*10) + 300; *degF = (*degC) * 9 / 5 + 320; // Update temperature on LCD displayTemp(); P1OUT &= ~BIT0; } } // Loop in LPM3 to while buttons are held down and debounce timer is running while(TA0CTL & MC__UP) { __bis_SR_register(LPM3_bits | GIE); // Enter LPM3 __no_operation(); } if (*mode == TEMPSENSOR_MODE) { // Disable ADC, TimerA1, Internal Ref and Temp used by TempSensor Mode ADC_disableConversions(ADC_BASE,ADC_COMPLETECONVERSION); ADC_disable(ADC_BASE); Timer_A_stop(TIMER_A1_BASE); PMM_disableInternalReference(); PMM_disableTempSensor(); PMM_turnOffRegulator(); __bis_SR_register(LPM4_bits | GIE); // re-enter LPM3.5 __no_operation(); } }
int main(void) { WDT_A_holdTimer(); //Configure Timer unsigned int dcoFrequency = 3E+6; MAP_CS_setDCOFrequency(dcoFrequency); MAP_CS_initClockSignal(CS_SMCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_1); // ( 3Mhz / 64 ) / (Period = 46875) = 1 second //Set LED's as outputs and turn off MAP_GPIO_setAsOutputPin(GPIO_PORT_P1,GPIO_PIN0); MAP_GPIO_setAsOutputPin(GPIO_PORT_P2,GPIO_PIN0); MAP_GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN0); MAP_GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN0); //////////////////////////////////////////////////////////////////////////////////////////////////// // ADC INITILIZATION P5.5 //////////////////////////////////////////////////////////////////////////////////////////////////// /* Initializing ADC */ MAP_ADC14_enableModule(); GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P5, GPIO_PIN5, GPIO_TERTIARY_MODULE_FUNCTION); //writes 0's in PXSEL registers for pins 1.2 and 1.3 MAP_ADC14_setResolution(ADC_10BIT); // initialize to use a 10 Bit ADC MAP_ADC14_initModule(ADC_CLOCKSOURCE_SMCLK, ADC_PREDIVIDER_1, ADC_DIVIDER_1,0); /* Configuring ADC Memory */ MAP_ADC14_configureSingleSampleMode(ADC_MEM0, false); //put results in this 16 bit register location to hold results, false means we are manually triggering MAP_ADC14_configureConversionMemory(ADC_MEM0, ADC_VREFPOS_AVCC_VREFNEG_VSS, ADC_INPUT_A0, false); //pin 5.5 is tied to channel 0 (A0) /* Configuring Sample Timer */ MAP_ADC14_enableSampleTimer(ADC_MANUAL_ITERATION); //Timer A setup MAP_Timer_A_configureUpMode(TIMER_A0_MODULE, &upConfig0); Interrupt_enableInterrupt(INT_TA0_0); Timer_A_startCounter(TIMER_A0_MODULE, TIMER_A_UP_MODE); /* Enabling/Toggling Conversion */ MAP_ADC14_enableConversion(); MAP_ADC14_toggleConversionTrigger(); //////////////////////////////////////////////////////////////////////////////////////////////////// // END ADC //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// // Timer A 2 and PWM on P5.6 //////////////////////////////////////////////////////////////////////////////////////////////////// GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P5, GPIO_PIN6, GPIO_PRIMARY_MODULE_FUNCTION); //writes 0's in PXSEL registers for pins 1.2 and 1.3 // Set Timer A period (PWM signal period) TA2CCR0 = 10000 ; // i think i wrote this wring, his example he showed in class is is 10000 // Set Duty cycle TA2CCR1 = DUTY_CYCLE * 10000; //inital DutyCycle of 0%.. Duty_Cycle is macro // Set output mode to Reset/Set TA2CCTL1 = OUTMOD_7 ; // Macro which is equal to 0x00e0, defined in msp432p401r.h // Initialize Timer A TA2CTL = TASSEL__SMCLK | MC__UP | TACLR ; // this bitwise or’s multiple settings at the same time (this just sets different bits in the register to set these functionalities/settings) //Does this start the timer?? (is next line necessary?) Timer_A_startCounter(TIMER_A2_MODULE, TIMER_A_UP_MODE); //////////////////////////////////////////////////////////////////////////////////////////////////// // END Timer A 2 and PWM on 5.6 //////////////////////////////////////////////////////////////////////////////////////////////////// Interrupt_enableMaster() ; while(1){ while(MAP_ADC14_isBusy()==0){ // poll the busy flag. we have a conversion going bc we already triggerted it above, so we are waiting for the conversion to be done, when it is, we are going to call the get result function. this just returns the 16 bit value rsults ADC_result = (uint16_t)MAP_ADC14_getResult(ADC_MEM0); //just goes to memory location takes results and returns the 16 bit value... this just continues to get repopulted. MAP_ADC14_toggleConversionTrigger(); ////once we got the result we start a new conversion and we go back to top and begin wiaitng until its dones again }//end nested while }//end inf while }//end main