#include "ANDS3080.h"
#include "spi.h"
#include "usart.h"
#include "srom.h"
#include "delay.h"
#define H 200 //oáÃ×
u8 success,revision;
#define ANDS_NOP_H() GPIO_SetBits(GPIOA, GPIO_Pin_12)
#define ANDS_NOP_L() GPIO_ResetBits(GPIOA, GPIO_Pin_12)
#define ANDS_RESET_H() GPIO_SetBits(GPIOA, GPIO_Pin_8)
#define ANDS_RESET_L() GPIO_ResetBits(GPIOA, GPIO_Pin_8)
#define ANDS_NCS_H() GPIO_SetBits(GPIOB, GPIO_Pin_12)
#define ANDS_NCS_L() GPIO_ResetBits(GPIOB, GPIO_Pin_12)
/*
Nop ---A12
reset--A8
NCS----B12
MOSI---PA7
MISO---PA6
SCLK---PA5
*/
/**********************************************************************************
oˉêyÔ-Dí£ovoid ADNS_3080_GPIO_Configuration(void)
1|Äü£oμ÷óøÃoˉêyÔò½øDDANDS3080ËùóÃIO¿ú½øDD3õê¼»ˉ Nop ËˉÃßÏà1Ø¡¢Ncs Ƭѡ¡¢Reset ¸′λ
êäèë£o
êä3ö£o
**********************************************************************************/
void ADNS_3080_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8| GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_Out_PP; //íÆíìêä3ö
GPIO_Init(GPIOA, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_Out_PP; //íÆíìêä3ö
GPIO_Init(GPIOB, &GPIO_InitStructure);
ANDS_NCS_H();
}
/**********************************************************************************
oˉêyÔ-Dí£ovoid ADNS3080_reset(void)
1|Äü£oμ÷óøÃoˉêyÔò½øDDANDS3080½øDD¸′λ2ù×÷
êäèë£o
êä3ö£o
**********************************************************************************/
void ADNS3080_reset(void) //ADNS3080 ¸′루¸ß£©
{
ANDS_RESET_L();
delay_ms(5);
ANDS_RESET_H();
delay_ms(5);
ANDS_RESET_L();//Âö3åDÅoÅ
}
/**********************************************************************************
oˉêyÔ-Dí£ovoid Write_srom(void)
1|Äü£oANDS3080í»·¢D′Ä£ê½
êäèë£o
êä3ö£o
**********************************************************************************/
void Write_srom(void)
{
int i;
ANDS_NCS_L(); //à-μíƬѡ£¬¿aê¼′«êä
writr_register(0x20,0x44);
delay_us(51);
writr_register(0x23,0x07);
delay_us(51);
writr_register(0x24,0x88);
delay_us(51);
ANDS_NCS_H(); //à-¸ßƬѡ£¬í£Ö1′«êä
delay_us(340);//μè′y′óóú1Ö¡ê±¼ä
ANDS_NCS_L(); //à-μíƬѡ£¬¿aê¼′«êä
writr_register(SROM_Enable,0x18);
ANDS_NCS_H(); //à-¸ßƬѡ£¬í£Ö1′«êä
delay_us(41);// >40us
ANDS_NCS_L(); //à-μíƬѡ£¬¿aê¼′«êä
for(i=0;i<=1985;i++)
{
writr_register(0x60,SROM[i]);
delay_us(11);// >10us
}
//OFF_CS();
delay_us(105);//>104us
ANDS_NCS_H(); //à-¸ßƬѡ£¬í£Ö1′«êä
}
/**********************************************************************************
oˉêyÔ-Dí£ovoid ADNS3080_Init(void)
1|Äü£oμ÷óøÃoˉêyÔò½øDDANDS3080×üμÄ3õê¼»ˉ
êäèë£o
êä3ö£o
**********************************************************************************/
void ADNS3080_Init(void)
{
ADNS_3080_GPIO_Configuration();
ADNS3080_reset(); //¸′λ
ANDS_NOP_H(); //à-¸ßNOP,ÃaËˉÃß
delay_ms(10);
Write_srom();
ADNS_Configuration();
revision=read_register(0x01);
// printf("revision = %x\n",revision);
success=read_register(0x1f); //2é¿′êÇ·ñÏÂÔØ3é1|
// printf("success = %x\n",success);
}
/**********************************************************************************
oˉêyÔ-Dí£ovoid ADNS_Configuration(void)
1|Äü£oμ÷óøÃoˉêyÔò½øDDANDS3080ÅäÖÃ
êäèë£o
êä3ö£o
**********************************************************************************/
void ADNS_Configuration(void)
{
ANDS_NCS_L();
writr_register(Configuration_bits,0x10);//éèÖ÷ֱæÂê 1600 //èôBit 4Îa0£¬ÔòÎa400μãÿó¢′ç
delay_ms(3);
writr_register(Extended_Config,0x01);
delay_ms(3);
if(read_busy()!=1)
{
//éèÎa3000֡ÿÃë
ANDS_NCS_H(); //í»·¢_D′Ä£ê½
delay_ms(2);
ANDS_NCS_L();
Spi1_SendReceive(Frame_Period_Max_Bound_Lower+0x80);//éèÖÃÖ¡Âê //ÏèD′μíλÔùD′¸ßλ
Spi1_SendReceive(0xe0); // 0x40 C0 5000Ö¡Âê
Spi1_SendReceive(Frame_Period_Max_Bound_Upper+0x80);
Spi1_SendReceive(0x1a); //0x1f 12
}
clear_motion();
ANDS_NCS_H();
}
/**********************************************************************************
oˉêyÔ-Dí£ou8 read_register(u8 adress)
1|Äü£oμ÷óøÃoˉêyÔò½øDDANDS3080Ö¸¶¨μØÖ·¶áè¡
êäèë£o
êä3ö£o
**********************************************************************************/
u8 read_register(u8 adress)
{
u8 temp;
ANDS_NCS_L();//Ƭѡê1Äü£¬×üÏßÑ¡ÖDANDS3080
temp=Spi1_SendReceive(adress+0x00);//¶á
delay_us(75);
temp=Spi1_SendReceive(0xff);//ìá1©ê±ÖóDÅoÅ_¶á
ANDS_NCS_H();//Ƭѡí£Ö1ê1Äü£¬ANDS3080àëÏß
return temp;
}
/**********************************************************************************
oˉêyÔ-Dí£ovoid ADNS_Configuration(void)
1|Äü£oμ÷óøÃoˉêyÔò½øDDANDS3080ÅäÖÃ
êäèë£o
êä3ö£o
**********************************************************************************/
void writr_register(u8 adress,u8 vlue)
{
ANDS_NCS_L();
Spi1_SendReceive(adress+0x80);
Spi1_SendReceive(vlue);
ANDS_NCS_H();
delay_us(51);
}
/**********************************************************************************
oˉêyÔ-Dí£ovoid ADNS_Configuration(void)
1|Äü£oμ÷óøÃoˉêyÔò½øDDANDS3080ÅäÖÃ
êäèë£o
êä3ö£o
**********************************************************************************/
u8 read_busy(void)//D′Ö¡ÂêμÄÅDÃ| ==1Ã|
{
u8 temp;
ANDS_NCS_L();
temp=Spi1_SendReceive(Extended_Config+0x00);
delay_us(75);
temp=Spi1_SendReceive(0xff);
temp&=0x80;
ANDS_NCS_H();
return temp;
}
/**********************************************************************************
oˉêyÔ-Dí£ovoid ADNS_Configuration(void)
1|Äü£oμ÷óøÃoˉêyÔò½øDDANDS3080ÅäÖÃ
êäèë£o
êä3ö£o
**********************************************************************************/
void clear_motion(void)
{
ANDS_NCS_L();
Spi1_SendReceive(Motion_Clear+0x80);
Spi1_SendReceive(0xff);//Çå3yX Yêy¾Y
ANDS_NCS_H();
}
/**********************************************************************************
oˉêyÔ-Dí£ovoid Get_Move(int move[2])
1|Äü£oμ÷óøÃoˉêyÔò½øDDANDS3080ÅäÖÃ
êäèë£omoveêy×飬move[0]ÎaXÖáμÄËù¶è£¬move[1]ÎaYÖáμÄËù¶è
êä3ö£o
**********************************************************************************/
void Get_Move(int move[2])
{
unsigned char flag=0;
move[0] = 0;
move[1] = 0;
ANDS_NCS_L();//Ƭѡê1Äü£¬×üÏßÑ¡ÖDANDS3080
Spi1_SendReceive(0x50);
delay_us(75);
flag=Spi1_SendReceive(0xFF); //òç3ö±ê־λ
move[0]=Spi1_SendReceive(0xFF); //¶áè¡XÖáμÄËù¶è
move[1]=Spi1_SendReceive(0xFF); //¶áè¡YÖáμÄËù¶è
if(move[0]&0x80)
{
//21Âë×a»»
move[0]-=256;
}
if(move[1]&0x80)
{
//21Âë×a»»
move[1]-=256;
}
ANDS_NCS_H();
delay_us(4);
ANDS_NCS_H();
if((flag&0x10)!=1)//êÇ·ñòç3ö
{
if(flag&0x80) //ûóDòç3öÔò′òó¡
{
//printf("x=%d,y=%d\n",SumX,SumY);
}
else //òç3öÔò½«x¡¢yÖáËù¶èÇåáã
{
move[0]=0;
move[1]=0;
}
flag = 0;
// delay_ms(10);
}
}
|