Si además quieres enviarnos un Artículo para el Blog y redes sociales, pulsa el siguiente botón:
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);
}
Has puesto resistencias entre las conexiones SDA y SDL de los dos PICs ?
Nosotros en un circuito que tenemos parecido conectamos 2 resistencias de 10k a +5v y a dichas conexiones.
Ciao.