Autor Tema: PCF8574A, EXPANSOR 8BITS DE BUS I2C PROTON  (Leído 3676 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado Noelillo

  • PIC12
  • **
  • Mensajes: 94
PCF8574A, EXPANSOR 8BITS DE BUS I2C PROTON
« en: 29 de Noviembre de 2013, 02:13:17 »
Os dejo un pequeño programa para el control del circuito PCF8574.

A1--- +5VCC
A2--- GND
A3 --- GND

Código: [Seleccionar]
'****************************************************************
'*  Name    : UNTITLED.BAS                                      *
'*  Author  : [select VIEW...EDITOR OPTIONS]                    *
'*  Notice  : Copyright (c) 2013 [select VIEW...EDITOR OPTIONS] *
'*          : All Rights Reserved                               *
'*  Date    : 23/11/2013                                        *
'*  Version : 1.0                                               *
'*  Notes   :                                                   *
'*          :                                                   *
'****************************************************************
 Device = 16F877A
XTAL 20
' LCD pin Configuracion
        LCD_TYPE = 0
        LCD_LINES = 4
        LCD_DTPIN = PORTB.4
        LCD_RSPIN = PORTB.2
        LCD_ENPIN = PORTB.3
        LCD_INTERFACE = 4
        ALL_DIGITAL = On   
 ' Send a byte to the I2C bus.
 Cls
 Dim VAR1 As Byte                     ' We'll only read 8-bits
 Dim VAR2 As Byte 
 Dim Address As Word                 ' 16-bit address required
 Dim n       As Byte
 Dim resultado As Byte
                  '12345678 
 Symbol Control = %01110010           ' PARA EL PCF8574 UTILIZAR 01000010 ESCRITURA
 Symbol SDA = PORTD.0           ' Alias the SDA (Data) line
 Symbol SCL = PORTD.1           ' Alias the SSL (Clock) line
                          '
  VAR2 = %11111111
 For n= 0 To 255
 VAR1 = n
 Print At 1,1,BIN8 VAR1
  resultado = VAR1 ^ VAR2                           '
 I2COUT SDA, SCL, Control , [resultado ]  ' Send the byte to the eeprom
 ' BusOut Control , Address , [ VAR1 ]
 
 Print At 1,14,DEC3 n
 Print At 2,1,BIN8 VAR1
 DelayMS 100                         ' Allow time for allocation of byte
 Next n
 'BusOut Control , Address , [ VAR1 ]
End
 

OUT PCF8574A ---------  LED (-) ---- R 330 ---- + 5VCC


Desconectado rotting79

  • PIC12
  • **
  • Mensajes: 91
Re: PCF8574A, EXPANSOR 8BITS DE BUS I2C PROTON
« Respuesta #1 en: 07 de Febrero de 2014, 02:18:07 »
Sorry, I haven't use the Proton compiler....

...but right here is a small library and code to use PCF8574 and XC8 (and for this code, the 16F8xx family)
hope u can find it useful!!

Main.c
Código: C
  1. #include <xc.h>
  2.  
  3. #define _XTAL_FREQ      FREQ_8MHz
  4.  
  5. #include "SetUp.c"
  6.  
  7. #define PCF8574_ADDRESS         0x40                    // high nibble: 0100 mandatory.
  8.  
  9. int main ( string [ ], args )   // @ 0x10
  10. {
  11.         unsigned char readI2CPORT;
  12.         SetUp ( );
  13.        
  14.         printf ( "Application Ready...\n" );
  15.  
  16.         StartI2C ( );
  17.         I2CAddressCommand ( PCF8574_ADDRESS, I2C_WRITE );
  18.         WriteI2C ( 0x10 );
  19.         StopI2C ( );
  20.  
  21.         __delay_ms ( 15 );
  22.        
  23.         StartI2C ( );
  24.         I2CAddressCommand ( PCF8574_ADDRESS, I2C_READ );
  25.         readI2CPORT = ReadI2C ( MASTER_NACK );
  26.         StopI2C ( );
  27.  
  28.         printf ( "I2C-PORT: 0x%X\n", readI2CPORT );
  29. //      EnableInterrupt ( GLOBAL_INTERRUPT );
  30.  
  31.         while ( TRUE )
  32.         {
  33.                 }
  34.  
  35.         return 0x00;
  36.         }

SetUp.c
Código: C
  1. /* CONFIG1 ************************/
  2. #pragma config  LVP             = OFF   // RB3 pin is digital I/O.
  3. #pragma config  FCMEN   = OFF   // Fail-Safe Clock Monitor is disabled.
  4. #pragma config  IESO    = OFF   // Internal/External Switchover mode is disabled.
  5. #pragma config  BOREN   = ON    // BOR enabled.
  6. #pragma config  CPD             = ON    // Data memory code protection is enabled.
  7. #pragma config  CP              = OFF   // Program memory code protection is disabled.
  8. #pragma config  MCLRE   = ON    // RE3/MCLR pin function is MCLR.
  9. #pragma config  PWRTE   = ON    // PWRT enabled.
  10. #pragma config  WDTE    = OFF   // WDT disabled.
  11. #pragma config  FOSC    = INTRC_NOCLKOUT        // I/O function on RA6 nad RA7.
  12.  
  13. /* CONFIG2 ************************/
  14. #pragma config  WRT             = OFF   // Write protection off.
  15. #pragma config  BOR4V   = 0             // Brown-out Reset set to 2.1V.
  16.  
  17.  
  18.  
  19. #include "Oscillator.c"
  20. #include "ADC.c"
  21. #include "UART.c"
  22. #include "PullUps.c"
  23. #include "Interrupts.c"
  24. #include "Timer0.c"
  25. #include "PWMs.c"
  26. #include "I2C.c"
  27.  
  28. #include "stdio.h"
  29.  
  30. #define LED1_ON         PORTCbits . RC0 = 0x01;
  31. #define LED2_ON         PORTCbits . RC1 = 0x01;
  32.  
  33. #define LED1_OFF        PORTCbits . RC0 = 0x00;
  34. #define LED2_OFF        PORTCbits . RC1 = 0x00;
  35.  
  36. #define ALL_LEDS_ON             PORTC  = 0x0F;
  37. #define ALL_LEDS_OFF    PORTC  = 0x00;
  38.  
  39. #define TRUE            0x01
  40. #define FALSE           0x00
  41.  
  42.  
  43. int SetUp ( void )
  44. {
  45. /* Set Up the Internal Oscillator. */
  46.         SetUpOscillator ( OSC_8MHz );
  47.        
  48. /* Enabling the whole PORTA as input, but RA6 and RA7. */
  49.         TRISA = 0x3F;
  50.         PORTAbits . RA6 = 0x01;
  51.         PORTAbits . RA7 = 0x00;
  52.  
  53. /* Enabling the Pull Up Resitor on PORTB. */
  54.         TRISB = 0xFF;
  55.         EnablePullUps ( ALL_WPU );
  56.  
  57. /* Setting Up the  PORTC as "input", but the Tx pin. */
  58.         TRISC = 0xBF;
  59.        
  60. /* Set Up the USAT Comunication. */
  61.         SetUpUART ( BAUD_RATE_57600 );
  62.  
  63. /* Set up the MSSP Port a`s I2C, in Master Mode and High Speed (400kHz) */
  64.         SetUpI2C ( MASTER, SLEW_RATE_STANDARD );
  65.  
  66. /* Set Up the ADC Register, Channels, Voltage Reference, etc. */
  67.         SetUpADCPORTs ( sAN0 | sAN1 | sAN2 | sAN3 | sAN4, NO_ADC_PORTB, NO_ADC_PORTE );
  68.         SetUpADC ( RIGHT_JUSTIFIED, VDD_VSS, FOSC_32 );
  69.  
  70. /* Set Up the CCP1 Register with PWMs */
  71.         SetUpPWM1 ( 250 );
  72.         DutyCyclePWM1 ( PWM_PERCENT_TRUE, 0 );
  73.  
  74. /* SetUp the Timer 0 */
  75.         SetUpTimer0 ( T0_SOURCE_INT, T0_PS_TMR0, T0_PS_1_256 );
  76.         SetTMR0 ( 180 );
  77.  
  78. /* Enabling Interrupts. */
  79.         EnableInterrupt ( PORTB_IOC );
  80.         EnableInterrupt ( TMR0_INTERRUPT );
  81.        
  82. //      EnableInterrupt ( GLOBAL_INTERRUPT );
  83.  
  84.         return 0x00;
  85.         }

« Última modificación: 26 de Febrero de 2014, 01:25:41 por rotting79 »
...On November 2nd, a PIC10 became self-aware and decided our fate in __delay_us ( 1 ); ...

Desconectado rotting79

  • PIC12
  • **
  • Mensajes: 91
Re: PCF8574A, EXPANSOR 8BITS DE BUS I2C PROTON
« Respuesta #2 en: 07 de Febrero de 2014, 02:20:23 »
and the libray....

I2C.c
Código: C
  1. #include "I2C.h"
  2.  
  3.  
  4.  
  5. void SetUpI2C ( unsigned char serialPortMode, unsigned char slewRate )
  6. {
  7. #if defined ( _PIC16F887_H_ ) || defined ( _PIC16F886_H_ )
  8.         PORTCbits . RC3 = 1;    // Set SCL and SDA pins as inputs.
  9.         PORTCbits . RC4 = 1;
  10. #endif
  11.  
  12.         SSPSTAT &= 0x3F;                                // Power on state.
  13.         SSPCON = 0x00;                                  // POR value.
  14.         SSPCON2 = 0x00;
  15.        
  16.         SSPCONbits . SSPM = serialPortMode;
  17.         SSPSTATbits . SMP = slewRate;
  18.        
  19. #if FREQ_8MHz
  20.         SSPADD = 19;            // I2C-freq = FOSC/(4 * (SSPADD + 1))
  21. #endif                                  // 100kHz Baud clock(19) @8MHz.
  22.  
  23.         SSPCONbits . SSPEN = 0x01;              // Synchronous Serial Port.
  24.  
  25.         IdleI2C ( );            // Puts the bus on idle state.
  26.         }
  27.        
  28.        
  29. /* Idle or trasmition is in progres */ 
  30. void WaitI2C ( void )
  31. {
  32.         while ( ( SSPCON2 & 0x1F ) || ( SSPSTAT & 0x04 ) );
  33.         }
  34.        
  35. void StartI2C ( void )
  36. {
  37. //      WaitI2C ( );                                    // Wait untill buffer is free.
  38.         SSPCON2bits . SEN = 1;                  // Start Condition Enabled.
  39.  
  40.         while ( SSPCON2bits . SEN );    // waits till the start
  41.         }                                                               // sequence is terminated.
  42.        
  43. void RestartI2C ( void )
  44. {
  45.         WaitI2C ( );                    // Wait untill buffer is free.
  46.         SSPCON2bits . RSEN = 1; // Repeated Start Condition.
  47.  
  48.         while ( SSPCON2bits . RSEN );   // waits till the restart
  49.         }                                                               // sequence is terminated.
  50.  
  51. void StopI2C ( void )
  52. {
  53.         WaitI2C ( );                                    // Wait untill buffer is free.
  54.         SSPCON2bits . PEN = 1;                  // Stop Condition Enable.
  55.  
  56.         while ( SSPCON2bits . PEN );    // Waits till the stop
  57.                                                                         // sequence is terminated.
  58.         IdleI2C ( );                                    // Puts the bus on idle state.
  59.         }
  60.        
  61. // sends one byte
  62. void WriteI2C ( unsigned char data )
  63. {
  64.         WaitI2C ( );            // Wait untill buffer is free.
  65.     SSPBUF = data;              // Passing the data to Synchronous Serial Port Transmit Register.
  66.  
  67.         while ( SSPCON2bits . ACKSTAT == 1 );   // Wait until slave acknowledge.
  68.         }
  69.  
  70.  
  71. void I2CAddressCommand ( unsigned char address, unsigned char mode )
  72. {
  73.         unsigned char temp;
  74.  
  75. //      temp = address << 1;//Passing the address and shift one place to a variable.           
  76. //      temp += mode;           // Adding the write/read on the 8th bit.
  77.  
  78.         temp = address + mode; 
  79.  
  80.         WaitI2C ( );            // Wait untill buffer is free.
  81.         SSPBUF = temp;          // Passing the address + command to Transmit Register.
  82.  
  83.         while ( SSPCON2bits . ACKSTAT == 1 );
  84.         }
  85.  
  86.  
  87.  
  88. // Read data from slave
  89. // ack should be 1 if there is going to be more data read
  90. // ack should be 0 if this is the last byte of data read
  91. unsigned char ReadI2C ( unsigned char ack )
  92. {
  93.         unsigned char I2CReadData;
  94.  
  95.         WaitI2C ( );                    // Wait untill buffer is free.
  96.        
  97.         SSPCON2bits . RCEN = 1; // Receive enable.
  98.         WaitI2C ( );                    // Wait untill receive complete.
  99.  
  100.         I2CReadData = SSPBUF;   // Passing data from Reciver Buffer to variable.
  101.         WaitI2C ( );                    // Wait untill receive complete.
  102.        
  103.         if ( ack )
  104.                 ACKDT = 0;                      // Acknowledge.
  105.         else      
  106.                 ACKDT = 1;                      // Not Acknowledge.
  107.        
  108.         ACKEN = 1;                              // Initiate Acknowledge sequence on SDA and SCL pins.
  109.  
  110.         return ( I2CReadData ); // Return data fron Slave.
  111.         }
  112.  
  113.  
  114. /* OR-ing R_W bit with SEN, RSEN, PEN, RCEN, or ACKEN will indicate if the MSSP is in Idle mode.
  115. */
  116. void IdleI2C ( void )
  117. {
  118.         while ( ( SSPCON2 & 0x1F ) | ( SSPSTATbits . R_W ) )
  119.         continue;
  120.         }

I2C.h
Código: C
  1. #define SLAVE_10_B_ADD_INT              0x0F
  2. #define SLAVE_7_B_ADD_INT               0x0E
  3. #define MASTER_FIRMWARE                 0x0B
  4. #define LOAD_MASK                               0x09
  5. #define MASTER                                  0x08
  6. #define SLAVE_10_BIT_ADDRESS    0x07
  7. #define SLAVE_7_BIT_ADDRESS             0x06
  8.  
  9. #define SLEW_RATE_STANDARD              0x01            // For 100kHz and 1MHz.
  10. #define SLEW_RATE_HIGH_SPEED    0x00            // For 400kHz.
  11.  
  12. #define I2C_READ                                0x01
  13. #define I2C_WRITE                               0x00
  14.  
  15. #define MASTER_ACK                              0x01
  16. #define MASTER_NACK                             0x00
  17.  
  18.  
  19. void SetUpI2C ( unsigned char slewRate, unsigned char serialPortMode );
  20. void WaitI2C ( void );
  21. void StartI2C ( void );
  22. void RestartI2C ( void );
  23. void StopI2C ( void );
  24. void WriteI2C ( unsigned char data );
  25. void I2CAddressCommand ( unsigned char address, unsigned char mode );
  26. unsigned char ReadI2C ( unsigned char ack );
  27. void IdleI2C ( void );
...On November 2nd, a PIC10 became self-aware and decided our fate in __delay_us ( 1 ); ...

Desconectado rotting79

  • PIC12
  • **
  • Mensajes: 91
Re: PCF8574A, EXPANSOR 8BITS DE BUS I2C PROTON
« Respuesta #3 en: 07 de Febrero de 2014, 02:32:12 »
here is a small PCF8574 Trainig Board to work with...





...On November 2nd, a PIC10 became self-aware and decided our fate in __delay_us ( 1 ); ...