华普微CMT2168A管脚的配置
.h文件中定义如下管脚:
//Port Function assign
#define LED P04
#define DISARM_KEY P02
#define MUTE_KEY P03
#define SERCH_KEY P01
#define FIND_KEY P00
#define KEY_MASK_VALUE 0x0F
#define DISARM_KEY_IDR M_GPIO11_IDR
#define MUTE_KEY_IDR M_GPIO14_IDR
#define SERCH_KEY_IDR M_GPIO9_IDR
#define FIND_KEY_IDR M_GPIO10_IDR
可以从"DISARM_KEY_IDR"等看到按键和管脚真正的关系,按键和P0X的关系是在代码中指定的:
void init_port_mapping(void)
{
P0 = 0xFF;
//output assign
// sys_set_hv_reg(CUS_PADCTL2_ADDR, GPIO7_MODE_AS_DIG_OUTPUT, M_GPIO7_MODE);
// sys_set_gpio7_out_sel(M_GPIO_SEL_P0_OUT); // A7 -> P0.7, LED
sys_set_hv_reg(CUS_PADCTL4_ADDR, GPIO12_MODE_AS_DIG_OUTPUT, M_GPIO12_MODE); //LED2 output
sys_set_gpio12_out_sel(M_GPIO_SEL_P0_OUT); // B4=P04=LED2
sys_set_hv_reg(CUS_PADCTL6_ADDR, GPIO12_CNF_OUTPUT_PUSH_PULL, M_GPIO12_CNF); // Must have
sys_set_hv_reg(CUS_PADCTL12_ADDR, GPIO12_INPUT_FLOATING, M_GPIO12_ODR); // Must have
sys_set_hv_reg(INT_SYSCTL3_ADDR, M_S3S_DISABLE, M_S3S_DISABLE); //S3S Disable for use B4
//input assign
sys_set_p0_1_input_from_gpio(10); // P00=B2=SERCH_KEY
sys_set_p0_1_input_from_gpio(9); // P01=B1=SERCH_KEY
sys_set_p0_2_input_from_gpio(11); // P02=B3=DISARM_KEY
sys_set_p0_3_input_from_gpio(14); // P03=B6=MUTE_KEY
}
这里把按键具体所在的管脚映射到P0X去,方便代码操作,代码直接读P0这个寄存器就知道了4个按键,比如代码操作如下:
byte read_key(void)
{
byte keydat;
P0 = 0xFF;
keydat = ((~P0) & KEY_MASK_VALUE);
if(keydat!=0)
{
switch(keydat)
{
case 0x01: keydat = 0x04; break; //寻车键
case 0x02: keydat = 0x01; break; //设防键
case 0x04: keydat = 0x02; break; //解除键 PB3
case 0x08: keydat = 0x03; break; //静音键
default: keydat = 0; break;
}
}
return(keydat); //读键码
}
对于输出也是一样,LED这个宏指向了P04,输出只要LED=0或者LED=1就可以了,输出映射的配置看上面init_port_mapping函数中的相关语句!
另外注意:上面用的是PB2,这个管脚是用来驱动flash的,所以PB2的功能只有在OTA的时候使用,flash的时候不能够使用!
需要代码的请联系我!