-
Notifications
You must be signed in to change notification settings - Fork 0
/
adns3080.c
117 lines (89 loc) · 2.59 KB
/
adns3080.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
#include "main.h"
#include "adns3080.h"
#include "spi.h"
void adns3080_init(void)
{
ADNS3080_PORT.DIRSET = PIN7_bm | PIN5_bm | PIN4_bm;
ADNS3080_PORT.DIRCLR = PIN6_bm;
spi_init(&ADNS3080_SPI);
ADNS3080_CS_H;
}
unsigned char adns3080_read_reg(unsigned char addr)
{
unsigned char rev;
ADNS3080_CS_L; asm("nop\n"); asm("nop\n");
spi_writeread(&ADNS3080_SPI, addr); _delay_us(50);
rev = spi_writeread(&ADNS3080_SPI, 0x00);
ADNS3080_CS_H;
return rev;
}
void adns3080_write_reg(unsigned char addr, unsigned char val)
{
ADNS3080_CS_L; asm("nop\n"); asm("nop\n");
spi_writeread(&ADNS3080_SPI, addr | 0x80); _delay_us(50);
spi_writeread(&ADNS3080_SPI, val);
ADNS3080_CS_H;
}
void adns3080_motion(unsigned char *motion,
signed char *dx, signed char *dy,
unsigned char *squal,
unsigned char *shutter_upper, unsigned char *shutter_lower,
unsigned char *max_pixel)
{
ADNS3080_CS_L; asm("nop\n"); asm("nop\n");
spi_writeread(&ADNS3080_SPI, A_MOTION_BURST); _delay_us(25);
*motion = spi_writeread(&ADNS3080_SPI, 0x00);
*dx = spi_writeread(&ADNS3080_SPI, 0x00);
*dy = spi_writeread(&ADNS3080_SPI, 0x00);
*squal = spi_writeread(&ADNS3080_SPI, 0x00);
*shutter_upper = spi_writeread(&ADNS3080_SPI, 0x00);
*shutter_lower = spi_writeread(&ADNS3080_SPI, 0x00);
*max_pixel = spi_writeread(&ADNS3080_SPI, 0x00);
ADNS3080_CS_H;
_delay_us(4);
}
void adns3080_capture(void)
{
unsigned int i = 0;
unsigned char start = 0;
unsigned char reg = 0x00;
adns3080_write_reg(A_FRAME_CAPTURE, 0x83);
_delay_ms(2);
ADNS3080_CS_L; asm("nop\n"); asm("nop\n");
spi_writeread(&ADNS3080_SPI, A_PIXEL_BURST); _delay_us(55);
// printf("--->");
for (;;)
{
reg = spi_writeread(&ADNS3080_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++;
}
ADNS3080_CS_H;
_delay_us(4);
}