在例程2的基础上增加端口9,7配置
#include "stm32f10x.h" __IO uint32_t TimingDelay; u16 i=0; void LED_GPIO_Config(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOC,&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU ; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOC,&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOC,&GPIO_InitStructure); } void NVIC_Configuare(void) { NVIC_InitTypeDef NVIC_InitStructure; NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0; NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x0F; NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; NVIC_Init(&NVIC_InitStructure); } void TIM3_Configuare(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_TimeBaseStructure.TIM_Period = 10-1; TIM_TimeBaseStructure.TIM_Prescaler =(200-1); TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); TIM_ITConfig(TIM3,TIM_IT_Update|TIM_IT_Trigger,ENABLE); TIM_Cmd(TIM3, ENABLE); } void delay_nms(__IO uint32_t nTime) { TimingDelay=nTime; while(TimingDelay!=0); } void Init_SysTick(void){ if(SysTick_Config(SystemCoreClock/1000)) while(1); } int main(void) { u8 kent=0; SystemInit(); RCC_ClockSecuritySystemCmd(ENABLE); LED_GPIO_Config(); NVIC_Configuare(); TIM3_Configuare(); Init_SysTick(); while(1) { if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_9)==0) { delay_nms(10); if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_9)==0) kent++; if(kent%2) GPIO_SetBits(GPIOC,GPIO_Pin_7); else GPIO_ResetBits(GPIOC,GPIO_Pin_7); while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_9)==0); } //GPIO_SetBits(GPIOC,GPIO_Pin_8); //delay_nms(1000); //GPIO_ResetBits(GPIOC,GPIO_Pin_8); //delay_nms(1000); } }
代码如上,增加红色部分,运行效果如下,达到目标预期