0

How to implement the code to use the peak in slave mode?I come to you because I have not found on the web a satisfactory answer to my search.

I am an experienced train principle on orders outsourced PIC16FXXX. The goal is to unload the electronic map of the less urgent tasks.

To do this, I would use the I2C. Until then, I merely driven components already established. And now, I have my own component.

You might say to me that all the crap is useless but it is important to know where you are going.

So I set up a toolchain cross between my PC and my pic with MPLAB and reliable compiler XC8 Hi-Tech.

I Locate a good example of code and it seems complete to send data to a component. I have not been able to receive control on my PIC.

This is what I wrote in C (the compilation is ok): i2c.h

 #define I2C_WRITE 0
#define I2C_READ 1

//Initialise MSSP port
void i2c_Init(void) {
    //Initialise I2C MSSP
    //Master 100KHz
    TRISA1 = 1; //Set SCL et SDA pins as inputs
    TRISA2 = 1;

    SSPCON = 0b00101000;    //I2C enabled Master mode
    SSPCON2 = 0x00;
    //I2C Master mode, clock = FOSC/(4 * (SSPADD + 1))
    SSPADD = 39;    //100KHz @ 16MHz Fosc

    SSPSTAT = 0b11000000;   //Slew rate disabled
}

//i2c_Wait - Wait for I2C transfet to finish
void i2c_Wait(void) {
    while((SSP1CON2 & 0x1F) || (SSPSTAT & 0x004));
}

//i2c_Start - Start I2C communication
void i2c_Start(void) {
    i2c_Wait();
    SEN = 1;
}

//i2c_Restart - Re-Start I2C communication
void i2c_Restart(void) {
    i2c_Wait();
    RSEN = 1;
}

//i2c_Stop - Stop I2C communication
void i2c_Stop(void) {
    i2c_Wait();
    PEN = 1;
}

//i2c_Write - Sends on byte of data
void i2c_Write(unsigned char data) {
    i2c_Wait();
    SSPBUF = data;
}

//i2c_Adress - Send Slave Adress and Read/Write mode
//mode is either I2C_WRITE or I2C_READ
void i2c_Adress(unsigned char adress, unsigned char mode) {
    unsigned char l_adress;
    l_adress = adress << 1;
    l_adress += mode;
    i2c_Wait();
    SSPBUF = l_adress;
}

//i2c_Read - Reads a byte from Slave device
unigned char i2c_Read(unsigned char ack) {
    //Read data from slave
    //ack should be 1 if there is going to be more data read
    //ack should be 0 if the last byte of data read
    unsigned char i2cReadData;

    i2c_Wait();
    RCEN = 1;
    i2c_Wait();
    i2cReadData = SSPBUF;
    i2c_Wait();
    if(ack) ACKDT = 0;  //Ack
    else    ACKDT = 1;  //NAck
    ACKEN = 1;

    return (i2cReadData);
}

main.c

//Includes
#include <xc.h>
#include <pic16f876a.h>
#include "i2c.h"

//Variables

//Interruptions

//Conf
#pragma config WDTE=OFF, FOSC=XT
#define _XTAL_FREQ 20000000

//Programme principal
void main(void) {

    //Init
    //Port A (out) -> led

    //Wait Num 11110000
    //ACK
    //Wait Write fo exemple 00000001 (led ON)
    //ACK
    //Wait STOP

    //Loop
    while(1) {

        //if Write (00000001)
            //Led ON
        //else
            //Led OFF
    }
}

Example of communication: Start bit 11110000 -> 1111000 (code) + 0 (write) ACK (slave) 00000001 -> LED 7 = 0/6 = 0 Led / Led 5 = 0/4 = 0 Led / Led 3 = 0/2 = 0 LED / LED 1 = 0/0 = 1 Led Stop bit

wait Master

Start bit 11110000 -> 1111000 (code) + 0 (write) ACK (slave) 00000000 -> LED 7 = 0/6 = 0 Led / Led 5 = 0/4 = 0 Led / Led 3 = 0/2 = 0 LED / LED 1 = 0/0 = 0 Led Stop bit

wait master Loop

Here are question I pose now. How to implement the code to use the peak in slave mode?

Thank you very much for your help.

4

0 回答 0