📄 use_mouse_driver.c
字号:
/*****************************************************************
* 函数库名称:AVR硬件资源配置函数源文件 *
* 版本: v0.01 *
* 作者: I ROBOT *
* 创建日期: Copyright (C) 2008年10月14日 *
*----------------------------------------------------------------*
* [支持库] *
* 支持库名称:Hardware_Apply_Init.h(AVR硬件资源配置头文件) *
* 支持库版本:v0.01 *
* 支持库说明:与本文件相应的函数声明 *
*----------------------------------------------------------------*
* [版本更新] *
* 更新: I ROBOT *
* 更新日期: *
* 版本: *
*----------------------------------------------------------------*
* [版本历史] *
* v0.01 创建版本,配置AVR硬件资源 *
*----------------------------------------------------------------*
* [使用说明] *
* 1.配置使用的AVR硬件资源 *
*****************************************************************/
/********************
* 头 文 件 配 置 区 *
********************/
# include "USE_Mouse_Driver.h"
# include "UART0.h"
/********************
* 系 统 宏 定 义 *
********************/
/*------------------*
* 常 数 宏 定 义 *
*------------------*/
/*------------------*
* 动 作 宏 定 义 *
*------------------*/
/********************
* 模块结构体定义区 *
********************/
/********************
* 函 数 声 明 区 *
********************/
void Mouse_Hardware_Configure(void);
void Mouse_Init(void);
static void MOUSE_Init_Finish_Configure(void);
static void MCU_Register_Configure(void);
static void MCU_Port_Configure(void);
static UINT16 Group_Frame(UINT8 chData);
static void Transmit_Frame_To_Mouse(UINT8 chData);
static UINT8 Receive_Mouse_Frame(void);
# ifdef USE_ONLY_PIN_CONFIGURE
Mouse *Mouse_Data_Process(void);
# endif
# ifdef USE_PIN_AND_EINT_CONFIGURE
void Insert_Mouse_EINT_ISR_Code(void);
# endif
# ifdef USE_EINT_AND_USART_CONFIGURE
# endif
/********************
* 模块函数声明区 *
********************/
/********************
* 模块变量声明区 *
********************/
/********************
* 全局变量声明区 *
********************/
# ifdef USE_ONLY_PIN_CONFIGURE
# endif
# ifdef USE_PIN_AND_EINT_CONFIGURE
static Mouse g_Mouse_Data = {0,0,FALSE,FALSE};
static BOOL g_b_Get_Mouse_Compelete = FALSE;
# endif
# ifdef USE_EINT_AND_USART_CONFIGURE
# endif
/******************************************************************
* 函 数 定 义 区 *
******************************************************************/
/****************************************
* 函数说明: 鼠标硬件配置函数 *
* 输入 :无 *
* 输出 :无 *
* 调用函数: MCU_Port_Configure() *
* MCU_Register_Configure() *
* Mouse_Init() *
****************************************/
void Mouse_Hardware_Configure(void)
{
MCU_Port_Configure();
Mouse_Init();
MOUSE_Init_Finish_Configure();
// MCU_Register_Configure();
}
/****************************************
* 函数说明: 鼠标初始化函数 *
* 输入 :无 *
* 输出 :无 *
* 调用函数: Transmit_Frame_To_Mouse() *
* Reiceve_Mouse_Frame() *
****************************************/
void Mouse_Init(void)
{
UINT8 chMouse_ACK_1 = 0;
UINT8 chMouse_ACK_2 = 0;
UINT8 chMouse_ACK_3 = 0;
Transmit_Frame_To_Mouse(MOUSE_RESET);
chMouse_ACK_1 = Receive_Mouse_Frame();
chMouse_ACK_2 = Receive_Mouse_Frame();
chMouse_ACK_3 = Receive_Mouse_Frame();
if ((chMouse_ACK_1 == MOUSE_ACK_1)
&& (chMouse_ACK_2 == MOUSE_ACK_2)
&& (chMouse_ACK_3 == MOUSE_ACK_3))
{
UINT8 chMouse_Live_ACK = 0;
Transmit_Frame_To_Mouse(MOUSE_LIVE);
chMouse_Live_ACK = Receive_Mouse_Frame();
/*
UART0_Transmit(chMouse_ACK_1);
UART0_Transmit(chMouse_ACK_2);
UART0_Transmit(chMouse_ACK_3);
UART0_Transmit(chMouse_Live_ACK);
*/
}
}
/****************************************
* 函数说明: 鼠标初始化结束配置函数 *
* 输入 :无 *
* 输出 :无 *
* 调用函数: Transmit_Frame_To_Mouse() *
* Reiceve_Mouse_Frame() *
****************************************/
static void MOUSE_Init_Finish_Configure(void)
{
//TODO:在这里添加鼠标初始化结束的配置
}
/****************************************
* 函数说明: MCU寄存器配置函数 *
* 输入 :无 *
* 输出 :无 *
* 调用函数: 无 *
****************************************/
static void MCU_Register_Configure(void)
{
//TODO:在这里添加寄存器配置
// MCUCR = 0x02;
// GICR = 0x40;
//在这里配置外中断
}
/****************************************
* 函数说明: MCU端口配置函数 *
* 输入 :无 *
* 输出 :无 *
* 调用函数: 无 *
****************************************/
static void MCU_Port_Configure(void)
{
//TODO:在这里添加端口配置
///////////////////////
CLK_FROM_MOUSE;
WRITE_MOUSE_CLK_HIGH;
MOUSE_DATA_FROM_MCU;
WRITE_MOUSE_DATA_HIGH;
///////////////////用宏定义
}
/****************************************
* 函数说明: 数据帧打包函数 *
* 输入 :传输数据 *
* 输出 :数据包 *
* 调用函数: 无 *
****************************************/
static UINT16 Group_Frame(UINT8 chData)
{
//主机到鼠标的通信中,START是由主机单独发送的,所以
//数据包由|DATA0-DATA7|Parity|StopBit|ACK|组成的11位
//与鼠标到主机的通信的数据包格式略有不同
UINT8 chCounter = 0;
UINT16 chParity = 1;
UINT16 chStopBit = 1;
UINT16 chACK = 0;
UINT8 chData_Temp = chData;
//奇校验异或计算
for (chCounter = 0;chCounter < 8;chCounter++)
{
chParity ^= (chData_Temp&(0x01));
chData_Temp >>= 1;
}
chParity <<= 8; //校验位
chStopBit <<= 9;//停止位
chACK <<= 10; //应答位
return ((UINT16)(chACK|chStopBit|chParity|chData));
}
/****************************************
* 函数说明: 传输数据包给鼠标函数 *
* 输入 :传输数据 *
* 输出 :无 *
* 调用函数: Group_Frame() *
****************************************/
static void Transmit_Frame_To_Mouse(UINT8 chData)
{
UINT16 wFrame = Group_Frame(chData);//数据打包成帧
MOUSE_CLK_FROM_MCU;
MOUSE_DATA_FROM_MCU;
WRITE_MOUSE_CLK_LOW;
WRITE_MOUSE_DATA_LOW;//设置数据流方向
{
UINT8 chCounter = 0;
for (chCounter = 0;chCounter < 100;chCounter++)
{
US_DELAY;
}
}
//设置CLK = 0,DATA = 0,延时100US,使鼠标检测到
//主机到鼠标的通信请求并表示主机到MOUSE
//的起始位START
WRITE_MOUSE_CLK_HIGH;
//鼠标上升沿读取数据
CLK_FROM_MOUSE;
//设置数据流方向
{
UINT8 chCounter = 0;
for (chCounter = 0;chCounter < MOUSE_FRAME_SIZE;chCounter++)
{
WAIT_MOUSE_CLK_LOW;
//在CLK下降沿主机设置数据
if (wFrame&BIT(chCounter))
{
WRITE_MOUSE_DATA_HIGH;
}
else
{
WRITE_MOUSE_DATA_LOW;
}
WAIT_MOUSE_CLK_HIGH;
//鼠标在CLK上升沿读取数据
}
}
//发送数据包
}
/****************************************
* 函数说明: 接收鼠标数据包函数 *
* 输入 :无 *
* 输出 :接收的鼠标数据 *
* 调用函数: 无 *
****************************************/
static UINT8 Receive_Mouse_Frame(void)
{
UINT8 chCounter = 0;
UINT8 chMouse_Data = 0;
DATA_FROM_MOUSE;
//设置数据流方向
for (chCounter = 0;chCounter < MOUSE_FRAME_SIZE;chCounter++)
{
WAIT_MOUSE_CLK_LOW;
if (chCounter >=FRAME_DATA0_BIT
&& chCounter <= FRAME_DATA7_BIT)
{
chMouse_Data >>= 1;
chMouse_Data |= ((READ_MOUSE_DATA_)? 0X80:0);
//移位读数 LSB先进
}
WAIT_MOUSE_CLK_HIGH;
}
return chMouse_Data;
}
# ifdef USE_ONLY_PIN_CONFIGURE
//时序问题很重要,指令越多可能赶不上时序要丢帧
Mouse *Mouse_Data_Process(void)
{
UINT8 chMouse_Flag_Byte = 0;
UINT8 chMouse_x_Byte = 0;
UINT8 chMouse_y_Byte = 0;
UINT8 chMouse_z_Byte = 0;
static Mouse Mouse_Data = {0,0,FALSE,FALSE};
chMouse_Flag_Byte = Receive_Mouse_Frame();
chMouse_x_Byte = Receive_Mouse_Frame();
chMouse_y_Byte = Receive_Mouse_Frame();
if (chMouse_Flag_Byte&BIT(MOUSE_X_SIGN))
{
Mouse_Data.x = -(UINT8)(~chMouse_x_Byte+1);
}
else
{
Mouse_Data.y = chMouse_x_Byte;
}
if (chMouse_Flag_Byte&BIT(MOUSE_Y_SIGN))
{
Mouse_Data.y = -(UINT8)(~chMouse_y_Byte+1);
}
else
{
Mouse_Data.y = chMouse_y_Byte;
}
if (chMouse_Flag_Byte&BIT(MOUSE_L_BTN_DOWN))
{
Mouse_Data.b_Left_Btn_Down = TRUE;
}
if (chMouse_Flag_Byte&BIT(MOUSE_R_BTN_DOWN))
{
Mouse_Data.b_Right_Btn_Down = TRUE;
}
//误帧过滤
return &Mouse_Data;
}
# endif
# ifdef USE_PIN_AND_EINT_CONFIGURE
//将得到的数据做一个镜像,通过CPU中函数的处理来处理数据,
//并且设置标志位通知函数处理更新。中断中代码要尽量简短,
//只求数据存储到缓存区中
void Insert_Mouse_EINT_ISR_Code(void)
{
//TODO:在这里添加外中断代码
UINT8 static s_chCounter = 0;
UINT8 static s_chByte_Counter = 0;
UINT8 static s_chMouse_Byte[3] = {0};
if (s_chCounter >= 1 && s_chCounter <= 8)
{
s_chMouse_Byte[s_chByte_Counter] >>= 1;
s_chMouse_Byte[s_chByte_Counter] |= ((READ_MOUSE_DATA_)? 0X80:0);
}
s_chCounter++;
if (s_chCounter == 11)
{
s_chCounter = 0;
s_chByte_Counter++;
if (s_chByte_Counter == 3)
{
s_chByte_Counter = 0;
//////////////////////////////////////////
///////////////////////////////////////////
if (s_chMouse_Byte[0]&BIT(MOUSE_X_SIGN))
{
g_Mouse_Data.x = -(UINT8)(~s_chMouse_Byte[1]+1);
}
else
{
g_Mouse_Data.x = s_chMouse_Byte[1]+1;
}
if (s_chMouse_Byte[0]&BIT(MOUSE_Y_SIGN))
{
g_Mouse_Data.y = -(UINT8)(~s_chMouse_Byte[2]+1);
}
else
{
g_Mouse_Data.y = s_chMouse_Byte[2];
}
if (s_chMouse_Byte[0]&BIT(MOUSE_L_BTN_DOWN))
{
g_Mouse_Data.b_Left_Btn_Down = TRUE;
}
else
{
g_Mouse_Data.b_Left_Btn_Down = FALSE;
}
if (s_chMouse_Byte[0]&BIT(MOUSE_R_BTN_DOWN))
{
g_Mouse_Data.b_Right_Btn_Down = TRUE;
}
else
{
g_Mouse_Data.b_Left_Btn_Down = FALSE;
}
//////////////////////////////////////////
///////////////////////////////////////////
///////////
g_b_Get_Mouse_Compelete = TRUE;//处理完成
///////////
}
}
}
Mouse *Get_Mouse_Data(void)
{
if (g_b_Get_Mouse_Compelete)
{
g_b_Get_Mouse_Compelete = FALSE;
return &g_Mouse_Data;
}
else
{
return NULL;
}
}
# endif
# ifdef USE_EINT_AND_USART_CONFIGURE
# endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -