void purpinsMotors::configurePWM(){ //Configure PWM Clock MAP_SysCtlPWMClockSet(SYSCTL_PWMDIV_2); MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC); MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1); MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0); pwmPeriod = MAP_SysCtlClockGet() / 2 / PWM_FREQUENCY; //PWM frequency MAP_GPIOPinConfigure(GPIO_PF1_M1PWM5); MAP_GPIOPinConfigure(GPIO_PF2_M1PWM6); MAP_GPIOPinConfigure(GPIO_PF3_M1PWM7); MAP_GPIOPinConfigure(GPIO_PC4_M0PWM6); MAP_GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3); MAP_GPIOPinTypePWM(GPIO_PORTC_BASE, GPIO_PIN_4); //gen 3 for m0pwm6 MAP_PWMGenConfigure(PWM0_BASE, PWM_GEN_3, PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC); //gen 3 for m1pwm6 and m1pwm7 MAP_PWMGenConfigure(PWM1_BASE, PWM_GEN_3, PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC); //gen 2 for m1pwm5 MAP_PWMGenConfigure(PWM1_BASE, PWM_GEN_2, PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC); //Set the Period (expressed in clock ticks) MAP_PWMGenPeriodSet(PWM0_BASE, PWM_GEN_3, pwmPeriod); MAP_PWMGenPeriodSet(PWM1_BASE, PWM_GEN_2, pwmPeriod); MAP_PWMGenPeriodSet(PWM1_BASE, PWM_GEN_3, pwmPeriod); //Set PWM duty-0% MAP_PWMPulseWidthSet(PWM1_BASE, PWM_OUT_5 , 0); MAP_PWMPulseWidthSet(PWM1_BASE, PWM_OUT_6 , 0); MAP_PWMPulseWidthSet(PWM1_BASE, PWM_OUT_7 , 0); MAP_PWMPulseWidthSet(PWM0_BASE, PWM_OUT_6 , 0); // Enable the PWM generators MAP_PWMGenEnable(PWM1_BASE, PWM_GEN_2); MAP_PWMGenEnable(PWM1_BASE, PWM_GEN_3); MAP_PWMGenEnable(PWM0_BASE, PWM_GEN_3); // Turn on the Output pins MAP_PWMOutputState(PWM1_BASE, PWM_OUT_5_BIT, true); MAP_PWMOutputState(PWM1_BASE, PWM_OUT_6_BIT, true); MAP_PWMOutputState(PWM1_BASE, PWM_OUT_7_BIT, true); MAP_PWMOutputState(PWM0_BASE, PWM_OUT_6_BIT, true); }
void rgb_init(void) { /* Set PWM clock to system clock */ MAP_SysCtlPWMClockSet(SYSCTL_PWMDIV_1); /* Make sure thet all required peripherals are enabled */ if (!MAP_SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOF)) MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); if (!MAP_SysCtlPeripheralReady(SYSCTL_PERIPH_PWM1)) MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1); /* Configure pins to PWM output function */ MAP_GPIOPinConfigure(GPIO_PF1_M1PWM5); MAP_GPIOPinConfigure(GPIO_PF2_M1PWM6); MAP_GPIOPinConfigure(GPIO_PF3_M1PWM7); MAP_GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_1); MAP_GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_2); MAP_GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_3); /* Configure PWM module */ MAP_PWMGenConfigure(PWM1_BASE, PWM_GEN_2, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_GEN_SYNC_LOCAL); MAP_PWMGenConfigure(PWM1_BASE, PWM_GEN_3, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_GEN_SYNC_LOCAL); MAP_PWMGenPeriodSet(PWM1_BASE, PWM_GEN_2, RGB_PWM_PERIOD); MAP_PWMGenPeriodSet(PWM1_BASE, PWM_GEN_3, RGB_PWM_PERIOD); MAP_PWMPulseWidthSet(PWM1_BASE, PWM_OUT_5, 0); MAP_PWMPulseWidthSet(PWM1_BASE, PWM_OUT_6, 0); MAP_PWMPulseWidthSet(PWM1_BASE, PWM_OUT_7, 0); //PWMOutputState(PWM1_BASE, PWM_OUT_5_BIT|PWM_OUT_6_BIT|PWM_OUT_7_BIT, true); MAP_PWMGenEnable(PWM1_BASE, PWM_GEN_2); MAP_PWMGenEnable(PWM1_BASE, PWM_GEN_3); MAP_PWMSyncTimeBase(PWM1_BASE, PWM_GEN_2_BIT|PWM_GEN_3_BIT); g_rgb_data.c = 0; g_rgb_data.i = 0; }
//PWM = PF1/M1PWM5 & PC4/M0PWM6 void configurePWM(void) { //Configure PWM Clock to match system MAP_SysCtlPWMClockSet(SYSCTL_PWMDIV_2); MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1); //PD6 & PD7 for direction MAP_GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE,GPIO_INT_PIN_6 |GPIO_INT_PIN_7); MAP_GPIOPinWrite(GPIO_PORTD_BASE,GPIO_INT_PIN_6 |GPIO_INT_PIN_7,0); ulPeriod = MAP_SysCtlClockGet() / 2 / PWM_FREQUENCY; //PWM frequency MAP_GPIOPinConfigure(GPIO_PF1_M1PWM5); MAP_GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_1); MAP_PWMGenConfigure(PWM1_BASE, PWM_GEN_2, PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC); //Set the Period (expressed in clock ticks) MAP_PWMGenPeriodSet(PWM1_BASE, PWM_GEN_2, ulPeriod); //Set PWM duty-0% MAP_PWMPulseWidthSet(PWM1_BASE, PWM_OUT_5 , 0); // Enable the PWM generator MAP_PWMGenEnable(PWM1_BASE, PWM_GEN_2); // Turn on the Output pins MAP_PWMOutputState(PWM1_BASE, PWM_OUT_5_BIT, true); }
int main (int argc, char **argv) { GPIO_InitTypeDef GPIO_InitStruct; board_init(); // Turn off WDT, init MCU clocks, ... board_systick_timer_config(); // configure 1 ms timer LED1_INIT(); // Setup LED1 for output // LED2_INIT(); // Setup LED2 for output not on STM32 #if (UART_CHECKOUT) while (uart_test1) // TEMP DEBUG of Baud Rate CONSOLE_WRITE ("\n\rInitialization complete. Type in a config parm:\n\r"); while ( ! CONSOLE_CHECK_FOR_INPUT() ) ; // loop till get some data CONSOLE_READ_STRING ((char*) uart_cmd_buf, 12); CONSOLE_WRITE ("Type in a single char: "); ret_code = 0; while (ret_code == 0) // wait till we get a char in ret_code = CONSOLE_GET_CHAR(); CONSOLE_WRITE_CHAR (ret_code); // echo it #endif //------------------------------------------------------------- // Setup GPIOs to operate in Timer/PWM mode // // Start with PA8 (Arduino D7) = TIM1_CH1 //------------------------------------------------------------- /* TIM1 module - Channel 1 output */ GPIO_InitStruct.Pin = GPIO_PIN_8; // PA8 = Arduino D7 GPIO_InitStruct.Alternate = GPIO_AF1_TIM1; // Alt Func = Timer/PWM GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; HAL_GPIO_Init (GPIOA, &GPIO_InitStruct); /* TIM1 module - Channel 1 complementary output */ GPIO_InitStruct.Pin = GPIO_PIN_13; // PB13 - not on Arduino - Morpho CN10 - 30 HAL_GPIO_Init (GPIOB, &GPIO_InitStruct); //------------------------------------ // Configure PWM module to be used //------------------------------------ // SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f4xx.c file. uwPeriod = (SystemCoreClock / 20000 ) - 1; // set period = 20 K Hz ret_code = board_pwm_init_module (PWM_MODULE_1, PWM_COUNT_DOWN, uwPeriod, 0); //------------------------------------ // Configure PWM channel(s) to be used //------------------------------------ ret_code = board_pwm_config_channel (PWM_MODULE_1, PWM_CHANNEL_1, (uwPeriod/4), 0); // 25 % duty cycle //--------------------------------------------------- // Enable the PWM module and its associated channels //--------------------------------------------------- ret_code = board_pwm_enable (PWM_MODULE_1, 0); while (1) { // initial checkout - see if PWM is working LED1_TOGGLE(); // show activity board_delay_ms (1000); } // ----- INITIAL CHECKOUT ----- #if (LATER) //------------------------------------------------------------- // Ensure DRV8848 is OFF during config. // setup nSLEEP PB_2 pin (J2-2) to LOW to turn off the DRV8848 //------------------------------------------------------------- pin_Config (Pin19, GPIO_OUTPUT); pin_Low (Pin19); //----------------------------------------------------------------- // Setup PWMs to drive the DRV8711 Stepper //----------------------------------------------------------------- // first, get the period for a 20 K Hz PWM frequency pwm_period = board_frequency_to_period_ticks (PWM_20K_FREQUENCY); // then initialize the needed PWMs, passing in startup period //------------------------------------------------------------- // do basic setup for PWM and Interval Timer (turn on clocks) // Skipping the /32 gives better accuracy (20.004 KHz vs 20.81 K Hz using /32) //------------------------------------------------------------- // MAP_SysCtlPWMClockSet (SYSCTL_PWMDIV_32); // do not do / 32 of Master clock and see results MAP_SysCtlPWMClockSet (SYSCTL_PWMDIV_1); // Setup Master PWM I/O clock MAP_SysCtlPeripheralEnable (SYSCTL_PERIPH_PWM1); // turn on PWM module 1 clock // mg_PWMclock = MAP_SysCtlClockGet() / 32; // save Max PWM Clock value mg_PWMclock = MAP_SysCtlClockGet(); // save Max PWM Clock value mg_Sysclock = MAP_SysCtlClockGet(); // This says 80 MHz nmap_SysClkTicks = MAP_SysCtlClockGet(); // This says 80 MHz after switching over to MAP_ version. // Before that, non MAP_ version was saying 66 MHz, which is whacked out //------------------------------------------------------------------------------ // Setup PWM pins that are used by DRV8848 // // PWM A / AIN1 = PA_4 TA1.2 J2-8 PWM B / BIN1 = PA_6 TA1.1 J1-9 // DIR A / AIN2 = PA_3 TA1CCR2 J2-9 DIR B / BIN2 = PA_7 TA1CCR1 J1-10 // nSLEEP = PB_2 J2-2 nFAULT = PE_0 J2-3 // VREF = PE_4 Adc9 J1-5 //------------------------------------------------------------------------------ // setup DRV8701 STEP_AIN1 PA_6 pin (J1-9) for PWM mode // setup DRV8848 BIN1 PA_6 pin (J1-9) for PWM mode MAP_GPIOPinTypePWM (GPIO_PORTA_BASE, GPIO_PIN_6); // setup BIN1 PWM (J1-9) MAP_GPIOPinConfigure (GPIO_PA6_M1PWM2); //------------------------------------------------ // Setup PWM and associated Count mode. // Tiva only supports DOWN counts, not UP counts //------------------------------------------------ MAP_PWMGenConfigure (PWM1_BASE, PWM_GEN_1, PWM_GEN_MODE_DOWN); // setup default PWM Period and duty cycle mg_PWMperiod = (mg_PWMclock / PWM_20K_FREQUENCY) - 1; // default period /32 = 124 (125 - 1) // default period /1 = 3999 (4000 - 1) MAP_PWMGenPeriodSet (PWM1_BASE, PWM_GEN_1, mg_PWMperiod); MAP_PWMPulseWidthSet (PWM1_BASE, PWM_OUT_2, mg_PWMperiod / 4); // duty of 25 % pwm_duty_value = mg_PWMperiod / 4; // save it for ADC checks //----------------------- // Start up the PWM //----------------------- MAP_PWMGenEnable (PWM1_BASE, PWM_GEN_1); // start up the PWM ??? need ??? MAP_PWMOutputState (PWM1_BASE, PWM_OUT_2_BIT, true); // Enable PWM pin output //----------------------------------------- // setup DIR BIN2 PA_7 pin (J1-10) to LOW //----------------------------------------- pin_Config (Pin10, GPIO_OUTPUT); // setup BIN2 DIR (J1-10) pin_Low (Pin10); //----------------------------------------- // Turn off AIN1 PWM and AIN2 DIR pins PA_4 / PA_3 pins (J2-8/J2-9) to LOW //----------------------------------------- pin_Config (Pin13, GPIO_OUTPUT); // setup AIN1 PWM (J2-8) pin_Low (Pin13); pin_Config (Pin12, GPIO_OUTPUT); // setup AIN2 DIR (J2-9) pin_Low (Pin12); //------------------------------------------------------------- // set nSLEEP PB_2 pin (J2-2) to HIGH to turn on the DRV8848 //------------------------------------------------------------- pin_High (Pin19); while (1) { // LED1_TOGGLE(); // Setup LED1 for output if (adc_Check_All_Complete()) { // we have a completed ADC reading. Pull in the latest reading adc_Read (&adc_value); if ( (adc_value > (adc_cur_value +10)) || (adc_value < (adc_cur_value - 1)) ) { // we have a major change in valu. Update the PWM adc_cur_value = adc_value; // scale the PWM Duty_Cycle (o - 4000) value to match the ADC value (0-4096) if (adc_cur_value > 3999) pwm_duty_value = 3999; else pwm_duty_value = adc_value; // set the new PWM duty ccyle MAP_PWMPulseWidthSet (PWM1_BASE, PWM_OUT_2, pwm_duty_value); // start a new ADC sampling cycle adc_Trigger_Start(); } } board_delay_ms (250); // wait 250 ms (1/4 sec) between samples } #endif }
//***************************************************************************** // // This example demonstrates how to send a string of data to the UART. // //***************************************************************************** int main(void) { uint32_t ui32_Period, ui32_PWM=0, ui32_PWM_step = 40, ui32_PWM_min=2000, ui32_PWM_max=4000; uint32_t ui32_sw1; uint32_t ui32_sw2; // // 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. // //MAP_FPUEnable(); //MAP_FPULazyStackingEnable(); // // Set the clocking to run directly from the crystal. // MAP_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); // // Enable the GPIO port that is used for the on-board LED. // MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); // // Enable the GPIO pins for the LED (PF2). // MAP_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_2); // Configure UART InitConsole(); // ----------------------------------------------------------------- // Configure PWM for servo control // ----------------------------------------------------------------- // follow page 1240 of the datasheet: ui32_Period = (MAP_SysCtlClockGet()/8) / 50; //PWM frequency 50HZ, T=20ms // GOTO middle of servo in the beginning ui32_PWM = ui32_PWM_min + (ui32_PWM_max - ui32_PWM_min) / 2; ui32_PWM_max = ui32_Period*.1; ui32_PWM_min = ui32_Period*.05; //Configure PWM Clock to match system MAP_SysCtlPWMClockSet(SYSCTL_PWMDIV_8); // Enable system clock for PWM0 module MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0); // Enable the GPIO port that is used for the PWM outputs MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); //Configure PB5,PB5 Pins as PWM MAP_GPIOPinConfigure(GPIO_PB4_M0PWM2); MAP_GPIOPinConfigure(GPIO_PB5_M0PWM3); MAP_GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_4); MAP_GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_5); //Configure PWM Options MAP_PWMGenConfigure(PWM0_BASE, PWM_GEN_1 , PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC); // // Set the PWM period to 50Hz. To calculate the appropriate parameter // use the following equation: N = (1 / f) * SysClk. Where N is the // function parameter, f is the desired frequency, and SysClk is the // system clock frequency. // In this case you get: (1 / 50Hz) * 16MHz/8div = 40000 cycles. Note that // the maximum period you can set is 2^16 - 1. // TODO: modify this calculation to use the clock frequency that you are // using. // PWMGenPeriodSet(PWM0_BASE, PWM_GEN_1, ui32_Period); //Set PWM duty - 25% and 75% PWMPulseWidthSet(PWM0_BASE, PWM_OUT_2, ui32_PWM); PWMPulseWidthSet(PWM0_BASE, PWM_OUT_3, ui32_PWM); // Enable the PWM generator MAP_PWMGenEnable(PWM0_BASE, PWM_GEN_1); // Turn on the Output pins MAP_PWMOutputState(PWM0_BASE, PWM_OUT_2_BIT | PWM_OUT_3_BIT, true); // ----------------------------------------------------------------- // Enable PORTF and Configure SW1 and SW2 as input // ----------------------------------------------------------------- MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); GPIO_PORTF_LOCK_R = 0x4C4F434B; // unlock GPIO Port F GPIO_PORTF_CR_R = 0x1F; // allow changes to PF4-0 MAP_GPIOPadConfigSet(GPIO_PORTF_BASE, (GPIO_PIN_4 | GPIO_PIN_0), GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU); MAP_GPIODirModeSet(GPIO_PORTF_BASE, (GPIO_PIN_4 | GPIO_PIN_0), GPIO_DIR_MODE_IN); // // Loop forever echoing data through the UART. // while(1) { // Get buttons: ui32_sw1 = MAP_GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_4); ui32_sw2 = MAP_GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_0); // if pressing SW1 on LaunchPad TM4C123G if( ui32_sw1 == 0 ) { if(ui32_PWM >= ui32_PWM_min+ui32_PWM_step) { ui32_PWM -= ui32_PWM_step; } } if( ui32_sw2 == 0 ) { if(ui32_PWM <= ui32_PWM_max-ui32_PWM_step) { ui32_PWM += ui32_PWM_step; } } UARTprintf("Period: %d, PWM: %d\n", ui32_Period, ui32_PWM); MAP_SysCtlDelay(2000000); MAP_PWMPulseWidthSet(PWM0_BASE, PWM_OUT_2 , ui32_Period - ui32_PWM); MAP_PWMPulseWidthSet(PWM0_BASE, PWM_OUT_3 , ui32_PWM); } }