Hi,
I have already searched through the numerous messages here already regarding using the DS1305 with AVRs. I still seem to be having a problem getting it to play nicely with my AVR Mega168, I have included the code I am using below (avr-gcc) in case anyone can spot what I'm doing wrong.
Running this code simply returns 0 from the SPI interface, no matter what address I try and read or what I write to the address. Note in the code below, write address A0 corresponds to read address 20.
void spiTest() { spiInit(); PORTB |= 1<<2; // Set SS high spiTransferByte (0x8f); // Select Control Register of DS1305 spiTransferByte (0x00); // Enable osc, enable write, disable alarms PORTB &= ~(1<<2); // Set SS low // read write test PORTB |= 1<<2; // Set SS high spiTransferByte (0xA0); // empty data space spiTransferByte (0xff); // set to 255 PORTB &= ~(1<<2); // Set SS low PORTB |= 1<<2; spiTransferByte(0x20); // read address corresponds to A0 write addres rprintf("output= %d \r\n", spiTransferByte(0x00)); PORTB &= ~(1<<2); } void spiInit() { // setup SPI I/O pins sbi(PORTB, 5); // set SCK hi sbi(DDRB, 5); // set SCK as output cbi(DDRB, 4); // set MISO as input sbi(DDRB, 3); // set MOSI as output sbi(DDRB, 2); // SS must be output for Master mode to work // setup SPI interface : // master mode sbi(SPCR, MSTR); // clock speed = Fosc/16 cbi(SPCR, SPR0); sbi(SPCR, SPR1); // select clock phase positive-going in middle of data cbi(SPCR, CPOL); sbi(SPCR, CPHA); // Data order MSB first cbi(SPCR,DORD); // enable SPI sbi(SPCR, SPE); // clear status inb(SPSR); spiTransferComplete = TRUE; } u08 spiTransferByte(u08 data) { // send the given data spiTransferComplete = FALSE; outb(SPDR, data); while(!(inb(SPSR) & (1<<SPIF))); // set flag spiTransferComplete = TRUE; // return the received data return inb(SPDR); }
I have tried applying +5v to MISO pin and output changes from 0 to 255 as expected. I have checked the wiring and everything seems to be ok, no shorts. If anyone can shed any light on the issue here I would be most grateful.
Thanks in advance,
John