stm32菜鸟自学代码

/*//delay
#include "delay.h"
void Delay(__IO u32 nCount)  
{
for(; nCount != 0; nCount--);
}*/


/*//led
void LED_GPIO_Config(void)
{
     GPIO_InitTypeDef GPIO_InitStructure;
     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;   
     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
     GPIO_Init(GPIOA, &GPIO_InitStructure);
     GPIO_SetBits(GPIOA,GPIO_Pin_8);   
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);   
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;      
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_SetBits(GPIOD,GPIO_Pin_2);  
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);   
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;      
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_SetBits(GPIOC,GPIO_Pin_12);
}
*/


/*
//外部中断
static void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure); 

  NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);    
}
void EXTI_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  EXTI_InitTypeDef EXTI_InitStructure;
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);
  GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_13|GPIO_Pin_15;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_0;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; 
  GPIO_Init(GPIOA, &GPIO_InitStructure);


  GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource13);
    
  EXTI_InitStructure.EXTI_Line=EXTI_Line13;
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;


  EXTI_Init(&EXTI_InitStructure);  


  GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource15);
    
  EXTI_InitStructure.EXTI_Line=EXTI_Line15;
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;


  EXTI_Init(&EXTI_InitStructure);  


  GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);


  EXTI_InitStructure.EXTI_Line=EXTI_Line0;
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;

  EXTI_Init(&EXTI_InitStructure);
    
    NVIC_Configuration();

}


//中断
void EXTI0_IRQHandler(void)
{     
    Delay(10000);
  if(EXTI_GetITStatus(EXTI_Line0) != RESET)  
{  
GPIO_WriteBit(GPIOC, GPIO_Pin_12, (BitAction)(1-(GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_12))));
}
EXTI_ClearITPendingBit(EXTI_Line0);  
}
 
void EXTI15_10_IRQHandler(void)
{   
Delay(10000);  
    if(EXTI_GetITStatus(EXTI_Line13) != RESET)
{   
  GPIO_WriteBit(GPIOA, GPIO_Pin_8, (BitAction)(1-(GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_8))));
}
     else if (EXTI_GetITStatus(EXTI_Line15) != RESET)
{
GPIO_WriteBit(GPIOD, GPIO_Pin_2, (BitAction)(1-(GPIO_ReadOutputDataBit(GPIOD, GPIO_Pin_2))));
}
EXTI_ClearITPendingBit(EXTI_Line13); 
EXTI_ClearITPendingBit(EXTI_Line15);  
}
*/


/*
//定时器
void TIME_NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; 
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; 
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; 
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
NVIC_Init(&NVIC_InitStructure);  
}




void TIME_Configuration(void)
{
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); 
TIM_TimeBaseStructure.TIM_Period = 5000; 
TIM_TimeBaseStructure.TIM_Prescaler =(7200-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 TIM3_IRQHandler(void)   
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) 
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update  ); 
    GPIO_WriteBit(GPIOD, GPIO_Pin_2, (BitAction)(1-(GPIO_ReadOutputDataBit(GPIOD, GPIO_Pin_2))));
}
}
*/


/*
//串口中断
void USART1_Config(unsigned int bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);

USART_InitStructure.USART_BaudRate = bound;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No ;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

USART_Cmd(USART1, ENABLE); 
}


void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure; 
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;  
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}


int fputc(int ch, FILE *f)
{
USART_SendData(USART1, (unsigned char) ch);
while( USART_GetFlagStatus(USART1,USART_FLAG_TC)!= SET);
return (ch);
}


//中断
void USART1_IRQHandler(void)
{
unsigned char code;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
   code=USART_ReceiveData(USART1);
  printf("%c",code);    

 
}
*/


/*
#include "stm32f10x.h"
#include "Key.h"
#include "delay.h"
void Key_GPIO_Config(void)

  GPIO_InitTypeDef GPIO_InitStructure;


  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);


GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_13|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  GPIO_Init(GPIOA, &GPIO_InitStructure);


GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; 
GPIO_Init(GPIOA, &GPIO_InitStructure);  


}
unsigned char KEY_Scan(void)
{  
unsigned char key_code;
  if(KEY1_Scan()==1)key_code=1;
else if(KEY0_Scan()==1)key_code=2;
else if(KEY2_Scan()==1)key_code=3;
else key_code=0;
return key_code;
}
unsigned char KEY1_Scan(void)
{  
static char key_up1=0;
  if(KEY1==0)
{
 Delay(10000);
if(KEY1==0)
{
key_up1=1;
    }
}
if(KEY1==1&&key_up1==1)
{
key_up1=0;
    return 1;
    }
return 0;
}
unsigned char KEY0_Scan(void)
{  
static char key_up0=0;


  if(KEY0==0)
{
Delay(10000);
if(KEY0==0)
{
key_up0=1;
    }

}
if(KEY0==1&&key_up0==1)
{
key_up0=0;
    return 1;
    }
return 0;
}
unsigned char KEY2_Scan(void)
{  
static char key_up2=0;


  if(KEY2==1)
{
Delay(10000);

if(KEY2==1)
{
key_up2=1;
    }
}
if(KEY2==0&&key_up2==1)
{
key_up2=0;
    return 1;
    }
return 0;
}
*/
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值