gaurav yadav
Newbie level 6
- Joined
- Mar 29, 2010
- Messages
- 12
- Helped
- 1
- Reputation
- 2
- Reaction score
- 1
- Trophy points
- 1,283
- Location
- gaziabad,india
- Activity points
- 1,366
basically i want help on SPI
but i m trying to interface adns 2610 optical mouse sensor with atmega 16 using SPI but its not responding
soo can somebody help me ths is my code
but i m trying to interface adns 2610 optical mouse sensor with atmega 16 using SPI but its not responding
soo can somebody help me ths is my code
Code:
define F_CPU 11059200UL
#include<avr/io.h>
#include<util/delay.h>
//DEFINING THE SPI PORT OF MICROCONTROLLER
#define DDR_SPI DDRB
#define SCK 7
#define MISO 6
#define MOSI 5
void INIT_SPI(void);//initializing the spi of the uP
void ADNS_WRITE(unsigned char,unsigned char);//writing to adns (data,address)
unsigned char ADNS_READ(unsigned char);//reading from adns inputting the address of register and getting data
void SPI_INIT(void) //INTIALIZATION
{
SPCR=(1<<SPE)|(1<<MSTR);
SPCR|=(0<<CPOL);// CLOCK TRALLING EDGE IS 1, LEADING EDGE IS 0
SPCR|=(1<<SPR0);// SELECTING MODE 2 FOR SCLK (Fosc/16)
SPCR|=(0<<DORD);//MSB FIRST
SPCR|=(1<<CPHA);//SETING BIT ON FALLING EDGE OF SCLK
DDR_SPI|=(0<<MOSI)|(0<<MISO);
PORTB|=(1<<MISO)|(1<<MOSI);// MISO AND MOSI IN HI Z
DDRB=(1<<SCK);//MAKING SCK OUTPUT AFTER MOSI AND MISO IS in Hi Z
// SO THAT NO SYNC PROBLEM
}
// MINIMUM DIFF B/W TWO WRITE SHOULD BE 100usec
void ADNS_WRITE(unsigned char addr,unsigned char data)
{
addr|=(1<<7);//MSB OF ADDRESS SHOULD BE 1 IN WRITE OP
SPDR=addr;
DDR_SPI|=(1<<MOSI);//OPENING MOSI
while(!(SPSR & (1<<SPIF)))
{}
SPDR=data;
while(!(SPSR & (1<<SPIF)))
{}
DDR_SPI|=(0<<MOSI);//
PORTB|=(1<<MISO)|(1<<MOSI);//TRISTATING THE MISO AND MOSI
}
// THE MINIMUM DIFFERENCE BETWEEN TWO READ OR WRITE SHOULD BE 250ns
unsigned char ADNS_READ(unsigned char addr1)
{
addr1|=(1<<7 ); //MSB OF ADDRESS SHOULD BE 0 IN READ OP
SPDR=addr1;
DDR_SPI|=(1<<MOSI);
while(!(SPSR & (1<<SPIF)))
{}
//HOLD LAST BIT OF ADDRESS FOR MORE THAN 250ns
DDR_SPI|=(0<<MOSI);
PORTB|=(1<<MOSI);//MAKING MISO HI Z
_delay_ms(1);//DELAY SO THAT ADNS-2610 CAN PREPARE DATA
PORTB=(0<<MISO);//CHANGI MISO FROM TRISATE TO INPUT PIN
while(!(SPSR & (1<<SPIF)))
{}
return SPDR;
}[color=blue][/color]