小学生
最后登录1970-1-1
在线时间 小时
注册时间2022-1-3
|
我在用野火开发板,不使用库函数,在使用位带操作键盘输入时不能检测按键输入,请帮忙看看问题在哪?
.h 文件如下
/*片上外设基地址 */
#define PERIPH_BASE ((unsigned int)0x40000000)
/*APB2 总线基地址 */
#define APB2PERIPH_BASE (PERIPH_BASE + 0x10000)
/* AHB总线基地址 */
#define AHBPERIPH_BASE (PERIPH_BASE + 0x20000)
/*GPIOB外设基地址*/
#define GPIOA_BASE (APB2PERIPH_BASE + 0x0800)
#define GPIOB_BASE (APB2PERIPH_BASE + 0x0C00)
#define GPIOC_BASE (APB2PERIPH_BASE + 0x1000)
/* GPIOB寄存器地址,强制转换成指针 */
#define GPIOA_CRL *(unsigned int*)(GPIOA_BASE+0x00)
#define GPIOA_CRH *(unsigned int*)(GPIOA_BASE+0x04)
#define GPIOA_IDR *(unsigned int*)(GPIOA_BASE+0x08)
#define GPIOA_ODR *(unsigned int*)(GPIOA_BASE+0x0C)
#define GPIOA_BSRR *(unsigned int*)(GPIOA_BASE+0x10)
#define GPIOA_BRR *(unsigned int*)(GPIOA_BASE+0x14)
#define GPIOA_LCKR *(unsigned int*)(GPIOA_BASE+0x18)
#define GPIOB_CRL *(unsigned int*)(GPIOB_BASE+0x00)
#define GPIOB_CRH *(unsigned int*)(GPIOB_BASE+0x04)
#define GPIOB_IDR *(unsigned int*)(GPIOB_BASE+0x08)
#define GPIOB_ODR *(unsigned int*)(GPIOB_BASE+0x0C)
#define GPIOB_BSRR *(unsigned int*)(GPIOB_BASE+0x10)
#define GPIOB_BRR *(unsigned int*)(GPIOB_BASE+0x14)
#define GPIOB_LCKR *(unsigned int*)(GPIOB_BASE+0x18)
#define GPIOC_CRL *(unsigned int*)(GPIOC_BASE+0x00)
#define GPIOC_CRH *(unsigned int*)(GPIOC_BASE+0x04)
#define GPIOC_IDR *(unsigned int*)(GPIOC_BASE+0x08)
#define GPIOC_ODR *(unsigned int*)(GPIOC_BASE+0x0C)
#define GPIOC_BSRR *(unsigned int*)(GPIOC_BASE+0x10)
#define GPIOC_BRR *(unsigned int*)(GPIOC_BASE+0x14)
#define GPIOC_LCKR *(unsigned int*)(GPIOC_BASE+0x18)
#define GPIOA_ODR_Addr (GPIOA_BASE+12) //0x4001080C
#define GPIOB_ODR_Addr (GPIOB_BASE+12) //0x40010C0C
#define GPIOC_ODR_Addr (GPIOC_BASE+12) //0x4001100C
#define GPIOD_ODR_Addr (GPIOD_BASE+12) //0x4001140C
#define GPIOE_ODR_Addr (GPIOE_BASE+12) //0x4001180C
#define GPIOF_ODR_Addr (GPIOF_BASE+12) //0x40011A0C
#define GPIOG_ODR_Addr (GPIOG_BASE+12) //0x40011E0C
#define GPIOA_IDR_Addr (GPIOA_BASE+8) //0x40010808
#define GPIOB_IDR_Addr (GPIOB_BASE+8) //0x40010C08
#define GPIOC_IDR_Addr (GPIOC_BASE+8) //0x40011008
#define GPIOD_IDR_Addr (GPIOD_BASE+8) //0x40011408
#define GPIOE_IDR_Addr (GPIOE_BASE+8) //0x40011808
#define GPIOF_IDR_Addr (GPIOF_BASE+8) //0x40011A08
#define GPIOG_IDR_Addr (GPIOG_BASE+8) //0x40011E08
// ???? GPIO????IO?,n(0,1,2...16),n????????IO?
#define PAout(n) BIT_ADDR(GPIOA_ODR_Addr,n) //??
#define PAin(n) BIT_ADDR(GPIOA_IDR_Addr,n) //??
#define PBout(n) BIT_ADDR(GPIOB_ODR_Addr,n) //??
#define PBin(n) BIT_ADDR(GPIOB_IDR_Addr,n) //??
#define PCout(n) BIT_ADDR(GPIOC_ODR_Addr,n) //??
#define PCin(n) BIT_ADDR(GPIOC_IDR_Addr,n) //??
#define PDout(n) BIT_ADDR(GPIOD_ODR_Addr,n) //??
#define PDin(n) BIT_ADDR(GPIOD_IDR_Addr,n) //??
#define PEout(n) BIT_ADDR(GPIOE_ODR_Addr,n) //??
#define PEin(n) BIT_ADDR(GPIOE_IDR_Addr,n) //??
#define PFout(n) BIT_ADDR(GPIOF_ODR_Addr,n) //??
#define PFin(n) BIT_ADDR(GPIOF_IDR_Addr,n) //??
#define PGout(n) BIT_ADDR(GPIOG_ODR_Addr,n) //??
#define PGin(n) BIT_ADDR(GPIOG_IDR_Addr,n) //??
#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
#define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
#define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))
#define LED_G PBout(0)
#define LED_B PBout(1)
#define LED_R PBout(5)
#define KEY1 (PAin(0)&1)
#define KEY2 (PCin(13)&1)
main函数如下:
// 开启GPIOB 端口时钟
RCC_APB2ENR |= (1<<3);
//清空控制PB0的端口位
//GPIOB_CRL &= ~( 0x0F<< (4*0));
GPIOB_CRL &= ~( 0x0F<< (4*0)|0x0F<< (4*1)|0x0F<< (4*5));
GPIOC_CRL &= ~( 0x0F<< (4*0));
GPIOA_CRL &= ~( 0x0F<< (4*0));
GPIOC_CRH &= ~( 0x0F<< (4*5));
// 配置PB0为通用推挽输出,速度为10M
// GPIOB_CRL |= (0x1<<4*1)|0x01<< (4*5);
GPIOB_CRL |= ( 0x01<< (4*0)|0x01<< (4*1)|0x01<< (4*5));
GPIOC_CRL |= ( 0x01<< (4*0));
GPIOA_CRL |= ( 0x04<< (4*0));
GPIOC_CRH |= ( 0x04<< (4*5));
GPIOB_ODR |= 0xff;
GPIOC_ODR |= 0xff;
//GPIOC_ODR &= ~(0x1<<0);
// PB0 输出 低电平
//GPIOB_ODR &= ~(1<<1);
// GPIOC_ODR |= (0x1<<0);
LED_G=0;
while(1)
{
if ( KEY1==1)
{ GPIOB_ODR |= 0xff;
LED_B=0;
}
if ( KEY2==1)
{
GPIOB_ODR |= 0xff;
LED_R=0;
}
}
}
不知道为何if ( KEY1==1) 和 if ( KEY2==1) 在按键按下时程序都不识别。
|
|