16. PRÁTICA 16: COMUNICAÇÃO I2C MASTER
Referencial Teórico: Modos de Comunicação I2C; Módulo MSSP.
Objetivo: Preparar um dispositivo Mestre para comunicação no barramento e protocolo I2C.
Materiais: Utilizar botão e led presentes na placa. A prática necessita da ligação física entre placas.
16.1 Arquivo Principal
#include "iniciog.h"
#include "pratica.h"
void main(){
configura_portas();
led_vermelho = 1;
configura_i2c_master();
while(1){
if (botao == 1) {
escrever_i2c(64);
led = 1;
}
if (botao == 0) {
escrever_i2c(68);
led = 0;
}
espere_por(250); // 100ms
}
}
16.2 Arquivo Secundário
// acrescentar ao iniciog.h ou arquivo pratica.h
void configura_i2c_master(){
OpenI2C( MASTER, SLEW_OFF);
SSPADD = 27; //SSPADD Baud Register – 100Khz
ADCON1 = 0x0F; // desabilita conversão a/d
}
void escrever_i2c(int databyte){
StartI2C();
IdleI2C();
putcI2C(0xB0); //send address
IdleI2C();
putcI2C(databyte); //send data
IdleI2C();
StopI2C();
}