使用CH579的TMR例程,將PB15設置為下拉輸入后,即使外部引腳懸空也讀出1,PB14 13 12 也是如此。而其他引腳卻一切正常。#include?"CH57x_common.h"
__align(4)?UINT32?CapBuf[100]; __align(4)?UINT32?PwmBuf[100]; volatile?UINT8?capFlag?=?0; void?DebugInit(void) { ????GPIOA_SetBits(GPIO_Pin_9); ????GPIOA_ModeCfg(GPIO_Pin_8,?GPIO_ModeIN_PU); ????GPIOA_ModeCfg(GPIO_Pin_9,?GPIO_ModeOut_PP_5mA); ????UART1_DefInit(); } int?main() {????? ????UINT8??i; UINT32?x?=?0; ?? /*?配置串口調試?*/??? ????DebugInit(); ????PRINT(?"Start?@ChipID=%02X\n",?R8_CHIP_ID?); ???? ????//設置GPIO為下拉,輸出結果 ????GPIOB_ModeCfg(?GPIO_Pin_15,?GPIO_ModeIN_PD?); ????PRINT("read?res?=?%d\r\n",?GPIOB_ReadPortPin(?GPIO_Pin_15?)); ???? #if?0???????/*?定時器0,設定100ms定時器進行IO口閃燈,?PB3-LED?*/ ???? ????GPIOB_SetBits(?GPIO_Pin_3?); ????GPIOB_ModeCfg(?GPIO_Pin_3,?GPIO_ModeOut_PP_5mA?); ???? ????TMR0_TimerInit(?FREQ_SYS/10?);??????????????????//?設置定時時間?100ms ????TMR0_ITCfg(ENABLE,?TMR0_3_IT_CYC_END);??????????//?開啟中斷 ????NVIC_EnableIRQ(?TMR0_IRQn?); #endif? #if?1???????/*?定時器3,PWM輸出?*/ ???? ????GPIOA_ResetBits(?GPIO_Pin_2?);????????????//?配置PWM口?PA2 ????GPIOA_ModeCfg(?GPIO_Pin_2,?GPIO_ModeOut_PP_5mA?); ???? ????TMR3_PWMInit(?High_Level,?PWM_Times_1?); ????TMR3_PWMCycleCfg(?3200?);????????//?周期?100us ????TMR3_Disable(); ????TMR3_PWMActDataWidth(?1000?);??????????????//?占空比設置?,?修改占空比必須暫時關閉定時器 ????TMR3_Enable(); ???? #endif??? #if?1???????/*?定時器1,CAP捕捉,?*/ ????PWR_UnitModCfg(?DISABLE,?UNIT_SYS_LSE?);?????//?注意此引腳是LSE晶振引腳,要保證關閉才能使用其他功能 ????GPIOA_ResetBits(?GPIO_Pin_10?);?????????????//?配置PWM口?PA10 ????GPIOA_ModeCfg(?GPIO_Pin_10,?GPIO_ModeIN_PU?); ????TMR1_CapInit(?Edge_To_Edge?); ????TMR1_CAPTimeoutCfg(?0xFFFFFFFF?);???//?設置捕捉超時時間 ????TMR1_DMACfg(?ENABLE,?(UINT16)(UINT32)&CapBuf[0],?(UINT16)(UINT32)&CapBuf[100],?Mode_Single?); ????TMR1_ClearITFlag(?TMR1_2_IT_DMA_END?);??????//?清除中斷標志?? ????TMR1_ITCfg(ENABLE,?TMR1_2_IT_DMA_END);??????????//?開啟DMA完成中斷 ????NVIC_EnableIRQ(?TMR1_IRQn?); ???? ????while(?capFlag?==?0?); ????capFlag?=?0; ????for(?i=0;?i<100;?i++?) ????{ ????????printf("%08ld?",?CapBuf[i]&0x1ffffff);??????//?bit26?最高位表示?高電平還是低電平 ????}printf("\n"); #endif #if?1???????/*?定時器2,計數器?*/ GPIOB_ModeCfg(?GPIO_Pin_11,?GPIO_ModeIN_PD?); GPIOPinRemap(?ENABLE,?RB_PIN_TMR2?); TMR2_CountInit(?FallEdge_To_FallEdge?); TMR2_CountOverflowCfg(?1000?);??????????????????//?設置計數上限1000 /*?開啟計數溢出中斷,計慢1000個周期進入中斷?*/??? TMR2_ClearITFlag(?TMR0_3_IT_CYC_END?); NVIC_EnableIRQ(TMR2_IRQn); TMR2_ITCfg(?ENABLE,?TMR0_3_IT_CYC_END); do { /*?約0.5s打印一次當前計數值,如果送入脈沖頻率較高,可能很快計數溢出,需要按實際情況修改?*/ x++; if(?!(?x?&?0xfffff?)?) printf("=%ld?",?TMR2_GetCurrentCount()); }while(1);? #endif #if?1?/*?定時器2,DMA?PWM.*/ ????GPIOB_ModeCfg(GPIO_Pin_11,?GPIO_ModeOut_PP_5mA); ????GPIOPinRemap(ENABLE,?RB_PIN_TMR2); ????PRINT("TMR2?DMA?PWM\n"); ????TMR2_PWMCycleCfg(120000);?//?周期?2000us ????for(i=0;?i<50;?i++) ????{ ??????PwmBuf[i]=2400*i; ????} ????for(i=50;?i<100;?i++) ????{ ??????PwmBuf[i]=2400*(100-i); ????} ????TMR2_PWMInit(Low_Level,?PWM_Times_16); ????/*?Note:?DMA須在PWM初始化后進行配置?*/ ????TMR2_DMACfg(ENABLE,?(uint16_t)(uint32_t)&PwmBuf[0],?(uint16_t)(uint32_t)&PwmBuf[100],?Mode_LOOP); ????/*?開啟計數溢出中斷,計慢1000個周期進入中斷?*/ ????TMR2_ClearITFlag(TMR1_2_IT_DMA_END); ????NVIC_EnableIRQ(TMR2_IRQn); ????TMR2_ITCfg(ENABLE,?TMR1_2_IT_DMA_END); #endif ????while(1);???? } void?TMR0_IRQHandler(?void?)????????//?TMR0?定時中斷 { ????if(?TMR0_GetITFlag(?TMR0_3_IT_CYC_END?)?) ????{ ????????TMR0_ClearITFlag(?TMR0_3_IT_CYC_END?);??????//?清除中斷標志 ????????GPIOB_InverseBits(?GPIO_Pin_3?);???????? ????} } void?TMR1_IRQHandler(?void?)????????//?TMR1?定時中斷 { ????if(?TMR1_GetITFlag(?TMR1_2_IT_DMA_END?)?) ????{ ????????TMR1_ITCfg(DISABLE,?TMR1_2_IT_DMA_END);???????//?使用單次DMA功能+中斷,注意完成后關閉此中斷使能,否則會一直上報中斷。 ????????TMR1_ClearITFlag(?TMR1_2_IT_DMA_END?);??????//?清除中斷標志?? ????????capFlag?=?1; ????????printf("*"); ????} } void?TMR2_IRQHandler(void) { if(?TMR2_GetITFlag(TMR0_3_IT_CYC_END)?)? { ?TMR2_ClearITFlag(?TMR0_3_IT_CYC_END?); ?/*?計數器計滿,硬件自動清零,重新開始計數?*/ ?/*?用戶可自行添加需要的處理?*/ } if(TMR2_GetITFlag(TMR1_2_IT_DMA_END)) { TMR2_ClearITFlag(TMR1_2_IT_DMA_END); PRINT("DMA?end\n"); /*?DMA?結束?*/ /*?用戶可自行添加需要的處理?*/ } }
熱門產品 :
CH397: 高集成度、低功耗USB網卡芯片