首页 > 其他 > 详细

STM32 例程-3 Proteus下单按键试验

时间:2021-02-14 09:41:55      阅读:38      评论:0      收藏:0      [点我收藏+]

在例程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);
	}
}

  代码如上,增加红色部分,运行效果如下,达到目标预期

技术分享图片

 

STM32 例程-3 Proteus下单按键试验

原文:https://www.cnblogs.com/zhaogaojian/p/14401230.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!