こんにちはI2Cでpic16f818にPIC18F4550からデータを送信しようとしているいくつかの問題を抱えてイム...I2Cを介してPICからPICへのデータ。それはプロテウスではなく、実際の生活では機能します。どうして?
このプロジェクトは4x4kbdとLCDライブラリを使用しますが、この部分は、あまりにも現実の生活の中で正常に動作します。
あなたが「1」マスターチップICは0xA0をとしてアドレス指定スレーブにバイトを送信します押し
、スレーブは、(両方のプログラムが含まれている)、このバイトを受信RAMまたはROMに格納しますマスターによって読み込まれます。
これはProteus 8ではうまく動作しますが、実際のところは動作しません。
3つの試行で3つの異なる結果が得られました。同じHEXファイルです。
1.PROTEUS 8:それは完璧に動作します。
2.PROTEUS 7:それは仕事をdoesntのが、それは
3.REALのLIFEハングdoesntの:チップハングと文句を言わない、私はそれをリセットするまで、他の命令を実行します。
マイコンパイラはPCW 4.106です。
You can download all the project files from here. [proteus simulation + ccs source code]
誰かがこれで私を助けることができますか?このプロジェクトを実行しようとして以来、それは長い時間であり、悪夢となっています。
両方のマイクロコントローラ間の通信が不可能になる原因は何ですか?
なぜチップがハングアップするのですか?
ご協力いただきありがとうございます。
ここに私のマスターコード:
#include <18f4550.h>
#fuses INTRC_IO,NOPROTECT,NOMCLR,NOWDT
#use delay(clock= 4000000)
#use i2c(Master,fast,sda=PIN_E0,scl=PIN_E1)
#define LCD_DATA_PORT getenv("SFR:PORTB")
#byte portd=0xf83
#include <lcd18.c>
#include <jkbd4x4d.c>
#include <i2c.c>
void SEND_I2C(direccion, posicion, dato){
i2c_start(); // Comienzo comunicación
i2c_write(direccion); // Dirección del esclavo en el bus I2C
i2c_write(posicion); // Posición donde se guardara el dato transmitido
i2c_write(dato); // Dato a transmitir
i2c_stop(); // Fin comunicación
delay_ms(50);
}
byte READ_I2C (byte direccion, byte posicion) {
byte dato;
i2c_start(); // Comienzo de la comunicación
i2c_write(direccion); // Dirección del esclavo en el bus I2C
i2c_write(posicion); // Posición de donde se leerá el dato en el esclavo
i2c_start(); // Reinicio
i2c_write(direccion+1); // Dirección del esclavo en modo lectura
dato=i2c_read(0); // Lectura del dato
i2c_stop(); // Fin comunicación
delay_ms(50); // Espera finalización del envio
return dato;
}
void main() {
char k;
lcd_init();
kbd_init();
port_b_pullups(TRUE);
lcd_putc("\fPress '1' for \r\nI2C test");
while (TRUE) {
k= getkey();
if(k=='x'){}else{ // Detecting the key
printf(lcd_putc,"\f%c",k); // Print the key
delay_ms(200); // An anti-re-pressing delay
if(k=='1'){ // key 1 detected
printf(lcd_putc,"\fSent AA to 00"); // some info
SEND_I2C(0xA0, 0x00, 0xAA); // send i2c(chip Address, eeprom address, data)
delay_ms(100); // Giving ages to slave to work
printf(lcd_putc,"\nRead %x",READ_I2C(0xA0, 0x00)); // read i2c (chip Address, eeprom address)
} } }}
そして、ここに私の奴隷コード
#include <16F818.h>
#device adc=10
#fuses INTRC_IO,NOWDT,NOPROTECT,NOLVP,NOMCLR
#use delay(clock=4000000)
#use i2c(slave, fast, sda=PIN_B1, scl=PIN_B4, address=0xA0,FORCE_HW)
byte fstate; //I2C bus state
byte posicion; //Memory buffer index
byte buffer[0x10]; //Ram address
#INT_SSP
void ssp_interupt(){
int incoming; //Everithing the master sends is gonna be placed here
fstate = i2c_isr_state(); //get I2C bus state
if(fstate == 0x80) { // If master asks for data
i2c_write(buffer[posicion]); // the slave will write the asked data to the I2C bus
}
else { //If the master sends data
incoming = i2c_read(); //we read it
if (fState == 1) { //if the information is about the position
posicion = incoming; //We save it as a position
}
else if (fState == 2) { //The info is a data
buffer[posicion]=incoming; // We save the data in the right position
} } }
void main(){
fState = 0;
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
while(true){ }}
プルアップ抵抗を確認してください。 –
そのような場合には一般的にPullupの問題がありました。 また、I2Cピンが他の周辺機器を共有する場合には、I2Cピンが正しく定義されていることを確認することをお勧めします。 – HallMark