void hardware_init(void) { //Set PWM clock at the same frequency as system clock SysCtlPWMClockSet(SYSCTL_PWMDIV_1); /* * Inverter system setup * period : Switching Frequency : 20Khz * cycle : Duty Cycle SPWM * deadband : Deadband */ inv.period = SysCtlClockGet()/20000; inv.deadband = 25*inv.period/100; inv.cycle = inv.period*500/1000; /* * Boost converter system setup * period: Switching frequency: 20kHz * cycle: Duty cycle * deadband: Deadband */ conv.period = inv.period; conv.deadband = inv.deadband; conv.cycle = conv.period*500/1000; /* * Setup SPWM on PWM0 GEN0 and GEN1 * PB6 : PWM0 GEN0 * PB7: PWM0 GEN1 */ SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); SysCtlPeripheralReset(SYSCTL_PERIPH_PWM0); SysCtlPeripheralReset(SYSCTL_PERIPH_GPIOB); GPIOPinConfigure(GPIO_PB6_M0PWM0); GPIOPinConfigure(GPIO_PB7_M0PWM1); GPIOPinTypePWM(GPIO_PORTB_BASE, (GPIO_PIN_6 | GPIO_PIN_7)); PWMGenConfigure(PWM0_BASE, PWM_GEN_0, (PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_GEN_SYNC_LOCAL | PWM_GEN_MODE_FAULT_UNLATCHED | PWM_GEN_MODE_DB_NO_SYNC)); PWMGenPeriodSet(PWM0_BASE, PWM_GEN_0, inv.period-1); PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0, inv.cycle); PWMDeadBandEnable(PWM0_BASE, PWM_GEN_0, 13, 0); PWMGenEnable(PWM0_BASE, PWM_GEN_0); PWMOutputState(PWM0_BASE, PWM_OUT_0_BIT|PWM_OUT_1_BIT, true); PWMSyncUpdate(PWM0_BASE, PWM_OUT_0_BIT|PWM_OUT_1_BIT); /* * Setup boost converter pwm on PWM1 * PA6: PWM1 GEN1 */ SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); SysCtlPeripheralReset(SYSCTL_PERIPH_PWM1); SysCtlPeripheralReset(SYSCTL_PERIPH_GPIOA); GPIOPinConfigure(GPIO_PA6_M1PWM2); //Map PWM1, P1 OP2 to PA6 GPIOPinTypePWM(GPIO_PORTA_BASE, GPIO_PIN_6); //Configure PA6 as PWM PWMGenConfigure(PWM1_BASE, PWM_GEN_1, (PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_GEN_NO_SYNC| PWM_GEN_MODE_DB_NO_SYNC)); //Configure PWM1, G1 as Down counter with no sync of updates PWMGenPeriodSet(PWM1_BASE, PWM_GEN_1, conv.period-1); //Set Period of PWM1, G1 PWMPulseWidthSet(PWM1_BASE, PWM_OUT_2, conv.cycle); //Set phase shift PWMIntEnable(PWM1_BASE, PWM_INT_GEN_1); PWMGenIntTrigEnable(PWM1_BASE, PWM_GEN_1, PWM_TR_CNT_LOAD|PWM_INT_CNT_LOAD); IntPrioritySet(INT_PWM1_1, 0x02); PWMOutputState(PWM1_BASE, PWM_OUT_2_BIT, true); PWMGenEnable(PWM1_BASE, PWM_GEN_1); /* * Setup ISR for updating SPWM duty cycle */ uint32_t ui32TimIntSine = SysCtlClockGet()/200000; SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER2); SysCtlPeripheralReset(SYSCTL_PERIPH_TIMER2); TimerConfigure(TIMER2_BASE, TIMER_CFG_A_PERIODIC); TimerLoadSet(TIMER2_BASE, TIMER_A, ui32TimIntSine-1); TimerIntEnable(TIMER2_BASE, TIMER_TIMA_TIMEOUT); IntPrioritySet(INT_TIMER2A, 0x03); TimerEnable(TIMER2_BASE, TIMER_A); uint32_t ui32TimIntIcontrol = SysCtlClockGet()/40000; SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1); SysCtlPeripheralReset(SYSCTL_PERIPH_TIMER1); TimerConfigure(TIMER1_BASE, TIMER_CFG_A_PERIODIC); TimerLoadSet(TIMER1_BASE, TIMER_A, ui32TimIntIcontrol-1); TimerIntEnable(TIMER1_BASE, TIMER_TIMA_TIMEOUT); IntPrioritySet(INT_TIMER1A, 0x01); TimerEnable(TIMER1_BASE, TIMER_A); /* * Setup PF1 as debug pin */ SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); // SysCtlPeripheralReset(SYSCTL_PERIPH_GPIOF); HWREG(0x40005520) = 0x4C4F434B; HWREG(0x40005524) |= 0x01; HWREG(0x40005520) = 0x00; GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_4); GPIOPadConfigSet(GPIO_PORTF_BASE,GPIO_PIN_4,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU); GPIOIntTypeSet(GPIO_PORTF_BASE, GPIO_PIN_4, GPIO_FALLING_EDGE); IntPrioritySet(INT_GPIOF,0x00); GPIOIntEnable(GPIO_PORTF_BASE, GPIO_PIN_4); GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1); /* * Setup ADC * Configuration currently in discussion * Interrupt at end might not be necessary */ SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); SysCtlPeripheralReset(SYSCTL_PERIPH_GPIOE); GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_2); GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_1); GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_0); SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0); SysCtlPeripheralReset(SYSCTL_PERIPH_ADC0); ADCHardwareOversampleConfigure(ADC0_BASE, 2); ADCSequenceDisable(ADC0_BASE, 2); ADCSequenceConfigure(ADC0_BASE, 2, ADC_TRIGGER_PWM1, 0x00); HWREG(0x4003801C) |= 0x1000; //Sec 13.4.2 Pt 3 ADCSequenceStepConfigure(ADC0_BASE, 2, 0, ADC_CTL_CH1); ADCSequenceStepConfigure(ADC0_BASE, 2, 1, ADC_CTL_CH2); ADCSequenceStepConfigure(ADC0_BASE, 2, 2, ADC_CTL_CH3|ADC_CTL_IE|ADC_CTL_END); ADCSequenceEnable(ADC0_BASE, 2); }
//***************************************************************************** // // This example demonstrates the use of the SoftI2C module to read and write an // Atmel AT24C08A EEPROM. // //***************************************************************************** int main(void) { uint8_t pui8Data[16]; uint32_t ui32Idx; // // Set the clocking to run directly from the external crystal/oscillator. // TODO: The SYSCTL_XTAL_ value must be changed to match the value of the // crystal on your board. // SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); // // For this example, PortB[3:2] are used for the SoftI2C pins. GPIO port B // needs to be enabled so these pins can be used. // TODO: change this to whichever GPIO port(s) you are using. // SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); // // For this example, Timer0 is used for the SoftI2C time base. This timer // needs to be enabled before it can be used. // TODO: change this to whichever timer you are using. // SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); // // Configure the appropriate pins to be I2C instead of GPIO. // GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3); // // Initialize the SoftI2C module, including the assignment of GPIO pins. // TODO: change this to whichever GPIO pins you are using. // memset(&g_sI2C, 0, sizeof(g_sI2C)); SoftI2CCallbackSet(&g_sI2C, SoftI2CCallback); SoftI2CSCLGPIOSet(&g_sI2C, GPIO_PORTB_BASE, GPIO_PIN_2); SoftI2CSDAGPIOSet(&g_sI2C, GPIO_PORTB_BASE, GPIO_PIN_3); SoftI2CInit(&g_sI2C); // // Enable the SoftI2C interrupt. // SoftI2CIntEnable(&g_sI2C); // // Configure the timer to generate an interrupt at a rate of 40 KHz. This // will result in a I2C rate of 10 KHz. // TODO: change this to whichever timer you are using. // TODO: change this to whichever I2C rate you require. // TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC); TimerLoadSet(TIMER0_BASE, TIMER_A, SysCtlClockGet() / 40000); TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT); TimerEnable(TIMER0_BASE, TIMER_A); // // Enable the timer interrupt. // TODO: change this to whichever timer interrupt you are using. // IntEnable(INT_TIMER0A); // // Set up the serial console to use for displaying messages. This is // just for this example program and is not needed for SoftI2C operation. // InitConsole(); // // Display the example setup on the console. // UARTprintf("SoftI2C Atmel AT24C08A example\n"); // // Write a data=address pattern into the first 16 bytes of the Atmel // device. // UARTprintf("Write:"); for(ui32Idx = 0; ui32Idx < 16; ui32Idx++) { pui8Data[ui32Idx] = ui32Idx; UARTprintf(" %02x", pui8Data[ui32Idx]); } UARTprintf("\n"); AtmelWrite(pui8Data, 0, 16); // // Read back the first 16 bytes of the Atmel device. // AtmelRead(pui8Data, 0, 16); UARTprintf("Read :"); for(ui32Idx = 0; ui32Idx < 16; ui32Idx++) { UARTprintf(" %02x", pui8Data[ui32Idx]); } UARTprintf("\n"); // // Tell the user that the test is done. // UARTprintf("Done.\n\n"); // // Return no errors. // return(0); }
//***************************************************************************** // // Configure Timer1B as a 16-bit PWM with a duty cycle of 66%. // //***************************************************************************** int main(void) { // // Set the clocking to run directly from the external crystal/oscillator. // TODO: The SYSCTL_XTAL_ value must be changed to match the value of the // crystal on your board. // #if defined(TARGET_IS_TM4C129_RA0) || \ defined(TARGET_IS_TM4C129_RA1) || \ defined(TARGET_IS_TM4C129_RA2) g_ui32SysClock = SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ | SYSCTL_OSC_MAIN | SYSCTL_USE_OSC), 25000000); #else SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); #endif // // The Timer1 peripheral must be enabled for use. // SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1); // // For this example T1CCP1 is used with port B pin 5. // The actual port and pins used may be different on your part, consult // the data sheet for more information. // GPIO port B needs to be enabled so these pins can be used. // TODO: change this to whichever GPIO port you are using. // SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); // // Configure the GPIO pin muxing for the Timer/CCP function. // This is only necessary if your part supports GPIO pin function muxing. // Study the data sheet to see which functions are allocated per pin. // TODO: change this to select the port/pin you are using // GPIOPinConfigure(GPIO_PB5_T1CCP1); // // Set up the serial console to use for displaying messages. This is // just for this example program and is not needed for Timer/PWM operation. // InitConsole(); // // Configure the ccp settings for CCP pin. This function also gives // control of these pins to the SSI hardware. Consult the data sheet to // see which functions are allocated per pin. // TODO: change this to select the port/pin you are using. // GPIOPinTypeTimer(GPIO_PORTB_BASE, GPIO_PIN_5); // // Display the example setup on the console. // UARTprintf("16-Bit Timer PWM ->"); UARTprintf("\n Timer = Timer1B"); UARTprintf("\n Mode = PWM"); UARTprintf("\n Duty Cycle = 66%%\n"); UARTprintf("\nGenerating PWM on CCP3 (PE4) -> "); // // Configure Timer1B as a 16-bit periodic timer. // TimerConfigure(TIMER1_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_B_PWM); // // Set the Timer1B load value to 50000. For this example a 66% duty cycle // PWM signal will be generated. From the load value (i.e. 50000) down to // match value (set below) the signal will be high. From the match value // to 0 the timer will be low. // TimerLoadSet(TIMER1_BASE, TIMER_B, 50000); // // Set the Timer1B match value to load value / 3. // TimerMatchSet(TIMER1_BASE, TIMER_B, TimerLoadGet(TIMER1_BASE, TIMER_B) / 3); // // Enable Timer1B. // TimerEnable(TIMER1_BASE, TIMER_B); // // Loop forever while the Timer1B PWM runs. // while(1) { // // Print out indication on the console that the program is running. // PrintRunningDots(); } }
PROCESS_THREAD(hello_world_process, ev, data) { //static struct etimer timer; PROCESS_BEGIN(); //begintimer(); //etimer_set(&timer, CLOCK_CONF_SECOND * 1); uint32_t ui32PrevCount = 0; // // Set the clocking to run directly from the external crystal/oscillator. // (no ext 32k osc, no internal osc) // //SysCtrlClockSet(false, false, SYS_CTRL_SYSDIV_32MHZ); // // Set IO clock to the same as system clock // //SysCtrlIOClockSet(SYS_CTRL_SYSDIV_32MHZ); // // The Timer0 peripheral must be enabled for use. // SysCtrlPeripheralEnable(SYS_CTRL_PERIPH_GPT0); // // Set up the serial console to use for displaying messages. This is // just for this example program and is not needed for Timer operation. // //InitConsole(); // // Display the example setup on the console. // printf(" 16-Bit Timer Interrupt ->\n\r"); printf(" Timer = Timer0B\n\r"); printf(" Mode = Periodic\n\r"); printf(" Number of interrupts = %d \n\r", NUMBER_OF_INTS); printf(" Rate = 1ms\n\r"); // // Configure Timer0B as a 16-bit periodic timer. // TimerConfigure(GPTIMER0_BASE, GPTIMER_CFG_SPLIT_PAIR | GPTIMER_CFG_A_PERIODIC | GPTIMER_CFG_B_PWM); /* TimerConfigure(GPTIMER0_BASE, GPTIMER_CFG_SPLIT_PAIR | GPTIMER_CFG_A_PWM | GPTIMER_CFG_B_PWM); */ // // Set the Timer0B load value to 1ms. // TimerLoadSet(GPTIMER0_BASE, GPTIMER_B, sys_ctrl_get_sys_clock() / 100 ); // TimerLoadSet(GPTIMER0_BASE, GPTIMER_B, SYS_CTRL_32MHZ ); // // The following call will result in a dynamic interrupt table being used. // The table resides in RAM. // Alternatively SysTickIntHandler can be statically registred in your // application. // TimerIntRegister(GPTIMER0_BASE, GPTIMER_B, Timer0BIntHandler); // // Enable processor interrupts. // //IntMasterEnable(); INTERRUPTS_ENABLE(); // // Configure the Timer0B interrupt for timer timeout. // TimerIntEnable(GPTIMER0_BASE, GPTIMER_TIMB_TIMEOUT); // // Enable the Timer0B interrupt on the processor (NVIC). // IntEnable(INT_TIMER0B); // // Initialize the interrupt counter. // g_ui32Counter = 0; // // Enable Timer0B. // TimerEnable(GPTIMER0_BASE, GPTIMER_B); // // Loop forever while the Timer0B runs. // while(1) { // // If the interrupt count changed, print the new value // if(ui32PrevCount != g_ui32Counter) { // // Print the periodic interrupt counter. // printf("Number of interrupts: %d\r", g_ui32Counter); ui32PrevCount = g_ui32Counter; } } /* while(1) { //PROCESS_YIELD(); //if(ev == PROCESS_EVENT_TIMER) { printf(" Hello Hi \n\r" ); //etimer_set(&timer, CLOCK_SECOND); //} } */ PROCESS_END(); }
int main(void) { unsigned long ulPeriod = 0; //Period for Timer0 //Enable all required peripherals PortFunctionInit(); //Enable console communication with UART UARTStdioInit(0); //Set system clock to 40 MHz SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ); /* * * Timer Configuration * */ //Configure timer to be periodic (counts down and then resets) TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC); //Calculate period for a pin toggle freq of 1MHz with 50% duty cycle ulPeriod = (SysCtlClockGet() / 10) / 2; TimerLoadSet(TIMER0_BASE, TIMER_A, ulPeriod - 1); /* * * ADC0 Configuration * */ //Set the ADC sample rate to 500 KSPS SysCtlADCSpeedSet(SYSCTL_ADCSPEED_500KSPS); //Disable ADC0 sequencer 3 (so that we can configure it) ADCSequenceDisable(ADC0_BASE, 3); //Disable ADC1 sequencer 3 (so that we can configure it) ADCSequenceDisable(ADC1_BASE, 3); //Configure ADC0 sequencer 3 to trigger based on TIMER0A ADCSequenceConfigure(ADC0_BASE, 3, ADC_TRIGGER_TIMER, 0); //Configure ADC1 sequencer 3 to trigger based on TIMER0A ADCSequenceConfigure(ADC1_BASE, 3, ADC_TRIGGER_TIMER, 0); //Configure the ADC0 to flag the interrupt flag when it finishes sampling ADCSequenceStepConfigure(ADC0_BASE, 3, 0, ADC_CTL_CH0|ADC_CTL_END|ADC_CTL_IE); //Configure the ADC1 to flag the interrupt flag when it finishes sampling ADCSequenceStepConfigure(ADC1_BASE, 3, 0, ADC_CTL_CH1|ADC_CTL_END|ADC_CTL_IE); //Enable ADC0 sequencer 3 ADCSequenceEnable(ADC0_BASE, 3); //Enable ADC1 sequencer 3 ADCSequenceEnable(ADC1_BASE, 3); //Enable ADC0 interrupt, it's redundant with line 80 but has to be done ADCIntEnable(ADC0_BASE, 3); //Enable ADC1 interrupt, it's redundant with line 89 but has to be done ADCIntEnable(ADC1_BASE, 3); //Enable timer TimerEnable(TIMER0_BASE, TIMER_A); //Configure TIMER0A to be the ADC sample trigger TimerControlTrigger(TIMER0_BASE, TIMER_A, true); //Clear any interrupts ADCIntClear(ADC0_BASE, 3); ADCIntClear(ADC1_BASE, 3); //Begin sampling ADCIntEnable(ADC0_BASE, 3); ADCIntEnable(ADC1_BASE, 3); //Turn on ADC0 sequence interrupts for sequence 3 IntEnable(INT_ADC0SS3); //Turn on ADC1 sequence interrupts for sequence 3 IntEnable(INT_ADC1SS3); UARTprintf("ADC0 Configured\n"); UARTprintf("ADC1 Configured\n"); //Enable all interrupts IntMasterEnable(); while(1) { } }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(hello_world_process, ev, data) { static struct etimer timer; static int count; PROCESS_BEGIN(); /* i2c_init(I2C_SCL_PORT, I2C_SCL_PIN, I2C_SDA_PORT, I2C_SDA_PIN, I2C_SCL_NORMAL_BUS_SPEED);*/ etimer_set(&timer, CLOCK_CONF_SECOND * 1); count = 0; relay_enable(PORT_D,LED_RELAY_PIN); while(1) { PROCESS_WAIT_EVENT(); if(ev == PROCESS_EVENT_TIMER) { if(count %2 == 0){ //relay_on(PORT_D,LED_RELAY_PIN); int delayIndex; unsigned int pwmDutyCycle = 0x0000; // // Initialize the interrupt counter. // int g_ui32Counter = 0; // // Set the clocking to run directly from the external crystal/oscillator. // (no ext 32k osc, no internal osc) // SysCtrlClockSet(false, false, 32000000); // // Set IO clock to the same as system clock // SysCtrlIOClockSet(32000000); // // The Timer0 peripheral must be enabled for use. // SysCtrlPeripheralEnable(SYS_CTRL_PERIPH_GPT0); // // Set up the serial console to use for displaying messages. This is // just for this example program and is not needed for Timer operation. // //InitConsole(); // // Display the example setup on the console. // UARTprintf("16-Bit Timer PWM ->"); UARTprintf("\n Timer = Timer0B"); UARTprintf("\n Mode = PWM with variable duty cycle"); // // Configure GPTimer0A as a 16-bit PWM Timer. // TimerConfigure(GPTIMER0_BASE, GPTIMER_CFG_SPLIT_PAIR | GPTIMER_CFG_A_PWM | GPTIMER_CFG_B_PWM); // // Set the GPTimer0B load value to 1sec by setting the timer load value // to SYSCLOCK / 255. This is determined by: // Prescaled clock = 16Mhz / 255 // Cycles to wait = 1sec * Prescaled clock TimerLoadSet(GPTIMER0_BASE, GPTIMER_A, SysCtrlClockGet() / 4000); TimerControlLevel(GPTIMER0_BASE, GPTIMER_A, false); // Configure GPIOPortA.0 as the Timer0_InputCapturePin.1 IOCPinConfigPeriphOutput(GPIO_A_BASE, GPIO_PIN_0, IOC_MUX_OUT_SEL_GPT0_ICP1); // Tell timer to use GPIOPortA.0 // Does Direction Selection and PAD Selection GPIOPinTypeTimer(GPIO_A_BASE, GPIO_PIN_0); // // Enable processor interrupts. // IntMasterEnable(); // // Enable GPTimer0B. // TimerEnable(GPTIMER0_BASE, GPTIMER_A); UARTprintf("\n"); // // Loop forever while the Timer0B runs. // while(1) { for (delayIndex = 0; delayIndex < 100000; delayIndex++); pwmDutyCycle += 0x0F; pwmDutyCycle &= 0xFFFF; TimerMatchSet(GPTIMER0_BASE, GPTIMER_A, pwmDutyCycle); UARTprintf("PWM DC Value: %04X -- %04X -- %04X\r", pwmDutyCycle, TimerValueGet(GPTIMER0_BASE, GPTIMER_A), TimerMatchGet(GPTIMER0_BASE, GPTIMER_A) ); } //SENSORS_ACTIVATE(cc2538_temp_sensor); // printf( "%d is temp\n",cc2538_temp_sensor.value); } else { //relay_off(PORT_D,LED_RELAY_PIN); } /* if(count %2 == 0) { relay_toggle(PORT_D,LED_RELAY_PIN); relay_status(PORT_D,LED_RELAY_PIN); } */ count ++; etimer_reset(&timer); } } PROCESS_END(); }
//***************************************************************************** // // Configure Timer1B as a 16-bit PWM with a duty cycle of 66%. // //***************************************************************************** int main(void) { // // Set the clocking to run directly from the external crystal/oscillator. // TODO: The SYSCTL_XTAL_ value must be changed to match the value of the // crystal on your board. // SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_8MHZ); // // The Timer1 peripheral must be enabled for use. // SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); // // For this example CCP3 is used with port E pin 4. // The actual port and pins used may be different on your part, consult // the data sheet for more information. // GPIO port E needs to be enabled so these pins can be used. // SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC); // // Configure the GPIO pin muxing for the Timer/CCP function. // This is only necessary if your part supports GPIO pin function muxing. // Study the data sheet to see which functions are allocated per pin. // //GPIOPinConfigure(GPIO_PC7_CCP4); GPIOPinTypeTimer(GPIO_PORTC_BASE, GPIO_PIN_5); // // Set up the serial console to use for displaying messages. This is // just for this example program and is not needed for Timer/PWM operation. // UartInit(); // // Configure the ccp settings for CCP pin. This function also gives // control of these pins to the SSI hardware. Consult the data sheet to // see which functions are allocated per pin. // // // Display the example setup on the console. // /*UARTprintf("16-Bit Timer PWM ->"); UARTprintf("\n Timer = Timer2A"); UARTprintf("\n Mode = PWM"); UARTprintf("\n Duty Cycle = 66%%\n"); UARTprintf("\nGenerating PWM on CCP4 (PC7) -> "); */ // // Configure Timer2A as a 16-bit periodic timer. //by default its a down timer, up timers can also be configured. TimerConfigure(TIMER0_BASE, TIMER_CFG_16_BIT_PAIR | TIMER_CFG_B_PWM); // Set the Timer2A load value to 50000. For this example a 66% duty // cycle PWM signal will be generated. From the load value (i.e. 50000) // down to match value (set below) the signal will be high. From the // match value to 0 the timer will be low. TimerLoadSet(TIMER0_BASE, TIMER_B, 50000); // Set the Timer2A match value to load value / 3. //TimerMatchSet(TIMER0_BASE, TIMER_B, TimerLoadGet(TIMER0_BASE, TIMER_B)/2); // // Enable Timer2A. // //TimerEnable(TIMER0_BASE, TIMER_B); // // Loop forever while the Timer2 A PWM runs. // while(1) { low_to_high(); high_to_low(); } }
int main(void) { volatile unsigned long ulLoop; // // Enable lazy stacking for interrupt handlers. This allows floating-point // instructions to be used within interrupt handlers, but at the expense of // extra stack usage. // ROM_FPULazyStackingEnable(); // // Set the clocking to run directly from the crystal. // ROM_SysCtlClockSet( SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN); // // Enable the GPIO port that is used for the on-board LED. // ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1 | SYSCTL_PERIPH_TIMER0); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); // // Enable the GPIO pins for the LED (PF2 & PF3). // //ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_2 | GPIO_PIN_1 | GPIO_PIN_3); ROM_IntMasterEnable(); // // Initialize the UART. // ROM_GPIOPinConfigure (GPIO_PA0_U0RX); ROM_GPIOPinConfigure (GPIO_PA1_U0TX); ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1); UARTStdioInit(0); UARTEchoSet(true); // // Enable the UART interrupt. // // /// Initialize PWM // GPIOPinConfigure(GPIO_PF1_T0CCP1); GPIOPinConfigure(GPIO_PF2_T1CCP0); GPIOPinConfigure(GPIO_PF3_T1CCP1); GPIOPinTypeTimer(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_1 | GPIO_PIN_1); // // Configure Timer as a 16-bit periodic timer. // TimerConfigure(TIMER0_BASE, TIMER_CFG_16_BIT_PAIR | TIMER_CFG_B_PWM); TimerConfigure(TIMER1_BASE, TIMER_CFG_16_BIT_PAIR | TIMER_CFG_A_PWM); TimerConfigure(TIMER1_BASE, TIMER_CFG_16_BIT_PAIR | TIMER_CFG_B_PWM); // Configure period is 10 KHz TimerLoadSet(TIMER0_BASE, TIMER_B, 16000); TimerLoadSet(TIMER1_BASE, TIMER_A, 16000); TimerLoadSet(TIMER1_BASE, TIMER_B, 16000); // // Set the Timer match value to load value (100% ON) . // TimerMatchSet(TIMER0_BASE, TIMER_B, PWM_CTR.CH1); TimerMatchSet(TIMER1_BASE, TIMER_A, PWM_CTR.CH2); TimerMatchSet(TIMER1_BASE, TIMER_B, PWM_CTR.CH3); TimerEnable(TIMER0_BASE, TIMER_B); TimerEnable(TIMER1_BASE, TIMER_BOTH); // // Hello! // UARTprintf("LED PWM Control Demo\n"); // // We are finished. Hang around doing nothing. // while (1) { } }
void timer0_handler_example(void) { TimerLoadSet(TIMER0_BASE, TIMER_A, 0xf); /* Clear the timer interrupt */ TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT); }
// ************ OS_Init ****************** // initialize operating system, disable interrupts until OS_Launch // initialize OS controlled I/O: serial, ADC, systick, select switch and timer2 // input: none // output: non void OS_Init(void) { int i; // Used for indexing // Disable interrupts OS_DisableInterrupts(); // Setting the clock to 50 MHz SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_8MHZ); // Initialze peripherals UART0_Init(); ADC_Open(); //Output_Init(); // Select switch SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_1); GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU); GPIOIntTypeSet(GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_RISING_EDGE); GPIOPinIntClear(GPIO_PORTF_BASE, GPIO_PIN_1); // Down switch SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); GPIOPinTypeGPIOInput(GPIO_PORTE_BASE, GPIO_PIN_1); GPIOPadConfigSet(GPIO_PORTE_BASE, GPIO_PIN_1, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU); GPIOIntTypeSet(GPIO_PORTE_BASE, GPIO_PIN_1, GPIO_RISING_EDGE); GPIOPinIntClear(GPIO_PORTE_BASE, GPIO_PIN_1); // Initialize Timer2A and Timer2B: Periodic Background Threads SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER2); TimerDisable(TIMER2_BASE, TIMER_A | TIMER_B); TimerConfigure(TIMER2_BASE, TIMER_CFG_16_BIT_PAIR | TIMER_CFG_A_PERIODIC | TIMER_CFG_B_PERIODIC); // Initialize Timer0B: Used for time keeping TimerDisable(TIMER0_BASE, TIMER_B); TimerConfigure(TIMER0_BASE, TIMER_CFG_16_BIT_PAIR | TIMER_CFG_A_PERIODIC | TIMER_CFG_B_PERIODIC); TimerIntDisable(TIMER0_BASE, TIMER_TIMB_TIMEOUT); TimerLoadSet(TIMER0_BASE, TIMER_B, 65535); TimerPrescaleSet(TIMER0_BASE, TIMER_B, 5); // One unit is 100ns TimerEnable(TIMER0_BASE, TIMER_B); // Initialize Timer1A: Used for sleep decrementing SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1); TimerDisable(TIMER1_BASE, TIMER_A); TimerConfigure(TIMER1_BASE, TIMER_CFG_16_BIT_PAIR | TIMER_CFG_A_PERIODIC | TIMER_CFG_B_PERIODIC); TimerIntDisable(TIMER1_BASE, TIMER_TIMA_TIMEOUT); TimerLoadSet(TIMER1_BASE, TIMER_A, 50000); // Every interrupt is 1ms // Setting priorities for all interrupts // To add more, look up correct names in inc\hw_ints.h IntPrioritySet(FAULT_PENDSV, (0x07 << 5)); IntPrioritySet(FAULT_SYSTICK, (0x06 << 5)); IntPrioritySet(INT_TIMER1A, (0x02 << 5)); IntPrioritySet(INT_UART0, (0x03 << 5)); IntPrioritySet(INT_ADC0SS0, (0x01 << 5)); IntPrioritySet(INT_ADC0SS3, (0x01 << 5)); // Initializing TCBs for(i = 0; i < MAXTHREADS; i++) { tcbs[i].valid = INVALID; tcbs[i].blockedState = '\0'; } RunPt = &tcbs[0]; // Thread 0 will run first }
int main(void) { unsigned long ulPeriod; unsigned long ReadData; char str[10]; SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_8MHZ); // Initialize the OLED display and write status. RIT128x96x4Init(1000000); RIT128x96x4StringDraw("----------------------", 0, 50, 15); // Status SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); GPIOPadConfigSet(GPIO_PORTF_BASE,GPIO_PIN_0,GPIO_STRENGTH_4MA,GPIO_PIN_TYPE_STD); GPIODirModeSet(GPIO_PORTF_BASE,GPIO_PIN_0,GPIO_DIR_MODE_OUT); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); // I2C SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0); I2CMasterInitExpClk(I2C_MASTER_BASE, SysCtlClockGet(), false); GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3); accelWrite(0x16, 0x05); ReadData = byteAccelRead(0x16); sprintf(str, "%u", ReadData); RIT128x96x4StringDraw(str, 0, 10, 15); SysCtlPWMClockSet(SYSCTL_PWMDIV_8); //pwm SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0); GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_0); PWMGenConfigure(PWM_BASE, PWM_GEN_1, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC); ulPeriod = SysCtlClockGet()/100/8; period = ulPeriod/2; maxPeriod = ulPeriod; PWMGenPeriodSet(PWM0_BASE, PWM_GEN_1, ulPeriod); PWMPulseWidthSet(PWM0_BASE, PWM_OUT_2, ulPeriod/2); //PWMPulseWidthSet(PWM_BASE, PWM_OUT_1, SysCtlClockGet() * 0.0015/8); PWMGenEnable(PWM0_BASE, PWM_GEN_1); //on/off //PWMOutputState(PWM0_BASE, (PWM_OUT_2_BIT | PWM_OUT_3_BIT), true); //PB1 SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); GPIOPadConfigSet(GPIO_PORTB_BASE,GPIO_PIN_1,GPIO_STRENGTH_4MA,GPIO_PIN_TYPE_STD); GPIODirModeSet(GPIO_PORTB_BASE,GPIO_PIN_1,GPIO_DIR_MODE_IN); IntEnable(INT_GPIOB); GPIOIntTypeSet(GPIO_PORTB_BASE, GPIO_PIN_1, GPIO_BOTH_EDGES); GPIOPinIntEnable(GPIO_PORTB_BASE, GPIO_PIN_1); IntPrioritySet(INT_GPIOB,0x80); SysTickPeriodSet(SysCtlClockGet()/10000); // 0.1ms SysTickIntEnable(); waitTime = 0; waitTime2 = 0; SysTickEnable(); //Timer SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC); TimerLoadSet(TIMER0_BASE, TIMER_A, SysCtlClockGet()/125); TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT); IntEnable(INT_TIMER0A); TimerEnable(TIMER0_BASE, TIMER_A); IntPrioritySet(INT_TIMER0A, 0x79); //x l=2 h=0 //y l=44 h=0 //z l=21 h=1 accelWrite(0x10, (signed char)0); accelWrite(0x11, (signed char)0); accelWrite(0x12, (signed char)44); accelWrite(0x13, (signed char)0); accelWrite(0x14, (signed char)214);//116 accelWrite(0x15, (signed char)7); IntMasterEnable(); while(1) { } }
void RMBD01Init(void) { // // Configure the ADC for Pitch and Roll // sequence 3 means a single sample // sequence 1, 2 means up to 4 samples // sequence 0 means up to 8 samples // we use sequence 1 // if (SysCtlPeripheralPresent(SYSCTL_PERIPH_ADC0)) { SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); // // Configure the pins to be used as analog inputs. // GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_2 | GPIO_PIN_3); // // Select the external reference for greatest accuracy. // //ADCReferenceSet(ADC0_BASE, ADC_REF_EXT_3V); // // Only for LM4F232 Apply workaround for erratum 6.1, in order to use the // external reference. // //SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); //HWREG(GPIO_PORTB_BASE + GPIO_O_AMSEL) |= GPIO_PIN_6; ADCSequenceConfigure(ADC0_BASE, 3, ADC_TRIGGER_TIMER, 0); // 1 captures up to 4 samples //ADCSequenceConfigure(ADC0_BASE, 0, ADC_TRIGGER_PROCESSOR, 0); // 1 captures up to 4 samples //ADCSequenceStepConfigure(ADC0_BASE, 0, 0, ENCODER_CHANNEL_PITCH); // sample pitch //ADCSequenceStepConfigure(ADC0_BASE, 0, 1, ENCODER_CHANNEL_ROLL | ADC_CTL_IE | ADC_CTL_END); // sample roll ADCHardwareOversampleConfigure(ADC0_BASE, 64); //ADCSoftwareOversampleConfigure(ADC0_BASE, 0, 8); //ADCSoftwareOversampleStepConfigure(ADC0_BASE, 0, 0, (ADC_CTL_CH1 | ADC_CTL_IE | ADC_CTL_END)); ADCSequenceStepConfigure(ADC0_BASE, 3, 0, ADC_CTL_IE | ADC_CTL_END | ADC_CTL_CH0); //ADCSoftwareOversampleStepConfigure(ADC0_BASE, 0, 0, (ADC_CTL_CH1 | ADC_CTL_IE | ADC_CTL_END)); //ADCSequenceStepConfigure(ADC0_BASE, 0, 0, ADC_CTL_CH0); // sample roll //ADCSequenceStepConfigure(ADC0_BASE, 0, 0, ADC_CTL_CH1 | ADC_CTL_IE | ADC_CTL_END); // sample roll //ADCIntRegister(ADC0_BASE,0,ADCIntHandler); // // Enable the sequencer and interrupt // ADCSequenceEnable(ADC0_BASE, 3); ADCIntEnable(ADC0_BASE, 3); IntEnable(INT_ADC3); // // Zero the oversample counter and the sum // g_ucOversampleCnt = 0; g_ulSum = 0; //ADCSequenceEnable(ADC0_BASE, 2); //ADCIntEnable(ADC0_BASE, 2); #if 0 // // Enable the ADC sequencers // ADCSequenceEnable(ADC0_BASE, 0); // // Flush the ADC sequencers to be sure there is no lingering data. // ADCSequenceDataGet(ADC0_BASE, 0, PitchRollAmp); // // Enable ADC interrupts // ADCIntClear(ADC0_BASE, 0); ADCIntEnable(ADC0_BASE, 0); IntEnable(INT_ADC0SS0); #else // // Setup timer for ADC_TRIGGER_TIMER // SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1); // // Configure the second timer to generate triggers to the ADC // TimerConfigure(TIMER1_BASE, TIMER_CFG_32_BIT_PER); TimerLoadSet(TIMER1_BASE, TIMER_A, ROM_SysCtlClockGet() / 20000); // 50 us == 20K ulTimeSysClock = ROM_SysCtlClockGet()/20000; TimerControlStall(TIMER1_BASE, TIMER_A, true); TimerControlTrigger(TIMER1_BASE, TIMER_A, true); // // Enable the timers. // TimerEnable(TIMER1_BASE, TIMER_A); #endif new_adc = false; // } // // Clear outstanding ADC interrupt and enable // //ADCIntClear(ADC0_BASE, 2); //ADCIntEnable(ADC0_BASE, 2); //IntEnable(INT_ADC2); // // Set the zero current to indicate that the initial sampling has just // begun. // g_usCurrentZero = 0xffff; } // InitADC
/** * Sets the commutation timer timeout value. This is the value at which the timer will reset to 0 * and generate an interrupt. * * @param value */ void CommutationControllerClass::setTimeoutValue(uint32_t value) { TimerLoadSet(TIMER_BASE, TIMER_A, value); }
/* * function: ADC3IntHandler * interrupt handler ADC0, sequence 3 * return: none * */ void ADC3IntHandler(void) { unsigned long ulStatus; static unsigned long uluDMACount = 0; static unsigned long ulDataXferd = 0; unsigned long ulNextuDMAXferSize = 0; unsigned short *pusDMABuffer; unsigned short *pusCopyBuffer; int i; ADCIntClear(ADC0_BASE, SEQUENCER); // If the channel's not done capturing, we have an error if (uDMAChannelIsEnabled(UDMA_CHANNEL_ADC3)) { // Increment error counter adcNode[0].g_ulBadPeriphIsr2++; ADCIntDisable(ADC0_BASE, SEQUENCER); IntPendClear(INT_ADC0SS3); return; } ulStatus = uDMAChannelSizeGet(UDMA_CHANNEL_ADC3); // If non-zero items are left in the transfer buffer // Something went wrong if (ulStatus) { adcNode[0].g_ulBadPeriphIsr1++; return; } if (g_ucDMAMethod == DMA_METHOD_SLOW) { // // We are using the slow DMA method, meaning there are not enough // samples in a second to generate a new set of FFT values and still // have data frequent enough to refresh at 15 frames per second // // // when pingpong is 0, uDMA just finished transferring into ping, so next // we transfer into pong. // if (g_ucDMApingpong == 0) { pusDMABuffer = g_usDMApong; pusCopyBuffer = g_usDMAping; g_ucDMApingpong = 1; } else { pusDMABuffer = g_usDMAping; pusCopyBuffer = g_usDMApong; g_ucDMApingpong = 0; } // // Set up the next uDMA transfer // uDMAChannelTransferSet(UDMA_CHANNEL_ADC3 | UDMA_PRI_SELECT, UDMA_MODE_BASIC, (void *) (ADC0_BASE + ADC_O_SSFIFO3),// + (0x20 * UDMA_ARB_1)), pusDMABuffer, DMA_SIZE); uDMAChannelEnable(UDMA_CHANNEL_ADC3); IntPendClear(INT_ADC0SS3); // // Shift everything back DMA_SIZE samples // for (i = 0; i < (NUM_SAMPLES - DMA_SIZE); i++) { g_ulADCValues[i] = g_ulADCValues[i + DMA_SIZE]; } // // Copy the new samples from the copy buffer into the sample array // for (i = 0; i < DMA_SIZE; i++) { g_ulADCValues[i + NUM_SAMPLES - DMA_SIZE] = pusCopyBuffer[i]; } // // Signal that we have new data to be processed // adcNode[0].g_ucDataReady = 1; } else { // Disable the sampling timer TimerDisable(TIMER0_BASE, TIMER_A); // how many times the DMA has been full without processing the data uluDMACount++; // The amount of data transferred increments in sets of 1024 ulDataXferd += UDMA_XFER_MAX; if (NUM_SAMPLES > ulDataXferd) { if ((NUM_SAMPLES - ulDataXferd) > UDMA_XFER_MAX) { ulNextuDMAXferSize = UDMA_XFER_MAX; } else { ulNextuDMAXferSize = NUM_SAMPLES - ulDataXferd; } #ifdef USE_TEMPORARY_BUFFER if (currentAdcBuffer == g_ulADCValues_B) { uDMAChannelTransferSet(UDMA_CHANNEL_ADC3 | UDMA_PRI_SELECT, UDMA_MODE_BASIC, (void *) (ADC0_BASE + ADC_O_SSFIFO3 + (0x20 * UDMA_ARB_1)), g_ulADCValues + (UDMA_XFER_MAX * uluDMACount), ulNextuDMAXferSize); } else { uDMAChannelTransferSet(UDMA_CHANNEL_ADC3 | UDMA_PRI_SELECT, UDMA_MODE_BASIC, (void *) (ADC0_BASE + ADC_O_SSFIFO3 + (0x20 * UDMA_ARB_1)), g_ulADCValues_B + (UDMA_XFER_MAX * uluDMACount), ulNextuDMAXferSize); } #endif uDMAChannelTransferSet(UDMA_CHANNEL_ADC3 | UDMA_PRI_SELECT, UDMA_MODE_BASIC, (void *) (ADC0_BASE + ADC_O_SSFIFO3 + (0x20 * UDMA_ARB_1)), g_ulADCValues + (UDMA_XFER_MAX * uluDMACount), ulNextuDMAXferSize); uDMAChannelEnable(UDMA_CHANNEL_ADC3); TimerLoadSet(TIMER0_BASE, TIMER_A, LoadTimer); TimerEnable(TIMER0_BASE, TIMER_A); } else { uluDMACount = 0; ulDataXferd = 0; ADCIntDisable(ADC0_BASE, SEQUENCER); IntPendClear(INT_ADC0SS3); // Signal that we have new data to be processed adcNode[0].g_ucDataReady = 1; #ifdef USING_BUFFER2 if (adcNode[0].g_ucDataReady) { memcpy(output2, g_ulADCValues, NUM_SAMPLES); countFullData++; } #endif } } }
// After a certain number of edges are captured, the application prints out // the results and compares the elapsed time between edges to the expected // value. // // Note that the "B" timer is used because on some devices the "A" timer does // not work correctly with the uDMA controller. Refer to the chip errata for // details. // //***************************************************************************** int main(void) { unsigned long ulIdx; unsigned short usTimerElapsed; //unsigned short usTimerErr; // // Set the clocking to run directly from the crystal. // ROM_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); // // Initialize the UART and write a status message. // ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0); GPIOPinConfigure(GPIO_PA0_U0RX); GPIOPinConfigure(GPIO_PA1_U0TX); ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1); ROM_UARTConfigSetExpClk(UART0_BASE, ROM_SysCtlClockGet(), 115200, (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_EVEN)); //UARTStdioInit(0); //UARTprintf("\033[2JuDMA edge capture timer example\n\n"); //UARTprintf("This example requires that PD0 and PD7 be jumpered together" // "\n\n"); // // Create a signal source that can be used as an input for the CCP1 pin. // SetupSignalSource(); // // Enable the GPIO port used for the CCP1 input. // ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); // // Configure the Timer0 CCP1 function to use PD7 // GPIOPinConfigure(GPIO_PB2_CCP3); GPIOPinTypeTimer(GPIO_PORTB_BASE, GPIO_PIN_2); // // Set up Timer0B for edge-timer mode, positive edge // ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1); TimerConfigure(TIMER1_BASE, TIMER_CFG_16_BIT_PAIR | TIMER_CFG_B_CAP_TIME); TimerControlEvent(TIMER1_BASE, TIMER_B, TIMER_EVENT_BOTH_EDGES); TimerLoadSet(TIMER1_BASE, TIMER_B, 0xffff); // // Enable the uDMA peripheral // ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UDMA); // // Enable the uDMA controller error interrupt. This interrupt will occur // if there is a bus error during a transfer. // ROM_IntEnable(INT_UDMAERR); // // Enable the uDMA controller. // ROM_uDMAEnable(); // // Point at the control table to use for channel control structures. // ROM_uDMAControlBaseSet(ucControlTable); // // Put the attributes in a known state for the uDMA Timer0B channel. These // should already be disabled by default. // ROM_uDMAChannelAttributeDisable(UDMA_CHANNEL_TMR1B, UDMA_ATTR_ALTSELECT | UDMA_ATTR_USEBURST | UDMA_ATTR_HIGH_PRIORITY | UDMA_ATTR_REQMASK); // // Configure DMA channel for Timer0B to transfer 16-bit values, 1 at a // time. The source is fixed and the destination increments by 16-bits // (2 bytes) at a time. // ROM_uDMAChannelControlSet(UDMA_CHANNEL_TMR1B | UDMA_PRI_SELECT, UDMA_SIZE_16 | UDMA_SRC_INC_NONE | UDMA_DST_INC_16 | UDMA_ARB_1); // // Set up the transfer parameters for the Timer0B primary control // structure. The mode is set to basic, the transfer source is the // Timer0B register, and the destination is a memory buffer. The // transfer size is set to a fixed number of capture events. // ROM_uDMAChannelTransferSet(UDMA_CHANNEL_TMR1B | UDMA_PRI_SELECT, UDMA_MODE_BASIC, (void *)(TIMER1_BASE + TIMER_O_TBR), g_usTimerBuf, MAX_TIMER_EVENTS); // // Enable the timer capture event interrupt. Note that this signal is // used to trigger the DMA request and not an actual interrupt. // Start the capture timer running and enable its interrupt channel. // The timer interrupt channel is used by the uDMA controller. // //UARTprintf("Starting timer and uDMA\n"); TimerIntEnable(TIMER1_BASE, TIMER_CAPB_EVENT); TimerEnable(TIMER1_BASE, TIMER_B); IntEnable(INT_TIMER1B); // // Now enable the DMA channel for Timer0B. It should now start performing // transfers whenever there is a rising edge detected on the CCP1 pin. // ROM_uDMAChannelEnable(UDMA_CHANNEL_TMR1B); // // Enable processor interrupts. // ROM_IntMasterEnable(); // // Wait for the transfer to complete. // //UARTprintf("Waiting for transfers to complete\n"); while(!g_bDoneFlag) { } // // Check for the expected number of occurrences of the interrupt handler, // and that there are no DMA errors // if(g_uluDMAErrCount != 0) { //UARTprintf("\nuDMA errors were detected!!!\n\n"); } if(g_ulTimer0BIntCount != 1) { //UARTprintf("\nUnexpected number of interrupts occurrred (%d)!!!\n\n", // g_ulTimer0BIntCount); } // // Display the timer values that were captured using the edge capture timer // with uDMA. Compare the difference between stored values to the PWM // period and make sure they match. This verifies that the edge capture // DMA transfers were occurring with the correct timing. // //UARTprintf("\n Captured\n"); //UARTprintf("Event Value Difference Status\n"); //UARTprintf("----- -------- ---------- ------\n"); const unsigned char nbColonnes = '1'; UARTSend(&nbColonnes,1); for(ulIdx = 1; ulIdx < MAX_TIMER_EVENTS; ulIdx++) { // // Due to timer erratum, when the timer rolls past 0 as it counts // down, it will trigger an additional DMA transfer even though there // was not an edge capture. This will appear in the data buffer as // a duplicate value - the value will be the same as the prior capture // value. Therefore, in this example we skip past the duplicated // value. // if(g_usTimerBuf[ulIdx] == g_usTimerBuf[ulIdx - 1]) { const unsigned char dup = '$'; UARTSend(&dup,1); //UARTprintf(" %2u 0x%04X skipped duplicate\n", ulIdx, // g_usTimerBuf[ulIdx]); continue; } // // Compute the difference between adjacent captured values, and then // compare that to the expected timeout period. // usTimerElapsed = g_usTimerBuf[ulIdx - 1] - g_usTimerBuf[ulIdx]; //usTimerErr = usTimerElapsed > TIMEOUT_VAL ? // usTimerElapsed - TIMEOUT_VAL : // TIMEOUT_VAL - usTimerElapsed; // // Print the captured value and the difference from the previous // unsigned char data[10]; itoa(usTimerElapsed,data); UARTSend(data,10); //UARTprintf(" %2u 0x%04X %8u ", ulIdx, g_usTimerBuf[ulIdx], // usTimerElapsed); // // Print error status based on the deviation from expected difference // between samples (calculated above). Allow for a difference of up // to 1 cycle. Any more than that is considered an error. // //if(usTimerErr > 1) //{ // UARTprintf(" ERROR\n"); //} //else //{ // UARTprintf(" OK\n"); //} } const unsigned char fin = '\n'; UARTSend(&fin,1); // // End of application // while(1) { } }
//***************************************************************************** // //! Handles the TIMER5A interrupt. //! //! This function responds to the TIMER5A interrupt, updating the duty cycle of //! the output waveform in order to produce sound. It is the application's //! responsibility to ensure that this function is called in response to the //! TIMER5A interrupt, typically by installing it in the vector table as the //! handler for the TIMER5A interrupt. //! //! \return None. // //***************************************************************************** void SoundIntHandler(void) { int32_t i32DutyCycle; // // If there is an adjustment to be made, the apply it and set allow the // update to be done on the next load. // if(g_sSoundState.i32RateAdjust) { g_sSoundState.ui32Period += g_sSoundState.i32RateAdjust; g_sSoundState.i32RateAdjust = 0; TimerLoadSet(TIMER5_BASE, TIMER_A, g_sSoundState.ui32Period); } // // Clear the timer interrupt. // ROM_TimerIntClear(TIMER5_BASE, TIMER_CAPA_EVENT); // // See if the startup ramp is in progress. // if(HWREGBITW(&g_sSoundState.ui32Flags, SOUND_FLAG_STARTUP)) { // // Increment the ramp count. // g_sSoundState.i32Step++; // // Increase the pulse width of the output by one clock. // ROM_TimerMatchSet(TIMER5_BASE, TIMER_A, g_sSoundState.i32Step); // // See if this was the last step of the ramp. // if(g_sSoundState.i32Step >= (g_sSoundState.ui32Period / 2)) { // // Indicate that the startup ramp has completed. // HWREGBITW(&g_sSoundState.ui32Flags, SOUND_FLAG_STARTUP) = 0; // // Set the step back to zero for the start of audio playback. // g_sSoundState.i32Step = 0; } // // There is nothing further to be done. // return; } // // See if the shutdown ramp is in progress. // if(HWREGBITW(&g_sSoundState.ui32Flags, SOUND_FLAG_SHUTDOWN)) { // // See if this was the last step of the ramp. // if(g_sSoundState.i32Step == 1) { // // Disable the output signals. // ROM_TimerMatchSet(TIMER5_BASE, TIMER_A, g_sSoundState.ui32Period); // // Clear the sound flags. // g_sSoundState.ui32Flags = 0; // // Disable the speaker amp. // ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_4, 0); } else { // // Decrement the ramp count. // g_sSoundState.i32Step--; // // Decrease the pulse width of the output by one clock. // ROM_TimerMatchSet(TIMER5_BASE, TIMER_A, g_sSoundState.i32Step); } // // There is nothing further to be done. // return; } // // Compute the value of the PCM sample based on the blended average of the // previous and current samples. It should be noted that linear // interpolation does not produce the best results with sound (it produces // a significant amount of harmonic aliasing) but it is fast. // i32DutyCycle = (((g_sSoundState.pi16Samples[0] * (8 - g_sSoundState.i32Step)) + (g_sSoundState.pi16Samples[1] * g_sSoundState.i32Step)) / 8); // // Adjust the magnitude of the sample based on the current volume. Since a // multiplicative volume control is implemented, the volume value // results in nearly linear volume adjustment if it is squared. // i32DutyCycle = (((i32DutyCycle * g_sSoundState.i32Volume * g_sSoundState.i32Volume) / 65536) + 32768); // // Set the PWM duty cycle based on this PCM sample. // i32DutyCycle = (g_sSoundState.ui32Period * i32DutyCycle) / 65536; ROM_TimerMatchSet(TIMER5_BASE, TIMER_A, i32DutyCycle); // // Increment the sound step based on the sample rate. // if(HWREGBITW(&g_sSoundState.ui32Flags, SOUND_FLAG_8KHZ)) { g_sSoundState.i32Step = (g_sSoundState.i32Step + 1) & 7; } else if(HWREGBITW(&g_sSoundState.ui32Flags, SOUND_FLAG_16KHZ)) { g_sSoundState.i32Step = (g_sSoundState.i32Step + 2) & 7; } else if(HWREGBITW(&g_sSoundState.ui32Flags, SOUND_FLAG_32KHZ)) { g_sSoundState.i32Step = (g_sSoundState.i32Step + 4) & 7; } // // See if the next sample has been reached. // if(g_sSoundState.i32Step == 0) { // // Copy the current sample to the previous sample. // g_sSoundState.pi16Samples[0] = g_sSoundState.pi16Samples[1]; // // Get the next sample from the buffer. // g_sSoundState.pi16Samples[1] = g_sSoundState.pi16Buffer[g_sSoundState.ui32Offset]; // // Increment the buffer pointer. // g_sSoundState.ui32Offset++; if(g_sSoundState.ui32Offset == g_sSoundState.ui32Length) { g_sSoundState.ui32Offset = 0; } // // Call the callback function if one of the half-buffers has been // consumed. // if(g_sSoundState.pfnCallback) { if(g_sSoundState.ui32Offset == 0) { g_sSoundState.pfnCallback(1); } else if(g_sSoundState.ui32Offset == (g_sSoundState.ui32Length / 2)) { g_sSoundState.pfnCallback(0); } } } }
int main() { //Set the Clock at 16MHZ SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN); //Enable Timer Peripheral SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); //Enable Peripherals SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); //Initialize Output ports GPIOPinTypeGPIOOutput(port_A, GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4); GPIOPinTypeGPIOOutput(port_C, GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7); GPIOPinTypeGPIOOutput(port_D, GPIO_PIN_6); GPIOPinTypeGPIOOutput(port_E, GPIO_PIN_0); GPIOPinTypeGPIOOutput(port_F, (GPIO_PIN_4 | GPIO_PIN_1 | GPIO_PIN_3)); //Input Pins GPIOPinTypeGPIOInput(port_F, GPIO_PIN_2 | GPIO_PIN_3); //Interrupt Enables GPIOIntEnable(GPIO_PORTF_BASE, (GPIO_INT_PIN_2 | GPIO_INT_PIN_3)); GPIOIntRegister(GPIO_PORTF_BASE, interrupt_handler); GPIOIntTypeSet(GPIO_PORTF_BASE, (GPIO_PIN_2 | GPIO_PIN_3) , GPIO_FALLING_EDGE); //Timer Setup TimerLoadSet(TIMER0_BASE, TIMER_A, 0xFFFF); TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC); TimerControlEvent(TIMER0_BASE, TIMER_A, TIMER_EVENT_NEG_EDGE); TimerEnable(TIMER0_BASE, TIMER_A); //Initialize the display initializeDisplay(); while(1) { if(display_flag == 1) { //Software Debouncing display_flag = 0; SysCtlDelay(160000); //Clear scree clear_screen(); //RS High for writing GPIOPinWrite(port_C, GPIO_PIN_5, pin_5); write_char_to_pins((char)(((int)'0')+ (0x00FF & TimerValueGet(TIMER0_BASE, TIMER_A))% 10)); //Back to Low GPIOPinWrite(port_C, GPIO_PIN_5, 0x0); } } }
//---------------------------------------------------------------------------- // Initialize the ADC void Init_ADC(void) { int i; // Init buffer for(i=0; i < ADC_BUFFER_SIZE; i++) { // Initialize to approx 65 degrees F adc_buffer[i] = 475.0; } // writes 0x00010000 to RCGC0 SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0); SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); SysCtlPeripheralReset(SYSCTL_PERIPH_TIMER0); // Setup timer, 32-bit periodic TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC); // The prescaler is not available 32-bit mode // Load timer value, approx 32Hz TimerLoadSet(TIMER0_BASE, TIMER_A, 0x17D784); //TimerLoadSet(TIMER0_BASE, TIMER_A, 0xBEBC); // Configure timer to trigger ADC TimerControlTrigger(TIMER0_BASE, TIMER_A, true); // Setup ADC ADCSequenceDisable(ADC0_BASE, ACTIVE_SS); // Configure the ADC to trigger off a timer and priority 3 (default) ADCSequenceConfigure(ADC0_BASE, ACTIVE_SS, ADC_TRIGGER_TIMER, 3); // // Configure step 0 on sequence 3. Sample channel 0 (ADC_CTL_CH0) in // single-ended mode (default) and configure the interrupt flag // (ADC_CTL_IE) to be set when the sample is done. Tell the ADC logic // that this is the last conversion on sequence 3 (ADC_CTL_END). Sequence // 3 has only one programmable step. Sequence 1 and 2 have 4 steps, and // sequence 0 has 8 programmable steps. Since we are only doing a single // conversion using sequence 3 we will only configure step 0. For more // information on the ADC sequences and steps, reference the datasheet. // ADCSequenceStepConfigure(ADC0_BASE, ACTIVE_SS, 0, ADC_CTL_CH2 | ADC_CTL_IE | ADC_CTL_END); ADCHardwareOversampleConfigure(ADC0_BASE, 64); // // Since sample sequence 3 is now configured, it must be enabled. // ADCSequenceEnable(ADC0_BASE, ACTIVE_SS); // Enable ADC interrupt ADCIntEnable(ADC0_BASE, ACTIVE_SS); IntEnable(INT_ADC0SS3); // Enable timer TimerEnable(TIMER0_BASE, TIMER_A); }
// void setup(void) runs ONCE when the program just STARTS void setup() { // First SET the SYSTEM CLOCK to 80 [MHz] // Sets the Clock DIVIDER to 2.5, so that SYS_CLOCK runs at 200/2.5 ==> 80 [MHz] SysCtlClockSet( SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN); uint32_t Timer0_Period; // Enable & Configure Serial(UART0) to BAUDRATE = 9216000 Serial.begin(BAUDRATE); // Configure PINS for LED OUTPUT pinMode(RED,OUTPUT); pinMode(BLUE,OUTPUT); pinMode(GREEN,OUTPUT); // Set LED OUTPUT pins to OFF initially digitalWrite(RED,LOW); digitalWrite(BLUE,LOW); digitalWrite(GREEN,LOW); state_led=0; // initialize the STATE to STATE0 // Initialize the 'Wire' class for the I2C-bus. Wire.begin(); // Clear the 'sleep' bit to start the sensor. MPU9150_writeSensor(MPU9150_PWR_MGMT_1, 0); int temp0; temp0= MPU9150_readSensor(0x1c); temp0 |= (0x10); temp0 &=~(0x08); MPU9150_writeSensor(0x1c,temp0); MPU9150_writeSensor(0x19,0x0f); MPU9150_setupCompass(); SysCtlPeripheralEnable( SYSCTL_PERIPH_TIMER0); // Enable Timer0 TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC); // Set Timer0 mode PERIODIC // Timer0_Period ==> 80e6 * A / B, (1/2)=0.5 Timer0_Period = ( 80000000 * A ) / B; TimerLoadSet( TIMER0_BASE, TIMER_A, Timer0_Period-1); // REGISTER ISR to TIMER0 INTERRUPT TimerIntRegister( TIMER0_BASE, TIMER_A, Timer0_isr ); // Enable Interrupts from Timer0_A IntEnable( INT_TIMER0A); // Set Timer Interrupt Condition and Enable Timer Interrupt TimerIntEnable( TIMER0_BASE, TIMER_TIMA_TIMEOUT); // Enable INTERRUPTS for the SYSTEM IntMasterEnable(); // Start Timer0 TimerEnable(TIMER0_BASE, TIMER_A); }
int main(void) { unsigned long adc_result[16]; unsigned long cnt; float temperature; // // Set the clocking to run directly from the crystal. // SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_8MHZ); // // Configure low-level I/O to use printf() // llio_init(115200); // // Configure ADC // SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC); SysCtlADCSpeedSet(SYSCTL_ADCSPEED_500KSPS); // ADC sequence #0 - setup with 2 conversions ADCSequenceDisable(ADC_BASE, 0); ADCSequenceConfigure(ADC_BASE, 0, ADC_TRIGGER_TIMER, 0); ADCIntRegister(ADC_BASE, 0, ADCIntHandler); ADCIntEnable(ADC_BASE, 0); // sequence step 0 - channel 0 ADCSequenceStepConfigure(ADC_BASE, 0, 0, ADC_CTL_CH0); // sequence step 1 - internal temperature sensor. Generate Interrupt & End of Sequence ADCSequenceStepConfigure(ADC_BASE, 0, 1, ADC_CTL_TS | ADC_CTL_IE | ADC_CTL_END); ADCSequenceEnable(ADC_BASE, 0); // // Configure Timer to trigger ADC // SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); TimerConfigure( TIMER0_BASE, TIMER_CFG_32_BIT_PER ); TimerControlTrigger(TIMER0_BASE, TIMER_A, true); TimerLoadSet( TIMER0_BASE, TIMER_A, SysCtlClockGet() / 2 ); // 2Hz trigger TimerEnable( TIMER0_BASE, TIMER_A ); printf("\r\n\r\nADC 2 Channel Example\r\n"); // // Loop forever. // while(1) { cnt = ADCSequenceDataGet(ADC_BASE, 0, adc_result); if (cnt == 2) { // Calculate temperature temperature = (2.7 - (float)adc_result[1] * 3.0 / 1024.) * 75. - 55.; printf("%d,%d,%.1f\r\n", adc_result[0], adc_result[1], temperature); } SysCtlSleep(); } }
void InitClocksGPIOAndTimer() { uint32_t ui32PWMClock; // // Enable lazy stacking for interrupt handlers. This allows floating-point // instructions to be used within interrupt handlers, but at the expense of // extra stack usage. // ROM_FPULazyStackingEnable(); // // Set the clocking to run directly from the crystal. // ROM_SysCtlClockSet( SYSCTL_SYSDIV_40 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN); ROM_SysCtlPWMClockSet(SYSCTL_PWMDIV_1); // PWM Setup SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC); GPIOPinConfigure(GPIO_PC4_M0PWM6); GPIOPinTypePWM(GPIO_PORTC_BASE, GPIO_PIN_4); GPIOPinConfigure(GPIO_PC5_M0PWM7); GPIOPinTypePWM(GPIO_PORTC_BASE, GPIO_PIN_5); ui32PWMClock = SysCtlClockGet(); ui32Load = (ui32PWMClock / PWM_FREQUENCY) - 1; PWMGenConfigure(PWM0_BASE, PWM_GEN_3, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC); PWMGenPeriodSet(PWM0_BASE, PWM_GEN_3, ui32Load); PWMPulseWidthSet(PWM0_BASE, PWM_OUT_6, PWM_LOW); PWMPulseWidthSet(PWM0_BASE, PWM_OUT_7, PWM_LOW); // Started as not active PWMOutputState(PWM0_BASE, PWM_OUT_6_BIT, false); PWMOutputState(PWM0_BASE, PWM_OUT_7_BIT, false); PWMGenEnable(PWM0_BASE, PWM_GEN_3); // PWM Setup for IR LED SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); GPIOPinConfigure(GPIO_PB6_M0PWM0); GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_6); PWMGenConfigure(PWM0_BASE, PWM_GEN_0, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC); PWMGenPeriodSet(PWM0_BASE, PWM_GEN_0, 1050/8); PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0, 525/8); PWMOutputState(PWM0_BASE, PWM_OUT_0_BIT, false); PWMGenEnable(PWM0_BASE, PWM_GEN_0); // // Enable peripheral and register interrupt handler // SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); GPIOIntRegister(GPIO_PORTA_BASE, GPIOMotionDetectorIsr); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); GPIOIntRegister(GPIO_PORTE_BASE, GPIOLightSwitchIsr); #ifdef RUN_AS_MASTER GPIOPinTypeGPIOInput(GPIO_PORTA_BASE, GPIO_PIN_5); GPIOIntTypeSet(GPIO_PORTA_BASE, GPIO_PIN_5, GPIO_BOTH_EDGES); #else GPIOPinTypeGPIOInput(GPIO_PORTA_BASE, GPIO_PIN_2); GPIOIntTypeSet(GPIO_PORTA_BASE, GPIO_PIN_2, GPIO_FALLING_EDGE); #endif GPIOPinTypeGPIOInput(GPIO_PORTE_BASE, GPIO_PIN_4); GPIOIntTypeSet(GPIO_PORTE_BASE, GPIO_PIN_4, GPIO_RISING_EDGE); // // Enable the pin interrupts. // GPIOIntEnable(GPIO_PORTA_BASE, GPIO_INT_PIN_2 | GPIO_INT_PIN_5); GPIOIntEnable(GPIO_PORTE_BASE, GPIO_INT_PIN_4); SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1); TimerConfigure(TIMER0_BASE, TIMER_CFG_A_ONE_SHOT); TimerConfigure(TIMER1_BASE, TIMER_CFG_A_ONE_SHOT); TimerIntRegister(TIMER0_BASE, TIMER_A, IRTimerIsr); TimerIntRegister(TIMER1_BASE, TIMER_A, IdleTimerIsr); ulPeriod = (SysCtlClockGet()); // once per second TimerLoadSet(TIMER0_BASE, TIMER_A, ulPeriod); TimerLoadSet(TIMER1_BASE, TIMER_A, ulPeriod * LIGHTS_ON_PERIOD_SEC); TimerLoadSet(TIMER1_BASE, TIMER_B, ulPeriod * 3); IntEnable(INT_TIMER0A); IntEnable(INT_TIMER1A); TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT); TimerIntEnable(TIMER1_BASE, TIMER_TIMA_TIMEOUT); // // Enable the GPIO port that is used for the on-board LED. // ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); // // Enable the GPIO pin for blue LED component (PF2). // ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_2); g_ulCountsPerMicrosecond = ROM_SysCtlClockGet() / 1000000; // 10ms = timeout delay g_ulIRPeriod = g_ulCountsPerMicrosecond * IR_TIMEOUT_VAL; }
//***************************************************************************** // // This example application demonstrates the use of the timers to generate // periodic interrupts. // //***************************************************************************** int main(void) { unsigned long ulPeriod, dutyCycle; // En ejemplo // // Enable lazy stacking for interrupt handlers. This allows floating-point // instructions to be used within interrupt handlers, but at the expense of // extra stack usage. // ROM_FPULazyStackingEnable(); // // Set the clocking to run directly from the crystal. // // ROM_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | // SYSCTL_XTAL_16MHZ); // Agregado Ejemplo // 40 MHz system clock SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL| SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN); ulPeriod = 1000; dutyCycle = 250; // Turn off LEDs SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3); GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0); // Configure PB6 as T0CCP0 SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); GPIOPinConfigure(GPIO_PB6_T0CCP0); GPIOPinTypeTimer(GPIO_PORTB_BASE, GPIO_PIN_6); // Configure timer SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); TimerConfigure(TIMER0_BASE, TIMER_CFG_SPLIT_PAIR|TIMER_CFG_A_PWM); TimerLoadSet(TIMER0_BASE, TIMER_A, ulPeriod -1); TimerMatchSet(TIMER0_BASE, TIMER_A, dutyCycle); // PWM TimerEnable(TIMER0_BASE, TIMER_A); // Fin Agregado // // Initialize the UART and write status. // ConfigureUART(); UARTprintf("\033[2JTimers example\n"); UARTprintf("T1: 0 T2: 0"); // // Enable the GPIO port that is used for the on-board LED. // // ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); // // Enable the GPIO pins for the LED (PF1 & PF2). // // ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_2 | GPIO_PIN_1); // // Enable the peripherals used by this example. // // ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); // ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1); // // Enable processor interrupts. // // ROM_IntMasterEnable(); // // Configure the two 32-bit periodic timers. // // ROM_TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC); // ROM_TimerConfigure(TIMER1_BASE, TIMER_CFG_PERIODIC); // ROM_TimerLoadSet(TIMER0_BASE, TIMER_A, ROM_SysCtlClockGet()); // ROM_TimerLoadSet(TIMER1_BASE, TIMER_A, ROM_SysCtlClockGet() / 2); // // Setup the interrupts for the timer timeouts. // // ROM_IntEnable(INT_TIMER0A); // ROM_IntEnable(INT_TIMER1A); // ROM_TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT); // ROM_TimerIntEnable(TIMER1_BASE, TIMER_TIMA_TIMEOUT); // // Enable the timers. // // ROM_TimerEnable(TIMER0_BASE, TIMER_A); // ROM_TimerEnable(TIMER1_BASE, TIMER_A); // // Loop forever while the timers run. // while(1) { } }
int main(void) { //Set the clock SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN); //Enable the peripherals SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1); //Set the input type GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4); GPIOPinTypeGPIOOutput(GPIO_PORTC_BASE, GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7); GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_2); GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_6); //Timer Configuration TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC); TimerConfigure(TIMER1_BASE, TIMER_CFG_PERIODIC); TimerControlEvent(TIMER0_BASE, TIMER_A, TIMER_EVENT_POS_EDGE); TimerControlEvent(TIMER1_BASE, TIMER_A, TIMER_EVENT_POS_EDGE); TimerLoadSet(TIMER1_BASE, TIMER_A, 50000); TimerEnable(TIMER1_BASE, TIMER_A); while(1) { //Check the state of the timer if(TimerValueGet(TIMER1_BASE, TIMER_A) == 0x0) { number++; //reanable the timer. TimerLoadSet(TIMER1_BASE, TIMER_A, 50000); } int first_nible = (number & first_digit) >> 4; int second_nible = (number & second_digit); if(first_nible >= 10) { first_nible = first_nible + 7; } if(second_nible >=10) { second_nible = second_nible + 7; } //Turn off both displays GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_4, pin_4); GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_2, pin_2); //Make sure all data pins are low lower_pins(); //Send Data sevenSegWrite((char)(((int)'0')+first_nible)); //Turn on first 7-seg control signal GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_4, 0x00); timer_delay(); //Turn off control signal for both 7-seg GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_4, pin_4); GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_2, pin_2); //Make sure all data pins are low lower_pins(); //Send Data sevenSegWrite((char)(((int)'0')+second_nible)); //Turn on second 7-seg control signal GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_2, 0x00); timer_delay(); } }
int main(void) { volatile uint32_t ui32Load; volatile uint32_t ui32PWMClock; volatile uint8_t ui8AdjustRed, ui8AdjustGreen, ui8AdjustBlue; ui8AdjustRed = 254; ui8AdjustGreen = 2; ui8AdjustBlue = 2; // Set the system clock to 40 MHz SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ); // Set the PWM Clock to 1/64 of the system clock i.e. 625 kHz SysCtlPWMClockSet(SYSCTL_PWMDIV_64); // Timer related intializations, time period is 10 ms. SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC); uint32_t ui32Period; ui32Period = (SysCtlClockGet()/100)/ 2; TimerLoadSet(TIMER0_BASE, TIMER_A, ui32Period -1); IntEnable(INT_TIMER0A); TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT); IntMasterEnable(); TimerEnable(TIMER0_BASE, TIMER_A); // Enable the PWM module 1 SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1); // Enable the GPIO Port F SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); // PWM related initializations GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3); GPIOPinConfigure(GPIO_PF1_M1PWM5); GPIOPinConfigure(GPIO_PF2_M1PWM6); GPIOPinConfigure(GPIO_PF3_M1PWM7); HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY; HWREG(GPIO_PORTF_BASE + GPIO_O_CR) |= 0x01; HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = 0; GPIODirModeSet(GPIO_PORTF_BASE, GPIO_PIN_4|GPIO_PIN_0, GPIO_DIR_MODE_IN); GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_4|GPIO_PIN_0, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU); ui32PWMClock = SysCtlClockGet() / 64; ui32Load = (ui32PWMClock / PWM_FREQUENCY) - 1; PWMGenConfigure(PWM1_BASE, PWM_GEN_2, PWM_GEN_MODE_DOWN); PWMGenConfigure(PWM1_BASE, PWM_GEN_3, PWM_GEN_MODE_DOWN); PWMGenPeriodSet(PWM1_BASE, PWM_GEN_2, ui32Load); PWMGenPeriodSet(PWM1_BASE, PWM_GEN_3, ui32Load); PWMPulseWidthSet(PWM1_BASE, PWM_OUT_5, ui8AdjustRed * ui32Load / 1000); PWMOutputState(PWM1_BASE, PWM_OUT_5_BIT, true); PWMGenEnable(PWM1_BASE, PWM_GEN_2); PWMPulseWidthSet(PWM1_BASE, PWM_OUT_6, ui8AdjustBlue * ui32Load / 1000); PWMOutputState(PWM1_BASE, PWM_OUT_6_BIT, true); PWMPulseWidthSet(PWM1_BASE, PWM_OUT_7, ui8AdjustGreen * ui32Load / 1000); PWMOutputState(PWM1_BASE, PWM_OUT_7_BIT, true); PWMGenEnable(PWM1_BASE, PWM_GEN_3); // Detecting the phase while in the auto mode, RG, GB or BR. int autoLedState = 0; while(1) { if(mode==0) { if(autoLedState==0) { ui8AdjustRed--; ui8AdjustGreen++; if(ui8AdjustRed==2 && ui8AdjustGreen==254) autoLedState = 1; } else if(autoLedState==1) { ui8AdjustGreen--; ui8AdjustBlue++; if(ui8AdjustGreen==2 && ui8AdjustBlue==254) autoLedState = 2; } else if(autoLedState==2) { ui8AdjustBlue--; ui8AdjustRed++; if(ui8AdjustBlue==2 && ui8AdjustRed==254) autoLedState = 0; } PWMPulseWidthSet(PWM1_BASE, PWM_OUT_5, ui8AdjustRed * ui32Load / 1000); PWMPulseWidthSet(PWM1_BASE, PWM_OUT_6, ui8AdjustBlue * ui32Load / 1000); PWMPulseWidthSet(PWM1_BASE, PWM_OUT_7, ui8AdjustGreen * ui32Load / 1000); SysCtlDelay(delay); } else { if(mode==1) { ui8AdjustBlue = 2; ui8AdjustGreen = 2; if(GPIOPinRead(GPIO_PORTF_BASE,GPIO_PIN_4)==0x00) { ui8AdjustRed--; if (ui8AdjustRed < 20) ui8AdjustRed = 20; } if(GPIOPinRead(GPIO_PORTF_BASE,GPIO_PIN_0)==0x00) { ui8AdjustRed++; if (ui8AdjustRed > 254) ui8AdjustRed = 254; } } else if(mode==2) { ui8AdjustRed = 2; ui8AdjustGreen = 2; if(GPIOPinRead(GPIO_PORTF_BASE,GPIO_PIN_4)==0x00) { ui8AdjustBlue--; if (ui8AdjustBlue < 20) ui8AdjustBlue = 20; } if(GPIOPinRead(GPIO_PORTF_BASE,GPIO_PIN_0)==0x00) { ui8AdjustBlue++; if (ui8AdjustBlue > 254) ui8AdjustBlue = 254; } } else if(mode==3) { ui8AdjustBlue = 2; ui8AdjustRed = 2; if(GPIOPinRead(GPIO_PORTF_BASE,GPIO_PIN_4)==0x00) { ui8AdjustGreen--; if (ui8AdjustGreen < 20) ui8AdjustGreen = 20; } if(GPIOPinRead(GPIO_PORTF_BASE,GPIO_PIN_0)==0x00) { ui8AdjustGreen++; if (ui8AdjustGreen > 254) ui8AdjustGreen = 254; } } PWMPulseWidthSet(PWM1_BASE, PWM_OUT_5, ui8AdjustRed * ui32Load / 1000); PWMPulseWidthSet(PWM1_BASE, PWM_OUT_6, ui8AdjustBlue * ui32Load / 1000); PWMPulseWidthSet(PWM1_BASE, PWM_OUT_7, ui8AdjustGreen * ui32Load / 1000); SysCtlDelay(100000); } } }
//***************************************************************************** // // Configure Timer0B as a 16-bit periodic counter with an interrupt // every 1ms. // //***************************************************************************** int main(void) { #if defined(TARGET_IS_TM4C129_RA0) || \ defined(TARGET_IS_TM4C129_RA1) || \ defined(TARGET_IS_TM4C129_RA2) uint32_t ui32SysClock; #endif uint32_t ui32PrevCount = 0; // // Set the clocking to run directly from the external crystal/oscillator. // TODO: The SYSCTL_XTAL_ value must be changed to match the value of the // crystal on your board. // #if defined(TARGET_IS_TM4C129_RA0) || \ defined(TARGET_IS_TM4C129_RA1) || \ defined(TARGET_IS_TM4C129_RA2) ui32SysClock = SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ | SYSCTL_OSC_MAIN | SYSCTL_USE_OSC), 25000000); #else SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); #endif // // The Timer0 peripheral must be enabled for use. // SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); // // Set up the serial console to use for displaying messages. This is // just for this example program and is not needed for Timer operation. // InitConsole(); // // Display the example setup on the console. // UARTprintf("16-Bit Timer Interrupt ->"); UARTprintf("\n Timer = Timer0B"); UARTprintf("\n Mode = Periodic"); UARTprintf("\n Number of interrupts = %d", NUMBER_OF_INTS); UARTprintf("\n Rate = 1ms\n\n"); // // Configure Timer0B as a 16-bit periodic timer. // TimerConfigure(TIMER0_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_B_PERIODIC); // // Set the Timer0B load value to 1ms. // #if defined(TARGET_IS_TM4C129_RA0) || \ defined(TARGET_IS_TM4C129_RA1) || \ defined(TARGET_IS_TM4C129_RA2) TimerLoadSet(TIMER0_BASE, TIMER_B, ui32SysClock / 1000); #else TimerLoadSet(TIMER0_BASE, TIMER_B, SysCtlClockGet() / 1000); #endif // // Enable processor interrupts. // IntMasterEnable(); // // Configure the Timer0B interrupt for timer timeout. // TimerIntEnable(TIMER0_BASE, TIMER_TIMB_TIMEOUT); // // Enable the Timer0B interrupt on the processor (NVIC). // IntEnable(INT_TIMER0B); // // Initialize the interrupt counter. // g_ui32Counter = 0; // // Enable Timer0B. // TimerEnable(TIMER0_BASE, TIMER_B); // // Loop forever while the Timer0B runs. // while(1) { // // If the interrupt count changed, print the new value // if(ui32PrevCount != g_ui32Counter) { // // Print the periodic interrupt counter. // UARTprintf("Number of interrupts: %d\r", g_ui32Counter); ui32PrevCount = g_ui32Counter; } } }
void stepper_set_stepping_rate(struct stepper *s, uint32_t stepping_rate){ s->stepping_rate = stepping_rate; TimerLoadSet(TIMER2_BASE, TIMER_A, SysCtlClockGet()/(s->stepping_rate * (256 / s->div))); }
/* * function: ADC3IntHandler * interrupt handler ADC0, sequence 3 * return: none * */ void ADC3IntHandler(void) { unsigned long ulStatus; static unsigned long uluDMACount = 0; static unsigned long ulDataXferd = 0; unsigned long ulNextuDMAXferSize = 0; ADCIntClear(ADC0_BASE, SEQUENCER); // If the channel's not done capturing, we have an error if (uDMAChannelIsEnabled(UDMA_CHANNEL_ADC3)) { // Increment error counter adcNode[0].g_ulBadPeriphIsr2++; ADCIntDisable(ADC0_BASE, SEQUENCER); IntPendClear(INT_ADC0SS3); return; } ulStatus = uDMAChannelSizeGet(UDMA_CHANNEL_ADC3); // If non-zero items are left in the transfer buffer // Something went wrong if (ulStatus) { adcNode[0].g_ulBadPeriphIsr1++; return; } // Disable the sampling timer TimerDisable(TIMER0_BASE, TIMER_A); // how many times the DMA has been full without processing the data uluDMACount++; // The amount of data transferred increments in sets of 1024 ulDataXferd += UDMA_XFER_MAX; if (NUM_SAMPLES > ulDataXferd) { if ((NUM_SAMPLES - ulDataXferd) > UDMA_XFER_MAX) { ulNextuDMAXferSize = UDMA_XFER_MAX; } else { ulNextuDMAXferSize = NUM_SAMPLES - ulDataXferd; } #ifdef USE_TEMPORARY_BUFFER if (currentAdcBuffer == g_ulADCValues_B) { uDMAChannelTransferSet(UDMA_CHANNEL_ADC3 | UDMA_PRI_SELECT, UDMA_MODE_BASIC, (void *) (ADC0_BASE + ADC_O_SSFIFO3 + (0x20 * UDMA_ARB_1)), g_ulADCValues + (UDMA_XFER_MAX * uluDMACount), ulNextuDMAXferSize); } else { uDMAChannelTransferSet(UDMA_CHANNEL_ADC3 | UDMA_PRI_SELECT, UDMA_MODE_BASIC, (void *) (ADC0_BASE + ADC_O_SSFIFO3 + (0x20 * UDMA_ARB_1)), g_ulADCValues_B + (UDMA_XFER_MAX * uluDMACount), ulNextuDMAXferSize); } #endif #ifdef NOT_TEMPORARY_BUFFER uDMAChannelTransferSet(UDMA_CHANNEL_ADC3 | UDMA_PRI_SELECT, UDMA_MODE_BASIC, (void *) (ADC0_BASE + ADC_O_SSFIFO3 + (0x20 * UDMA_ARB_1)), g_ulADCValues + (UDMA_XFER_MAX * uluDMACount), ulNextuDMAXferSize); #endif uDMAChannelEnable(UDMA_CHANNEL_ADC3); TimerLoadSet(TIMER0_BASE, TIMER_A, LoadTimer); TimerEnable(TIMER0_BASE, TIMER_A); } else { uluDMACount = 0; ulDataXferd = 0; ADCIntDisable(ADC0_BASE, SEQUENCER); IntPendClear(INT_ADC0SS3); // Signal that we have new data to be processed adcNode[0].g_ucDataReady = 1; #ifdef USING_BUFFER2 if (adcNode[0].g_ucDataReady) { memcpy(output2, g_ulADCValues, NUM_SAMPLES); countFullData++; } #endif } }
//***************************************************************************** // //! Initializes the touch screen driver. //! //! This function initializes the touch screen driver, beginning the process of //! reading from the touch screen. This driver uses the following hardware //! resources: //! //! - ADC sample sequence 3 //! - Timer 0 subtimer A //! //! \return None. // //***************************************************************************** void TouchScreenInit(void) { unsigned short usController; // // Set the initial state of the touch screen driver's state machine. // g_ulTSState = TS_STATE_INIT; // // There is no touch screen handler initially. // g_pfnTSHandler = 0; // // Determine which display controller is in use and, from this, determine // what the sense of the Y axis must be. The -F03 version of the display // containing an ILI9320 controller reverses the sense of the Y axis // relative to the later -F05 and -F02 versions containing ILI9325 and // ILI9328 controllers respectively. // usController = Formike240x320x16_ILI9320ControllerIdGet(); g_bReverseLongAxis = ((usController != 0x9320) ? true : false); // // Enable the peripherals used by the touch screen interface. // SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); // // Configure the ADC sample sequence used to read the touch screen reading. // ADCHardwareOversampleConfigure(ADC0_BASE, 4); ADCSequenceConfigure(ADC0_BASE, 3, ADC_TRIGGER_TIMER, 0); ADCSequenceStepConfigure(ADC0_BASE, 3, 0, ADC_CTL_CH6 | ADC_CTL_END | ADC_CTL_IE); ADCSequenceEnable(ADC0_BASE, 3); // // Enable the ADC sample sequence interrupt. // ADCIntEnable(ADC0_BASE, 3); IntEnable(INT_ADC0SS3); // // Configure the GPIOs used to drive the touch screen layers. // GPIOPinTypeGPIOOutput(TS_X_BASE, TS_XP_PIN | TS_XN_PIN); GPIOPinTypeGPIOOutput(TS_Y_BASE, TS_YP_PIN | TS_YN_PIN); GPIOPinWrite(TS_X_BASE, TS_XP_PIN | TS_XN_PIN, 0x00); GPIOPinWrite(TS_Y_BASE, TS_YP_PIN | TS_YN_PIN, 0x00); // // See if the ADC trigger timer has been configured, and configure it only // if it has not been configured yet. // if((HWREG(TIMER0_BASE + TIMER_O_CTL) & TIMER_CTL_TAEN) == 0) { // // Configure the timer to trigger the sampling of the touch screen // every millisecond. // TimerConfigure(TIMER0_BASE, (TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_PERIODIC | TIMER_CFG_B_PERIODIC)); TimerLoadSet(TIMER0_BASE, TIMER_A, (SysCtlClockGet() / 1000) - 1); TimerControlTrigger(TIMER0_BASE, TIMER_A, true); // // Enable the timer. At this point, the touch screen state machine // will sample and run once per millisecond. // TimerEnable(TIMER0_BASE, TIMER_A); } }
/********************************************************************************************** * Main *********************************************************************************************/ void main(void) { //After tomorrow's test flight. FOR THE LOVE OF GOD MOVE THESE INITALIZATIONS TO FUNCTIONS /********************************************************************************************** * Local Variables *********************************************************************************************/ unsigned long ultrasonic = 0; // Enable lazy stacking for interrupt handlers. This allows floating-point // instructions to be used within interrupt handlers, but at the expense of // extra stack usage. FPULazyStackingEnable(); //Set the clock speed to 80MHz aka max speed SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); /*unsigned long test[2]; test[0] = 180; test[1] = 10; short bob[1]; bob[0] = ((char)test[0]<<8)|(char)test[1]; float jimmy = (short)(((char)test[0]<<8)|(char)test[1]); jimmy /= 26;*/ /********************************************************************************************** * Peripheral Initialization Awake *********************************************************************************************/ SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); //Turn on GPIO communication on F pins for switches SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); //Turn on GPIO for ADC SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); //Turn on GPIO for the PWM comms SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); //Turn on GPIO for LED test SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); //Turn on GPIO for UART SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); //Turn on Timer for PWM SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1); //Turn on Timer for PWM SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER2); //Turn on Timer for PWM SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER3); //Turn on Timer for PWM SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C1); //Turn on I2C communication I2C slot 0 SysCtlPeripheralEnable(SYSCTL_PERIPH_UART2); //Turn on the UART com SysCtlPeripheralEnable(SYSCTL_PERIPH_WDOG); //Turn on the watchdog timer. This is a risky idea but I think it's for the best. /********************************************************************************************** * Peripheral Initialization Sleep *********************************************************************************************/ /*SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_GPIOF); //This sets what peripherals are still enabled in sleep mode while UART would be nice, it would require the clock operate at full speed which is :P SysCtlPeripheralSleepDisable(SYSCTL_PERIPH_GPIOB); SysCtlPeripheralSleepDisable(SYSCTL_PERIPH_GPIOA); SysCtlPeripheralSleepDisable(SYSCTL_PERIPH_TIMER0); SysCtlPeripheralSleepDisable(SYSCTL_PERIPH_TIMER1); SysCtlPeripheralSleepDisable(SYSCTL_PERIPH_TIMER2); SysCtlPeripheralSleepDisable(SYSCTL_PERIPH_TIMER3); SysCtlPeripheralSleepDisable(SYSCTL_PERIPH_SSI0); SysCtlPeripheralSleepDisable(SYSCTL_PERIPH_I2C1); SysCtlPeripheralSleepDisable(SYSCTL_PERIPH_ADC); SysCtlPeripheralClockGating(true); //I'm not sure about this one maybe remove it */ /********************************************************************************************** * PWM Initialization *********************************************************************************************/ SysCtlDelay((SysCtlClockGet() / (1000 * 3))*100); //This shouldn't be needed will test to remove //PWM pin Setup //PWM 0 on GPIO PB6, PWM 1 on pin 4... etc GPIOPinConfigure(GPIO_PB6_T0CCP0); //Pitch - yaw + GPIOPinConfigure(GPIO_PB4_T1CCP0); //Pitch + yaw + GPIOPinConfigure(GPIO_PB0_T2CCP0); //Roll - yaw - GPIOPinConfigure(GPIO_PB2_T3CCP0); //Roll + yaw - GPIOPinTypeTimer(GPIO_PORTB_BASE, (GPIO_PIN_6|GPIO_PIN_4|GPIO_PIN_0|GPIO_PIN_2)); //Prescale the timers so they are slow enough to work with the ESC TimerPrescaleSet(TIMER0_BASE,TIMER_A,2); TimerPrescaleSet(TIMER1_BASE,TIMER_A,2); TimerPrescaleSet(TIMER2_BASE,TIMER_A,2); TimerPrescaleSet(TIMER3_BASE,TIMER_A,2); //Basic LED Out Test Not sure why this is here look into This just turns on an LED that I don't have plugged in. should remove later GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_3); GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_3,0xFF); //GPIOPinTypeGPIOOutputOD(GPIO_PORTB_BASE,GPIO_PIN_0); //Timers Setup for PWM and the load for the countdown TimerConfigure(TIMER0_BASE, TIMER_CFG_SPLIT_PAIR|TIMER_CFG_A_PWM); TimerLoadSet(TIMER0_BASE, TIMER_A, ulPeriod -1); TimerConfigure(TIMER1_BASE, TIMER_CFG_SPLIT_PAIR|TIMER_CFG_A_PWM); TimerLoadSet(TIMER1_BASE, TIMER_A, ulPeriod -1); TimerConfigure(TIMER2_BASE, TIMER_CFG_SPLIT_PAIR|TIMER_CFG_A_PWM); TimerLoadSet(TIMER2_BASE, TIMER_A, (ulPeriod -1)); TimerConfigure(TIMER3_BASE, TIMER_CFG_SPLIT_PAIR|TIMER_CFG_A_PWM); TimerLoadSet(TIMER3_BASE, TIMER_A, ulPeriod -1); //TimerPrescaleSet(TIMER2_BASE, TIMER_A, extender1); //TimerLoadSet(TIMER2_BASE, TIMER_A, period1); //Set the match which is when the thing will pull high TimerMatchSet(TIMER0_BASE, TIMER_A, 254); //Duty cycle = (1-%desired)*1000 note this means this number is percent low not percent high TimerMatchSet(TIMER1_BASE, TIMER_A, 254); TimerMatchSet(TIMER2_BASE, TIMER_A, 254); TimerMatchSet(TIMER3_BASE, TIMER_A, 254); //TimerPrescaleMatchSet(TIMER2_BASE, TIMER_A, extender2); //TimerMatchSet(TIMER2_BASE, TIMER_A, period2); //Enable the timers TimerEnable(TIMER0_BASE, TIMER_A); TimerEnable(TIMER1_BASE, TIMER_A); TimerEnable(TIMER2_BASE, TIMER_A); TimerEnable(TIMER3_BASE, TIMER_A); /********************************************************************************************** * onboard Chip interrupt Initialization *********************************************************************************************/ //These two buttons are used to reset the bluetooth module in case of disconnection GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE,GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3); //RGB LED's GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3,0x00); HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY_DD; //Sw1 (PF4) is unaviable unless you make it only a GPIOF input via these commands HWREG(GPIO_PORTF_BASE + GPIO_O_CR) = 0x1; GPIOPinTypeGPIOInput(GPIO_PORTF_BASE,GPIO_PIN_0|GPIO_PIN_4); //Onboard buttons (PF0=Sw2,PF4=Sw1 GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_0|GPIO_PIN_4, GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU); //This will make the buttons falling edge (a press pulls them low) //void (*functionPtr)(void) = &onBoardInteruptHandle; GPIOPortIntRegister(GPIO_PORTF_BASE, onBoardInteruptHandle); //set function to handle interupt GPIOIntTypeSet(GPIO_PORTF_BASE,GPIO_PIN_0|GPIO_PIN_4,GPIO_FALLING_EDGE); //Set the interrupt as falling edge GPIOPinIntEnable(GPIO_PORTF_BASE,GPIO_PIN_0|GPIO_PIN_4); //Enable the interrupt //IntMasterEnable(); IntEnable(INT_GPIOF); /********************************************************************************************** * UART Initialization *********************************************************************************************/ //Unlock PD7 HWREG(GPIO_PORTD_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY_DD; HWREG(GPIO_PORTD_BASE + GPIO_O_CR) = 0x80; GPIOPinConfigure(GPIO_PD7_U2TX); //Set PD7 as TX GPIOPinTypeUART(GPIO_PORTD_BASE, GPIO_PIN_7); GPIOPinConfigure(GPIO_PD6_U2RX); //Set PD6 as RX GPIOPinTypeUART(GPIO_PORTD_BASE, GPIO_PIN_6); UARTConfigSetExpClk(UART2_BASE,SysCtlClockGet(),115200,UART_CONFIG_WLEN_8|UART_CONFIG_STOP_ONE|UART_CONFIG_PAR_NONE); //I believe the Xbee defaults to no parity I do know it's 9600 baud though, changed to 115200 for bluetooth reasons UARTFIFOLevelSet(UART2_BASE,UART_FIFO_TX1_8,UART_FIFO_RX1_8); //Set's how big the fifo needs to be in order to call the interrupt handler, 2byte UARTIntRegister(UART2_BASE,Uart2IntHandler); //Regiester the interrupt handler UARTIntClear(UART2_BASE, UART_INT_TX | UART_INT_RX); //Clear the interrupt UARTIntEnable(UART2_BASE, UART_INT_TX | UART_INT_RX); //Enable the interrupt to trigger on both TX and RX event's. Could possibly remove TX UARTEnable(UART2_BASE); //Enable UART IntEnable(INT_UART2); //Second way to enable handler not sure if needed using anyway /********************************************************************************************** * I2C Initialization *********************************************************************************************/ //Serious credit to the man who made the Arduino version of this. he gave me addresses and equations. Sadly Arduino obfuscates what really is happening //Link posted on blog page //gyro address = 0x68 not 0x69 GPIOPinConfigure(GPIO_PA7_I2C1SDA); GPIOPinTypeI2C(GPIO_PORTA_BASE, GPIO_PIN_7); //Set GPA7 as SDA GPIOPinConfigure(GPIO_PA6_I2C1SCL); GPIOPinTypeI2CSCL(GPIO_PORTA_BASE, GPIO_PIN_6); //Set GPA6 as SCL I2CMasterInitExpClk(I2C1_MASTER_BASE,SysCtlClockGet(),false); //I think it operates at 100kbps I2CMasterEnable(I2C1_MASTER_BASE); //Initalize the accelerometer Address = 0x53 GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3,0x02); UARTSend(0xAB); I2CTransmit(0x53,0x2D,0x00); I2CTransmit(0x53,0x2D,0x10); I2CTransmit(0x53,0x2D,0x08); //Initalize the gyroscope Address = 0x68 I2CTransmit(0x68,0x3E,0x00); I2CTransmit(0x68,0x15,0x07); I2CTransmit(0x68,0x16,0x1E); I2CTransmit(0x68,0x17,0x00); UARTSend(0xAC); /********************************************************************************************** * SysTick Initialization *********************************************************************************************/ SysTickIntRegister(SysTickIntHandler); SysTickIntEnable(); SysTickPeriodSet((SysCtlClockGet() / (1000 * 3))*timeBetweenCalculations); //This sets the period for the delay. the last num is the num of milliseconds SysTickEnable(); /********************************************************************************************** * Watchdog Initialization *********************************************************************************************/ WatchdogReloadSet(WATCHDOG_BASE, 0xFEEFEEFF); //Set the timer for a reset WatchdogIntRegister(WATCHDOG_BASE,WatchdogIntHandler); //Enable interrupt WatchdogIntClear(WATCHDOG_BASE); WatchdogIntEnable(WATCHDOG_BASE); WatchdogEnable(WATCHDOG_BASE); //Enable the actual timer IntEnable(INT_WATCHDOG); /********************************************************************************************** * Preflight motor inialization maybe not necessary not going to test *********************************************************************************************/ PWMSet(TIMER0_BASE,998); PWMSet(TIMER1_BASE,998); PWMSet(TIMER2_BASE,998); PWMSet(TIMER3_BASE,998); recievedCommands[0]=253; SysCtlDelay((SysCtlClockGet() / (1000 * 3))*100); //Very important to ensure motor see's a start high (998 makes 0 sense but it works so shhhh don't tell anyone) GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3,0x06); while(1){ WatchdogReloadSet(WATCHDOG_BASE, 0xFEEFEEFF); //Feed the dog a new time //UARTSend(recievedCommands[0]); //SysCtlDelay(50000); //Set 4 PWM Outputs //Get Acc data I2CRead(0x53,0x32,6,quadAcc); //Address blah blah 2 for each axis rawAccToG(quadAcc,RwAcc); /*GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3,0x04); //Blue //Get Gyro data /************************************** Gyro ITG-3200 I2C registers: temp MSB = 1B, temp LSB = 1C x axis MSB = 1D, x axis LSB = 1E y axis MSB = 1F, y axis LSB = 20 z axis MSB = 21, z axis LSB = 22 *************************************/ I2CRead(0x68,0x1B,8,quadGyro); //Address blah blah 2 for each axis + 2 for temperature. why. because why not rawGyroToDegsec(quadGyro,Gyro_ds); //GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3,0x02); //Red //Get the actual angles in XYZ. Store them in RwEst //getInclination(RwAcc, RwEst, RwGyro, Gyro_ds, Awz); //After this function is called RwEst will hold the roll pitch and yaw //RwEst will be returned in PITCH, ROLL, YAW 0, 1, 2 remember this order very important. Little obvious but yaw is worthless /*if(RwEst[1]>0.5){ GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3,0x06); //Red Blue, Correct data read in }else{ GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3,0x0A); //Red Green, The correct data is not there }*/ /*GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3,0x06); //Red Blue, Correct data read in float test=RwAcc[0]*100; //These two commands work char temp = (char)test; //UARTSend((char)(RwAcc[0])*100); //This one does not UARTSend(temp); //UARTSend((char)(RwAcc[1])*100); UARTSend(0xAA); SysCtlDelay((SysCtlClockGet() / (1000 * 3))*1); */ } }
//***************************************************************************** // // Configure Timer0B as a 16-bit one-shot counter with a single interrupt // after 1ms. // //***************************************************************************** int main(void) { // // Set the clocking to run directly from the external crystal/oscillator. // TODO: The SYSCTL_XTAL_ value must be changed to match the value of the // crystal on your board. // SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); // // The Timer0 peripheral must be enabled for use. // SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); // // Set up the serial console to use for displaying messages. This is // just for this example program and is not needed for Timer operation. // InitConsole(); // // Display the example setup on the console. // UARTprintf("16-Bit Timer Interrupt ->"); UARTprintf("\n Timer = Timer0B"); UARTprintf("\n Mode = One Shot"); UARTprintf("\n Rate = 1ms"); // // The Timer0 peripheral must be enabled for use. // TimerConfigure(TIMER0_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_B_ONE_SHOT); // // Set the Timer0B load value to 1ms. // TimerLoadSet(TIMER0_BASE, TIMER_B, SysCtlClockGet() / 1000); // // Enable processor interrupts. // IntMasterEnable(); // // Configure the Timer0B interrupt for timer timeout. // TimerIntEnable(TIMER0_BASE, TIMER_TIMB_TIMEOUT); // // Enable the Timer0B interrupt on the processor (NVIC). // IntEnable(INT_TIMER0B); // // Enable Timer0B. // TimerEnable(TIMER0_BASE, TIMER_B); // // Wait for interrupt to occur // while(!g_bIntFlag) { } // // Display a message indicating that the one shot interrupt was received. // UARTprintf("\n\nOne shot timer interrupt received."); // // Loop forever. // while(1) { } }