|
|
View previous topic :: View next topic |
Author |
Message |
Sigma
Joined: 03 May 2004 Posts: 48 Location: Sg
|
12f629 with i2c eeprom |
Posted: Tue Oct 26, 2004 2:32 am |
|
|
Hi, all,
I am now testing a serial eeprom 24aa32 with 12f629 with compiler version 3.178. I use pin_a5 as SDA pin, pin_a4 as SCL pin with 1k resistors pull-up. And also pin_a0 with a 1k pull up resistor to control the WP pin of the eeprom chip. However, the code is not working. Then i emulate the code using icepic emulator of 12C672 (of course with some include statement changed), it is working fine. Is it due to some i/o property problems of 12F629? But i already disable the comparator, and make them all digital i/o in the code. what went wrong? Pls advice.
Sigma
Code: |
#include "12F629.h"
//#include "12C672.h"
#fuses INTRC,NOWDT,NOPROTECT,NOMCLR,PUT
//#fuses XT,NOWDT,NOPROTECT,NOMCLR,PUT
#use delay (clock=4000000)
#define WP PIN_A0 //GP0: Write Protect for FRAM
#define LED PIN_A1 //GP1: 1: Energised, 0: dis-energised
#define EE_SDA PIN_A4 //GP4: I2C data line
#define EE_SCL PIN_A5 //GP5: I2C clock line
char cmd;
#byte CMCON=0x19
#byte ANSEL=0x9f
/*******************************************************************************************
* 24AA32 functions - I2C Bus
*******************************************************************************************/
#use i2c(master, SDA=EE_SDA, SCL=EE_SCL) //I2C bus
//------------------------------------------------------------------------------------------
void init_i2c(void) { //Init EEPROM
output_float(EE_SCL);
output_float(EE_SDA);
}
//------------------------------------------------------------------------------------------
char read_24AA32(long int addr) { //Read 1 byte
char data;
i2c_start();
i2c_write(0xa0);
i2c_write((((0xff00)&addr)>>8));
i2c_write(addr&(0x00ff));
i2c_start();
i2c_write(0xa1);
data=i2c_read(0);
i2c_stop();
return(data);
}
//------------------------------------------------------------------------------------------
void write_24AA32(long int addr, char data) //Write 1 byte
{
output_low(WP);
i2c_start();
i2c_write(0xa0);
i2c_write((((0xff00)&addr)>>8));
i2c_write(addr&(0x00ff));
i2c_write(data);
i2c_stop();
delay_ms(10);
output_high(WP);
}
/*******************************************************************************************
* main
*******************************************************************************************/
main() {
char i;
short done;
CMCON=0x07; // Comparator off.
ANSEL=0x00; // All i/o digital.
setup_comparator(NC_NC_NC_NC);
init_i2c(); //Init EEPROM
output_low(LED);
ext_int_edge(h_to_l);
disable_interrupts(GLOBAL);
setup_counters(RTCC_INTERNAL, WDT_1152MS); // The RTCC prescaler is set to 1
// because we are setting the
// WDT prescaler
done=FALSE;
delay_ms(2000);
while (1) {
if(!done){
done=TRUE;
for(i=0;i<254;i++){
write_24AA32(100,i);
cmd=read_24AA32(100);
if(cmd==i) {
output_high(LED);
delay_ms(50);
output_low(LED);
delay_ms(50);
}
else if(cmd!=i){
output_low(LED);
i++;
break;
}
}
}
} //while (1)
}
/******************************************************************************************/
|
|
|
|
Sigma
Joined: 03 May 2004 Posts: 48 Location: Sg
|
|
Posted: Tue Oct 26, 2004 3:23 am |
|
|
It seems like pin_a4 is not functioning in my case. I make the pin_a4 to control one led to blink. But the led is always on. Yet for other pins, the led blinks. I also test the i2c on pin_a0 as scl and pin_a1 as sda, it is working. Anyone know why pin_a4 not functioning as a digital i/o? It seems to me it still in the analog mode.
Sigma |
|
|
Will Reeve
Joined: 30 Oct 2003 Posts: 209 Location: Norfolk, England
|
|
Posted: Tue Oct 26, 2004 3:41 am |
|
|
On a lot of PICs A4 is an open drain output. |
|
|
Sigma
Joined: 03 May 2004 Posts: 48 Location: Sg
|
|
Posted: Tue Oct 26, 2004 3:49 am |
|
|
Solved. Inside the #fuse statement, modify intrc to intrc_io. Then led can be blinked. and i2c also working. Thanks.
Sigma |
|
|
|
|
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
|