旋转按钮的程序仿照了机器人天空的帖子,普通按键就是很普通的做法延迟去抖,开始写简单的代码,大家多多指教(本程序已经在仪器上用气了)
/*********************************************************************************************************
** Function name: uart0SendByte
** Descriptions: 从串口0发送数据
** input parameters: data: 发送的数据
** output parameters: 无
** Returned value: 无
*********************************************************************************************************/
void uart1SendByte (INT8U ucData)
{
U1THR = ucData;
while ( (U1LSR & 0x20) == 0 );
}
/*********************************************************************************************************
** Function name: uart1GetByte
** Descriptions: 从串口接收1字节数据,使用查询方式接收
** input parameters: 无
** output parameters: 无
** Returned value: ucRcvData: 接收到的数据
*********************************************************************************************************/
INT8U uart1GetByte (void)
{
INT8U ucRcvData;
while ((U1LSR & 0x01) == 0); /* 等待接收标志置位 */
ucRcvData = U1RBR; /* 读取数据 */
return (ucRcvData);
}
/*********************************************************************************************************
* Function Name: uart1Isr
* Description: UART1中断服务函数
* Input: 无
* Output: 无
* Return: 无
*********************************************************************************************************/
void uart1Isr (void)
{
// GulNum = 0;
while ((U1IIR & 0x01) == 0)
{ /* 判断是否有中断挂起 */
switch (U1IIR & 0x0E)
{ /* 判断中断标志 */
case 0x04: /* 接收数据中断 */
{
GucRcvNew = 1; /* 置接收新数据标志 */
// for (GulNum = 0; GulNum < 8; GulNum++) /* 连续接收8个字节 */
// {
// GucRcvBuf[GulNum] = U1RBR;
GucRcvBuf = U1RBR;
// }
break;
case 0x0C: /* 字符超时中断 */
GucRcvNew = 1;
while ((U1LSR & 0x01) == 0x01) /* 判断数据是否接收完毕 */
// {
// GucRcvBuf[GulNum] = U1RBR;
// GulNum++;
GucRcvBuf = U1RBR;
// }
break;
default:
break;
}
}
}
}
/*********************************************************************************************************
** Function name: uart1GetStr
** Descriptions: 串口接收字符串
** input parameters: pucStr: 指向接收数据数组的指针
** ulNum: 接收数据的个数
** output parameters: 无
** Returned value: 无
*********************************************************************************************************/
void uart1GetStr (INT8U *pucStr, INT32U ulNum)
{
for (; ulNum > 0; ulNum--){
*pucStr++ = uart1GetByte ();
}
}
/*********************************************************************************************************
** Function name: uart1SendStr
** Descriptions: 向串口发送字符串
** input parameters: puiStr: 要发送的字符串指针
** output parameters: 无
** Returned value: 无
*********************************************************************************************************/
//void uart1SendStr (INT8U const *pucStr)
//{
// while (1){
// if (*pucStr == '\0') break; /* 遇到结束符,退出 */
// uart1SendByte (*pucStr++);
// }
//}
/*********************************************************************************************************
** Function name: uart1SendStr
** Descriptions: 向串口发送字符串
** input parameters: pucStr: 要发送的字符串指针
** ulNum: 要发送的数据个数
** output parameters: 无
** Returned value: 无
*********************************************************************************************************/
void uart1SendStr (INT8U const *pucStr, INT32U ulNum)
{
INT32U i;
for (i = 0; i < ulNum; i++)
{ /* 发送指定个字节数据 */
uart1SendByte (*pucStr++);
}
}
/*********************************************************************************************************
** Function name: uart0Init
** Descriptions: 按默认值初始化串口0的引脚和通讯参数。设置为8位数据位,1位停止位,无奇偶校验
** input parameters: 无
** output parameters: 无
** Returned value: 无
*********************************************************************************************************/
void uart1Init (void)
{
INT16U usFdiv;
U1LCR = 0x83; /* 允许设置波特率 */
usFdiv = (FPCLK / 16) / UART_BPS; /* 设置波特率 */
U1DLM = usFdiv / 256;
U1DLL = usFdiv % 256;
U1LCR = 0x03; /* 锁定波特率 */
U1FCR = 0x07;
U1IER = 0x01; /* 使能接收中断 */
}
/*********************************************************************************************************
** Function name: Scan
** Descriptions: 独立键盘扫描
** input parameters: 无
** output parameters: 无
** Returned value: 键码编值
*********************************************************************************************************/
INT8U scan (void)
{
INT32U temp;
INT8U keydata=0;
temp=(FIO0PIN0&0xef)|((FIO0PIN1&0x8f)<<8)|((FIO0PIN2&0xff)<<16)|((FIO0PIN3&0x7f)<<24);
if(temp!=0)
{
switch(temp)
{
case 0x00200000: keydata = 1; break; /* SW1 */
case 0x00100000: keydata = 2; break;
case 0x00000200: keydata = 3; break;
case 0x00000800: keydata = 4; break;
case 0x00040000: keydata = 5; break;
case 0x00010000: keydata = 6; break;
case 0x00000400: keydata = 7; break;
case 0x00000008: keydata = 8; break;
case 0x00000100: keydata = 9; break;
case 0x00000080: keydata = 10; break;
case 0x00000040: keydata = 11; break;
case 0x00000020: keydata = 12; break;
case 0x00000004: keydata = 13; break;
case 0x00400000: keydata = 14; break;
case 0x00080000: keydata = 15; break;
case 0x00020000: keydata = 16; break;
case 0x00008000: keydata = 17; break;
case 0x00000001: keydata = 18; break;
case 0x00000002: keydata = 19; break;
case 0x10000000: keydata = 20; break;
case 0x01000000: keydata = 21; break;
case 0x02000000: keydata = 22; break;
case 0x40000000: keydata = 23; break;
case 0x20000000: keydata = 24; break;
case 0x08000000: keydata = 25; break;
case 0x00800000: keydata = 26; break;
case 0x04000000: keydata = 27; break;
default : break;
}
}
return(keydata) ;
}
INT8U keyin(void)
{
INT8U t1,t2;
t1=scan();
myDelay(5); //延时消抖
t2=scan();
if(t1=t2)
return(t1);
else
return(0);
// while(t1=t2);
}
/*********************************************************************************************************
** Function name: knob_can
** Descriptions: 独立键盘扫描
** input parameters: 无
** output parameters: 无
** Returned value: 键码编值
*********************************************************************************************************/
INT8U knob_can (void)
{
// INT32U knob_temp;
INT8U knob_data=0;
if((knob_data=EncoderProcess(9,10,4))!=0)
return(knob_data) ;
if((knob_data=EncoderProcess(16,17,8))!=0)
return(knob_data) ;
if((knob_data=EncoderProcess(22,23,14))!=0)
return(knob_data) ;
if((knob_data=EncoderProcess(4,8,2))!=0)
return(knob_data);
if((knob_data=EncoderProcess(14,15,6))!=0)
return(knob_data);
if((knob_data=EncoderProcess(18,19,10))!=0)
return(knob_data);
if((knob_data=EncoderProcess(20,21,12))!=0)
return(knob_data);
if((knob_data=EncoderProcess(22,23,14))!=0)
return(knob_data);
if((knob_data=EncoderProcess(0,1,0))!=0)
return(knob_data);
}
//========================================================================================
/*********************************************************************************************************
** Function name: knob_can
** Descriptions: 独立键盘扫描
** input parameters: 无
** output parameters: 无
** Returned value: 返回键码值
*********************************************************************************************************/
INT8U EncoderProcess(INT8U a,INT8U b,INT8U c)
{
INT8U keytmp;
INT8U WheelNow=0x00;
INT16U tmpstate=((b<<8)|a);
switch(tmpstate)
{
case 0x0100:
WheelOld = wheelold_data[0];
break;
case 0x0804:
WheelOld = wheelold_data[1];
break;
case 0x0A09:
WheelOld = wheelold_data[2];
break;
case 0x0F0E:
WheelOld = wheelold_data[3];
break;
case 0x1110:
WheelOld = wheelold_data[4];
break;
case 0x1312:
WheelOld = wheelold_data[5];
break;
case 0x1514:
WheelOld = wheelold_data[6];
break;
case 0x1716:
WheelOld = wheelold_data[7];
break;
default: break;
}
WheelNow=WheelNow<<1;
if (GetPIN1(a)==1)
WheelNow=WheelNow+1; // 读 PINA
WheelNow=WheelNow<<1;
if (GetPIN1(b)==1) // 读 PINB
WheelNow=WheelNow+1;
WheelNow=WheelNow & 0x03; // 将 WheelNow 的 2 - 7 位清零,保留 0- 1 两个位的状态.
if (WheelNow==0x00)
return(NULL); //当 PINA 和 PINB都为低电平时退出,低电平区不做处理
keytmp=WheelNow;
keytmp ^=WheelOld; // 判断新读的数据同旧数据
if (keytmp==0)
return(NULL); // 新读的数据同旧数据一样时退出.
if (WheelOld==0x01 && WheelNow==0x02) // 是左旋转否
{
switch(tmpstate)
{
case 0x0100:
wheelold_data[0]=WheelNow;
break;
case 0x0804:
wheelold_data[1]=WheelNow;
break;
case 0x0A09:
wheelold_data[2]=WheelNow;
break;
case 0x0F0E:
wheelold_data[3]=WheelNow;
break;
case 0x1110:
wheelold_data[4]=WheelNow;
break;
case 0x1312:
wheelold_data[5]=WheelNow;
break;
case 0x1514:
wheelold_data[6]=WheelNow;
break;
case 0x1716:
wheelold_data[7]=WheelNow;
break;
default: break;
}
// WheelOld=WheelNow;
// return(WheelLeft()); //左旋转
return(knob_coding[c]); //左旋转
}
else if (WheelOld==0x02 && WheelNow==0x01) // 是右旋转否
{
switch(tmpstate)
{
case 0x0100:
wheelold_data[0]=WheelNow;
break;
case 0x0804:
wheelold_data[1]=WheelNow;
break;
case 0x0A09:
wheelold_data[2]=WheelNow;
break;
case 0x0F0E:
wheelold_data[3]=WheelNow;
break;
case 0x1110:
wheelold_data[4]=WheelNow;
break;
case 0x1312:
wheelold_data[5]=WheelNow;
break;
case 0x1514:
wheelold_data[6]=WheelNow;
break;
case 0x1716:
wheelold_data[7]=WheelNow;
break;
default: break;
}
// WheelOld=WheelNow;
// return(WheelRight()); //右旋转
return(knob_coding[c+1]); //左旋转
}
switch(tmpstate)
{
case 0x0100:
wheelold_data[0]=WheelNow;
break;
case 0x0804:
wheelold_data[1]=WheelNow;
break;
case 0x0A09:
wheelold_data[2]=WheelNow;
break;
case 0x0F0E:
wheelold_data[3]=WheelNow;
break;
case 0x1110:
wheelold_data[4]=WheelNow;
break;
case 0x1312:
wheelold_data[5]=WheelNow;
break;
case 0x1514:
wheelold_data[6]=WheelNow;
break;
case 0x1716:
wheelold_data[7]=WheelNow;
break;
default: break;
}
// WheelOld=WheelNow; // 保存当前值
return(NULL); // 当 PINA 和 PINB 都为高电平时表示编码器没有动作,退出
}
//========================================================================================
/*********************************************************************************************************
** Function name: Key_LED
** Descriptions: 接收数据,相应的LED灯点亮
** input parameters: 无
** output parameters: 无
** Returned value: 无
*********************************************************************************************************/
void Key_LED (INT8U d)
{ INT8U led_data=0;
led_data=d;
FIO1SET3=0x03;
FIO2SET =0xFFFF;
if(led_data!=0)
{
switch(led_data)
{
case 0x01:
FIO2CLR|=0x0004;
break;
case 0x02:
FIO2CLR|=0x0008;
break;
case 0x03:
FIO2CLR|=0x0010;
break;
case 0x04:
FIO2CLR|=0x0020;
break;
case 0x05:
FIO2CLR|=0x0040;
break;
case 0x06:
FIO2CLR|=0x0080;
break;
case 0x07:
FIO2CLR|=0x0100;
break;
case 0x08:
FIO2CLR|=0x0200;
break;
case 0x09:
FIO2CLR|=0x0400;
break;
case 0x0a:
FIO2CLR|=0x0800;
break;
case 0x0b:
FIO2CLR|=0x1000;
break;
case 0x0c:
FIO2CLR|=0x2000;
break;
case 0x0d:
FIO1CLR|=0x01000000;
break;
case 0x0e:
FIO1CLR|=0x02000000;
break;
case 0x10:
FIO1CLR|=0x04000000;
break;
default: break;
}
}
}
//========================================================================================
/*********************************************************************************************************
** Function name: main
** Descriptions: 发送和接收数据
** input parameters: 无
** output parameters: 无
** Returned value: 无
*********************************************************************************************************/
int main (void)
{
// rcvdata=0;
targetInit(); /* 初始化目标板,切勿删除 */
pinInit(); /* 引脚初始化 */
FIO0DIR0=0x00;
FIO0DIR1=0x00;
FIO0DIR2=0x00;
FIO0DIR3=0x00;
FIO1DIR0=0x00;
FIO1DIR1=0x00;
FIO1DIR2=0x00;
FIO1DIR3=0x07;
FIO1SET3=0x07;
FIO2DIR =0xFFFF;
FIO2SET =0xFFFF;
GucRcvNew = 0;
PCONP = PCONP | (1<<4); /* 开启串口1功能模块 */
uart1Init();
zyIsrSet(NVIC_UART1,(unsigned long)uart1Isr,PRIO_ONE);
while (1)
{
data =keyin();
if(data!=0&&(data!=dlay_data))
{
uart1SendByte(data);
}
dlay_data=data;
if (GucRcvNew == 1)
{ /* 判断是否有新数据 */
GucRcvNew = 0; /* 清除标志 */
if(GucRcvBuf!=0)
Key_LED(GucRcvBuf);
uart1SendByte (GucRcvBuf);
}
data1=knob_can();
if(data1!=0)
{
uart1SendByte(data1);
myDelay(1);
}
}
}
转载
|