|
|
View previous topic :: View next topic |
Author |
Message |
tixmcprods
Joined: 06 Jul 2010 Posts: 3
|
I2C : multiple read |
Posted: Tue Jul 06, 2010 9:31 am |
|
|
Hi,
I have a problem with my project. The master do some loops and freeze at the second i2c_read (after PIN_A2 turned on).
I tried delay, i2c settings but randomly (from 1 to 27 loops), the master stops.
Here the slave complete .h code :
Code: |
#include <18F252.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOOSCSEN //Oscillator switching is disabled, main oscillator is source
#FUSES NOBROWNOUT //No brownout reset
#FUSES BORV20 //Brownout reset at 2.0V
#FUSES NOPUT //No Power Up Timer
#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 NOWRTB //Boot block not write protected
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOCPD //No EE protection
#FUSES NOCPB //No Boot Block code protection
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#use delay(clock=10000000)
#use i2c(Slave,Fast,sda=PIN_C4,scl=PIN_C3,address=0x20)
int state;
int data;
int data1r, data2r;
int data1w = 15;
int data2w = 240;
|
The slave complete .c code :
Code: |
#include "main.h"
#ZERO_RAM
#int_SSP
void SSP_isr(void) {
state = i2c_isr_state();
if(state < 0x80) {
data = i2c_read();
switch(state) {
case 1: data1r = data; break;
case 2: data2r = data; break;
}
}
else {
state -= 0x80;
switch(state) {
case 0: i2c_write(data1w); break;
case 1: i2c_write(data2w); break;
}
}
}
void main() {
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
while(true) {
if(data1r == 'A' && data2r == 'B') output_high(PIN_B0);
else output_low(PIN_B0);
}
}
|
The master complete .h code :
Code: |
#include <18F2550.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPROTECT //Code not protected from reading
#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 PBADEN //PORTB pins are configured as analog input channels 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
#FUSES CPUDIV1
#FUSES NOUSBDIV
#FUSES VREGEN //USB voltage regulator enabled
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#use i2c(Master,Fast,sda=PIN_B0,scl=PIN_B1)
int data1, data2;
|
The master complete .c code :
Code: |
#include "main.h"
#ZERO_RAM
#int_RDA
void RDA_isr(void) {
}
#int_SSP
void SSP_isr(void) {
}
void main() {
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
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_RDA);
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
while(true) {
i2c_start();
i2c_write(0x20);
i2c_write('A');
i2c_write('B');
i2c_stop();
output_high(PIN_A0);
delay_ms(500);
i2c_start();
i2c_write(0x21);
data1 = i2c_read(1);
output_high(PIN_A2);
data2 = i2c_read(0); // RANDOMLY FREEZE HERE !!!
output_low(PIN_A2);
i2c_stop();
if(data1 == 15 && data2 == 240) output_high(PIN_A1);
else output_low(PIN_A1);
output_low(PIN_A0);
delay_ms(500);
}
}
|
Thanks a lot in advance.
TiX |
|
|
tixmcprods
Joined: 06 Jul 2010 Posts: 3
|
|
Posted: Tue Jul 06, 2010 10:29 am |
|
|
Re,
With "SLOW" settings in master and slave, it doesn't freeze anymore.
Both signal and data are OK.
Have you an idea why in "FAST" speed I have that problem ?
Thanks in advance.
TiX |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Tue Jul 06, 2010 12:07 pm |
|
|
Just at a quick glance, I noticed that your master is running at 20MHZ while your slave is running at 10MHZ. I would have them swapped. In my opinion, your slave should be running faster than the master. Odds are, your master, in Fast mode, is sending commands faster than the slave can process them and is probably just barely keeping up until it misses one command and then freezes.
Ronald |
|
|
|
|
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
|