Hello, I'm trying to bitbang communication from a 16F877A (running at 4 MHz) to a DS1302, and I haven't managed to get anything out of the DS1302. I'm using Microchip MPLAB with Hi-Tech PICC-lite 9.50. I'm using delay functions by Shane Tolmie from http://microchipc.com/sourcecode/#delay. My program is included. So far, I have: Read and applied the "gotchas" page (http://www.piclist.com/techref/microchip/gotchas.htm) Checked the connections (at least 10 times). Replaced the 32.786 kHz crystal with one that is known to have the correct capacitance (new from Digikey). Replaced the DS1302. Read the data sheet multiple times. Searched the PicList archive (I found very few mentions of the DS1302). I have put a scope on the clock and data lines, and as far as I can tell, the proper command byte is being sent. You can see it at http://trevyn.ca/sclkio.jpg The command byte is being transmitted in the way that I intend (but that may not be the way the DS1302 expects it). There are 8 falling edges after the last bit of the command byte. My only ideas are: 1. The command byte I am sending is malformed/incorrect. I can't figure out how to translate a data address (range 81h-FFh) into a 5 bit number that goes in the middle of the command byte. 2. I'm connecting the DS1302 wrong. I still haven't had any confirmation that the chip actually works. I've seen nothing out of it. 3. I am assuming that the counter starts when power is applied, and that reading it every half second will give some sort of output after one second. If this is not the case, then what I am seeing is perfectly normal. Basically, my questions are: 1. Should I use a different chip? (I need one that is available in 8-SOIC) 2. Is there a better way to talk to the DS1302? 3. If not, what am I doing wrong? Thanks for reading. I appreciate any help or ideas you have. Trevyn Watson /* A binary clock using DS1302. 17 LEDs are connected to the digital IO pins. Serial interface to DS1302 is bitbanged. Output of DS1302 is in packed BCD and needs to be converted to binary. Hours on PORTA Minutes on PORTC Seconds on PORTB Hour set on RB6 Minute set on RB7 CE on RA5. Output. I/O on RC6. Bidirectional. SCLK on RC7. Output. Frequency should not be more than 500 kHz. 1/500 kHz = 2 us. */ #include #include "delay.c" #include #define HOURS PORTA #define MINUTES PORTC #define SECONDS PORTB // defs for DS1302 #define CE RA5 #define IO RC6 #define SCLK RC7 __CONFIG(WDTDIS & XT & LVPDIS & BORDIS); // Config fuses. Disable WDT, osc is crystal <= 4 MHz, disable LVP, disable brown-out reset void Setup(void); char GetTime(char); char BCD2bin(char, char); void main() { Setup(); LCD_init(); RA0 = 1; RB0 = 1; DelayS(1); while(1) { CE = 0; SCLK = 0; IO = 0; // read the time. Seconds first. SECONDS = GetTime(0b10000011); //command byte. Clock data, register A0, read. write the next byte, which is hopefully seconds, to PORTB. need to convert packed BCD to binary first DelayBigMs(500); } } // new way, closely matches fig 5 in data sheet char GetTime(char command) { int bits = 0; char response = 0; RA1 = 1; TRISC = 0b00000000; //set IO (RC6) to output. BAD. FIX. CE = 1; // "Driving the CE input high initiates all data transfers." for(bits = 0; bits < 8; bits++) { SCLK = 0; IO = (bit)(command >> bits); dly1u; SCLK = 1; dly1u; //gives a SCLK frequency of ~500 kHz, i hope } IO = 0; TRISC = 0b01000000; //set IO (RC6) to input. BAD. FIX. for(bits = 0; bits < 7; bits++) { SCLK = 0; dly1u; SCLK = 1; response += ((char)IO << 7); response >> 1; dly1u; } SCLK = 0; CE = 0; RA2 = 1; return response; } void Setup() // setup adc, tris, ints, peripherals, etc { TRISA = 0; TRISB = 0; TRISC = 0; TRISD = 0; TRISE = 0; // set all pins to output PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; // set all pins to 0 RBPU = 0; // enable PORTB pull-ups ADCON1 = 0b00000110; // turn off adc, all pins digital } -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist