rot_module.c

来自「* Use 10 MHz crystal frequency. * Use 」· C语言 代码 · 共 58 行

C
58
字号
#define ROT1      RB7#define ROT0      RB6#define ROT1_IN  TRISB7=1#define ROT0_IN  TRISB6=1#define ROT_S0 0#define ROT_S1 1#define ROT_S2 2#define ROT_S3 3volatile unsigned char state,last_state,rotcount,last_rotcount;volatile unsigned char rotdir,rot_min,rot_max;void update_rotary_state(void){  state = 0;  if (ROT0) state = state | 0x01;  //set LSb  if (ROT1) bitset(state,1);  switch(state) {  case ROT_S0:    if (last_state == ROT_S1) {      rotcount++; rotdir = 1; last_state = state;    }else if (last_state == ROT_S2) {      rotcount--;rotdir = 0; last_state = state;    }break;  case ROT_S1:    if (last_state == ROT_S3) {      rotcount++;rotdir=1; last_state = state;    }    else if (last_state == ROT_S0) {      rotcount--; rotdir=0; last_state = state;    }break;  case ROT_S2:    if (last_state == ROT_S0) {      rotcount++; rotdir=1;last_state = state;    }    else if (last_state == ROT_S3) {      rotcount--;rotdir=0;last_state = state;    }break;  case ROT_S3:    if (last_state == ROT_S2) {      rotcount++; rotdir=1;last_state = state;    }    else if (last_state == ROT_S1) {      rotcount--; rotdir=0;last_state = state;    }break;  }  if (rotdir){    // incremented    if (rotcount == (rot_max+1)) rotcount = rot_min;  } else {    //decremented    if (!rot_min && (rotcount > rot_max)) rotcount = rot_max;    else if (rotcount == (rot_min-1)) rotcount = rot_max;  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?