[$is_event_jimu] 2 [$group_e] 元件事件库.4x4矩阵键盘 [$group] 元件库.$pname$ [$last_ver_update_time] 2021-04-03 [$min_need_ver] 1.23 [$p_list] [$short_show_UIname] [$code_area_type] codes,exe_per_1ms,init_code [$blocks_type] [$name] 4x4 key的初始化积木 [$引脚占用列表,用于提示冲突] [($_xgm$x1),type=pin]|4x4矩阵键盘的x1脚 [($_xgm$x2),type=pin]|4x4矩阵键盘的x2脚 [($_xgm$x3),type=pin]|4x4矩阵键盘的x3脚 [($_xgm$x4),type=pin]|4x4矩阵键盘的x4脚 [($_xgm$y1),type=pin]|4x4矩阵键盘的y1脚 [($_xgm$y2),type=pin]|4x4矩阵键盘的y2脚 [($_xgm$y3),type=pin]|4x4矩阵键盘的y3脚 [($_xgm$y4),type=pin]|4x4矩阵键盘的y4脚 [$寄存器占用列表,用于提示冲突,但=>后的值没变则不提示冲突] [$中断函数号占用列表,用于提示冲突] [$thread_do] thread_anniu4x4_t_$rnd_varname_pre$();//虚拟线程,不能含阻塞执行的部分,每1ms执行一次 [$cs] [$shared_func_In_main_c] [$function_ref_define_in_headfile] @@file=key4x4.h void thread_anniu4x4_t_$rnd_varname_pre$(); u8 read_anniu4x4_is_pressed_t_$rnd_varname_pre$(); //单个按钮返回1-16.长按返回21-36,没按钮按下则返回0 u8 read_anniu4x4_first_pressed_key_value_$rnd_varname_pre$(); u8 read_anniu4x4_shishi_value_$rnd_varname_pre$(); //同时按下两个按钮时,返回第2个按钮的值:1-16.长按返回21-36,没第2个按钮按下则返回0 u8 read_anniu4x4_sec_pressed_key_value_$rnd_varname_pre$(); extern u8 anniu_state_$rnd_varname_pre$[16]; extern u8 anniu_state2_$rnd_varname_pre$[16]; extern u16 anniu_preesed_time_count_$rnd_varname_pre$[16]; extern u16 anniu_unpreesed_time_count_$rnd_varname_pre$[16]; extern u8 anniu_pre_state_is_pressed_$rnd_varname_pre$[16]; //extern __xdata u8 anniu_uint_is_pressed1_or_changan2_or0[16]; extern u8 anniu_uint_cur_st_$rnd_varname_pre$[16];//每个按钮的实时电平状态 extern u8 step_$rnd_varname_pre$; extern u8 step_time_count_$rnd_varname_pre$; extern u8 iii_$rnd_varname_pre$; extern u8 cur_anniu_index_$rnd_varname_pre$; [$replace_map] $_xgm$x1 = $field$x1,mytype|pin, ptype|TIMES $_xgm$x2 = $field$x2,mytype|pin, ptype|TIMES $_xgm$x3 = $field$x3,mytype|pin, ptype|TIMES $_xgm$x4 = $field$x4,mytype|pin, ptype|TIMES $_xgm$y1 = $field$y1,mytype|pin, ptype|TIMES $_xgm$y2 = $field$y2,mytype|pin, ptype|TIMES $_xgm$y3 = $field$y3,mytype|pin, ptype|TIMES $_xgm$y4 = $field$y4,mytype|pin, ptype|TIMES [$init_code_for_define_area] @@file=key4x4.c u8 anniu_uint_cur_st_$rnd_varname_pre$[16];//每个按钮的实时电平状态 u8 iii_$rnd_varname_pre$; u8 step_$rnd_varname_pre$; u8 step_time_count_$rnd_varname_pre$; u8 anniu_state_$rnd_varname_pre$[16]; u8 anniu_state2_$rnd_varname_pre$[16]; u16 anniu_preesed_time_count_$rnd_varname_pre$[16]; u8 anniu_pre_state_is_pressed_$rnd_varname_pre$[16]; u16 anniu_unpreesed_time_count_$rnd_varname_pre$[16]; u8 cur_anniu_index_$rnd_varname_pre$; void thread_anniu4x4_t_$rnd_varname_pre$(){ if(step_$rnd_varname_pre$==0){ IO_Write([($_xgm$y1),type=pin] ,0) ; IO_Write([($_xgm$y2),type=pin] ,1) ; IO_Write([($_xgm$y3),type=pin] ,1) ; IO_Write([($_xgm$y4),type=pin] ,1) ; }else if(step_$rnd_varname_pre$==1){ IO_Write([($_xgm$y1),type=pin] ,1) ; IO_Write([($_xgm$y2),type=pin] ,0) ; IO_Write([($_xgm$y3),type=pin] ,1) ; IO_Write([($_xgm$y4),type=pin] ,1) ; }else if(step_$rnd_varname_pre$==2){ IO_Write([($_xgm$y1),type=pin] ,1) ; IO_Write([($_xgm$y2),type=pin] ,1) ; IO_Write([($_xgm$y3),type=pin] ,0) ; IO_Write([($_xgm$y4),type=pin] ,1) ; }else if(step_$rnd_varname_pre$==3){ IO_Write([($_xgm$y1),type=pin] ,1) ; IO_Write([($_xgm$y2),type=pin] ,1) ; IO_Write([($_xgm$y3),type=pin] ,1) ; IO_Write([($_xgm$y4),type=pin] ,0) ; } for(u8 x=0;x<4;x++){ u8 val2=0; if(x==0){if( IO_Read([($_xgm$x1),type=pin])==0){ val2=1;}} else if(x==1){if(IO_Read([($_xgm$x2),type=pin])==0){ val2=1;}} else if(x==2){if( IO_Read([($_xgm$x3),type=pin])==0){ val2=1;}} else if(x==3){if( IO_Read([($_xgm$x4),type=pin])==0){ val2=1;}} anniu_uint_cur_st_$rnd_varname_pre$[4*x+step_$rnd_varname_pre$]=val2; } // for(u8 ppp=0;ppp<2;ppp++)//一次读2个按钮,否则一次16个的话 卡timer0而异常(比如数码管显示乱码,OLED12864乱码)! { u8 anniu_index=cur_anniu_index_$rnd_varname_pre$; if(anniu_uint_cur_st_$rnd_varname_pre$[anniu_index]==1) { if(anniu_preesed_time_count_$rnd_varname_pre$[anniu_index]<65530){ anniu_preesed_time_count_$rnd_varname_pre$[anniu_index]++; } anniu_pre_state_is_pressed_$rnd_varname_pre$[anniu_index]=1; if(anniu_preesed_time_count_$rnd_varname_pre$[anniu_index]>4){ //去抖动 anniu_state2_$rnd_varname_pre$[anniu_index]=1;//按钮状态2 置为1 } if(anniu_preesed_time_count_$rnd_varname_pre$[anniu_index]>(3000/10)&& anniu_state_$rnd_varname_pre$[anniu_index]!=99 ){ //20221114 anniu_state_$rnd_varname_pre$[anniu_index]=2;//按钮状态1 置为2,表示长按了按钮 } anniu_unpreesed_time_count_$rnd_varname_pre$[anniu_index]=0;//按钮未完全释放时重置已释放时间为0 }else{ anniu_unpreesed_time_count_$rnd_varname_pre$[anniu_index]++;//按钮释放时间去抖动 if(anniu_unpreesed_time_count_$rnd_varname_pre$[anniu_index]>4){ //去抖动 if( anniu_pre_state_is_pressed_$rnd_varname_pre$[anniu_index]==1){//如果上次按下1次 anniu_pre_state_is_pressed_$rnd_varname_pre$[anniu_index]=0;//清除 按下1次 状态 if( anniu_state_$rnd_varname_pre$[anniu_index]==0&&anniu_state2_$rnd_varname_pre$[anniu_index]==1){//如果不是长按触发状态,且按钮按下时间达标 anniu_state_$rnd_varname_pre$[anniu_index]=1;//按钮状态1 置为1,表示按了一次按钮并松开 } } if( anniu_state_$rnd_varname_pre$[anniu_index]==2){ anniu_state_$rnd_varname_pre$[anniu_index]=0; } if( anniu_state_$rnd_varname_pre$[anniu_index]==99){//要放在 if(anniu_unpreesed_time_count_$rnd_varname_pre$[anniu_index]>20)内部!!! anniu_state_$rnd_varname_pre$[anniu_index]=0; } anniu_state2_$rnd_varname_pre$[anniu_index]=0;//按钮状态2 清零 anniu_preesed_time_count_$rnd_varname_pre$[anniu_index]=0; anniu_unpreesed_time_count_$rnd_varname_pre$[anniu_index]=0; } } cur_anniu_index_$rnd_varname_pre$++; if(cur_anniu_index_$rnd_varname_pre$>=16){ cur_anniu_index_$rnd_varname_pre$=0; } } step_time_count_$rnd_varname_pre$++; if(step_time_count_$rnd_varname_pre$>=10){ step_time_count_$rnd_varname_pre$=0; step_$rnd_varname_pre$++; if(step_$rnd_varname_pre$>=4){ step_$rnd_varname_pre$=0; } } } //没按钮按下则返回0 u8 read_anniu4x4_is_pressed_t_$rnd_varname_pre$(){ u8 iii; for(iii=0;iii<16;iii++){ if(anniu_state_$rnd_varname_pre$[iii]==1){ return 1; } if(anniu_state_$rnd_varname_pre$[iii]==2){ return 1; } } return 0; } //单个按钮返回1-16.长按返回21-36,没按钮按下则返回0 u8 read_anniu4x4_first_pressed_key_value_$rnd_varname_pre$(){ u8 iii; for(iii=0;iii<16;iii++){ if(anniu_state_$rnd_varname_pre$[iii]==1){ return iii+1; } if(anniu_state_$rnd_varname_pre$[iii]==2){ // return 12+1+20; // return iii+1; return iii+1+20; } } return 0; } //单个按钮返回1-16.没按钮按下则返回0 u8 read_anniu4x4_shishi_value_$rnd_varname_pre$(){ u8 iii; for(iii=0;iii<16;iii++){ if(anniu_state2_$rnd_varname_pre$[iii]==1){ return iii+1; } } return 0; } //同时按下两个按钮时,返回第2个按钮的值:1-16.长按返回21-36,没第2个按钮按下则返回0 u8 read_anniu4x4_sec_pressed_key_value_$rnd_varname_pre$(){ u8 iii; u8 firest_anniu_index=0; for(iii=0;iii<16;iii++){ if(anniu_state_$rnd_varname_pre$[iii]==1){ firest_anniu_index= iii+1; } if(anniu_state_$rnd_varname_pre$[iii]==2){ firest_anniu_index= iii+1; } } if(firest_anniu_index!=0){ for(iii=firest_anniu_index;iii<16;iii++){ if(anniu_state_$rnd_varname_pre$[iii]==1){ return iii+1; } if(anniu_state_$rnd_varname_pre$[iii]==2){ return iii+1+20; } } } return 0; } [$init_code_for_define_area_once] @@file=key4x4.c [$init_code_for_init_val] #get_P口_to_PxM1_PxM0_set_code(group_xml,rand1,block_name,ref_complier,extinfo,"[($_xgm$x1),type=pin]","0","矩阵键盘 的x1")% #get_P口_to_PxM1_PxM0_set_code(group_xml,rand1,block_name,ref_complier,extinfo,"[($_xgm$x2),type=pin]","0","矩阵键盘 的x2")% #get_P口_to_PxM1_PxM0_set_code(group_xml,rand1,block_name,ref_complier,extinfo,"[($_xgm$x3),type=pin]","0","矩阵键盘 的x3")% #get_P口_to_PxM1_PxM0_set_code(group_xml,rand1,block_name,ref_complier,extinfo,"[($_xgm$x4),type=pin]","0","矩阵键盘 的x4")% #get_P口_to_PxM1_PxM0_set_code(group_xml,rand1,block_name,ref_complier,extinfo,"[($_xgm$y1),type=pin]","1","矩阵键盘 的y1")% #get_P口_to_PxM1_PxM0_set_code(group_xml,rand1,block_name,ref_complier,extinfo,"[($_xgm$y2),type=pin]","1","矩阵键盘 的y2")% #get_P口_to_PxM1_PxM0_set_code(group_xml,rand1,block_name,ref_complier,extinfo,"[($_xgm$y3),type=pin]","1","矩阵键盘 的y3")% #get_P口_to_PxM1_PxM0_set_code(group_xml,rand1,block_name,ref_complier,extinfo,"[($_xgm$y4),type=pin]","1","矩阵键盘 的y4")% IO_Write([($_xgm$y1),type=pin] ,1) ; IO_Write([($_xgm$y2),type=pin] ,1) ; IO_Write([($_xgm$y3),type=pin] ,1) ; IO_Write([($_xgm$y4),type=pin] ,1) ; IO_Write([($_xgm$x1),type=pin] ,1) ; IO_Write([($_xgm$x2),type=pin] ,1) ; IO_Write([($_xgm$x3),type=pin] ,1) ; IO_Write([($_xgm$x4),type=pin] ,1) ; step_$rnd_varname_pre$=0; cur_anniu_index_$rnd_varname_pre$=0; [$timer0_1ms_code] [$run] [$toolbox_block_code]