Skip to main content
Aviso removido Recompensar resposta existente por animuson
Aviso adicionado Recompensar resposta existente por Farewell Stack Exchange
tags editadas
Link
Victor Stafusa
  • 64,9mil
  • 9
  • 120
  • 189
Tirando o "merda"... Apesar de alguns terem gostado, né
Fonte Link
Wallace Vizerra
  • 107,4mil
  • 106
  • 440
  • 920
/* 
* File: main.c
* Author: SusanaEca
*
* Created on 26 de Março de 2015, 19:26
*/
//Libraries
#include "p24fxxxx.h"
#include "i2c.h"
#include "leds.h"

//Calculate baud rate of I2C
/*
* I2CxBRG = (FCY/FSCL - FCY/10,000,000) - 1
* Based on FCY = FOSC/2; Doze mode and PLL are disabled.
*/
#define Fosc (8000000) // crystal
#define Fcy (Fosc*4/2) // w.PLL (Instruction Per Second)
#define Fsck 400000 // 400kHz I2C
#define I2C1_BRG ((Fcy/2/Fsck)-1)

int main(void)
{
    //Variable declaration
    char SlaveAddress = 0x4;
    unsigned char *letras;
    unsigned char tx_data[] = {'P', 'I', '\0'};
    // char c='M';
    //Get your pointer to a variable
    letras = tx_data;
    //Set-up LEDs
    LED_Enable(LED_D6);
    LED_Enable(LED_D3);
    //Initialize I2C1
    OpenI2C1(I2C_ON, I2C1_BRG);

    while(1)
    {
        //Start I2C1 condition
        StartI2C1(); //Send the Start Bit
        IdleI2C1(); //Wait to complete
        //Write-to-Slave Address
        MasterWriteI2C1((SlaveAddress<<0)|0); //Send device advice adress byte to the slave with the write indication
        IdleI2C1(); //Wait to complete
        //If ACK is received...
        if (I2C1STATbits.ACKSTAT == 0)
        {
            LED_On(LED_D6); //Light up LED #6
            MasterputsI2C1(letras); //Send your string :)
            IdleI2C1(); //Wait to complete
            //If NACK (merdaerro)...
            if (I2C1STATbits.ACKSTAT == 1) 
            {
                LED_On(LED_D3); //Light up LED #3
            }
        }
    }
}
 
/* 
* File: main.c
* Author: SusanaEca
*
* Created on 26 de Março de 2015, 19:26
*/
//Libraries
#include "p24fxxxx.h"
#include "i2c.h"
#include "leds.h"

//Calculate baud rate of I2C
/*
* I2CxBRG = (FCY/FSCL - FCY/10,000,000) - 1
* Based on FCY = FOSC/2; Doze mode and PLL are disabled.
*/
#define Fosc (8000000) // crystal
#define Fcy (Fosc*4/2) // w.PLL (Instruction Per Second)
#define Fsck 400000 // 400kHz I2C
#define I2C1_BRG ((Fcy/2/Fsck)-1)

int main(void)
{
    //Variable declaration
    char SlaveAddress = 0x4;
    unsigned char *letras;
    unsigned char tx_data[] = {'P', 'I', '\0'};
    // char c='M';
    //Get your pointer to a variable
    letras = tx_data;
    //Set-up LEDs
    LED_Enable(LED_D6);
    LED_Enable(LED_D3);
    //Initialize I2C1
    OpenI2C1(I2C_ON, I2C1_BRG);

    while(1)
    {
        //Start I2C1 condition
        StartI2C1(); //Send the Start Bit
        IdleI2C1(); //Wait to complete
        //Write-to-Slave Address
        MasterWriteI2C1((SlaveAddress<<0)|0); //Send device advice adress byte to the slave with the write indication
        IdleI2C1(); //Wait to complete
        //If ACK is received...
        if (I2C1STATbits.ACKSTAT == 0)
        {
            LED_On(LED_D6); //Light up LED #6
            MasterputsI2C1(letras); //Send your string :)
            IdleI2C1(); //Wait to complete
            //If NACK (merda)...
            if (I2C1STATbits.ACKSTAT == 1) 
            {
                LED_On(LED_D3); //Light up LED #3
            }
        }
    }
}
 
/* 
* File: main.c
* Author: SusanaEca
*
* Created on 26 de Março de 2015, 19:26
*/
//Libraries
#include "p24fxxxx.h"
#include "i2c.h"
#include "leds.h"

//Calculate baud rate of I2C
/*
* I2CxBRG = (FCY/FSCL - FCY/10,000,000) - 1
* Based on FCY = FOSC/2; Doze mode and PLL are disabled.
*/
#define Fosc (8000000) // crystal
#define Fcy (Fosc*4/2) // w.PLL (Instruction Per Second)
#define Fsck 400000 // 400kHz I2C
#define I2C1_BRG ((Fcy/2/Fsck)-1)

int main(void)
{
    //Variable declaration
    char SlaveAddress = 0x4;
    unsigned char *letras;
    unsigned char tx_data[] = {'P', 'I', '\0'};
    // char c='M';
    //Get your pointer to a variable
    letras = tx_data;
    //Set-up LEDs
    LED_Enable(LED_D6);
    LED_Enable(LED_D3);
    //Initialize I2C1
    OpenI2C1(I2C_ON, I2C1_BRG);

    while(1)
    {
        //Start I2C1 condition
        StartI2C1(); //Send the Start Bit
        IdleI2C1(); //Wait to complete
        //Write-to-Slave Address
        MasterWriteI2C1((SlaveAddress<<0)|0); //Send device advice adress byte to the slave with the write indication
        IdleI2C1(); //Wait to complete
        //If ACK is received...
        if (I2C1STATbits.ACKSTAT == 0)
        {
            LED_On(LED_D6); //Light up LED #6
            MasterputsI2C1(letras); //Send your string :)
            IdleI2C1(); //Wait to complete
            //If NACK (erro)...
            if (I2C1STATbits.ACKSTAT == 1) 
            {
                LED_On(LED_D3); //Light up LED #3
            }
        }
    }
}
 
Tweeted twitter.com/#!/StackOverflowPT/status/583221443372154880
Identação
Fonte Link
Victor Stafusa
  • 64,9mil
  • 9
  • 120
  • 189

Arduino I2C envia NACK inesperado

Estou a usar uma development board da microchip (explorer 16) com o pic24f128ga010 como MasterMaster e o Arduino como SlaveSlave. O PicPIC está a enviar tudo direitinho, mas por alguma razão o arduino envia NACK para todos os datadata, apenas manda ACK no inicioinício quando se envia o Address.

Código PIC (MASTERMaster) CODE:

/* 
* File: main.c
* Author: SusanaEca
*
* Created on 26 de Março de 2015, 19:26
*/
//Libraries
#include "p24fxxxx.h"
#include "i2c.h"
#include "leds.h"

//Calculate baud rate of I2C
/*
* I2CxBRG = (FCY/FSCL - FCY/10,000,000) - 1
* Based on FCY = FOSC/2; Doze mode and PLL are disabled.
*/
#define Fosc (8000000) // crystal
#define Fcy (Fosc*4/2) // w.PLL (Instruction Per Second)
#define Fsck 400000 // 400kHz I2C
#define I2C1_BRG ((Fcy/2/Fsck)-1)
int main(void)
{
//Variable declaration
char SlaveAddress = 0x4;
unsigned char *letras;
unsigned char tx_data[] = {'P', 'I', '\0'};
// char c='M';
//Get your pointer to a variable
letras = tx_data;
//Set-up LEDs
LED_Enable (LED_D6);
LED_Enable (LED_D3);
//Initialize I2C1
OpenI2C1(I2C_ON, I2C1_BRG);
while(1)
{
//Start I2C1 condition
StartI2C1 (); //Send the Start Bit
IdleI2C1(); //Wait to complete
//Write-to-Slave Address
MasterWriteI2C1((SlaveAddress<<0)|0); //Send device advice adress byte to the slave with the write indication
IdleI2C1(); //Wait to complete
//If ACK is received...
if( I2C1STATbits.ACKSTAT == 0 )
{
LED_On (LED_D6); //Light up LED #6
MasterputsI2C1(letras); //Send your string :)
IdleI2C1(); //Wait to complete
//If NACK (merda)...
if( I2C1STATbits.ACKSTAT == 1 ) 
{
LED_On (LED_D3); //Light up LED #3
}
}
}
}
 
/* 
* File: main.c
* Author: SusanaEca
*
* Created on 26 de Março de 2015, 19:26
*/
//Libraries
#include "p24fxxxx.h"
#include "i2c.h"
#include "leds.h"

//Calculate baud rate of I2C
/*
* I2CxBRG = (FCY/FSCL - FCY/10,000,000) - 1
* Based on FCY = FOSC/2; Doze mode and PLL are disabled.
*/
#define Fosc (8000000) // crystal
#define Fcy (Fosc*4/2) // w.PLL (Instruction Per Second)
#define Fsck 400000 // 400kHz I2C
#define I2C1_BRG ((Fcy/2/Fsck)-1)

int main(void)
{
    //Variable declaration
    char SlaveAddress = 0x4;
    unsigned char *letras;
    unsigned char tx_data[] = {'P', 'I', '\0'};
    // char c='M';
    //Get your pointer to a variable
    letras = tx_data;
    //Set-up LEDs
    LED_Enable(LED_D6);
    LED_Enable(LED_D3);
    //Initialize I2C1
    OpenI2C1(I2C_ON, I2C1_BRG);

    while(1)
    {
        //Start I2C1 condition
        StartI2C1(); //Send the Start Bit
        IdleI2C1(); //Wait to complete
        //Write-to-Slave Address
        MasterWriteI2C1((SlaveAddress<<0)|0); //Send device advice adress byte to the slave with the write indication
        IdleI2C1(); //Wait to complete
        //If ACK is received...
        if (I2C1STATbits.ACKSTAT == 0)
        {
            LED_On(LED_D6); //Light up LED #6
            MasterputsI2C1(letras); //Send your string :)
            IdleI2C1(); //Wait to complete
            //If NACK (merda)...
            if (I2C1STATbits.ACKSTAT == 1) 
            {
                LED_On(LED_D3); //Light up LED #3
            }
        }
    }
}
 

ARDUINOCódigo Arduino (Slave) CODE:

// Wire Slave Receiver
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Receives data as an I2C/TWI slave device
// Refer to the "Wire Master Writer" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>

void setup()
{
  Wire.begin(4); // join i2c bus with address #4
  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
}
 
// Wire Slave Receiver
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Receives data as an I2C/TWI slave device
// Refer to the "Wire Master Writer" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>

void setup()
{
    Wire.begin(4); // join i2c bus with address #4
    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
}

Arduino I2C NACK

Estou a usar uma development board da microchip (explorer 16) com o pic24f128ga010 como Master e o Arduino como Slave. O Pic está a enviar tudo direitinho mas por alguma razão o arduino envia NACK para todos os data, apenas manda ACK no inicio quando se envia o Address.

PIC (MASTER) CODE:

/* 
* File: main.c
* Author: SusanaEca
*
* Created on 26 de Março de 2015, 19:26
*/
//Libraries
#include "p24fxxxx.h"
#include "i2c.h"
#include "leds.h"

//Calculate baud rate of I2C
/*
* I2CxBRG = (FCY/FSCL - FCY/10,000,000) - 1
* Based on FCY = FOSC/2; Doze mode and PLL are disabled.
*/
#define Fosc (8000000) // crystal
#define Fcy (Fosc*4/2) // w.PLL (Instruction Per Second)
#define Fsck 400000 // 400kHz I2C
#define I2C1_BRG ((Fcy/2/Fsck)-1)
int main(void)
{
//Variable declaration
char SlaveAddress = 0x4;
unsigned char *letras;
unsigned char tx_data[] = {'P', 'I', '\0'};
// char c='M';
//Get your pointer to a variable
letras = tx_data;
//Set-up LEDs
LED_Enable (LED_D6);
LED_Enable (LED_D3);
//Initialize I2C1
OpenI2C1(I2C_ON, I2C1_BRG);
while(1)
{
//Start I2C1 condition
StartI2C1 (); //Send the Start Bit
IdleI2C1(); //Wait to complete
//Write-to-Slave Address
MasterWriteI2C1((SlaveAddress<<0)|0); //Send device advice adress byte to the slave with the write indication
IdleI2C1(); //Wait to complete
//If ACK is received...
if( I2C1STATbits.ACKSTAT == 0 )
{
LED_On (LED_D6); //Light up LED #6
MasterputsI2C1(letras); //Send your string :)
IdleI2C1(); //Wait to complete
//If NACK (merda)...
if( I2C1STATbits.ACKSTAT == 1 ) 
{
LED_On (LED_D3); //Light up LED #3
}
}
}
}
 

ARDUINO (Slave) CODE:

// Wire Slave Receiver
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Receives data as an I2C/TWI slave device
// Refer to the "Wire Master Writer" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>

void setup()
{
  Wire.begin(4); // join i2c bus with address #4
  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
}
 

Arduino I2C envia NACK inesperado

Estou a usar uma development board da microchip (explorer 16) com o pic24f128ga010 como Master e o Arduino como Slave. O PIC está a enviar tudo direitinho, mas por alguma razão o arduino envia NACK para todos os data, apenas manda ACK no início quando se envia o Address.

Código PIC (Master):

/* 
* File: main.c
* Author: SusanaEca
*
* Created on 26 de Março de 2015, 19:26
*/
//Libraries
#include "p24fxxxx.h"
#include "i2c.h"
#include "leds.h"

//Calculate baud rate of I2C
/*
* I2CxBRG = (FCY/FSCL - FCY/10,000,000) - 1
* Based on FCY = FOSC/2; Doze mode and PLL are disabled.
*/
#define Fosc (8000000) // crystal
#define Fcy (Fosc*4/2) // w.PLL (Instruction Per Second)
#define Fsck 400000 // 400kHz I2C
#define I2C1_BRG ((Fcy/2/Fsck)-1)

int main(void)
{
    //Variable declaration
    char SlaveAddress = 0x4;
    unsigned char *letras;
    unsigned char tx_data[] = {'P', 'I', '\0'};
    // char c='M';
    //Get your pointer to a variable
    letras = tx_data;
    //Set-up LEDs
    LED_Enable(LED_D6);
    LED_Enable(LED_D3);
    //Initialize I2C1
    OpenI2C1(I2C_ON, I2C1_BRG);

    while(1)
    {
        //Start I2C1 condition
        StartI2C1(); //Send the Start Bit
        IdleI2C1(); //Wait to complete
        //Write-to-Slave Address
        MasterWriteI2C1((SlaveAddress<<0)|0); //Send device advice adress byte to the slave with the write indication
        IdleI2C1(); //Wait to complete
        //If ACK is received...
        if (I2C1STATbits.ACKSTAT == 0)
        {
            LED_On(LED_D6); //Light up LED #6
            MasterputsI2C1(letras); //Send your string :)
            IdleI2C1(); //Wait to complete
            //If NACK (merda)...
            if (I2C1STATbits.ACKSTAT == 1) 
            {
                LED_On(LED_D3); //Light up LED #3
            }
        }
    }
}
 

Código Arduino (Slave):

// Wire Slave Receiver
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Receives data as an I2C/TWI slave device
// Refer to the "Wire Master Writer" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>

void setup()
{
    Wire.begin(4); // join i2c bus with address #4
    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
}
Adicionado tag, alterada formatação
Fonte Link
Giovani
  • 2,9mil
  • 1
  • 26
  • 54
Carregando
Pergunta reaberta por stderr, Vinícius Gobbo A. de Oliveira, OnoSendai, Erlon Charles, Felipe Avelar
removeu 18 caracteres do conteúdo
Fonte Link
Erlon Charles
  • 5,5mil
  • 11
  • 30
  • 48
Carregando
removeu 186 caracteres do conteúdo
Fonte Link
Carregando
Pergunta fechada como "Não é adequada para este site" por Largato, Giovani, Victor Stafusa, ramaral, Erlon Charles
Fonte Link
Carregando