业余爱好,STC8H8K64U, 32脚的,P2口配置为准双向,另外I2C用了P2.4、P2.5,
P26、P27分别接了个五向开关的2个脚,上电设置为1,用万用表检测这两个I/O口一直为低电平,请问该怎么配置?
P0口相同配置,P00、P01、P02也是接五向开关,P0口就正常,单机双击长按检测均正常,就P26、P27一直是低电平,外接上拉也不行。
1:main.c
- #include "GPIO.h"
- #include "I2C.h"
- #include "timer.h"
- #include "key.h"
- #include "delay.h"
- #include "ssd1312.h"
- #include "bmp.h"
-
- bit T_10ms = 0;
-
- void GPIO_config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure; //结构定义
-
- GPIO_InitStructure.Pin = GPIO_Pin_All; //指定要初始化的IO, GPIO_Pin_0 ~ GPIO_Pin_7, 或操作
- GPIO_InitStructure.Mode = GPIO_PullUp; //指定IO的输入或输出方式,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
- GPIO_Inilize(GPIO_P0,&GPIO_InitStructure); //初始化
-
- GPIO_InitStructure.Pin = GPIO_Pin_All; //指定要初始化的IO, GPIO_Pin_0 ~ GPIO_Pin_7, 或操作
- GPIO_InitStructure.Mode = GPIO_PullUp; //指定IO的输入或输出方式,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
- GPIO_Inilize(GPIO_P2,&GPIO_InitStructure); //初始化
-
- }
-
- /**************** I2C初始化函数 *****************/
- void I2C_config(void)
- {
- I2C_InitTypeDef I2C_InitStructure;
- I2C_InitStructure.I2C_Mode = I2C_Mode_Master; //主从选择 I2C_Mode_Master, I2C_Mode_Slave
- I2C_InitStructure.I2C_Enable = ENABLE; //I2C功能使能, ENABLE, DISABLE
- I2C_InitStructure.I2C_MS_WDTA = DISABLE; //主机使能自动发送, ENABLE, DISABLE
- I2C_InitStructure.I2C_MS_Interrupt = DISABLE; //使能主机模式中断, ENABLE, DISABLE
- I2C_InitStructure.I2C_Speed = 32; //总线速度=Fosc/2/(Speed*2+4), 0~63
- I2C_InitStructure.I2C_IoUse = I2C_P24_P25; //IO口切换 I2C_P14_P15, I2C_P24_P25, I2C_P33_P32
-
- I2C_Init(&I2C_InitStructure);
- }
-
- void Timer_config(void)
- {
- TIM_InitTypeDef TIM_InitStructure; //结构定义
- TIM_InitStructure.TIM_Mode = TIM_16BitAutoReload; //指定工作模式, TIM_16BitAutoReload,TIM_16Bit,TIM_8BitAutoReload,TIM_16BitAutoReloadNoMask
- TIM_InitStructure.TIM_Priority = Priority_0; //指定中断优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
- TIM_InitStructure.TIM_Interrupt = ENABLE; //中断是否允许, ENABLE或DISABLE
- TIM_InitStructure.TIM_ClkSource = TIM_CLOCK_12T; //指定时钟源, TIM_CLOCK_1T,TIM_CLOCK_12T,TIM_CLOCK_Ext
- TIM_InitStructure.TIM_ClkOut = DISABLE; //是否输出高速脉冲, ENABLE或DISABLE
- TIM_InitStructure.TIM_Value = 65536UL - (MAIN_Fosc / (100*12)); //中断频率, 100次/秒
- TIM_InitStructure.TIM_Run = ENABLE; //是否初始化后启动定时器, ENABLE或DISABLE
- Timer_Inilize(Timer3,&TIM_InitStructure); //初始化Timer0 Timer0,Timer1,Timer2,Timer3,Timer4
- }
-
- void Settings(void)
- {
- OLED_Clear();
- OLED_ShowChinese(48, 3, 0, 16);
- OLED_ShowChinese(64, 3, 1, 16);
- }
-
- void main()
- {
- u8 key;
- // GPIO_config();
- P0M0 = 0x00;
- P0M1 = 0x00;
- P2M0 = 0x00;
- P2M1 = 0x00;
- I2C_config();
- OLED_Init();
- Timer_config();
- OLED_WR_Byte(0xA5, OLED_CMD); //全亮
- OLED_Clear();
- OLED_WR_Byte(0xA4, OLED_CMD); //正常模式
- OLED_DrawBMP(30, 0, 69, 64, BMP1);
- delay_ms(1000);
- OLED_Clear();
- EA = 1;
- KA=1;KB=1;KC=1;KD=1;KP=1;
- while(1)
- {
- if(T_10ms)
- {
- T_10ms = 0;
- key = key_read();
- if(key == L_key) OLED_ShowString(32, 3, "L_KEY", 16);
- else if(key == D_key) OLED_ShowString(32, 3, "D_KEY", 16);
- else if(key == S_key) OLED_ShowString(32, 3, "S_KEY", 16);
- }
- }
- }
复制代码
2: Key
- #define KA P26
- #define KB P27
- #define KC P00
- #define KD P01
- #define KP P02
-
复制代码
3:原理图
|