--=====================_1077642336==_ Content-Type: text/plain; charset="us-ascii" Ok, try again, same source, Julian Winpenny(is he on this list?), this time on the F877 At 06:26 PM 24/02/04 +0000, you wrote: >Hi Guys (& Gals ?) > >I have just bought a couple of Philips PCF8753 RTC chips from RS components, >(seemed like a good idea at the time!) but I am having a mare of a time >talking to them on my 16F877A. > >I have the wiring ok (I am absolutely 100% sure of this - after all it is >only 2 wire I2C :) I have followed some of the not-very-helpful philips >datasheets (is this me, or are all philips datasheets hard to read?). >but can't get it to talk to me... > >I am using CCSinfo's PCWH C Compiler. but I can read ASM if someone has any >ideas/code/guidelines/valium... > >Thanks >Jim > >-- >http://www.piclist.com#nomail Going offline? Don't AutoReply us! >email listserv@mitvma.mit.edu with SET PICList DIGEST in the body > -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body --=====================_1077642336==_ Content-Type: text/plain; charset="us-ascii" Content-Disposition: attachment; filename="I2C on F877_msspi.txt" /***************************************************************************/ /* Example implementation of I2C using PIC16F8XX MSSP module. */ /* */ /* */ /* */ /* By : J. Winpenny */ /* Date : 25/9/99 */ /* Version : 1.0 */ /* */ /* */ /* */ /* Mode : I2C MASTER */ /* Hardware : PIC16F877 */ /* */ /* */ /***************************************************************************/ // Configuration Registers for I2C char SSPSTAT@0x94; // In Bank 1 char SSPCON@0x14; // In Bank 0 char SSPCON2@0x91; // In Bank 1 char SSPBUF@0x13; // I2C Buffer char SSPADD@0x93; // I2C Slave Address register // Bits of SSPSTAT #define SMP 7 #define CKE 6 #define D_A 5 #define P 4 #define S 3 #define R_W 2 #define R_W_MASK 0x04 #define UA 1 #define BF 0 // Bits of SSPCON2 #define GCEN 7 #define ACKSTAT 6 #define ACKDT 5 #define ACKEN 4 #define RCEN 3 #define PEN 2 #define RSEN 1 #define SEN 0 // E2C Addresses #define ClockWrite 0xA2 // PCF8583 Clock Write mode #define ClockRead 0xA3 // PCF8583 Clock Read mode #define SetupClock 0x08 // PCF8583 Clock registers #define control 0x00 #define hundredths 0x01 #define seconds 0x02 #define minutes 0x03 #define hours 0x04 #define date 0x05 #define month 0x06 #define timer 0x07 #define alarm_ctrl 0x08 #define alarm_hundreds 0x09 #define alarm_seconds 0x0a #define alarm_minutes 0x0b #define alarm_hours 0x0c #define alarm_date 0x0d #define alarm_month 0x0e #define alarm_timer 0x0f #define ramzero 0x10 // Bits of PIR1 #define PSPIF 7 #define ADIF 6 #define RCIF 5 #define TXIF 4 #define SSPIF 3 #define SSPIF_MASK 0x08 #define CCP1IF 2 #define TMR2IF 1 #define TMR1IF 0 // Bits of SSPCON #define WCOL 7 #define SSPOV 6 #define SSPEN 5 #define CKP 4 #define SSPM3 3 #define SSPM2 2 #define SSPM1 1 #define SSPM0 0 /* Function Prototypes */ void ConfigureI2C(void); void I2CSend( char ); char I2CReceive(void); void I2CAck(void); void I2CNak(void); void I2CStart(void); void I2CStop(void); void I2CWait(void); void SetClock(void); void ReadClock(void); /************************* I2C Routines ************************************/ /**************************************/ /* Configure the MSSP as an I2C Port */ /* For PIC16F877 */ /* */ /* Relevant port pins configured as */ /* Inputs */ /**************************************/ void ConfigureI2C(void) { set_bit( SSPCON, SSPEN ); // Enable I2C mode set_bit( SSPCON, SSPM3 ); // Setup I2C clear_bit( SSPCON, SSPM2 ); clear_bit( SSPCON, SSPM1 ); clear_bit( SSPCON, SSPM0 ); set_bit( STATUS, RP0 ); // *** Register page 1 *** SSPCON2 = 0; clear_bit( SSPSTAT, SMP ); clear_bit( SSPSTAT, CKE ); // Set I2C Levels // Set I2C Speed SSPADD = 10; // 100k at 4Mhz clock clear_bit( STATUS, RP0 ); // *** Register page 0 *** } /***********************/ /* Send a char via I2C */ /***********************/ void I2CSend( char ch ) { SSPBUF = ch; // Load DATA to send I2CWait(); // Wait for completion } /**************************/ /* Receive a char via I2C */ /**************************/ char I2CReceive(void) { set_bit( STATUS, RP0 ); // *** Register page 1 *** while ( SSPSTAT & R_W_MASK ); // Wait for Transmit to end set_bit( SSPCON2, RCEN ); // Enable I2C receiver clear_bit( STATUS, RP0 ); // *** Register page 0 *** I2CWait(); // Wait for data to arrive. return SSPBUF; // Return the data } /**************************/ /* Send an I2C ACK */ /**************************/ void I2CAck(void) { set_bit( STATUS, RP0 ); // *** Register page 1 *** clear_bit( SSPCON2, ACKDT ); // Setup for ACK set_bit( SSPCON2, ACKEN ); // Send ACK clear_bit( STATUS, RP0 ); // *** Register page 0 *** I2CWait(); // Wait for completion } /**************************/ /* Send an I2C ACK */ /**************************/ void I2CNak(void) { set_bit( STATUS, RP0 ); // *** Register page 1 *** set_bit( SSPCON2, ACKDT ); // Setup for NAK set_bit( SSPCON2, ACKEN ); // Send NAK clear_bit( STATUS, RP0 ); // *** Register page 0 *** I2CWait(); // Wait for completion } /**************************/ /* Generate an I2C START */ /**************************/ void I2CStart(void) { set_bit( STATUS, RP0 ); // *** Register page 1 *** set_bit( SSPCON2, SEN ); // Initiate START condition clear_bit( STATUS, RP0 ); // *** Register page 0 *** I2CWait(); // Wait for completion } /***************************/ /* Generate an I2C RESTART */ /***************************/ void I2CRestart(void) { set_bit( STATUS, RP0 ); // *** Register page 1 *** set_bit( SSPCON2, RSEN ); // Initiate START condition clear_bit( STATUS, RP0 ); // *** Register page 0 *** I2CWait(); // Wait for completion } /**************************/ /* Generate an I2C STOP */ /**************************/ void I2CStop(void) { set_bit( STATUS, RP0 ); // *** Register page 1 *** set_bit( SSPCON2, PEN ); // Generate STOP condition clear_bit( STATUS, RP0 ); // *** Register page 0 *** I2CWait(); // Wait for completion } /**************************************/ /* Waits for I2C process to complete */ /**************************************/ void I2CWait(void) { while ( !( PIR1 & SSPIF_MASK ) ); // Wait for Interrupt clear_bit( PIR1, SSPIF ); // Clear Interrupt flag } /*****************************************************************************/ /* Examples of using the above functions */ /*****************************************************************************/ /*****************************************************************/ /* Set the PCF8593 Low Power Clock Calendar */ /* */ /* see : http://www-us.semiconductors.philips.com/index.html */ /* */ /* Illustrates setting the clock */ /* ( NOT Year 2000 tested ) */ /*****************************************************************/ void SetClock(void) { // Example = 25/09/99 at 19:45:00.00 I2CStart(); I2CSend( ClockWrite ); I2CSend( control ); I2CSend( 0x08 ); // Setup the Clock Calendar // to read date & month directly I2CSend( 0x00 ); // Hundredths of seconds I2CSend( 0x00 ); // Seconds I2CSend( 0x45 ); // Minutes I2CSend( 0x19 ); // Hours I2CSend( 0x25 ); // Date ( Bits 7,6 are The Year ( 0-3 ) ) I2CSend( 0x09 ); // Month I2CStop(); // Stop data transfer } /**********************************************/ /* Read the PCF8593 Low Power Clock Calendar. */ /* Display on LCD */ /**********************************************/ void ReadClock(void) { /* Lets waste some registers ! */ /* ( These should really be passed or global ) */ char s; // To save seconds value char m; // To save minutes value char h; // To save hours value char d; // To save days value char mo; // To save month value char nn; // Temporary storage I2CStart(); // Set a START condition I2C Bus I2CSend( ClockWrite ); I2CSend( seconds ); // Setup address ( Then auto increment ) I2CRestart(); I2CSend( ClockRead ); s = I2CReceive(); // Read Seconds I2CAck(); m = I2CReceive(); // Read Minutes I2CAck(); h = I2CReceive(); // Read Hours I2CAck(); d = I2CReceive(); // Read Days I2CAck(); mo = I2CReceive(); // Read Month I2CNak(); I2CStop(); nn = ( d & 0x0f ); // Units of Day d >>= 4 & 0x0f; // Tens of Days LCD_Line_2(); // Goto line 2 of LCD LCD_Write_4_Bit( d + '0' ); LCD_Write_4_Bit( nn + '0' ); LCD_Write_4_Bit( '/' ); nn = ( mo & 0x0f); // Units of Months mo >>= 4 & 0x0f; // Tens of Months LCD_Write_4_Bit( mo + '0' ); LCD_Write_4_Bit( nn + '0' ); LCD_Write_4_Bit( ' ' ); nn = (h & 0x0f); // Units of Hours h >>= 4 & 0x0f; // Tens of Hours LCD_Write_4_Bit( h + '0' ); LCD_Write_4_Bit( nn + '0' ); LCD_Write_4_Bit( ':' ); nn = (m & 0x0f); // Units of Minutes m >>= 4 & 0x0f; // Tens of Minutes LCD_Write_4_Bit( m + '0' ); LCD_Write_4_Bit( nn + '0' ); LCD_Write_4_Bit( '.' ); nn = (s & 0x0f); // Units of Seconds s >>= 4 & 0x0f; // Tens of Seconds LCD_Write_4_Bit( s + '0' ); LCD_Write_4_Bit( nn + '0' ); } -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body --=====================_1077642336==_ Content-Type: text/plain; charset="us-ascii" Regards Roland Jollivet JeM Electric cc PO Box 1460 Kloof 3640 Kwazulu Natal South Africa Tel: +27 31 7024412 Fax: +27 31 7011674 \o Cell: +27 83 255 6017 l> Email: enquiries@caon.co.za < \ -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body --=====================_1077642336==_--