int main(void) { uint32_t LPTM_Value = 0; LPTM_InitTypeDef LPTM_InitStruct1; FTM_InitTypeDef FTM_InitStruct1; SystemClockSetup(ClockSource_EX50M,CoreClock_100M); DelayInit(); LED_Init(LED_PinLookup_CHK60EVB, kNumOfLED); UART_DebugPortInit(UART4_RX_C14_TX_C15, 115200); DisplayCPUInfo(); //在PC3上产生 1KHz 占空比50%的 方波 FTM_InitStruct1.Frequency = 1000; FTM_InitStruct1.FTMxMAP = FTM0_CH3_PC4; FTM_InitStruct1.FTM_Mode = FTM_Mode_EdgeAligned; FTM_InitStruct1.InitalDuty = 5000; FTM_Init(&FTM_InitStruct1); //开启PTC5上的脉冲计数引脚 LPTM_InitStruct1.LPTMxMap = LPTM_CH2_PC5; LPTM_InitStruct1.LPTM_InitCompareValue = 200; //在脉冲计数模式下无意义 LPTM_InitStruct1.LPTM_Mode = LPTM_Mode_PC_FALLING; //下降沿触发计数 LPTM_Init(&LPTM_InitStruct1); while(1) { //读取脉冲技术值 LPTM_Value = LPTM_GetTimerCounterValue(LPTMR0); //清空技术值 LPTM_ResetTimeCounter(LPTMR0); UART_printf("LPTMR:%dHz\r\n", LPTM_Value); DelayMs(1000); } }
/*! * @brief Main function */ void test_audio(void) { if(!yesno("Audio?")) return; const gpio_pin_config_t OUTFALSE = {kGPIO_DigitalOutput, 0}; CLOCK_EnableClock(kCLOCK_PortC); PORT_SetPinMux(PORTC, 12U, kPORT_MuxAsGpio); PORT_SetPinMux(PORTC, 13U, kPORT_MuxAsGpio); GPIO_PinInit(GPIOC, 12U, &OUTFALSE); GPIO_PinInit(GPIOC, 13U, &OUTFALSE); GPIO_WritePinOutput(GPIOC, 12, true); GPIO_WritePinOutput(GPIOC, 13, true); i2c_init(&i2c_config_default); uint8_t cmd[1] = {0b00001111}; send_cmd(cmd, sizeof(cmd)); GPIO_WritePinOutput(GPIOC, 12, false); PORT_SetPinMux(PORTC, 9U, kPORT_MuxAlt3); ftm_config_t ftmInfo; uint8_t updatedDutycycle = 100U; ftm_chnl_pwm_signal_param_t ftmParam[2]; /* Configure ftm params with frequency 24kHZ */ ftmParam[0].chnlNumber = (ftm_chnl_t) BOARD_SECOND_FTM_CHANNEL; ftmParam[0].level = kFTM_LowTrue; ftmParam[0].dutyCyclePercent = 0U; ftmParam[0].firstEdgeDelayPercent = 0U; FTM_GetDefaultConfig(&ftmInfo); FTM_Init(BOARD_FTM_BASEADDR, &ftmInfo); FTM_SetupPwm(BOARD_FTM_BASEADDR, ftmParam, 1U, kFTM_EdgeAlignedPwm, 1000000, FTM_SOURCE_CLOCK); FTM_StartTimer(BOARD_FTM_BASEADDR, kFTM_SystemClock); FTM_UpdatePwmDutycycle(BOARD_FTM_BASEADDR, (ftm_chnl_t) BOARD_SECOND_FTM_CHANNEL, kFTM_EdgeAlignedPwm, updatedDutycycle); int idx = 0; int cnt = 0; while (1) { if(cnt++ % 100 == 0) { uint8_t duty_cycle = (uint8_t) abs((sin(idx++ * 3.14159265358979f / 180) * 100.0)); if (idx > 360) idx = 0; FTM_UpdatePwmDutycycle(BOARD_FTM_BASEADDR, (ftm_chnl_t) BOARD_SECOND_FTM_CHANNEL, kFTM_EdgeAlignedPwm, (uint8_t) (duty_cycle++ % 100)); /* Software trigger to update registers */ FTM_SetSoftwareTrigger(BOARD_FTM_BASEADDR, true); } } }
void FTM_Configuration (void) { FTM_InitTypeDef FTM_InitStruct; FTM_PWM_MapTypeDef* pFTM_Map = (FTM_PWM_MapTypeDef*) FTM1; FTM_InitStruct.Frequency = (uint32_t)(4096*5); // FTM_InitStruct.FTMxMAP = FTM1_CH1_PB1; // FTM_InitStruct.FTM_Mode = FTM_Mode_EdgeAligned; // FTM_InitStruct.InitalDuty = FTM_DUTY; //0~10000 FTM_Init(&FTM_InitStruct); AD7687_CNV_PORT->PCR[AD7687_CNV_Pin]|= PORT_PCR_ODE_MASK; FTM_PWM_ChangeDuty(FTM1_CH1_PB1,0); }
void PWM_Init(uint8_t instance) { #if FSL_FEATURE_SOC_FTM_COUNT ftm_config_t config; FTM_GetDefaultConfig(&config); FTM_Init(mFtmBase[instance], &config); /* Enable TPM compatibility. Free running counter and synchronization compatible with TPM */ mFtmBase[instance]->MODE &= ~(FTM_MODE_FTMEN_MASK); FTM_StartTimer(mFtmBase[instance], kFTM_SystemClock); #else tpm_config_t config; TPM_GetDefaultConfig(&config); TPM_Init(mTpmBase[instance], &config); TPM_StartTimer(mTpmBase[instance], kTPM_SystemClock); #endif }
/************************************************************************************ ************************************************************************************* * Public functions ************************************************************************************* ************************************************************************************/ void StackTimer_Init(void (*cb)(void)) { IRQn_Type irqId; #if FSL_FEATURE_SOC_FTM_COUNT FTM_Type *ftmBaseAddr = (FTM_Type*)mFtmBase[gStackTimerInstance_c]; FTM_Init(ftmBaseAddr, &mFtmConfig); FTM_StopTimer(ftmBaseAddr); ftmBaseAddr->MOD = 0xFFFF; /* Configure channel to Software compare; output pin not used */ FTM_SetupOutputCompare(ftmBaseAddr, (ftm_chnl_t)gStackTimerChannel_c, kFTM_NoOutputSignal, 0x01); /* Install ISR */ irqId = mFtmIrqId[gStackTimerInstance_c]; FTM_EnableInterrupts(ftmBaseAddr, kFTM_TimeOverflowInterruptEnable | (1 << gStackTimerChannel_c)); #else TPM_Type *tpmBaseAddr = (TPM_Type*)mTpmBase[gStackTimerInstance_c]; TPM_Init(tpmBaseAddr, &mTpmConfig); TPM_StopTimer(tpmBaseAddr); /* Set the timer to be in free-running mode */ tpmBaseAddr->MOD = 0xFFFF; /* Configure channel to Software compare; output pin not used */ TPM_SetupOutputCompare(tpmBaseAddr, (tpm_chnl_t)gStackTimerChannel_c, kTPM_NoOutputSignal, 0x01); /* Install ISR */ irqId = mTpmIrqId[gStackTimerInstance_c]; TPM_EnableInterrupts(tpmBaseAddr, kTPM_TimeOverflowInterruptEnable | (1 << gStackTimerChannel_c)); #endif /* Overwrite old ISR */ OSA_InstallIntHandler(irqId, cb); /* set interrupt priority */ NVIC_SetPriority(irqId, gStackTimer_IsrPrio_c >> (8 - __NVIC_PRIO_BITS)); NVIC_ClearPendingIRQ(irqId); NVIC_EnableIRQ(irqId); }
int main(void) { unsigned char i; unsigned char Send_Count; uint32_t DMA_Value0 = 0; uint32_t DMA_Value1 = 0; PIT_InitTypeDef PIT_InitStruct1; GPIO_InitTypeDef GPIO_InitStruct1; FTM_InitTypeDef FTM_InitStruct1; DMACNT_InitTypeDef DMACNT_InitStruct1; SystemClockSetup(ClockSource_EX50M,CoreClock_100M); UART_DebugPortInit(UART0_RX_PA14_TX_PA15,115200); DisplayCPUInfo(); DelayInit(); OLED_Init(); DMACNT_InitStruct1.DMACNT_Mode = DMACNT_MODE_FALLING; //ÉÏÉýÑؼÆÊý DMACNT_InitStruct1.DMA_Chl = DMA_CH0; //ʹÓÃͨµÀ 0 DMACNT_InitStruct1.GPIOx = PTC; //PTC5 DMACNT_InitStruct1.GPIO_Pin = GPIO_Pin_5; DMACNT_Init(&DMACNT_InitStruct1); GPIO_InitStruct1.GPIO_Pin = GPIO_Pin_0; GPIO_InitStruct1.GPIO_InitState = Bit_RESET; GPIO_InitStruct1.GPIO_IRQMode = GPIO_IT_DISABLE; GPIO_InitStruct1.GPIO_Mode = GPIO_Mode_OPP; GPIO_InitStruct1.GPIOx = PTB; GPIO_Init(&GPIO_InitStruct1); GPIO_InitStruct1.GPIO_Pin = GPIO_Pin_1; GPIO_InitStruct1.GPIO_InitState = Bit_SET; GPIO_InitStruct1.GPIOx = PTB; GPIO_Init(&GPIO_InitStruct1); PIT_InitStruct1.PITx=PIT2; PIT_InitStruct1.PIT_Interval=100; PIT_Init(&PIT_InitStruct1); NVIC_EnableIRQ(PIT2_IRQn); PIT_ITConfig(PIT2,PIT_IT_TIF,ENABLE); PIT_InitStruct1.PITx=PIT0; PIT_InitStruct1.PIT_Interval=20000; PIT_Init(&PIT_InitStruct1); PIT_ITConfig(PIT0,PIT_IT_TIF,ENABLE); NVIC_EnableIRQ(PIT0_IRQn); FTM_InitStruct1.Frequency = 800; FTM_InitStruct1.FTM_Mode = FTM_Mode_EdgeAligned; // FTM_InitStruct1.InitalDuty = 10000; FTM_InitStruct1.FTMxMAP = FTM0_CH0_PC1; FTM_Init(&FTM_InitStruct1); UART_printf("DMACNT_CH0_Value:%d \n",*Pid_Out); while(1) { // if(key_detect1()){ // Pid_Set += 500; // } // if(key_detect3()){ // Pid_Set -= 500; // } GPIO_ResetBits(PTB,GPIO_Pin_0); PidError[2] = Pid_Set - *Pid_Out; PidOut = Kp * (PidError[2] - PidError[1]); PidOut += Ki * PidError[2] * Td; PidOut += Kd * (PidError[2] - 2 * PidError[1] + PidError[0]) / Td; PidError[0] = PidError[1]; PidError[1] = PidError[2]; PID_FinalOut =PidOut*2; // // if(key_detect5()){ // Kd+=0.2;OLED_Clear(); // } // else if(key_detect6()){ // Kd-=0.2;OLED_Clear(); // } // // OLED_Write_String(2,0,"Kp"); // OLED_Write_String(2,2,"Ki"); // OLED_Write_String(2,4,"Kd"); // OLED_Write_Num3(6,0,Kp*10); // OLED_Write_Num3(6,2,Ki*10); // OLED_Write_Num3(6,4,Kd*10); // OLED_Write_Num4(9,6,*Pid_Out-1000); // if(PID_FinalOut>10000) PID_FinalOut=10000; else if(PID_FinalOut<0) PID_FinalOut=0; FTM_PWM_ChangeDuty(FTM0_CH0_PC1,PID_FinalOut); } }