View previous topic :: View next topic |
Author |
Message |
cheehow
Joined: 15 Sep 2010 Posts: 28
|
I2C master and slave problem (PIC16F877A) |
Posted: Wed Sep 22, 2010 10:01 pm |
|
|
Hi everyone, I have met some problem during the interfacing between master and slave connection. I'm using PIC C compiler (CCS C compiler) to write the codes. My project is to make the communication between two MCU (PIC16F877A) by using I2C.
The following are the code for master:
Code: |
#include "C:\Users\Tan Chee How\Desktop\testing I2C\master.h"
#define Device_SDA PIN_C3
#define Device_SCL PIN_C4
#USE I2C (MASTER, SDA=Device_SDA, SCL=Device_SCL)
int x;
BYTE Data;
void main()
{
SET_TRIS_C(0XFF);
SET_TRIS_B(0X00);
SET_TRIS_D(0X00);
OUTPUT_B(0X00);
WHILE(TRUE)
{
if(INPUT(PIN_D1))
OUTPUT_HIGH(PIN_B6);
delay_ms(500);
OUTPUT_LOW(PIN_B6);
delay_ms(500);
OUTPUT_HIGH(PIN_B7);
delay_ms(500);
OUTPUT_LOW(PIN_B7);
for(x=0; x<10; x++)
{
i2c_start();
i2c_write(0xA0);
i2c_write(x);
i2c_write(10-x);
i2c_stop();
delay_ms(100);
i2c_start();
i2c_write(0xA1);
Data=i2c_read(0);
i2c_stop();
delay_ms(100);
}
}
} |
the following are the slave code:
Code: |
#include "C:\Users\Tan Chee How\Desktop\testing I2C\slave.h"
#define Device_SDA PIN_C3
#define Device_SCL PIN_C4
#use i2c (slave, SDA= Device_SDA, SCL=Device_SCL,address=0xA0, FORCE_HW)
#INT_SSP
BYTE address, buffer[0x10];
void ssp_interupt ()
{
BYTE incoming, state;
state = i2c_isr_state();
if(state < 0x80)
{
incoming = i2c_read();
if(state == 1)
address = incoming;
if(state == 2)
buffer[address] = incoming;
}
if(state == 0x80)
{
i2c_write(buffer[address]);
}
}
void main ()
{
SET_TRIS_C(0XFF);
SET_TRIS_B(0X00);
SET_TRIS_D(0X00);
OUTPUT_B(0X00);
enable_interrupts(GLOBAL);
enable_interrupts(INT_SSP);
while (TRUE) {}
} |
Therefore, it doesn't work while downloaded into the MCU and test it in hardware. So, can anyone help me see whether which part of my programming having problem or mistake and give me the reply as soon as possible.
My hardware part is just two MCU connected with the pin that reserve for I2C and simple power up MCU connection. Besides, in order to show the I2C communication, I added in the LED light for port B and push button for port D1. When I press the push button of master MCU, the slave LED suppose will be lighting up.
Please help me to figure out what's the problem .... thx you very much !! |
|
|
arunb
Joined: 08 Sep 2003 Posts: 492 Location: India
|
RE: |
Posted: Wed Sep 22, 2010 11:26 pm |
|
|
what pull up resistances have you used in the SCL and SDA lines ?
The #use delay directive is also missing.
thanks
a |
|
|
cheehow
Joined: 15 Sep 2010 Posts: 28
|
|
Posted: Wed Sep 22, 2010 11:46 pm |
|
|
my pullup resistor used 1K.....
because the hex file inside already have built-in delay ... so that, i didnt write #use delay.
the following are the hex file that auto-generate by the ccs c compiler:
#include <16F877A.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES LP //Low power osc < 200 khz
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#FUSES WRT_50% //Lower half of Program Memory is Write Protected
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
thxs for helping me checking .... hope to hear ur good solution soon =) thxs !! =) |
|
|
cheehow
Joined: 15 Sep 2010 Posts: 28
|
|
Posted: Thu Sep 23, 2010 1:18 am |
|
|
I have modified my master code ...... please help me to check whether my master code have any problem or not ??
master code :
Code: |
#include "C:\Users\Tan Chee How\Desktop\testing I2C\master.h"
#define Device_SDA PIN_C3
#define Device_SCL PIN_C4
#USE I2C (MASTER, SDA=Device_SDA, SCL=Device_SCL)
#USE fast_io(C)
#USE standard_io(B)
#USE fast_io(D)
int x;
BYTE Data;
void main()
{
SET_TRIS_C(0X00);
SET_TRIS_D(0XFF);
OUTPUT_B(0X00);
WHILE(TRUE)
{
for(x=0x00; x<0x0A; x++)
{
i2c_start();
i2c_write(0xA0);
i2c_write(x);
i2c_write(0X0A-x);
i2c_stop();
delay_ms(100);
i2c_start();
i2c_write(0xA1);
Data=i2c_read(0);
i2c_stop();
delay_ms(100);
}
if(INPUT(PIN_D1))
OUTPUT_HIGH(PIN_B6);
delay_ms(500);
OUTPUT_LOW(PIN_B6);
delay_ms(500);
OUTPUT_HIGH(PIN_B7);
delay_ms(500);
OUTPUT_LOW(PIN_B7);
}
}
|
The slave code is taken from the ccs c compiler example one (EX_SLAVE.C) and modified a little bit.
Code: |
#include "C:\Users\Tan Chee How\Desktop\testing I2C\slave.h"
#define Device_SDA PIN_C3
#define Device_SCL PIN_C4
#use i2c (slave, SDA= Device_SDA, SCL=Device_SCL,address=0xA0, FORCE_HW)
#INT_SSP
BYTE address, buffer[0x10];
void ssp_interupt ()
{
BYTE incoming, state;
state = i2c_isr_state();
if(state < 0x80)
{
incoming = i2c_read();
if(state == 1)
address = incoming;
if(state == 2)
buffer[address] = incoming;
}
if(state == 0x80)
{
i2c_write(buffer[address]);
}
}
void main ()
{
SET_TRIS_C(0XFF);
SET_TRIS_B(0X00);
SET_TRIS_D(0X00);
OUTPUT_B(0X00);
enable_interrupts(GLOBAL);
enable_interrupts(INT_SSP);
while (TRUE) {}
} |
Please help me to check whether I have any problem for my code or not ... thxs for checking =) |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Thu Sep 23, 2010 2:01 am |
|
|
You are cross posting the same problem in 2 threads, I sugest you stop posting in the multimaster problem thread as it is nothing to do with multimaster and just stick with this thread.
You are still trying to use fast_io on port C which your I2C resides on.
The data line on the I2C is bidirectional, You are forcing it to be output only using set_tris and fast_io, I don't know if the code generated will still control the port properly if you are using fast_io on it, I would expect it to.
BUT, why are you trying to use fast io anyway, try removing the fast_io and set_tris lines and see if that helps.
How do you know it is not working ? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Thu Sep 23, 2010 2:02 am |
|
|
The file created by the compiler, is an 'include' file, not a 'hex' file. It is _not_ in 'hex'.....
It is part of the program, and is essential.
The master one, has some major problems.
It creates what you 'ask' for in the wizard, and if what you put are impossible combinations, these will be used.
Do you think it is possible for the 'LP' oscillator (designed for sources under 200KHz), to run at 20MHz?.
Have you got a 20MHz crystal attached, with the right capacitors for the loading capacitance (these are _not_ the same as the loading capacitance value, and will depend on your board layout....)?.
What is in the slave include file?.
Best Wishes |
|
|
cheehow
Joined: 15 Sep 2010 Posts: 28
|
|
Posted: Thu Sep 23, 2010 7:50 am |
|
|
wayne, ok ok .. i will try it out ... thxs =)
Ttelmah, u means if i use 20MHz crystal oscillator it wouldnt work ??
my slave include is:
#include <16F877A.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES LP //Low power osc < 200 khz
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#FUSES WRT_50% //Lower half of Program Memory is Write Protected
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8
so my main problem is in my low power oscillator ?? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Thu Sep 23, 2010 9:23 am |
|
|
Have you actually got a crystal oscillator (external module with it's own power supply connections), or a crystal?.
Best Wishes |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Sep 23, 2010 10:59 am |
|
|
Quote: | Ttelmah, u means if i use 20MHz crystal oscillator it wouldnt work ??
|
Quote: | #FUSES LP //Low power osc < 200 khz | Have a look at the text shown in bold, then what do you think yourself? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Sep 23, 2010 12:45 pm |
|
|
Quote: | The slave code is taken from the ccs c compiler example one
(EX_SLAVE.C) and modified a little bit. |
Not true. You have changed the order of some lines in the Ex_Slave.c
example, and the order of the lines is very important. Your changes have
disabled the interrupt routine. Compare your code below with the
Ex_slave.c code in the same section. You will notice a important change
that you have done. Put it back the way it was. Then your program will
work better.
Quote: |
#use i2c (slave, SDA= Device_SDA, SCL=Device_SCL,address=0xA0, FORCE_HW)
#INT_SSP
BYTE address, buffer[0x10];
void ssp_interupt ()
{
|
|
|
|
cheehow
Joined: 15 Sep 2010 Posts: 28
|
|
Posted: Thu Sep 23, 2010 9:29 pm |
|
|
PCM programmer : I have used bac the exactly same with the example codes and the void main part I have written some LED to response the i2c communication, but it doesn't work also.
ckielstra: yaya .... if I'm using 20Mhz crystal oscillator, it won't be worked right ??
Ttelmah: is crystal oscillator .... for external connection in order to power up the MCU ...... (pin 13 and 14) .... |
|
|
|