/* Initializes PWM timers for ESC control \param none Motor 1: PB4 using TIMER1_A Motor 2: PB5 using TIMER1_B Motor 3: PB0 using TIMER2_A Motor 4: PB1 using TIMER2_B \return none */ void initPWM() { // Configure output pins SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); GPIOPinConfigure(GPIO_PB4_T1CCP0); GPIOPinTypeTimer(GPIO_PORTB_BASE, GPIO_PIN_4); GPIOPinConfigure(GPIO_PB5_T1CCP1); GPIOPinTypeTimer(GPIO_PORTB_BASE, GPIO_PIN_5); GPIOPinConfigure(GPIO_PB0_T2CCP0); GPIOPinTypeTimer(GPIO_PORTB_BASE, GPIO_PIN_0); GPIOPinConfigure(GPIO_PB1_T2CCP1); GPIOPinTypeTimer(GPIO_PORTB_BASE, GPIO_PIN_1); // Configure the two timers as half width (16bit) pwm (though we get 24 bits with the "prescale") SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1); TimerConfigure(TIMER1_BASE, TIMER_CFG_SPLIT_PAIR|TIMER_CFG_A_PWM|TIMER_CFG_B_PWM); TimerControlLevel(TIMER1_BASE, TIMER_BOTH, true); SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER2); TimerConfigure(TIMER2_BASE, TIMER_CFG_SPLIT_PAIR|TIMER_CFG_A_PWM|TIMER_CFG_B_PWM); TimerControlLevel(TIMER2_BASE, TIMER_BOTH, true); //calculate pulse repetition rate uint32_t cycles_per_interval = SysCtlClockGet() / MOTORUPDATE /*Hz*/; uint32_t periodl = cycles_per_interval; uint8_t periodh = periodl >> 16; periodl &= 0xFFFF; //set pulse repetition rate TimerPrescaleSet(TIMER1_BASE, TIMER_A, periodh); TimerLoadSet(TIMER1_BASE, TIMER_A, periodl); TimerPrescaleSet(TIMER1_BASE, TIMER_B, periodh); TimerLoadSet(TIMER1_BASE, TIMER_B, periodl); TimerPrescaleSet(TIMER2_BASE, TIMER_A, periodh); TimerLoadSet(TIMER2_BASE, TIMER_A, periodl); TimerPrescaleSet(TIMER2_BASE, TIMER_B, periodh); TimerLoadSet(TIMER2_BASE, TIMER_B, periodl); //set all motors to zero motorSet(1,0); motorSet(2,0); motorSet(3,0); motorSet(4,0); //enable timers TimerEnable(TIMER1_BASE, TIMER_A); TimerEnable(TIMER1_BASE, TIMER_B); TimerEnable(TIMER2_BASE, TIMER_A); TimerEnable(TIMER2_BASE, TIMER_B); }
void control_init() { //// laser control // Setup Timer0 for a 488.28125Hz "phase correct PWM" wave (assuming a 16Mhz clock) // Timer0 can pwm either PD5 (OC0B) or PD6 (OC0A), we use PD6 // TCCR0A and TCCR0B are the registers to setup Timer0 // see chapter "8-bit Timer/Counter0 with PWM" in Atmga328 specs // OCR0A sets the duty cycle 0-255 corresponding to 0-100% // also see: http://arduino.cc/en/Tutorial/SecretsOfArduinoPWM GPIOPinTypeGPIOOutput(LASER_EN_PORT, LASER_EN_MASK); GPIOPinWrite(LASER_EN_PORT, LASER_EN_MASK, LASER_EN_INVERT); // Configure timer SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); TimerConfigure(LASER_TIMER, TIMER_CFG_SPLIT_PAIR|TIMER_CFG_A_PWM|TIMER_CFG_B_ONE_SHOT); TimerControlLevel(LASER_TIMER, TIMER_A, 1); // PPI = PWMfreq/(feedrate/MM_PER_INCH/60) // Set PPI Pulse timer ppi_cycles = SysCtlClockGet() / 1000 * CONFIG_LASER_PPI_PULSE_MS; ppi_divider = ppi_cycles >> 16; ppi_cycles /= (ppi_divider + 1); TimerPrescaleSet(LASER_TIMER, TIMER_B, ppi_divider); TimerLoadSet(LASER_TIMER, TIMER_B, ppi_cycles); // Setup ISR TimerIntRegister(LASER_TIMER, TIMER_B, laser_isr); TimerIntEnable(LASER_TIMER, TIMER_TIMB_TIMEOUT); IntPrioritySet(INT_TIMER0B, CONFIG_LASER_PRIORITY); // Set PWM refresh rate laser_cycles = SysCtlClockGet() / CONFIG_LASER_PWM_FREQ; /*Hz*/ laser_divider = laser_cycles >> 16; laser_cycles /= (laser_divider + 1); // Setup Laser PWM Timer TimerPrescaleSet(LASER_TIMER, TIMER_A, laser_divider); TimerLoadSet(LASER_TIMER, TIMER_A, laser_cycles); TimerPrescaleMatchSet(LASER_TIMER, TIMER_A, laser_divider); laser_intensity = 0; // Set default value control_laser_intensity(0); control_laser(0, 0); TimerEnable(LASER_TIMER, TIMER_A); // ToDo: Map the timer ccp pin sensibly GPIOPinConfigure(GPIO_PB6_T0CCP0); GPIOPinTypeTimer(LASER_PORT, (1 << LASER_BIT)); //// air and aux assist control GPIOPinTypeGPIOOutput(ASSIST_PORT, ASSIST_MASK); control_air_assist(false); control_aux1_assist(false); }
void initLCD() { SysCtlPeripheralEnable(LCDPORTENABLE); GPIOPinTypeGPIOOutput(LCDPORT, 0xff); // Please refer to the HD44780 datasheet for how these initializations work! SysCtlDelay((500e-3)*CLKSPEED/3); GPIOPinWrite(LCDPORT, RS, 0x00 ); GPIOPinWrite(LCDPORT, D4 | D5 | D6 | D7, 0x30 ); GPIOPinWrite(LCDPORT, E, 0x02);SysCtlDelay((20e-6)*CLKSPEED/3); GPIOPinWrite(LCDPORT, E, 0x00); SysCtlDelay((50e-3)*CLKSPEED/3); GPIOPinWrite(LCDPORT, D4 | D5 | D6 | D7, 0x30 ); GPIOPinWrite(LCDPORT, E, 0x02);SysCtlDelay((20e-6)*CLKSPEED/3);GPIOPinWrite(LCDPORT, E, 0x00); SysCtlDelay((50e-3)*CLKSPEED/3); GPIOPinWrite(LCDPORT, D4 | D5 | D6 | D7, 0x30 ); GPIOPinWrite(LCDPORT, E, 0x02);SysCtlDelay((20e-6)*CLKSPEED/3); GPIOPinWrite(LCDPORT, E, 0x00); SysCtlDelay((10e-3)*CLKSPEED/3); GPIOPinWrite(LCDPORT, D4 | D5 | D6 | D7, 0x20 ); GPIOPinWrite(LCDPORT, E, 0x02);SysCtlDelay((20e-6)*CLKSPEED/3); GPIOPinWrite(LCDPORT, E, 0x00); SysCtlDelay((10e-3)*CLKSPEED/3); LCDCommand(0x01); // Clear the screen. LCDCommand(0x06); // Cursor moves right. LCDCommand(0x0f); // Cursor blinking, turn on LCD. // Now change contrast, by using PWM output on GPIO pin V0 (required configuring timer) // You could also use the pwm.h functions instead? SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER3); GPIOPinConfigure(GPIO_PB2_T3CCP0); GPIOPinTypeTimer(GPIO_PORTB_BASE, GPIO_PIN_2); //GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_2); //GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_2,0); unsigned long ulPeriod = (SysCtlClockGet() / 50000)/2; unsigned long dutyCycle1 = (unsigned long)(ulPeriod-1)*0.7; TimerConfigure(TIMER3_BASE, (TIMER_CFG_SPLIT_PAIR|TIMER_CFG_A_PWM|TIMER_CFG_B_PWM)); TimerControlLevel(TIMER3_BASE, TIMER_BOTH, 0); TimerLoadSet(TIMER3_BASE, TIMER_A, ulPeriod -1); TimerMatchSet(TIMER3_BASE, TIMER_A, dutyCycle1); TimerEnable(TIMER3_BASE, TIMER_A); LCDCommand(0x0c); }
void Config_PWM(void) { SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); #ifdef PB2_T3CCP0 // Configure timer SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER3); GPIOPinConfigure(GPIO_PB2_T3CCP0); TimerConfigure(TIMER3_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_PWM); TimerLoadSet(TIMER3_BASE, TIMER_A, DEFAULT_FREQ); TimerMatchSet(TIMER3_BASE, TIMER_A, DEFAULT_FREQ); // PWM TimerControlLevel(TIMER3_BASE, TIMER_A, false); TimerEnable(TIMER3_BASE, TIMER_A); #endif SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); GPIOPinConfigure(GPIO_PB6_T0CCP0); //Right GPIOPinTypeTimer(GPIO_PORTB_BASE, GPIO_PIN_6); TimerConfigure(TIMER0_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_PWM); TimerLoadSet(TIMER0_BASE, TIMER_A, DEFAULT_FREQ); TimerMatchSet(TIMER0_BASE, TIMER_A, DEFAULT_FREQ); // PWM TimerControlLevel(TIMER0_BASE, TIMER_A, false); TimerEnable(TIMER0_BASE, TIMER_A); #ifdef PB4_T1CCP0 SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1); GPIOPinConfigure(GPIO_PB4_T1CCP0); GPIOPinTypeTimer(GPIO_PORTB_BASE, GPIO_PIN_4); TimerConfigure(TIMER1_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_PWM); TimerLoadSet(TIMER1_BASE, TIMER_A, DEFAULT_FREQ); TimerMatchSet(TIMER1_BASE, TIMER_A, DEFAULT_FREQ); // PWM TimerControlLevel(TIMER1_BASE, TIMER_A, false); TimerEnable(TIMER1_BASE, TIMER_A); #endif SetPWM(PWM_LEFT,DEFAULT_FREQ, 0); SetPWM(PWM_RIGHT,DEFAULT_FREQ, 0); }
//***************************************************************************** // //! Initializes the sound driver. //! //! \param ui32SysClock is the frequency of the system clock. //! //! This function initializes the sound driver, preparing it to output sound //! data to the speaker. //! //! The system clock should be as high as possible; lower clock rates reduces //! the quality of the produced sound. For the best quality sound, the system //! should be clocked at 120 MHz. //! //! \note In order for the sound driver to function properly, the sound driver //! interrupt handler (SoundIntHandler()) must be installed into the vector //! table for the timer 5 subtimer A interrupt. //! //! \return None. // //***************************************************************************** void SoundInit(uint32_t ui32SysClock) { // // Enable the peripherals used by the sound driver. // SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER5); // // Compute the PWM period based on the system clock. // g_sSoundState.ui32Period = ui32SysClock / 64000; // // Set the default volume. // g_sSoundState.i32Volume = 255; // // Configure the timer to run in PWM mode. // if((HWREG(TIMER5_BASE + TIMER_O_CTL) & TIMER_CTL_TBEN) == 0) { TimerConfigure(TIMER5_BASE, (TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_PWM | TIMER_CFG_B_PERIODIC)); } TimerLoadSet(TIMER5_BASE, TIMER_A, g_sSoundState.ui32Period - 1); TimerMatchSet(TIMER5_BASE, TIMER_A, g_sSoundState.ui32Period); TimerControlLevel(TIMER5_BASE, TIMER_A, true); HWREG(TIMER5_BASE + TIMER_O_TAMR) |= TIMER_TAMR_TAMRSU; // // Configure the timer to generate an interrupt at every time-out event. // TimerIntEnable(TIMER5_BASE, TIMER_CAPA_EVENT); // // Enable the timer. At this point, the timer will generate an interrupt // every 15.625 us. // TimerEnable(TIMER5_BASE, TIMER_A); IntEnable(INT_TIMER5A); // // Clear the sound flags. // g_sSoundState.ui32Flags = 0; }
//***************************************************************************** // //! Initializes the sound output. //! //! This function prepares the sound driver to play songs or sound effects. It //! must be called before any other sound functions. The sound driver uses //! timer 2 subtimer A to produce the PWM output, and timer 2 subtimer B to be //! the time base for the playback of sound effects. It is the responsibility //! of the application to ensure that SoundIntHandler() is called when the //! timer 2 subtimer B interrupt occurs (typically by placing a pointer to this //! function in the appropriate location in the processor's vector table). //! //! \return None. // //***************************************************************************** void SoundInit(void) { // // Get the current clock frequency, which is used to determine the counter // values that result in a desired output audio frequency. // g_ulSystemClock = SysCtlClockGet(); // // Enable the GPIO and timer peripherals used to produce sound. // SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC); SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER2); // // Configure the GPIO pin connected to the speaker as a GPIO output and set // it low (producing silence). // GPIOPinTypeGPIOOutput(GPIO_PORTC_BASE, GPIO_PIN_7); GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_7, 0); // // Configure the second timer as a 16-bit pair, with the first producing a // PWM output and the other in periodic mode. // TimerConfigure(TIMER2_BASE, (TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_PWM | TIMER_CFG_B_PERIODIC)); // // Invert the PWM output, so that the match value relative to zero always // defines the amount of time the PWM signal is high (regardless of the // current load value). // TimerControlLevel(TIMER2_BASE, TIMER_A, true); }
int main (void) { // Set the clock SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); // Set output SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE,GPIO_PIN_1); // Configure PF1 as T0CCP1 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,SysCtlClockGet()); TimerMatchSet(TIMER0_BASE,TIMER_A,SysCtlClockGet()/2); TimerControlLevel(TIMER0_BASE,TIMER_A,false); TimerEnable(TIMER0_BASE,TIMER_A); }
/*---------------------------------------------------------------------------*/ 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(); }