Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC18F2525 SPI/USART interface with ADNS-2610 (optical mouse sensor)

Status
Not open for further replies.

NotJessica

Newbie level 2
Joined
Mar 20, 2011
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,298
I am trying to interface a PIC18F2525 with an optical mouse sensor (the **broken link removed**).

The sensor uses a serial interface. I eventually want to be able to use the sensor to track distance/speed for a MicroMouse project, but for now I am just trying to figure out how to use the sensor. I am starting by trying to force the sensor to stay "awake" which is done by writing 0x01 to address 0x00 to the sensor.

Unfortunately I have been unable to do this. I first tried using the PIC's MSSP/SPI mode, then USART, and lastly the method detailed here.

My attempts are below. Am I going about this the correct way? Is my code even remotely correct? Thanks for any help :)

SPI:
Code:
#include<delays.h>
#include<stdio.h>
#include<p18f2525.h>
#include<spi.h>

#pragma config OSC		= INTIO67
#pragma config FCMEN	= OFF
#pragma config IESO		= OFF
#pragma config PWRT		= OFF
#pragma config BOREN	= OFF
#pragma config BORV		= 3
#pragma config WDT		= OFF
#pragma config WDTPS	= 1
#pragma config CCP2MX	= PORTC
#pragma config PBADEN	= OFF
#pragma config LPT1OSC	= OFF
#pragma config MCLRE	= OFF
#pragma config STVREN	= OFF
#pragma config LVP		= OFF

void delay_1us(int x);

void main(void)
{
	unsigned int x = 0;
	unsigned int i = 0;
	unsigned int y = 0;

	TRISA = 0; // output
//	TRISB = 1; // input
	TRISC = 0; // output

	LATA = 0x00; // clear A

// SSP Config
	TRISCbits.TRISC5 = 0;
		// SDO = out
	TRISCbits.TRISC4 = 1;
		// SDI = in
	TRISCbits.TRISC3 = 0;
		// SCLK = out
//	TRISAbits.TRISA5 = 1;
//

	SSPCON1bits.SSPEN	= 1;
		// enable SSP
	SSPSTATbits.SMP = 1;
		//  input sampled @ end of output time
	SSPSTATbits.CKE = 1;
		// transmit occurs on active->idle
	SSPCON1bits.CKP	= 1;
		// SCLK idle is high
	SSPCON1bits.SSPM3	= 0;
	SSPCON1bits.SSPM2	= 0;
	SSPCON1bits.SSPM1	= 0;
	SSPCON1bits.SSPM0	= 0;
		// SPI Master Mode, clock = Fosc/4

	DisableIntSPI;

	while(1)
	{
		Delay100TCYx(1);
		SSPBUF = 0x80;
		delay_1us(100);
		SSPBUF = 0x81;
		Delay10KTCYx(1);
		while(1);
	}

}

void delay_1us(int x)
{
	unsigned int i = 0;
	unsigned int j = 0;
	for(i = 0; i < x; i++)
		for(j = 0; j < 32; j++);
}

USART:
Code:
#include<delays.h>
#include<stdio.h>
#include<p18f2525.h>
#include"spi.h"

(PRAGMA CONFIG BITS HERE)

void delay_1us(int x);

void main(void)
{
	unsigned int x = 0;
	unsigned int i = 0;
	unsigned int y = 0;

	TRISA = 0; // output
//	TRISB = 1; // input
	TRISC = 0; // ouput
	PORTA = 0; // clear A
	PORTC = 0; // clear C


	TRISCbits.TRISC7 = 1;
		// USART config
	TRISCbits.TRISC6 = 1;
		// USART config

	TXSTAbits.CSRC	= 1;
		// synchronous, master
	TXSTAbits.TX9	= 0;
		// 8-bit transmission
	//TXSTAbits.TXEN	= 1;
		// transmit enable
	TXSTAbits.SYNC	= 1;
		// Synchronous mode
	RCSTAbits.SPEN	= 1;
		// serial port enable
	RCSTAbits.RX9	= 0;
		// 8-bit reception

	BAUDCONbits.SCKP = 1;
		// idle clock is HIGH

	TXREG = 0x00;
		// initialize transmitted info


	while(1)
	{
		Delay100TCYx(1000);
			// delay to allow sensor to start up
		TXSTAbits.TXEN = 1;
			// enable transmission
		TXREG = 0x80;
			// start transmission by loading data to TXREG
// 0b10000000 MSB indicates direction, rest is address
		delay_1us(100);
		TXREG = 0x81;
			// forced awake mode
		while(1);
			// wait forever
	}

}

void delay_1us(int x)
{
	unsigned int i = 0;
	unsigned int j = 0;
	for(i = 0; i < x; i++)
		for(j = 0; j < 32; j++);
}

Other:
Code:
#include<delays.h>
#include<stdio.h>
#include<p18f2525.h>
#include<spi.h>

(PRAGMA CONFIG BITS HERE)

#define MOUSE_CLK PORTCbits.RC3
#define MOUSE_DATA PORTCbits.RC5


void WriteRegister(const register unsigned short int address);
void delay_1us(int x);


void main(void)
{
	unsigned int x = 0;
	unsigned int i = 0;
	unsigned int y = 0;

	TRISA = 0;
	TRISC = 0;

	LATA = 0x00;

	for(x = 0; x < 128; x++)
	{
		MOUSE_CLK = 1;
		MOUSE_CLK = 0;
	}

	WriteRegister(0x80);
	delay_1us(100);
	WriteRegister(0x81);

	while(1);

}

void WriteRegister(const register unsigned short int address)
{
	signed short output = 0;
	signed short i = 0;

	//direction WRITING
	TRISC = 0;
	
	//write address of register to be read
	for(i=7; i!=-1; i--)
	{
		MOUSE_CLK = 0;
		MOUSE_DATA = 1 && ( (address & (1<<i) )>0 );
		MOUSE_CLK = 1;
	}
	
}

void delay_1us(int x)
{
	unsigned int i = 0;
	unsigned int j = 0;
	for(i = 0; i < x; i++)
		for(j = 0; j < 32; j++);
}
 

I have interfaced a ADNS-6090 Gaming Laser Mouse Sensor to a PIC24FJ64GB002 which was simple using SPI
however, the ADNS-2610 to a PIC18 may be more difficult as the ADNS-2610 uses a single pin SDIO for data input and output (rather than seperate MISO and MOSI outut and input pins) - I assume you have read
**broken link removed**

The first thing is to check the signals from the PIC18 using an oscilloscope - if you loop writing to the device you should be able to see the SCK and SDIO signals

this may help
**broken link removed**
 

Thank you very much for the link! And yes, I read the datasheet.
I tried out the **broken link removed** that was in the thread you linked. The code is a manual version of SPI (it doesn't actually use the SPI functions/module on the PIC), but it works :] I am able to read and write to the registers on the ADNS-2610.
 

good to hear it worked ! I have no idea if the normal PIC SPI module would have worked as the ADNS-2610 has a single pin SDIO line rather then seperate MISO and MOSI pins. The ADNS-6090 interfaced to the PIC24 SPI without any problems.
 

Hi there

Sorry to bring this thread back to life but I have been trying to do a similar thing only using the adns2620. I am new to c programming but I do have a bit of experience in basic. For the past few weeks I'v been trying to learn c and in particular how to communicate using SDIO and I2C.
I have been trying for a while now how to get the code from **broken link removed** to work to no avail. The main problem being that I get a number of errors. I was just wondering how you managed to write and read the registers? All I want to do is force wake the sensor and read the x and y direction registers.

Any help would be much appreciated. I have attached the code which I'v been trying with the address to force wake the sensor and read the y register.

PHP:
#include <htc.h>
#include <conio.h>

#define SCK RC0 //create alias for C0 pin
#define SDIO RC1 //create alias for C1 pin



void Delay1(long x) //delay loop to ensure timing is correct
{	
	long y=0;
	while (y < x)
		{
		y++;
		}
}

void PulseClock() //used to clock the data in/out of the ADNS-2610
	{
		SCK=0;
		SCK=0;
		SCK=1;
		SCK=1;
	}

void WriteSensor(int 0x40, int 0x01) //used to write data to the ADNS2610
{
	TRISC=0; //set all pins in PORTC to output
	SCK=1;
	SDIO=0;

	SDIO=1; //bit 7 is set for write instructions
	PulseClock();

}

int ReadSensor(int 0x42)// Used to read registers of the ADNS2610
{
	TRISC=0; //set all pins in PORTC to output
	SCK=1;
	SDIO=0;

	int x=0;
	int y=128;
	
	while (x<8)
	{

		if (0x42>=y)
		{SDIO=1;
		0x42=0x42-y;}
		else
		{SDIO=0;}	
		PulseClock();

		y=y/2;
		x++;
	}

	x=0;
	y=128;
	int Data=0;

	SDIO=0;
	TRISC=2; //set SDIO to input

	Delay1(50); //This delay is madatory to give the ADNS-2610 time to load the data

	while (x<8)
	{
		PulseClock();
		if (SDIO==1) 
		{Data=Data+y;}

		y=y/2;
		x++;

	}	

	TRISC=0;
	SCK=1;
	SDIO=0;

	return Data;
}

And these are the errors I am getting.

PHP:
Error   [194] C:\Documents and Settings\awybrown\Desktop\clarkemouse2.c; 25.18 ")" expected
Warning [374] C:\Documents and Settings\awybrown\Desktop\clarkemouse2.c; 28.4 missing basic type; int assumed
Error   [984] C:\Documents and Settings\awybrown\Desktop\clarkemouse2.c; 28.4 type redeclared
Error   [1098] C:\Documents and Settings\awybrown\Desktop\clarkemouse2.c; 28.4 conflicting declarations for variable "RC0" (C:\Program Files\HI-TECH Software\PICC\9.70\include\pic16f685.h:148)
Warning [374] C:\Documents and Settings\awybrown\Desktop\clarkemouse2.c; 29.4 missing basic type; int assumed
Error   [984] C:\Documents and Settings\awybrown\Desktop\clarkemouse2.c; 29.4 type redeclared
Error   [1098] C:\Documents and Settings\awybrown\Desktop\clarkemouse2.c; 29.4 conflicting declarations for variable "RC1" (C:\Program Files\HI-TECH Software\PICC\9.70\include\pic16f685.h:149)
Warning [374] C:\Documents and Settings\awybrown\Desktop\clarkemouse2.c; 31.4 missing basic type; int assumed
Error   [984] C:\Documents and Settings\awybrown\Desktop\clarkemouse2.c; 31.4 type redeclared
Error   [1098] C:\Documents and Settings\awybrown\Desktop\clarkemouse2.c; 31.4 conflicting declarations for variable "RC1" (C:\Documents and Settings\awybrown\Desktop\clarkemouse2.c:29)
Error   [1098] C:\Documents and Settings\awybrown\Desktop\clarkemouse2.c; 31.4 conflicting declarations for variable "RC1" (C:\Documents and Settings\awybrown\Desktop\clarkemouse2.c:29)
Error   [984] C:\Documents and Settings\awybrown\Desktop\clarkemouse2.c; 32.13 type redeclared
Error   [1098] C:\Documents and Settings\awybrown\Desktop\clarkemouse2.c; 32.13 conflicting declarations for variable "PulseClock" (C:\Documents and Settings\awybrown\Desktop\clarkemouse2.c:18)
Error   [285] C:\Documents and Settings\awybrown\Desktop\clarkemouse2.c; 34.1 no identifier in declaration
Warning [374] C:\Documents and Settings\awybrown\Desktop\clarkemouse2.c; 34.1 missing basic type; int assumed
Error   [314] C:\Documents and Settings\awybrown\Desktop\clarkemouse2.c; 34.1 ";" expected
Error   [194] C:\Documents and Settings\awybrown\Desktop\clarkemouse2.c; 36.21 ")" expected
Warning [374] C:\Documents and Settings\awybrown\Desktop\clarkemouse2.c; 39.4 missing basic type; int assumed
Error   [984] C:\Documents and Settings\awybrown\Desktop\clarkemouse2.c; 39.4 type redeclared
Error   [1098] C:\Documents and Settings\awybrown\Desktop\clarkemouse2.c; 39.4 conflicting declarations for variable "RC0" (C:\Documents and Settings\awybrown\Desktop\clarkemouse2.c:28)
Error   [1098] C:\Documents and Settings\awybrown\Desktop\clarkemouse2.c; 39.4 conflicting declarations for variable "RC0" (C:\Documents and Settings\awybrown\Desktop\clarkemouse2.c:28)
Warning [374] C:\Documents and Settings\awybrown\Desktop\clarkemouse2.c; 40.4 missing basic type; int assumed
Error   [984] C:\Documents and Settings\awybrown\Desktop\clarkemouse2.c; 40.4 type redeclared
Error   [1098] C:\Documents and Settings\awybrown\Desktop\clarkemouse2.c; 40.4 conflicting declarations for variable "RC1" (C:\Documents and Settings\awybrown\Desktop\clarkemouse2.c:31)
Error   [1098] C:\Documents and Settings\awybrown\Desktop\clarkemouse2.c; 40.4 conflicting declarations for variable "RC1" (C:\Documents and Settings\awybrown\Desktop\clarkemouse2.c:31)
Error   [285] C:\Documents and Settings\awybrown\Desktop\clarkemouse2.c; 45.1 no identifier in declaration
Warning [374] C:\Documents and Settings\awybrown\Desktop\clarkemouse2.c; 45.1 missing basic type; int assumed
Error   [314] C:\Documents and Settings\awybrown\Desktop\clarkemouse2.c; 45.1 ";" expected
Advisory[1] too many errors (21)

Thanks.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top