-
Notifications
You must be signed in to change notification settings - Fork 0
/
adns9800.c
152 lines (112 loc) · 3.32 KB
/
adns9800.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "main.h"
#include "adns9800.h"
#include "spi.h"
#include "io.h"
volatile unsigned char MOTION = 0;
ISR (ADNS9800_MOTION_vect)
{
LED_PORT.OUTTGL = LED_DIAG;
// adns9800_write_reg(A_MOTION, 0x00);
MOTION = 1;
}
void adns9800_init(void)
{
unsigned char reg;
ADNS9800_PORT.DIRSET = PIN7_bm | PIN5_bm | PIN4_bm;
ADNS9800_PORT.DIRCLR = PIN6_bm;
ADNS9800_PORT.DIRCLR = ANDS9800_MOTION_PIN;
ADNS9800_PORT.ADNS9800_MOTION_PINCTRL = PORT_ISC_FALLING_gc;
ADNS9800_PORT.INT0MASK |= ANDS9800_MOTION_PIN;
ADNS9800_PORT.INTCTRL |= PORT_INT0LVL_LO_gc;
spi_init(&ADNS9800_SPI);
ADNS9800_CS_H;
_delay_ms(100);
adns9800_write_reg(A_POWER_UP_RESET, 0x5a);
_delay_ms(100);
adns9800_read_reg(A_MOTION);
adns9800_read_reg(A_DX_L);
adns9800_read_reg(A_DX_H);
adns9800_read_reg(A_DY_L);
adns9800_read_reg(A_DY_H);
reg = adns9800_read_reg(A_LASER_CTRL0);
reg &= 0xf0;
adns9800_write_reg(A_LASER_CTRL0, reg);
}
unsigned char adns9800_read_reg(unsigned char addr)
{
unsigned char rev;
ADNS9800_CS_L; asm("nop\n"); asm("nop\n");
spi_writeread(&ADNS9800_SPI, addr); _delay_us(50);
rev = spi_writeread(&ADNS9800_SPI, 0x00);
ADNS9800_CS_H;
return rev;
}
void adns9800_write_reg(unsigned char addr, unsigned char val)
{
ADNS9800_CS_L; asm("nop\n"); asm("nop\n");
spi_writeread(&ADNS9800_SPI, addr | 0x80); _delay_us(50);
spi_writeread(&ADNS9800_SPI, val);
ADNS9800_CS_H;
}
void adns9800_motion(unsigned char *motion, unsigned char *observation,
signed short *dx, signed short *dy,
unsigned char *squal)
{
unsigned char reg;
ADNS9800_CS_L; asm("nop\n"); asm("nop\n");
spi_writeread(&ADNS9800_SPI, A_MOTION_BURST); _delay_us(25);
*motion = spi_writeread(&ADNS9800_SPI, 0x00);
*observation = spi_writeread(&ADNS9800_SPI, 0x00);
reg = spi_writeread(&ADNS9800_SPI, 0x00); /* dx_l */
*dx = (spi_writeread(&ADNS9800_SPI, 0x00) << 8) | reg;
reg = spi_writeread(&ADNS9800_SPI, 0x00);
*dy = (spi_writeread(&ADNS9800_SPI, 0x00) << 8) | reg;
*squal = spi_writeread(&ADNS9800_SPI, 0x00);
ADNS9800_CS_H;
_delay_us(1);
}
void adns9800_capture(void)
{
unsigned int i = 0;
unsigned char start = 0;
unsigned char reg = 0x00;
adns9800_write_reg(A_FRAME_CAPTURE, 0x83);
_delay_ms(2);
ADNS9800_CS_L; asm("nop\n"); asm("nop\n");
spi_writeread(&ADNS9800_SPI, A_PIXEL_BURST); _delay_us(55);
// printf("--->");
for (;;)
{
reg = spi_writeread(&ADNS9800_SPI, 0x00);
if (!start && (reg & 0x80) && (reg & 0x40))
{
start = 1;
reg <<= 2;
// printf("%02x ", reg);
CDC_Device_SendByte(&CDC_IF, reg);
}
else if (start)
{
reg <<= 2;
// printf("%02x ", reg);
CDC_Device_SendByte(&CDC_IF, reg);
_delay_us(1);
if (i >= 900)
{
// printf("\n****\n");
break;
}
}
else
{
if (i > 32)
{
printf("\nfailed to find img head\n");
break;
}
}
i++;
}
ADNS9800_CS_H;
_delay_us(4);
}