fbpx

Expresate

Si además quieres enviarnos un Artículo para el Blog y redes sociales, pulsa el siguiente botón:

Alguien me puede ay...
 
Avisos
Vaciar todo

Alguien me puede ayudar con este codigo i2c?

6 Respuestas
5 Usuarios
0 Reactions
4,813 Visitas
f7dcfa5a050c30eabab0f36d1a66edb5?s=80&r=g
Respuestas: 99
Topic starter
(@Anónimo 906)
Registrado: hace 18 años

Muy buenas a todos...
El caso es que tengo un problemilla, ando intentando comunicar dos pics por i2c (un master pic16f877a, y un slave pic18f2550) pero no hay manera...
Como en mi busqueda por la red no he podido solucionar mi problema, me he visto obligado a solicitaros ayuda...
El código que os pongo tiene por intención hacer que el maestro envie un dato (0x00) al esclavo, cuya dirección es 0x30, el esclavo simplemente ha de recibirlo, y al hacerlo activar recibido=1, estonces (cuando recibido=1) será cuando se haga output_a(0x02) y output_b(0x80)...
Por favor, podríais echarle una ojeada a mi código y comentarme??

Mil gracias de antemano y perdón por las molestias,
Burnaron

MAESTRO

#include <16F877A>
#device adc=8

#FUSES NOWDT //No Watch Dog Timer
#FUSES LP //Low power osc < 200 khz
#FUSES PUT //Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NODEBUG //No Debug mode for ICD
#FUSES BROWNOUT //Reset when brownout detected
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected

#use delay(clock=4000000)
#use i2c(Master,Fast,sda=PIN_C4,scl=PIN_C3,force_hw)

void main()
{

setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);

// TODO: USER CODE!!

delay_ms(100);
i2c_start();
i2c_write(0x30);
i2c_write(0x00);
i2c_stop();
delay_ms(100);

do {
} while (TRUE);
}

ESCLAVO

#include <18F2550>
#device adc=8

#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES XTPLL //Crystal/Resonator with PLL enabled
#FUSES NOPROTECT //Code not protected from reading
#FUSES BROWNOUT //Reset when brownout detected
#FUSES BORV20 //Brownout reset at 2.0V
#FUSES PUT //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 NOIESO //Internal External Switch Over mode disabled
#FUSES NOFCMEN //Fail-safe clock monitor disabled
#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 XINST //Extended set extension and Indexed Addressing mode enabled
#FUSES PLL1 //No PLL PreScaler
#FUSES CPUDIV4 //System Clock by 4
#FUSES NOUSBDIV //USB clock source comes from primary oscillator
#FUSES NOVREGEN //USB voltage regulator disabled

#use delay(clock=4000000)
#use i2c(Slave,sda=PIN_B0,scl=PIN_B1,force_hw,address=0x30)

//VARIABLES GLOBALES
int1 recibido=0;

#int_ssp //Rutina Interrupción I2C
void int_sspi2c()
{
BYTE incoming;
if (i2c_poll() == FALSE) {
i2c_write (0x01);
}
else {
incoming = i2c_read();
recibido = 1;
}
}

void main()
{

setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_spi(SPI_SS_DISABLED);
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);

set_tris_a(0x00);
set_tris_b(0x03);

enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);

//Setup_Oscillator parameter not selected from Intr Oscillotar Config tab

// TODO: USER CODE!!
//VARIABLES

do{
if (recibido==1){
output_a(0x02);
output_b(0x80);
}
}while(true);
}

Responder
5 respuestas
diaio
Respuestas: 186
(@diaio)
Ardero
Registrado: hace 19 años

pues no te se decir gran cosa nunca he programado un i2C pero este Do lo tienes vacio, es asi?

do {
} while (TRUE);
}

un saludo

Responder
f7dcfa5a050c30eabab0f36d1a66edb5?s=80&r=g
Respuestas: 99
Topic starter
(@Anónimo 906)
Registrado: hace 18 años

Sí, así es, vacio, vacio...

Es de agradecer un poco de atención :-), gracias.

Saludos y mil gracias,
Burnaron

Responder
davemaster
Respuestas: 1
(@davemaster)
New Member
Registrado: hace 16 años

Muy buenas a todos...
El caso es que tengo un problemilla, ando intentando comunicar dos pics por i2c (un master pic16f877a, y un slave pic18f2550) pero no hay manera...
Como en mi busqueda por la red no he podido solucionar mi problema, me he visto obligado a solicitaros ayuda...
El código que os pongo tiene por intención hacer que el maestro envie un dato (0x00) al esclavo, cuya dirección es 0x30, el esclavo simplemente ha de recibirlo, y al hacerlo activar recibido=1, estonces (cuando recibido=1) será cuando se haga output_a(0x02) y output_b(0x80)...
Por favor, podríais echarle una ojeada a mi código y comentarme??

Mil gracias de antemano y perdón por las molestias,
Burnaron

MAESTRO

#include <16F877A>
#device adc=8

#FUSES NOWDT //No Watch Dog Timer
#FUSES LP //Low power osc < 200 khz
#FUSES PUT //Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NODEBUG //No Debug mode for ICD
#FUSES BROWNOUT //Reset when brownout detected
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected

#use delay(clock=4000000)
#use i2c(Master,Fast,sda=PIN_C4,scl=PIN_C3,force_hw)

void main()
{

setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);

// TODO: USER CODE!!

delay_ms(100);
i2c_start();
i2c_write(0x30);
i2c_write(0x00);
i2c_stop();
delay_ms(100);

do {
} while (TRUE);
}

ESCLAVO

#include <18F2550>
#device adc=8

#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES XTPLL //Crystal/Resonator with PLL enabled
#FUSES NOPROTECT //Code not protected from reading
#FUSES BROWNOUT //Reset when brownout detected
#FUSES BORV20 //Brownout reset at 2.0V
#FUSES PUT //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 NOIESO //Internal External Switch Over mode disabled
#FUSES NOFCMEN //Fail-safe clock monitor disabled
#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 XINST //Extended set extension and Indexed Addressing mode enabled
#FUSES PLL1 //No PLL PreScaler
#FUSES CPUDIV4 //System Clock by 4
#FUSES NOUSBDIV //USB clock source comes from primary oscillator
#FUSES NOVREGEN //USB voltage regulator disabled

#use delay(clock=4000000)
#use i2c(Slave,sda=PIN_B0,scl=PIN_B1,force_hw,address=0x30)

//VARIABLES GLOBALES
int1 recibido=0;

#int_ssp //Rutina Interrupción I2C
void int_sspi2c()
{
BYTE incoming;
if (i2c_poll() == FALSE) {
i2c_write (0x01);
}
else {
incoming = i2c_read();
recibido = 1;
}
}

void main()
{

setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_spi(SPI_SS_DISABLED);
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);

set_tris_a(0x00);
set_tris_b(0x03);

enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);

//Setup_Oscillator parameter not selected from Intr Oscillotar Config tab

// TODO: USER CODE!!
//VARIABLES

do{
if (recibido==1){
output_a(0x02);
output_b(0x80);
}
}while(true);
}

Saludos,

Antes que nada:
Ayuda gratis= paciencia
contrato $$$= ayuda YA!

No mandaste tu circuito I2C master/slave. Es vital revisar el diseño, pues que funcione no sólo depende del software.

Trataré de adivinar, pusiste las resistencias "pull-up"??

suerte.

Responder
boops
Respuestas: 1813
(@boops)
Ardero
Registrado: hace 19 años

no me gusta la interrupción del esclavo...

¿podrías poner un bucle en el maestra por ejemplo cada segundo?

¿y encender un led cada vez que salte la interrupción?

¿has revisado que los force_hw no den problemas, es decir estén correctamente conectados?

Yo nunca he tenido problemas con los masters en cambio con los esclavos no me he llevado nunca bien.

Responder
Página 1 / 2
Compartir: