XC8 pic16f876a I2C从器件

时间:2014-04-20 15:09:38

标签: i2c slave xc8

如何实现代码在奴隶模式下使用峰值?我来找你,因为我没有在网上找到满意的搜索答案。

对于外包PIC16FXXX的订单,我是一个经验丰富的列车原则。目标是卸载不太紧急的任务的电子地图。

为此,我会使用I2C。在那之前,我只是驱动已经建立的组件。现在,我有自己的组件。

你可能会对我说,所有废话都没用,但知道你要去哪里很重要。

所以我用MPLAB和可靠的编译器XC8 Hi-Tech在我的PC和我的照片之间设置了一个工具链。

我找到一个很好的代码示例,将数据发送到组件似乎已经完成了。我无法控制我的PIC。

这是我在C中编写的(编译没问题): 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
    }
}

沟通示例: 开始位 11110000 - &gt; 1111000(代码)+ 0(写) ACK(奴隶) 00000001 - &gt; LED 7 = 0/6 = 0 Led / Led 5 = 0/4 = 0 Led / Led 3 = 0/2 = 0 LED / LED 1 = 0/0 = 1 Led 停止位

等待大师

开始位 11110000 - &gt; 1111000(代码)+ 0(写) ACK(奴隶) 00000000 - &gt; LED 7 = 0/6 = 0 Led / Led 5 = 0/4 = 0 Led / Led 3 = 0/2 = 0 LED / LED 1 = 0/0 = 0 Led 停止位

等待主人 环

以下是我现在提出的问题。 如何实现代码在从模式下使用峰值?

非常感谢你的帮助。

0 个答案:

没有答案