void MotorInit(uint8_t report) { SystickInit(1); // 设置PWM时钟和系统时钟一致 SysCtlPWMClockSet(SYSCTL_PWMDIV_1); // 使能PWM外设 SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0); // 使能外设端口 SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); //设置对应管脚的PWM信号功能 GPIOPinConfigure(GPIO_PB4_M0PWM2); GPIOPinConfigure(GPIO_PB5_M0PWM3); GPIOPinConfigure(GPIO_PB6_M0PWM0); GPIOPinConfigure(GPIO_PB7_M0PWM1); //设置PWM信号端口 GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7); //PWM生成器配置 PWMGenConfigure(PWM0_BASE, PWM_GEN_0, PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC); PWMGenConfigure(PWM0_BASE, PWM_GEN_1, PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC); //设置PWM信号周期 PWMGenPeriodSet(PWM0_BASE, PWM_GEN_0, PERIOD_TIME); PWMGenPeriodSet(PWM0_BASE, PWM_GEN_1, PERIOD_TIME); //设置PWM信号占空比 PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0, 0); PWMPulseWidthSet(PWM0_BASE, PWM_OUT_1, 0); PWMPulseWidthSet(PWM0_BASE, PWM_OUT_2, 0); PWMPulseWidthSet(PWM0_BASE, PWM_OUT_3, 0); // 使能PWM输出端口 PWMOutputState(PWM0_BASE, PWM_OUT_0_BIT|PWM_OUT_1_BIT|PWM_OUT_2_BIT|PWM_OUT_3_BIT, true); // 使能PWM生成器 PWMGenEnable(PWM0_BASE, PWM_GEN_0); PWMGenEnable(PWM0_BASE, PWM_GEN_1); // 使能PWm生成器模块的及时功能. PWMSyncTimeBase(PWM0_BASE, PWM_GEN_0); PWMSyncTimeBase(PWM0_BASE, PWM_GEN_1); if(report) UARTprintf("PWM初始化完成!\r\n"); }
int main(void) { // Setup // activate grader and set system clock to 80 MHz TExaS_Init(SW_PIN_PE210, LED_PIN_PB543210, ScopeOff); PortsInit(); // Port B, Port F, and Port E initialization SystickInit(); // SysTick timer initialization this_state = GO_SOUTH; // GO_SOUTH is initial state EnableInterrupts(); // enable interrupts for grader // Loop while (1) { // make outputs GPIO_PORTB_DATA_R = FSM[this_state].port_b_out; // to cars (port B) GPIO_PORTF_DATA_R = FSM[this_state].port_f_out; // to pedestrians (port F) SystickWait1mss(FSM[this_state].wait); // get inputs // if no switch is pressed if (GPIO_PORTE_DATA_R == 0x00) { switch_input = 0; // then it is case 0 of the next[] array... } // ... all LEDs stay the way they are since the last pressing // if south switch is pressed else if (GPIO_PORTE_DATA_R == 0x02) { switch_input = 1; // then it is case 1 of the next[] array... } // ... all LEDs correspond to Go South mode // if west switch is pressed else if (GPIO_PORTE_DATA_R == 0x01) { switch_input = 2; // then it is case 2 of the next[] array... } // ... all LEDs correspond to Go West mode // if pedestrian switch is pressed else if (GPIO_PORTE_DATA_R == 0x04) { switch_input = 3; // then it is case 3 of the next[] array... } // ... all LEDs correspond to Go Pedestrian mode // if all switches are pressed else if (GPIO_PORTE_DATA_R == 0x07) { switch_input = 4; // then it is case 4 of the next[] array... } // ... all LEDs correspond periodically: South, West, Pedestrian // change state based on input and current state this_state = FSM[this_state].next[switch_input]; } }
void InitializeSystem() { #ifdef BOARD_UBW32 // Disable ADC port (allows PORTB to be used for digital I/O) AD1PCFG = 0xFFFF; TRISE = 0x0000; TRISB = 0x0000; TRISC = 0x0000; TRISD = 0x0000; LATE = 0x0000; LATB = 0x0000; LATC = 0x0000; LATD = 0x0000; #endif #ifdef BOARD_HEXLIGHT ANSELA = 0x0000; ANSELB = 0x0000; #endif LATA = 0x0000; LATB = 0x0000; // Ensure LED drivers are driven low as soon as possible // _TRIS(PIO_OC1) = 0; // _TRIS(PIO_OC2) = 0; // _TRIS(PIO_OC3) = 0; // _TRIS(PIO_OC4) = 0; // _LAT(PIO_OC1) = OUTPUT; // _LAT(PIO_OC2) = OUTPUT; // _LAT(PIO_OC3) = OUTPUT; // _LAT(PIO_OC4) = OUTPUT; // Force disconnect of USB bootloader U1CON = 0x00000000; U1PWRC = 0x00000000; // LEDs // _TRIS(PIO_LED1) = OUTPUT; // _TRIS(PIO_LED2) = OUTPUT; #ifdef BOARD_UBW32 _TRIS(PIO_LED3) = OUTPUT; _TRIS(PIO_LED_USB) = OUTPUT; _TRIS(PIO_BTN_PGM) = 1; _TRIS(PIO_BTN_USR) = 1; #elif BOARD_HEXLIGHT _TRIS(PIO_BTN1) = INPUT; _TRIS(PIO_BTN2) = INPUT; #endif // _TRIS(PIO_USBP) = INPUT; // _TRIS(PIO_USBN) = INPUT; // _LAT(PIO_LED1) = LOW; // _LAT(PIO_LED2) = LOW; #ifdef BOARD_UBW32 _LAT(PIO_LED3) = HIGH; _LAT(PIO_LED_USB) = LOW; #endif mJTAGPortEnable(0); // Initializethe PIC32 core //OSCConfig(OSC_POSC_PLL, OSC_PLL_MULT_20, OSC_PLL_POST_2, OSC_FRC_POST_2); sys_clock = F_SYSCLK; mOSCSetPBDIV(OSC_PB_DIV_1); pb_clock = SYSTEMConfig(sys_clock, SYS_CFG_ALL); INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR); INTEnableInterrupts(); // Initialize core time base SystickInit(); }