標題:
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);//暫時關閉中斷,防止抖動觸發(fā)中斷,待處理完成后再次開啟中斷
DelayMs(30);//消除抖動
if((GPIO_ReadInputData(GPIOB)&0XF000)!=0X0000)//檢查是否按鍵仍舊處于按下狀態(tài)。無按鍵按下時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); //發(fā)送
printf("已經(jīng)向從機發(fā)送了將至最低功率命令\n");
break;
case KeyReturn:
Motor_PowerLevelSatatus=0;
Motor_PowerLevelPast=0;
Lcd_PageCreate(Lcd_LogoPage);
Motor_SendCammand(Motor_SetPowerLevel,Motor_PowerLevelSatatus); //發(fā)送
printf("已經(jīng)向從機發(fā)送了將至最低功率命令\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); //發(fā)送
printf("已經(jīng)向從機發(fā)送了無操作命令\n");
}
else
{
Lcd_RefreshCommandStatus(1); //顯示加速
Motor_PowerLevelSatatus++;
Motor_SendCammand(Motor_SetMotorPowerHigher,Motor_PowerLevelSatatus); //發(fā)送
printf("已經(jīng)向從機發(fā)送了加速命令\n");
}
break;
case KeyDown:
if(Motor_PowerLevelSatatus<=0)
{
Motor_PowerLevelSatatus=0;
Lcd_RefreshCommandStatus(2);//無操作
Motor_SendCammand(Motor_SetMotorPowerLower,Motor_PowerLevelSatatus); //發(fā)送
printf("已經(jīng)向從機發(fā)送了無操作命令\n");
}
else
{
Lcd_RefreshCommandStatus(0);//顯示減速
Motor_PowerLevelSatatus--;
Motor_SendCammand(Motor_SetMotorPowerLower,Motor_PowerLevelSatatus); //發(fā)送
printf("已經(jīng)向從機發(fā)送了減速命令\n");
}
break;
default:break;
}
}
else
{
Motor_PowerLevelPast=Motor_PowerLevelSatatus;
Motor_PowerLevelSatatus=KeyValue;
if(Motor_PowerLevelPast<Motor_PowerLevelSatatus)
{
Lcd_RefreshCommandStatus(1);//顯示提速
printf("已將功率大小信息發(fā)送到從機(提速)\n");
}
if(Motor_PowerLevelPast>Motor_PowerLevelSatatus)
{
Lcd_RefreshCommandStatus(0);//顯示減速
printf("已將功率大小信息發(fā)送到從機(減速)\n");
}
if(Motor_PowerLevelPast==Motor_PowerLevelSatatus)
{
Lcd_RefreshCommandStatus(2);//顯示無操作
printf("已將功率大小信息發(fā)送到從機(無操作)\n");
}
Motor_SendCammand(Motor_SetPowerLevel,Motor_PowerLevelSatatus); //發(fā)送
}
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))//密碼正確,驗證輸入次數(shù)的目的是防止密碼是8個0
{
Key_ClearInputInformation();
Lcd_PageCreate(Lcd_PasswordRightPage);
Motor_SendCammand(Motor_InitESC,1);
DelayMs(5000);//等待電調初始化才能操作
Lcd_PageCreate(Lcd_MainPage);//進入主菜單
printf("已經(jīng)向從機發(fā)送了解鎖命令\n");
}
else
{
Key_ClearInputInformation();
Lcd_PageCreate(Lcd_PasswordFaultPage);//進入輸入密碼出錯頁面
}
break;
default:break;
}
}
else//數(shù)字輸入
{
Key_NumberKeyInputTimes=Key_NumberKeyInputTimes+1;
printf("輸入數(shù)字次數(shù)是:%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)//電調沒完成初始化時用戶不能自行操作,要等待系統(tǒng)完成初始化后自動返回.
{
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;//我規(guī)定了鍵0-9是數(shù)字鍵.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)//檢查是否是數(shù)字輸入
// {
// Key_NumberKeyInputTimes++;//開啟數(shù)字進入次數(shù)計數(shù);
// 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))//密碼正確,驗證輸入次數(shù)的目的是防止密碼是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))//數(shù)字輸入
// 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, 下載次數(shù): 51)
2017-5-10 16:04 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1