Boa noite,
Eu sou novo no forum, peço alguma ajuda na programação de um pic 18f e um arduino.
Estou tentando fazer ligação I2C entre um pic 18f e um arduino, mas não estou conseguindo ter sucesso.
Eu estou utilizando o compilador c18 do MPAB. alguém me pode ajudar ou tem algum pequeno código que me possa ajudar.
Vou portar o código que estou fazendo:
MASTER:
//Inclusão das bibliotecas
#include <p18f4585.h>
#include<timers.h>
#include <delays.h>
#include <i2c.h>
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//Configuração do Oscilador
#pragma config WDT=OFF //indica o tipo de sinal(tipo de oscilador)
#pragma config LVP=OFF //Desactiva o watch Timer
#pragma config OSC=HS //Desactiva low voltage programing
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//MAIN
void main(void)
{
TRISCbits.TRISC3=1;//C3
TRISCbits.TRISC4=1;//C4
OpenI2C( MASTER, SLEW_OFF);
SSPADD = 0x9; //SSPADD contains I2C device address in SLAVE mode
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
while(1) //Ciclo infinito while
{
Delay10KTCYx(100);
IdleI2C(); // Wait until the bus is idle
StartI2C(); // Send START condition
IdleI2C(); // Wait for the end of the START condition
WriteI2C( 0x8 ); // Send address with R/W cleared for write
IdleI2C(); // Wait for ACK
WriteI2C( 0b111111); // Write first byte of data
IdleI2C(); // Wait for ACK
StopI2C();
}
}
SLAVE:
#include <Wire.h>
void setup() {
Wire.begin(0x8); // join i2c bus with address #8
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
}
void loop() {
delay(100);
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
while (1 < Wire.available()) { // loop through all but the last
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
int x = Wire.read(); // receive byte as an integer
Serial.println(x); // print the integer
}