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 support@ccsinfo.com

PIC18F4550 i2c problem

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



Joined: 23 Aug 2005
Posts: 93

View user's profile Send private message Visit poster's website

PIC18F4550 i2c problem
PostPosted: Fri Jan 09, 2009 9:23 am     Reply with quote

Hi, I have a problem with a non working i2c on my PIC.
It seems that I havent got a clock pulse on the scl pin?
Is there something that can interfere with this pin?
Im using the HW i2c on the uP. Compiler ver:4.068

Code:
#include <18F4550.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES XT                       //Crystal osc <= 4mhz
#FUSES NOPROTECT                //Code not protected from reading
#FUSES BROWNOUT_NOSL            //Brownout enabled during operation, disabled during SLEEP
#FUSES NOBROWNOUT               //No brownout reset
#FUSES BORV20                   //Brownout reset at 2.0V
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOCPD                    //No EE protection
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES NOPBADEN                 //PORTB pins are configured as digital I/O on RESET
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOEBTRB                  //Boot block not protected from table reads
#FUSES NOCPB                    //No Boot Block code protection
#FUSES MCLR                     //Master Clear pin enabled
#FUSES LPT1OSC                  //Timer1 configured for low-power operation
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES PLL1                     //No PLL PreScaler
#FUSES ICPRT                  //spec prog port

#use delay(clock=4000000,RESTART_WDT)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,restart_wdt,errors)
#use i2c(MASTER, SDA=PIN_B0, SCL=PIN_B1, FORCE_HW)


init sequence: only using timer0 at this time
Code:

void init(void)
{
   SET_TRIS_A( 0xfF );
   SET_TRIS_B( 0xa0 );//RB0,RB1 out
   SET_TRIS_C( 0xa3 );//10100011 0xa3
   SET_TRIS_D( 0xff );
   SET_TRIS_E( 0x00 );
   setup_adc_ports( NO_ANALOGS );
   //setup_adc_ports(AN0_TO_AN4|VSS_VDD);
   //setup_adc(ADC_CLOCK_INTERNAL);
   //setup_psp(PSP_DISABLED);
   //setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_CLK_DIV_4);
   //setup_spi(FALSE);
   //setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_16|RTCC_8_bit);
   //setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
   //setup_timer_2(T2_DISABLED,0,1);
   ///setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   //setup_comparator(NC_NC_NC_NC);
   //setup_vref(FALSE);
   enable_interrupts(INT_TIMER0);
   //enable_interrupts(INT_RDA);
   //enable_interrupts(INT_TBE);
   enable_interrupts(GLOBAL);
   //setup_low_volt_detect(FALSE);
   //setup_oscillator(False);
}


And then I use the i2c by:

Code:

void get_temperature(SENSOR_ADDR)
{
    i2c_start();     
    i2c_write(SENSOR_ADDR);
    i2c_write(REG_0_TEMP);               
    i2c_start();     
    i2c_write(SENSOR_ADDR | READ_BIT);   
    bTempHi = i2c_read(ACK);           
    bTempLo = i2c_read(NO_ACK);
    i2c_stop();
    liTemp = MAKE16(bTempHi,bTempLo);     
    liTemp=(liTemp >> 7 );                               
    fTemp = (float)liTemp*KTEMP;
}

But the code gets stuck already at "i2c_write(SENSOR_ADDR);"
When measering with an oscilloscope there is no sign of a scl puls?
And I do have a pull-up, 8k2 on the SDA pin.
Anyone got an idea?
Thanks
Ttelmah
Guest







PostPosted: Fri Jan 09, 2009 9:49 am     Reply with quote

First, you need pull-ups on both SCL, and SDA.
Second, you are enabling INT_TIMER0, but show no handler for this. Repeat after me, you _must_ have a handler present for any interrupt that is completely enabled. If you don't, this can hang the processor...
Third, have you got a pull-up on MCLR?. Otherwise the processor probably won't start, with MCLR enabled.
You don't need the TRIS statements. Let the compiler handle these for you.
Have you got a 4MHz external crystal attached?.

Best Wishes
Tagge



Joined: 23 Aug 2005
Posts: 93

View user's profile Send private message Visit poster's website

PostPosted: Fri Jan 09, 2009 1:36 pm     Reply with quote

Hi and thanks, got it rolling by removing the set_tris() , altough I cant figure out whats wrong with them??
where can I make sure how the ports are configured now?

And, yes, the timer0 isr is handled, 1 sec clock. And working fine.
And, yes, I got a 4MHz crystal. And mclr is allways pulled-up!
Thanks again for the quick response, best forum on the web! Smile
Ttelmah
Guest







PostPosted: Fri Jan 09, 2009 4:25 pm     Reply with quote

If you look at table 10-3, and look at the settings for RB0, and RB1, you will find that RB0 and RB1, both need their TRIS bits set as 1 (input), to work with the I2C hardware. Setting it to '0', makes the pins into digital outputs, and override the I2C peripheral. Hence your TRIS settings are stopping I2C from working.
This is typical of the 'complexities' of TRIS settings on the PIC. They are easy, when dealing with simple I/O,, but not when dealing with the peripherals. You will (for example), find some older chips that on their serial pins do require the TRIS set for input on the receive, and output on the transmit pins, but other latter chips require the same peripheral to have the pins both set as inputs. Hence in general, it is much simpler to leave TRIS settings to the compiler, and only use them for specific 'overrides', where you have checked everything on the data sheet, or for the ultimate speed....

Best Wishes
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