Buonasera,
sto tentando di far comunicare un pic16F876A in cuna periferica via I2C.
Ho scritto questi 2 file
I2C.C
/*
#include <xc.h> // include processor files - each processor file is guarded.
#include <stdint.h>
#include <i2c.h>
void I2C_Initialize(const unsigned long feq_K) //Begin IIC as master
{
//PIN 18 -> RC3 -> SCL
//PIN 23 -> RC4 ->SDA
//Set SDA and SCL pins as input pins
TRISCbits.TRISC3 = 1;
TRISCbits.TRISC4 = 1;
SSPCON = 0b00101000; //pg84/234
SSPCON2 = 0b00000000; //pg85/234
SSPADD = (uint8_t)(_XTAL_FREQ/(4*feq_K*100))-1; //Setting Clock Speed pg99/234
SSPSTAT = 0b00000000; //pg83/234
}
void I2C_Hold()
{
while ( (SSPCON2 & 0b00011111) || (SSPSTAT & 0b00000100) ) ; //check the bis on registers to make sure the IIC is not in progress
}
void I2C_Begin()
{
I2C_Hold(); //Hold the program is I2C is busy
SSPCON2bits.SEN = 1; //Begin IIC pg85/234
}
void I2C_End()
{
I2C_Hold(); //Hold the program is I2C is busy
SSPCON2bits.PEN = 1; //End IIC pg85/234
}
void I2C_Write(unsigned data)
{
I2C_Hold(); //Hold the program is I2C is busy
SSPBUF = data; //pg82/234
}
unsigned short I2C_Read(unsigned short ack)
{
unsigned short incoming;
I2C_Hold();
SSPCON2bits.RCEN = 1;
I2C_Hold();
incoming = SSPBUF; //get the data saved in SSPBUF
I2C_Hold();
SSPCON2bits.ACKDT = (ack)?0:1; //check if ack bit received
SSPCON2bits.ACKEN = 1; //pg 85/234
return incoming;
}
e I2C.h
#ifndef XC_HEADER_TEMPLATE_H
#define XC_HEADER_TEMPLATE_H
#include <xc.h> // include processor files - each processor file is guarded.
void I2C_Initialize(const unsigned long feq_K);
void I2C_Hold();
void I2C_Begin();
void I2C_End();
void I2C_Write(unsigned data);
unsigned short I2C_Read(unsigned short ack);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* XC_HEADER_TEMPLATE_H */
main.c
#include <xc.h>
#include <i2c.h>
#define _XTAL_FREQ 20000000 // refenrence freq for the compiler
// PIC16F876A Configuration Bit Settings
// 'C' source line config statements
// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
void main(void) {
I2C_Initialize(100); //Initialize I2C Master with 100KHz clock
while(1)
{
I2C_Begin();
I2C_Write(0xD0);
I2C_Write(0x88);
I2C_Write(0xFF);
I2C_End();
__delay_ms(1000);
}
return;
}
ora nel programma principale includo il file I2C.h e quando compilo mi viene mostrato questo errore :
newmain.c:31: warning: (1464) number of arguments passed to function "_I2C_Initialize" does not match function's prototype
:0: error: (500) undefined symbols:
_I2C_Write(dist/default/production\Test_I2C.X.production.obj) _I2C_Begin(dist/default/production\Test_I2C.X.production.obj) _I2C_End(dist/default/production\Test_I2C.X.production.obj) _I2C_Initialize(dist/default/production\Test_I2C.X.production.obj)
make[2]: *** [dist/default/production/Test_I2C.X.production.hex] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
(908) exit status = 1
nbproject/Makefile-default.mk:132: recipe for target 'dist/default/production/Test_I2C.X.production.hex' failed
make[2]: Leaving directory 'D:/MPLABXProject/Test_I2C.X'
nbproject/Makefile-default.mk:91: recipe for target '.build-conf' failed
make[1]: Leaving directory 'D:/MPLABXProject/Test_I2C.X'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
Se invece di includere il file I2C.h includo il file I2c.c tutto funziona. Dove sbaglio secondo voi?
Grazie per l'aiuto