【嵌入式/学习笔记】第5章 GPIO按键实验

实验1:绿色LED/按键K1开关

实验2:绿蓝白LED/按键K1开关/按键K2切换色彩

一、实验1(文件夹template -replica)

1.文件夹key,文件夹led,新建文件.c和.h,魔术棒设置路径

2.文件bsp_led.c

开头

#include "bsp_led.h"

函数

void LED_Init(void){...}

3.文件bsp_led.h

#include "stm32f10x.h"

void KEY_Init(void);

4.文件bsp_key.c

#include "bsp_key.h"

void KEY_Init(void){代码内容,很多,此处略}

5.文件bsp_key.h

#include "stm32f10x.h"

void KEY_Init(void);

6.文件main.c

#include "stm32f10x.h"
#include "bsp_led.h"
#include "bsp_key.h" 
void Delay( uint32_t count )
{ for(; count!=0; count--);}

uint8_t   Key_Scan(GPIO_TypeDef* GPIOx,   uint16_t GPIO_Pin)
{			
     if(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin) == 1 ) 
     {      /*????????*/
          while(GPIO_ReadInputDataBit(GPIOx,  GPIO_Pin) == 1) ;   
          return 1;	 
      }
      else 
      {
          return 0;
      }
}

/*
{  __IO uint32_t CRL;
  __IO uint32_t CRH;
  __IO uint32_t IDR;
  __IO uint32_t ODR;
  __IO uint32_t BSRR;
  __IO uint32_t BRR;
  __IO uint32_t LCKR;
} GPIO_TypeDef; */       //不清楚什么意思,放在这里编译不通过,已被我注释

#define LED_G_Toggle {GPIOB->ODR ^= GPIO_Pin_0;}
#define LED_B_Toggle {GPIOB->ODR ^= GPIO_Pin_1;}     //LED_B对应引脚1号,根据上条自行更改

int main(void)
{ 
      LED_Init();//LED???
      KEY_Init();//?????
      while (1) 
      {  
            //??KEY1??,????LED_G
            if( Key_Scan(GPIOA,GPIO_Pin_0 ) == 1 )
            { LED_G_Toggle; }
            //??KEY2??,????LED_B
            if( Key_Scan(GPIOC,GPIO_Pin_13) == 1 )
            { LED_B_Toggle; }                  
       }
}





/* * ?? :????????? 
 *?? :GPIOx:x ??? A,B,C,D?? E 
 *GPIO_Pin:??????? 
 * ?? :0(?????)?1(????)*/

//注意把int main(){}放在函数定义之后

注:若出现单片机连接错误,点进魔术棒→debug→settings等界面调节(见第一节课ppt)

二、实验2(文件夹QQrecvFile led)

1.led.c

// bsp £ºboard support package °å¼¶Ö§³Ö°ü
#include "bsp_led.h"



void LED_Init(void)
{
	GPIO_InitTypeDef  GPIO_InitStruct;
	//??GPIOB????
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	//??GPIO??????
	GPIO_InitStruct.GPIO_Pin     = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_5;
	GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_Out_PP;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	//???LED?GPIO
	GPIO_Init(GPIOB, &GPIO_InitStruct);
	//???LED???
	LED_R(0);LED_G(0);LED_B(0);
}

2.led.h

#ifndef __BSP_LED_H
#define __BSP_LED_H

#include "stm32f10x.h"

#define LED_G_GPIO_PIN              GPIO_Pin_0//Â̵Æ
#define LED_G_GPIO_PORT             GPIOB
#define LED_G_GPIO_CLK              RCC_APB2Periph_GPIOB

#define LED_B_GPIO_PIN              GPIO_Pin_1
#define LED_B_GPIO_PORT             GPIOB
#define LED_B_GPIO_CLK              RCC_APB2Periph_GPIOB

#define LED_R_GPIO_PIN              GPIO_Pin_5
#define LED_R_GPIO_PORT             GPIOB
#define LED_R_GPIO_CLK              RCC_APB2Periph_GPIOB

#define LED_G_Toggle {GPIOB->ODR ^= GPIO_Pin_0;}
#define LED_B_Toggle {GPIOB->ODR ^= GPIO_Pin_1;}
#define LED_R_Toggle {GPIOB->ODR ^= GPIO_Pin_5;}

#define    ON        1
#define    OFF       0

// \  CÓïÑÔÀïÃæ½ÐÐøÐзû£¬ºóÃæ²»ÄÜÓÐÈκεĶ«Î÷

#define   LED_G(a)   if(a) \
	                       GPIO_ResetBits(LED_G_GPIO_PORT, LED_G_GPIO_PIN); \
                     else  GPIO_SetBits(LED_G_GPIO_PORT, LED_G_GPIO_PIN);
#define   LED_B(a)   if(a) \
	                       GPIO_ResetBits(LED_B_GPIO_PORT, LED_B_GPIO_PIN); \
                     else  GPIO_SetBits(LED_B_GPIO_PORT, LED_B_GPIO_PIN);
#define   LED_R(a)   if(a) \
	                       GPIO_ResetBits(LED_R_GPIO_PORT, LED_R_GPIO_PIN); \
                     else  GPIO_SetBits(LED_R_GPIO_PORT, LED_R_GPIO_PIN);

void LED_Init(void);

#endif /* __BSP_LED_H */

3.key.c

#include "bsp_key.h"

void KEY_Init(void)
{
GPIO_InitTypeDef  GPIO_InitStruct;
//??GPIOA????
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
//??GPIO??????
GPIO_InitStruct.GPIO_Pin  = GPIO_Pin_0;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
//???Key?GPIO
GPIO_Init(GPIOA, &GPIO_InitStruct);	
	
//??GPIOC????
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
//??GPIO??????
GPIO_InitStruct.GPIO_Pin  = GPIO_Pin_13;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
//???Key?GPIO
GPIO_Init(GPIOC, &GPIO_InitStruct); 
}

uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx,   uint16_t GPIO_Pin);


/* * ?? :key1ɨÃè
 *?? :GPIOx:x ??? A,B,C,D?? E 
 *GPIO_Pin:??????? 
 * ?? :0(?????)?1(????)*/
//uint8_t   Key1_Scan(GPIO_TypeDef* GPIOA,   uint16_t GPIO_Pin_0)
//{			
//     if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0) == 1 ) 
//     {      /*????????*/
//          while(GPIO_ReadInputDataBit(GPIOA,  GPIO_Pin_0) == 1) ;   
//          return 1;	 
//      }
//      else 
//      {
//          return 0;
//      }
//}

uint8_t   Key_Scan(GPIO_TypeDef* GPIOx,   uint16_t GPIO_Pin)
{			
     if(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin) == 1 ) 
     {      /*????????*/
          while(GPIO_ReadInputDataBit(GPIOx,  GPIO_Pin) == 1) ;   
          return 1;	 
      }
      else 
      {
          return 0;
      }
}


4.key.h

#include "stm32f10x.h"

void KEY_Init(void);
uint8_t   Key_Scan(GPIO_TypeDef* GPIOx,   uint16_t GPIO_Pin);

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值