标题:
stm32航模无刷电机遥控驱动程序-里面有很多模块lcd键盘24l01主机和丛机程序一体
[打印本页]
作者:
zqy181818
时间:
2017-5-10 15:55
标题:
stm32航模无刷电机遥控驱动程序-里面有很多模块lcd键盘24l01主机和丛机程序一体
stm32航模无刷电机遥控驱动程序-里面有很多模块lcd键盘24l01主机和丛机程序一体
单片机源码:
#include "Defineall.h"
#include "stm32f10x.h"
#include "stdio.h"
uchar Key_NumberKeyInputTimes=0;
void Key_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;//键盘纵轴,用作中断输入(下拉电阻,上升沿)
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPD;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;//键盘横轴,输出
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_2MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9);//拉高X轴
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource12); //将GPIO连接到外部中断
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource13);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource14);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource15);
EXTI_InitStructure.EXTI_Line=EXTI_Line12|EXTI_Line13|EXTI_Line14|EXTI_Line15;//Y轴做中断
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Rising;//上升沿
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
EXTI_Init(&EXTI_InitStructure);
// NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel=EXTI15_10_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void EXTI15_10_IRQHandler(void)
{
char KeyValue=0;
uint16_t KeyValueStatus=0;
uchar KeyValueY=0,KeyValueX=0;
uchar Times=0;
uint TestKeyValue=GPIO_Pin_6;
EXTI_ClearITPendingBit(EXTI_Line12|EXTI_Line13|EXTI_Line14|EXTI_Line15);//复位中断挂起标志位,防止因为中断挂起标志位置位,反复进入中断
EXTI->IMR&=~(EXTI_Line12|EXTI_Line13|EXTI_Line14|EXTI_Line15);//暂时关闭中断,防止抖动触发中断,待处理完成后再次开启中断
DelayMs(30);//消除抖动
if((GPIO_ReadInputData(GPIOB)&0XF000)!=0X0000)//检查是否按键仍旧处于按下状态。无按键按下时GPIOB Bit15_12(下拉电阻)的值都为低。
{
//Led亮
KeyValueStatus=GPIO_ReadInputData(GPIOB);
KeyValueY=(uchar)(KeyValueStatus>>12)&0X0F;
for(Times=0;Times<4;Times++) //检测纵轴
{
if((KeyValueY&0X01)==0X01)
{
KeyValueY=Times*4;
break;
}
KeyValueY=KeyValueY>>1;
}
for(Times=0;Times<4;Times++)//检测横轴
{
GPIO_ResetBits(GPIOB,TestKeyValue);
if((GPIO_ReadInputData(GPIOB)&0XF000)==0X0000)
{
KeyValueX=Times+1;
break;
}
TestKeyValue=TestKeyValue<<1;
}
KeyValue=KeyValueX+KeyValueY;
GPIO_SetBits(GPIOB,GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9);//拉高X轴
do
{
KeyValueStatus=GPIO_ReadInputData(GPIOB)&0XF000;
DelayMs(1);
}
while(KeyValueStatus==0X0000);//等待按键释放
//led灭
// printf("KeyValueY:%d\n",KeyValueY+KeyValueX);
Key_Redefine((char)KeyValue);
}
EXTI->IMR|=(EXTI_Line12|EXTI_Line13|EXTI_Line14|EXTI_Line15);//开启中断
}
/***************************
按键分布
1 2 3 Enter // 1 2 3 4
4 5 6 Return // 5 6 7 8
7 8 9 Delete // 9 10 11 12
0 Home // 13 14 15 16
****************************/
void Key_Redefine(uchar KeyValue)//重定义按键
{
switch(KeyValue)
{
case 1: KeyValue=1;break;
case 2: KeyValue=2;break;
case 3: KeyValue=3;break;
case 4: KeyValue=KeyEnter;break;//11
case 5: KeyValue=4;break;
case 6: KeyValue=5;break;
case 7: KeyValue=6;break;
case 8: KeyValue=KeyReturn;break;//12
case 9: KeyValue=7;break;
case 10: KeyValue=8;break;
case 11: KeyValue=9;break;
case 12: KeyValue=KeyDelete;break;//13
case 13: KeyValue=KeyUp;break;//15
case 14: KeyValue=0;break;
case 15: KeyValue=KeyDown;break;//16
case 16: KeyValue=KeyHome;break;//14
default:break;
}
printf("键值是:%d\n",KeyValue);
Key_Check(KeyValue);
}
void Key_Check(uchar KeyValue)//检查当前页面,以确定需要传递处理和屏蔽的干扰按键
{
uchar LcdPageChecked=Lcd_PageStatusCheck();
switch(LcdPageStatus)
{
case Lcd_LogoPage:
{
Key_Proccess(LcdPageChecked,KeyValue);
break;
}
case Lcd_PasswordInputPage:
{
if((KeyValue!=KeyUp)||(KeyValue!=KeyDown))
{
Key_Proccess(LcdPageChecked,KeyValue);
}
break;
}
case Lcd_MainPage:
{
if((KeyValue!=KeyDelete)||(KeyValue!=KeyEnter))
{
Key_Proccess(LcdPageChecked,KeyValue);
}
break;
}
case Lcd_PasswordRightPage:
{
Key_Proccess(LcdPageChecked,KeyValue);
break;
}
case Lcd_PasswordFaultPage:
{
Key_Proccess(LcdPageChecked,KeyValue);
break;
}
default:break;
}
}
void Key_ClearInputInformation(void)//清除输入信息
{
uchar Times=0;
Key_NumberKeyInputTimes=0;
for(Times=0;Times<MotorPasswordLength;Times++)
{
MotorPassWordInput[Times]=0;
}
}
void Lcd_RefreshCommandStatus(uchar Selection)
{
Lcd_ClearScreen(1,1,16);
if(Selection==1)
{
Lcd_PrintString(1,1,"Rsing");
}
else if(Selection==0)
{
Lcd_PrintString(1,1,"Downiing");
}
else
{
Lcd_PrintString(1,1,"No Operation");
}
Lcd_PrintAByte(4,6,Motor_PowerLevelSatatus+0x30);
Lcd_DisplayPosition(4,8);
Lcd_PrintString(4,1,"Th");
//Lcd_PrintAByte(4,7,'%');
}
void Key_Proccess(uchar PageStatusChecked,uchar KeyValue)
{
uchar Times=0;
switch(PageStatusChecked)
{
case Lcd_LogoPage:
if(MotorPasswordRightFlag==1)
{
Lcd_PageCreate(Lcd_MainPage);
}
else
{
Lcd_PageCreate(Lcd_PasswordInputPage);
}
break;
case Lcd_MainPage:
if(KeyValue>9)
{
switch(KeyValue)
{
case KeyHome:
Motor_PowerLevelSatatus=0;
Motor_PowerLevelSatatus=0;
Motor_PowerLevelPast=0;
Lcd_PageCreate(Lcd_LogoPage);
Motor_SendCammand(Motor_SetPowerLevel,Motor_PowerLevelSatatus); //发送
printf("已经向从机发送了将至最低功率命令\n");
break;
case KeyReturn:
Motor_PowerLevelSatatus=0;
Motor_PowerLevelPast=0;
Lcd_PageCreate(Lcd_LogoPage);
Motor_SendCammand(Motor_SetPowerLevel,Motor_PowerLevelSatatus); //发送
printf("已经向从机发送了将至最低功率命令\n");
break;
case KeyDelete:__nop();break;
case KeyEnter:__nop();break;
case KeyUp:
if(Motor_PowerLevelSatatus>=9)
{
Motor_PowerLevelSatatus=9;
Lcd_RefreshCommandStatus(2);//无操作
Motor_SendCammand(Motor_SetMotorPowerHigher,Motor_PowerLevelSatatus); //发送
printf("已经向从机发送了无操作命令\n");
}
else
{
Lcd_RefreshCommandStatus(1); //显示加速
Motor_PowerLevelSatatus++;
Motor_SendCammand(Motor_SetMotorPowerHigher,Motor_PowerLevelSatatus); //发送
printf("已经向从机发送了加速命令\n");
}
break;
case KeyDown:
if(Motor_PowerLevelSatatus<=0)
{
Motor_PowerLevelSatatus=0;
Lcd_RefreshCommandStatus(2);//无操作
Motor_SendCammand(Motor_SetMotorPowerLower,Motor_PowerLevelSatatus); //发送
printf("已经向从机发送了无操作命令\n");
}
else
{
Lcd_RefreshCommandStatus(0);//显示减速
Motor_PowerLevelSatatus--;
Motor_SendCammand(Motor_SetMotorPowerLower,Motor_PowerLevelSatatus); //发送
printf("已经向从机发送了减速命令\n");
}
break;
default:break;
}
}
else
{
Motor_PowerLevelPast=Motor_PowerLevelSatatus;
Motor_PowerLevelSatatus=KeyValue;
if(Motor_PowerLevelPast<Motor_PowerLevelSatatus)
{
Lcd_RefreshCommandStatus(1);//显示提速
printf("已将功率大小信息发送到从机(提速)\n");
}
if(Motor_PowerLevelPast>Motor_PowerLevelSatatus)
{
Lcd_RefreshCommandStatus(0);//显示减速
printf("已将功率大小信息发送到从机(减速)\n");
}
if(Motor_PowerLevelPast==Motor_PowerLevelSatatus)
{
Lcd_RefreshCommandStatus(2);//显示无操作
printf("已将功率大小信息发送到从机(无操作)\n");
}
Motor_SendCammand(Motor_SetPowerLevel,Motor_PowerLevelSatatus); //发送
}
break;
case Lcd_PasswordInputPage:
if(KeyValue>9)
{
switch(KeyValue)
{
case KeyDelete:
Key_ClearInputInformation();// 清除输入信息
Lcd_PageCreate(Lcd_PasswordInputPage);//返回重新输入
break;
case KeyHome:
Key_ClearInputInformation();// 清除输入信息
Lcd_PageCreate(Lcd_LogoPage);//返回logo
break;
case KeyReturn:
Key_ClearInputInformation();// 清除输入信息
Lcd_PageCreate(Lcd_LogoPage);//返回logo
break;
case KeyEnter:
if((Key_NumberKeyInputTimes==MotorPasswordLength)&&(Motor_CheckPassword()==1))//密码正确,验证输入次数的目的是防止密码是8个0
{
Key_ClearInputInformation();
Lcd_PageCreate(Lcd_PasswordRightPage);
Motor_SendCammand(Motor_InitESC,1);
DelayMs(5000);//等待电调初始化才能操作
Lcd_PageCreate(Lcd_MainPage);//进入主菜单
printf("已经向从机发送了解锁命令\n");
}
else
{
Key_ClearInputInformation();
Lcd_PageCreate(Lcd_PasswordFaultPage);//进入输入密码出错页面
}
break;
default:break;
}
}
else//数字输入
{
Key_NumberKeyInputTimes=Key_NumberKeyInputTimes+1;
printf("输入数字次数是:%d\n",Key_NumberKeyInputTimes);
if(Key_NumberKeyInputTimes>8)
{
Key_NumberKeyInputTimes=0;
}
else
{
MotorPassWordInput[Key_NumberKeyInputTimes-1] =KeyValue;
Lcd_DisplayPosition(4,Key_NumberKeyInputTimes);
Lcd_WriteAByte(0,MotorPassWordInput[Key_NumberKeyInputTimes-1]+0X30);
Lcd_DisplayPosition(4,Key_NumberKeyInputTimes);
Lcd_WriteAByte(0,MotorPassWordInput[Key_NumberKeyInputTimes-1]+0X30);
for(Times=0;Times<8;Times++)
{
printf("输入的密码是:%d\n",MotorPassWordInput[Times]);
}
}
}
break;
case Lcd_PasswordFaultPage:
Lcd_PageCreate(Lcd_PasswordInputPage);
break;
case Lcd_PasswordRightPage:
if(Motor_BSCInitCompleteFlag==1)//电调没完成初始化时用户不能自行操作,要等待系统完成初始化后自动返回.
{
switch(KeyValue)
{
case KeyHome:Lcd_PageCreate(Lcd_LogoPage);break;
case KeyReturn:Lcd_PageCreate(Lcd_LogoPage);break;
default:break;
}
}
else
{
__nop();
}
break;
default:break;
}
}
//void Key_Proccess(char KeyValue)
//{
// uchar Key_NumberInputFlag=1;
// if(KeyValue>9)
// {
// Key_NumberInputFlag=0;//我规定了键0-9是数字键.11-16是功能键
// }
// //下面启动页的
// switch(KeyValue)
// {
// case Lcd_LogoPage:
// {
// if(Motor_CheckPassword()==1)//先检查密码是否正确
// {
// Lcd_PageCreate(Lcd_MainPage);
// }
// else
// {
// Lcd_PageCreate(Lcd_PasswordInputPage);
// }
// break;
// }
////下面是密码输入正确的页面
// case Lcd_PasswordRightPage:
// {
// Lcd_PageCreate(Lcd_MainPage);//进入调速页
// break;
// }
////密码输入错误页面
// case Lcd_PasswordFaultPage:
// {
// switch(KeyValue)
// {
// case KeyReturn:Lcd_PageCreate(Lcd_PasswordInputPage);break;
// case KeyHome:Lcd_PageCreate(Lcd_LogoPage);break;
// case KeyEnter:Lcd_PageCreate(Lcd_PasswordInputPage);break;
// default:break;
// }
// break;
// }
////密码输入页面
// case Lcd_PasswordInputPage:
// {
// if(Key_NumberInputFlag==1)//检查是否是数字输入
// {
// Key_NumberKeyInputTimes++;//开启数字进入次数计数;
// if(Key_NumberKeyInputTimes<=MotorPasswordLength)
// {
// MotorPassWordInput[Key_NumberKeyInputTimes-1] =KeyValue;
// Lcd_PrintAByte(4,Key_NumberKeyInputTimes,MotorPassWordInput[Key_NumberKeyInputTimes-1]+0X30);
// }
// if(Key_NumberKeyInputTimes>=MotorPasswordLength)
// {
// Key_NumberKeyInputTimes=0;
// }
// }
// else
// {
// switch(KeyValue)
// {
// case KeyReturn:
// {
// Lcd_PageCreate(Lcd_LogoPage);
// Key_ClearInputInformation();
// break;
// }
// case KeyHome:
// {
// Lcd_PageCreate(Lcd_LogoPage);
// Key_ClearInputInformation();
// break;
// }
// case KeyEnter:
// {
// if((Key_NumberKeyInputTimes==MotorPasswordLength)&&(Motor_CheckPassword()==1))//密码正确,验证输入次数的目的是防止密码是8个0
// {
// Key_ClearInputInformation();
// Lcd_PageCreate(Lcd_MainPage);//进入主菜单
// }
// break;
// }
// case KeyDelete:
// {
// Key_ClearInputInformation();// 清除输入信息
// Lcd_PageCreate(Lcd_PasswordInputPage);//返回重新输入
// break;
// }
// default:break;
// }
// }
// break;
// }
////下面是主页的
// case Lcd_MainPage:
// {
// if((KeyValue==KeyReturn)||(KeyValue==KeyHome))
// {
// Lcd_PageCreate(Lcd_LogoPage);
// Key_ClearInputInformation();
// }
// break;
// }
// default:break;
// }
//}
//void Key_Proccess(char KeyValue)
//{
// uchar Motor_PowerLevelPast=0;
// switch(LcdPageStatus)
// {
// case Lcd_LogoPage:
// {
// if(Motor_CheckPassword())
// {
// Lcd_PageCreate(Lcd_MainPage);
// }
// else
// {
// Lcd_PageCreate(Lcd_PasswordInputPage);
// }
// break;
// }
//
// case Lcd_PasswordInputPage:
// {
//// if((KeyValue>=0)||(KeyValue<=9))//数字输入
// switch(KeyValue)
// {
// case KeyEnter:
// {
// if(Key_NumberKeyInputTimes==MotorPasswordLength)
// {
// if(Motor_CheckPassword()==1)//密码正确
// {
// //提示密码正确,等待机器初始化
// Key_ClearInputInformation();
// Lcd_PageCreate(Lcd_PasswordRightPage);
// }
// else
// {
// //页面提示密码错误,清楚输入并返回输入界面
// Key_ClearInputInformation();
// Lcd_PageCreate(Lcd_PasswordFaultPage);
// }
// }
// break;
// }
//
// case KeyDelete:
// {
// Key_ClearInputInformation();
// Lcd_PageCreate(Lcd_PasswordInputPage);
// break;
// }
// case KeyHome:
// {
// Key_ClearInputInformation();
// Lcd_PageCreate(Lcd_LogoPage);
// break;
// }
//
// case KeyReturn:
// {
// Key_ClearInputInformation();
// Lcd_PageCreate(Lcd_LogoPage);
// break;
// }
//
// default:
// {
// Key_NumberKeyInputTimes++;
// if(Key_NumberKeyInputTimes<=MotorPasswordLength)
// {
// MotorPassWordInput[Key_NumberKeyInputTimes-1] =KeyValue;
// Lcd_PrintAByte(4,Key_NumberKeyInputTimes,MotorPassWordInput[Key_NumberKeyInputTimes-1]+0X30);
// }
// break;
// }
// }
// }
//// if(KeyValue<=9)
//// {
//// if(Key_NumberKeyInputTimes<=MotorPasswordLength)
//// {
//// Key_NumberKeyInputTimes++;
//// MotorPassWordInput[Key_NumberKeyInputTimes-1] =KeyValue;
//// Lcd_PrintAByte(4,Key_NumberKeyInputTimes,MotorPassWordInput[Key_NumberKeyInputTimes-1]+0X30);
//// }
//// }
////
//// if(KeyValue==KeyEnter)
//// {
//// if(Key_NumberKeyInputTimes==MotorPasswordLength)
//// {
//// if(Motor_CheckPassword()==1)//密码正确
//// {
//// //提示密码正确,等待机器初始化
//// Key_ClearInputInformation();
//// Lcd_PageCreate(Lcd_PasswordRightPage);
//// }
//// else
//// {
//// //页面提示密码错误,清楚输入并返回输入界面
//// Key_ClearInputInformation();
//// Lcd_PageCreate(Lcd_PasswordInputPage);
…………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
完整代码:
stm32航模无刷电机遥控驱动程序-里面有很多模块lcd键盘24l01主机和丛机程序一体.rar
(331.18 KB, 下载次数: 51)
2017-5-10 16:04 上传
点击文件名下载附件
下载积分: 黑币 -5
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1