CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Problem with hardware i2c pic16f877a

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
tn88test



Joined: 26 Feb 2013
Posts: 3

View user's profile Send private message

Problem with hardware i2c pic16f877a
PostPosted: Tue Feb 26, 2013 9:45 pm     Reply with quote

I had been successful with software i2c to display timer with ds1307. Now I try with hardware i2c and there is a problem. The i2c bits doesn't automatically cleared by hardware.

example:
My program will stuck here because SSPIF is always 0. If I replace it with delay. The program passed through but displaying wrong result.

Code:
void WaitMSSP(void){
    while(!SSPIF);         // Wait to complete
    SSPIF = 0;
}


The same problem with other SEN, PEN , .... It doesn't automatically cleared by hardware. I check bit SEN, PEN bit to lcd. It's always 1 and not cleared to 0.

Here is my code
Code:
#ifndef _TWI_INCLUDED_
#define _TWI_INCLUDED_

#BYTE SSPSTAT = 0x094
#BIT    BF = SSPSTAT.0
#BIT    UA = SSPSTAT.1
#BIT    R_W = SSPSTAT.2
#BIT    CKE = SSPSTAT.6
#BIT    SMP = SSPSTAT.7
#BYTE SSPCON0 = 0x014
#BIT    SSPM0 = SSPCON0.0
#BIT    SSPM1 = SSPCON0.1
#BIT    SSPM2 = SSPCON0.2
#BIT    SSPM3 = SSPCON0.3
#BIT    CKP = SSPCON0.4
#BIT    SSPEN = SSPCON0.5
#BIT    SSPOV = SSPCON0.6
#BIT    WCOL = SSPCON0.7
#BYTE SSPCON2 = 0x091
#BIT    SEN = SSPCON2.0
#BIT    RSEN = SSPCON2.1
#BIT    PEN = SSPCON2.2
#BIT    RCEN = SSPCON2.3
#BIT    ACKEN = SSPCON2.4
#BIT    ACKDT = SSPCON2.5
#BIT    ACKSTAT = SSPCON2.6
#BIT    GCEN = SSPCON2.7
#BYTE PIR1 = 0x0c
#BIT    TMR1IF = PIR1.0
#BIT    TMR2IF = PIR1.1
#BIT    CCP1IF = PIR1.2
#BIT    SSPIF = PIR1.3
#BIT    TXIF = PIR1.4
#BIT    RCIF = PIR1.5
#BIT    ADIF = PIR1.6
#BIT    PSPIF = PIR1.7
#BYTE SSPBUF = 0x13
#BYTE SSPADD = 0x93

void WaitMSSP(void){
    while(!SSPIF);         // Wait to complete
    SSPIF = 0;
}

void twi_init(void){                                     
    SSPCON0 = ((1 << SSPEN) | (1 << SSPM3));   // Synchronous Serial Port Enable bit, Master mode
    SSPSTAT = (1 << SMP);                  // Standard speed mode (100 kHz and 1 MHz)
    SSPADD = 0x12;                        // clock = 100Khz
}

void twi_start(void){
    SEN = 1;            // Send START condition
    WaitMSSP();
}

void twi_stop(void){
    PEN = 1;             //Send STOP condition                 
    WaitMSSP();
}

void twi_write(int8 tData){
    SSPBUF = tData;         // Send Data
    WaitMSSP();
}

int8 twi_read(int8 ack){
    RCEN = 1;            // Receive Mode
    WaitMSSP();
   
    if (ack != 0){
       ACKDT = 0;
        ACKEN = 1;
        WaitMSSP();
    }else{
       ACKDT = 1;
        ACKEN = 1;
        WaitMSSP();
    }
   
    return SSPBUF;
}

#endif
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Tue Feb 26, 2013 11:23 pm     Reply with quote

Hi,

What is your compiler version?

John
Ttelmah



Joined: 11 Mar 2010
Posts: 19553

View user's profile Send private message

PostPosted: Wed Feb 27, 2013 12:53 am     Reply with quote

Er...

What on earth are you trying to do with the instructions like:

SSPCON0 = ((1 << SSPEN) | (1 << SSPM3));

I think you misunderstand how bit variables work in CCS. They are not a reference to the bit 'in' a byte, but a direct reference to a single bit variable. As such SSPEN, will be just 0 or 1, according to what the value in the hardware is at this point. The same with SSPM3. To enable the SSP, just requires:

SSPEN=TRUE;

As such, the port won't be setup as you expect.....

Best Wishes
tn88test



Joined: 26 Feb 2013
Posts: 3

View user's profile Send private message

PostPosted: Wed Feb 27, 2013 2:12 am     Reply with quote

I used PCWHD version 4.104

I have change my code as you suggested. I can see the SEN, PEN bits are cleared by hardware, but SSPIF is not set to 1. My program still stuck at while(!SSPIF);

Code:
void WaitMSSP(void){
   while(!SSPIF);      // Wait to complete
    SSPIF = 0;
}


Here is my new code
Code:
#ifndef _TWI_INCLUDED_
#define _TWI_INCLUDED_

#BYTE SSPSTAT = 0x094
#BIT    BF = SSPSTAT.0
#BIT    UA = SSPSTAT.1
#BIT    R_W = SSPSTAT.2
#BIT    CKE = SSPSTAT.6
#BIT    SMP = SSPSTAT.7
#BYTE SSPCON0 = 0x014
#BIT    SSPM0 = SSPCON0.0
#BIT    SSPM1 = SSPCON0.1
#BIT    SSPM2 = SSPCON0.2
#BIT    SSPM3 = SSPCON0.3
#BIT    CKP = SSPCON0.4
#BIT    SSPEN = SSPCON0.5
#BIT    SSPOV = SSPCON0.6
#BIT    WCOL = SSPCON0.7
#BYTE SSPCON2 = 0x091
#BIT    SEN = SSPCON2.0
#BIT    RSEN = SSPCON2.1
#BIT    PEN = SSPCON2.2
#BIT    RCEN = SSPCON2.3
#BIT    ACKEN = SSPCON2.4
#BIT    ACKDT = SSPCON2.5
#BIT    ACKSTAT = SSPCON2.6
#BIT    GCEN = SSPCON2.7
#BYTE PIR1 = 0x0c
#BIT    TMR1IF = PIR1.0
#BIT    TMR2IF = PIR1.1
#BIT    CCP1IF = PIR1.2
#BIT    SSPIF = PIR1.3
#BIT    TXIF = PIR1.4
#BIT    RCIF = PIR1.5
#BIT    ADIF = PIR1.6
#BIT    PSPIF = PIR1.7
#BYTE SSPBUF = 0x13
#BYTE SSPADD = 0x93

void WaitMSSP(void){
   while(!SSPIF);      // Wait to complete
    SSPIF = 0;
}

void twi_init(void){
   SSPEN = 1;         // Synchronous Serial Port Enable bit,             
   SSPM3 = 1;        // Master mode             
   SMP = 1;            // Standard speed mode (100 kHz and 1 MHz)                          
    SSPADD = 0x12;      // clock = 100KHz
}

void twi_start(void){
    SEN = 1;         // Send START condition
      WaitMSSP();
}

void twi_stop(void){
    PEN = 1;          //Send STOP condition
   WaitMSSP();
}

void twi_write(int8 tData){
    SSPBUF = tData;      // Send Data
   WaitMSSP();
}

int8 twi_read(int8 ack){
    RCEN = 1;         // Receive Mode
    WaitMSSP();
   
    if (ack != 0){
       ACKDT = 0;
        ACKEN = 1;
        WaitMSSP();
    }else{
       ACKDT = 1;
        ACKEN = 1;
        WaitMSSP();
    }
   
    return SSPBUF;
}
#endif
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Feb 27, 2013 9:23 pm     Reply with quote

Why are you doing this ? CCS has built-in hardware i2c routines, which
can be enabled with this statement:
Code:

#use i2c(master, sda=PIN_C4, scl=PIN_C3,  FORCE_HW)

Then use the i2c routines listed in the CCS manual:
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf

Also look at examples of how to use the CCS i2c routines with i2c chips
such as eeproms. The driver files are in this directory:
c:\program files\picc\drivers

Example of eeprom driver file:
24256.c

Make sure you have a 4.7K pull-up resistor on each i2c bus line, sda and scl.
tn88test



Joined: 26 Feb 2013
Posts: 3

View user's profile Send private message

PostPosted: Wed Feb 27, 2013 10:11 pm     Reply with quote

PCM programmer wrote:
Why are you doing this ? CCS has built-in hardware i2c routines, which
can be enabled with this statement:
Code:

#use i2c(master, sda=PIN_C4, scl=PIN_C3,  FORCE_HW)

Then use the i2c routines listed in the CCS manual:
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf

Also look at examples of how to use the CCS i2c routines with i2c chips
such as eeproms. The driver files are in this directory:
c:\program files\picc\drivers

Example of eeprom driver file:
24256.c

Make sure you have a 4.7K pull-up resistor on each i2c bus line, sda and scl.


Thanks! The CCS hardware i2c worked perfectly.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group