NotJessica
Newbie level 2
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:
USART:
Other:
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++);
}