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

I2C bus causes reset

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



Joined: 09 May 2007
Posts: 6

View user's profile Send private message

I2C bus causes reset
PostPosted: Wed May 09, 2007 1:59 am     Reply with quote

Hello,

I'd like to program a i2c bus between a pic and a DAC. The pic I'am using is a PIC18F2480 with hardware i2c. The DAC is a MAX5812L with the device adress 0x20.

My code is running until the first i2c_write() instruction. It seems to me, that the watchdog causes a reset of the pic after I´ve called this function.

My PCWH compiler version is 4.023.

Can anybody explain this error to me?

Here´s my code...

// ************************************************************************
//
// I2C testprogram
//
// date: 09-May-2007
//
// software information:
//
// compiler: PCWH
// version: V4.023
// IDE: MPLAB V7.50
//
// hardware information:
//
// master: PIC18F2480 use of hardware I2C
//
// slave: MAX5812L write adress: 0x20
// read adress: 0x21
//
// I2C bus: pull up resistors in scl and sda (4k7)
//
// ************************************************************************

// Header Dateien
#include "18f2480.h"

#define STATUS_LED1 PIN_C1
#define STATUS_LED2 PIN_C2

// I2C...
#define Cmd_PowerUp 0x40
#define Cmd_PowerDown0 0x44
#define Cmd_PowerDown1 0x48
#define Cmd_PowerDown2 0x4C
#define SLAVE_WRITE_ADRESS 0x20
#define SLAVE_READ_ADRESS 0x21

// Configuration Bytes
//#fuses H4,WDT128,PROTECT, BROWNOUT, BORV20, NOLVP, NODEBUG
#fuses H4, PROTECT
#fuses BROWNOUT
#fuses WDT128, BORV42
#fuses PUT, NOCPD, STVREN, NODEBUG
#fuses NOLVP, NOWRT, NOWRTD, NOIESO, FCMEN
#fuses NOPBADEN, BBSIZ1K, NOWRTC, NOWRTB, NOEBTR
#fuses NOEBTRB, CPB, NOLPT1OSC, MCLR
#fuses NOXINST


#use delay(clock=40000000)
#use fast_io(A)
#use fast_io(B)
#use fast_io(C)
#use I2C(Master, sda=PIN_C4, scl=PIN_C3, FORCE_HW, STREAM=DAC)


#zero_ram

void main(void)
{
// lokals...
int i8_temp1, i8_outputhigh, i8_outputlow;


// set up´s
SETUP_ADC_PORTS(ALL_ANALOG);
SETUP_ADC(ADC_CLOCK_DIV_16);
SET_TRIS_A(0b11111111);
SET_TRIS_B(0b11001000);
SET_TRIS_C(0b00000000);
SETUP_TIMER_0(RTCC_8_BIT | RTCC_DIV_32 );
SETUP_WDT(WDT_ON);

// switch off the status led´s
OUTPUT_BIT(STATUS_LED1, 0);
OUTPUT_BIT(STATUS_LED2, 0);
delay_ms(20);


// main loop
do {

OUTPUT_BIT(STATUS_LED1, 1);
delay_ms(20);

// Power up slave device MAX5812L
I2C_START(DAC);
I2C_WRITE(SLAVE_WRITE_ADRESS); // device adress SLAVE_WRITE_ADRESS = 0x20
I2C_WRITE(Cmd_PowerUp); // device command Cmd_PowerUp = 0x40
I2C_STOP(DAC);

OUTPUT_BIT(STATUS_LED2, 1);
delay_ms(20);

i8_outputhigh = 0xFF;
i8_outputlow = 0xFF;

// output dac value...(12Bit)
i8_temp1 = i8_outputhigh & 0x0F;
i8_temp1 = 0xC0 | i8_temp1;

I2C_START(DAC);
I2C_WRITE(SLAVE_WRITE_ADRESS); // device adress SLAVE_WRITE_ADRESS = 0x20
I2C_WRITE(i8_temp1); // device command 0xC..+ 12 Bit data
I2C_WRITE(i8_outputlow);
I2C_STOP(DAC);


// Power down slave device MAX5812L
I2C_START(DAC);
I2C_WRITE(SLAVE_WRITE_ADRESS); // device adress SLAVE_WRITE_ADRESS = 0x20
I2C_WRITE(Cmd_PowerDown0); // device command Cmd_PowerDown0 = 0x44
I2C_STOP(DAC);

// switch off the status led´s
OUTPUT_BIT(STATUS_LED1, 0);
OUTPUT_BIT(STATUS_LED2, 0);
delay_ms(20);


restart_wdt(); // restart watchdog timer


} while (TRUE);


}

Thank you.

Tomson[/code]
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed May 09, 2007 12:20 pm     Reply with quote

Quote:
My code is running until the first i2c_write() instruction.

SET_TRIS_A(0b11111111);
SET_TRIS_B(0b11001000);
SET_TRIS_C(0b00000000);


Notice the line in bold above. Then read this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=29935
Tomson



Joined: 09 May 2007
Posts: 6

View user's profile Send private message

I2C bus problem obviously solved!
PostPosted: Thu May 10, 2007 2:20 am     Reply with quote

Hello PCM Programmer,

thanks for your fast reply. Obviously the problem is solved. But I have to change the #use i2c instruction too.

The following code (snipped) does work well.


#use I2C(Master, sda=PIN_C4, scl=PIN_C3, STREAM=DAC)

SET_TRIS_C(0b00011000);

Thanks again.

Best regards

Tomson
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