|
|
View previous topic :: View next topic |
Author |
Message |
whung.john Guest
|
use i2c question! |
Posted: Sat Sep 02, 2006 12:10 pm |
|
|
hi!
have body can help me?
i set master send a nember for slave
but slave not keep master send currectly number
how to fix error area ,thank for u
i question is down area:
i2c master:
#include <16F77.h>
#use delay(clock=4000000)
#fuses XT,NOWDT
#use i2c(Master,sda=PIN_C4,scl=PIN_C3,FAST)
INT8 SP;
#int_timer1
void timer1_isr(void)
{
i2c_start();
i2c_write(0xA0);
i2c_write(1);
i2c_stop();
}
void main()
{
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
enable_interrupts(int_timer1);
enable_interrupts(global);
while(1)
{
SP=1;
//output_high (PIN_B7);
//DELAY_MS(1000);
//output_LOW (PIN_B7);
}
}
i2c slave:
#include <16F77.h>
#use delay(clock=4000000)
#fuses XT,NOWDT
#use i2c(SLAVE,sda=PIN_C4,scl=PIN_C3,address=0XA0,FAST)
#include "lcd.c"
INT8 ST;
#int_ssp
ssp_isr()
{
IF (I2C_POLL())
{
ST=i2c_read();
LCD_GOTOXY(1,1); PRINTF(LCD_PUTC,"\fST1=%4C",ST);
}
}
void main()
{
LCD_INIT();
enable_interrupts(int_ssp);
enable_interrupts(global);
WHILE(1)
{
output_high (PIN_B6);
DELAY_MS(2000);
output_low (PIN_B6);
}
} |
|
|
Ttelmah Guest
|
|
Posted: Sun Sep 03, 2006 6:44 am |
|
|
Without looking to deeply,_get rid of the printf in the slave_.
The slave will interrupt the first time, when it receives the _address_. At this point it'll start printing, and depending on the baud rate (which you don't show), take about 10mSec to display the value. At this point the number following will have been missed. Repeat the mantra _keep interrupt handlers fast_. It is _essential_.
Also, you will never 'see' the number. %C, will print the value as an ascii character, and character 1 (depending on your font), will be some odd graphic symbol. Move the print to the main code. have it activated whan a character is seen, and then use %d, rather than %c.
Best Wishes |
|
|
thanks for Ttelmah Guest
|
yat question! |
Posted: Sat Sep 09, 2006 12:01 pm |
|
|
Ttelmah wrote: | Without looking to deeply,_get rid of the printf in the slave_.
The slave will interrupt the first time, when it receives the _address_. At this point it'll start printing, and depending on the baud rate (which you don't show), take about 10mSec to display the value. At this point the number following will have been missed. Repeat the mantra _keep interrupt handlers fast_. It is _essential_.
Also, you will never 'see' the number. %C, will print the value as an ascii character, and character 1 (depending on your font), will be some odd graphic symbol. Move the print to the main code. have it activated whan a character is seen, and then use %d, rather than %c.
Best Wishes |
***************************************************
thks for your answear!
but i rewrited have find other question!but i make a simple code,
blow here!
i2C master code :
#include <16F77.h>
#use delay(clock=4000000)
#fuses XT,noWDT
#use i2c(Master,sda=PIN_C4,scl=PIN_C3,slow)
int8 dk;
void main()
{
while(1)
{
i2c_start();
i2c_write(0xA0);
i2c_write('a');
i2c_stop();
i2c_start();
i2c_write(0xA1);
dk=i2c_read();
i2c_stop();
output_high (PIN_B7);
DELAY_MS(1000);
output_LOW (PIN_B7);
}
}
slave code ,here
#include <16F77.h>
#use delay(clock=4000000)
#fuses XT,WDT
#include "lcd.c"
#use i2c(SLAVE,address=0XA0,sda=PIN_C4,scl=PIN_C3,slow)
INT8 GG;
void main()
{
int i;
WHILE(!I2C_POLL())
{
GG=i2c_read();
BREAK;
}
LCD_INIT();
DELAY_MS(200);
LCD_GOTOXY(1,1); PRINTF(LCD_PUTC,"\fGG=%d",GG);
}
but look at LCD PANEL,GG NUMBER GET A NUMBER (-96)
,I DO'T IMPROVE THE QUESTION!
SIR I NEED TO HELP !PLEASE TEACH MY ! OK! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Sep 09, 2006 12:28 pm |
|
|
To debug an i2c interface, you should use "%X" in printf, which will
display the bytes as hexadecimal. For example, -96 in decimal
is A0 in hexadecimal. Do you see why it's better to use "%X" ? |
|
|
WHUNG.JOHN Guest
|
yet have question! |
Posted: Mon Sep 11, 2006 6:06 am |
|
|
PCM programmer wrote: | To debug an i2c interface, you should use "%X" in printf, which will
display the bytes as hexadecimal. For example, -96 in decimal
is A0 in hexadecimal. Do you see why it's better to use "%X" ? |
*****************************************
hi pcmgrammer sir !
i read u teach method addtion %X into programm,but i
dont continue to get other number ,can u help me
,rewritten the programm how to made,ok! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Sep 11, 2006 10:28 am |
|
|
Every day you post another new thread, instead of asking for more help
on this existing thread.
My suggesting is to study the CCS example for an i2c slave. The file
is called EX_SLAVE.C and it's in this folder:
c:\program files\picc\examples |
|
|
WHUNG.JOHN Guest
|
tks u answear |
Posted: Tue Sep 12, 2006 6:13 am |
|
|
PCM programmer wrote: | Every day you post another new thread, instead of asking for more help
on this existing thread.
My suggesting is to study the CCS example for an i2c slave. The file
is called EX_SLAVE.C and it's in this folder:
c:\program files\picc\examples |
**********************************************************
sorry i post more thread , to lead u angry ,i shell change aberration.
but i readed the ex_slave.c ,but i try angin into my programm.
can get a number (a0) address,but i am command i2c_read(),the second
command dont operate,and send the next number can my lcd panel display.
the message how can i correction.
thks u .
but i have a question. i want to purchase icd-s or icd-u.
please tell me ,how can i purchase,ATM OR OTHER ! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Sep 12, 2006 1:11 pm |
|
|
If I worked on your problem, I would take the CCS example file,
Ex_Slave.c, compile it, and program it into a 16F877 on one of my
PicDem2-Plus boards. Then I would create another program to
send i2c commands to the slave board. I would jumper the two
boards together. Then I would make a post on here and announce
to you that it all worked.
OK, I had this idea. Instead of me doing all this, why don't you do it ?
You are the one studying i2c, so you should test the CCS example files
and learn from them. |
|
|
whung.john Guest
|
tks very much !proffer(pcm programmer) |
Posted: Thu Sep 14, 2006 8:24 pm |
|
|
PCM programmer wrote: | If I worked on your problem, I would take the CCS example file,
Ex_Slave.c, compile it, and program it into a 16F877 on one of my
PicDem2-Plus boards. Then I would create another program to
send i2c commands to the slave board. I would jumper the two
boards together. Then I would make a post on here and announce
to you that it all worked.
OK, I had this idea. Instead of me doing all this, why don't you do it ?
You are the one studying i2c, so you should test the CCS example files
and learn from them. |
*******************************************************
i listen u proffer ,and practice the example file(EX_slave.c).
but the program to execute,the master is program as follows:
i use chip is pic16f77 and use i2c communication.
while(1)
{
i2c_start();
i2c_write(0xa0)
i2c_write("a")
i2c_stop();
}
but my slave addtion LCD panel display,so i can read number right
,the program then same is EX_slave,
i see a curious things,the lcd display only send a number A0.and
when my master send the second number "a",the slave dont receive
the word,
cleverly sir,can u answer my question.
wait for u answear. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Sep 14, 2006 9:31 pm |
|
|
Quote: | while(1)
{
i2c_start();
i2c_write(0xa0)
i2c_write("a")
i2c_stop();
} |
1. You don't have an i2c_write() statement for the address of the
byte in the eeprom. You need to add this statement. See the
program below.
2. You need to put the letter in single quotes, like this: i2c_write('a');
See the program below.
Also:
1. Don't use a letter such as 'a' for the data, because it can be confused
with the address of the chip (0xa0). Use some other letter for the data.
See the program below which shows some corrections for your code.
The following program works OK with EX_SLAVE.C:
Code: |
#include <16F877.H>
#fuses XT, NOWDT, PROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#use i2c(Master, sda=PIN_C4, scl=PIN_C3)
//====================================
void main()
{
int8 data;
// Write the letter 'B' to the slave board.
i2c_start();
i2c_write(0xA0);
i2c_write(0x00);
i2c_write('B');
i2c_stop();
// Read from the slave board and display the data.
i2c_start();
i2c_write(0xA0);
i2c_write(0x00);
i2c_start();
i2c_write(0xA1);
data = i2c_read(0);
i2c_stop();
printf("read %c \n\r", data);
while(1);
} |
|
|
|
WHUNG.JOHN Guest
|
TKS U PROGRAMMER |
Posted: Fri Sep 15, 2006 12:01 pm |
|
|
PCM programmer wrote: | Quote: | while(1)
{
i2c_start();
i2c_write(0xa0)
i2c_write("a")
i2c_stop();
} |
1. You don't have an i2c_write() statement for the address of the
byte in the eeprom. You need to add this statement. See the
program below.
2. You need to put the letter in single quotes, like this: i2c_write('a');
See the program below.
Also:
1. Don't use a letter such as 'a' for the data, because it can be confused
with the address of the chip (0xa0). Use some other letter for the data.
See the program below which shows some corrections for your code.
The following program works OK with EX_SLAVE.C:
Code: |
#include <16F877>
#fuses XT, NOWDT, PROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#use i2c(Master, sda=PIN_C4, scl=PIN_C3)
//====================================
void main()
{
int8 data;
// Write the letter 'B' to the slave board.
i2c_start();
i2c_write(0xA0);
i2c_write(0x00);
i2c_write('B');
i2c_stop();
// Read from the slave board and display the data.
i2c_start();
i2c_write(0xA0);
i2c_write(0x00);
i2c_start();
i2c_write(0xA1);
data = i2c_read(0);
i2c_stop();
printf("read %c \n\r", data);
while(1);
} |
|
****************************************************
TKS u very much pcm progammer ,i learned i2c communication
agreement with u ,my progamm corrections as follws;
master progam:
#include <16F77.h>
#fuses XT, NOWDT, PROTECT, BROWNOUT, PUT
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#use i2c(Master, sda=PIN_C4, scl=PIN_C3,SLOW)
//====================================
void main()
{
WHILE(1)
{
i2c_start();
i2c_write(0xA0);
DELAY_MS(100);
i2c_write('5');
DELAY_MS(100);
i2c_write('H');
DELAY_MS(500);
i2c_stop();
DELAY_MS(200);
}
}
slave progamm:
include <16F77.h>
#use delay(clock=4000000)
#fuses XT,NOWDT
#include "lcd.c"
#use i2c(SLAVE,address=0XA0,sda=PIN_C4,scl=PIN_C3,slow)
INT8 GG;
void main()
{
int i;
LCD_INIT();
WHILE(1)
{
IF (I2C_POLL()==TRUE)
{
GG=i2c_read();
LCD_GOTOXY(1,1); PRINTF(LCD_PUTC,"\fGG=%C",GG);
}
}
}
the progamm is right ,and lcd panel can display number right,
i think to simplify,do u (pcm progammer) have time teach?
gramercy for pcm progammer . |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Sep 15, 2006 12:27 pm |
|
|
Quote: | The progam is right. I think to simplify. |
It looks very simple right now. You only have two lines of i2c code in
the slave.
I don't really want to teach i2c. I like to help people solve immediate
problems, so their project can move forward. I don't want to do the
whole project for them. |
|
|
domdom
Joined: 06 Sep 2006 Posts: 29
|
|
Posted: Wed Mar 21, 2007 8:19 pm |
|
|
Hi, PCM_programmer,
Does this code work for master 16f877a? I pulled up two SDA and SCL with 4.7K resistor and program the slave chip(16f877a) with EX_SLAVE.c (without any modification)
But i failed to get the communication working. Kindly please give some comment
Code: |
#include <16F877>
#fuses HS, NOWDT, PROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=20000000)
#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#use i2c(Master, sda=PIN_C4, scl=PIN_C3)
//====================================
void main()
{
int8 data;
// Write the letter 'B' to the slave board.
i2c_start();
i2c_write(0xA0);
i2c_write(0x00);
i2c_write('B');
i2c_stop();
// Read from the slave board and display the data.
i2c_start();
i2c_write(0xA0);
i2c_write(0x00);
i2c_start();
i2c_write(0xA1);
data = i2c_read(0);
i2c_stop();
printf("read %c \n\r", data);
while(1);
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Mar 21, 2007 8:49 pm |
|
|
It should work with Ex_Slave.c. It's basically similar to the master code
that I posted in this link:
http://www.ccsinfo.com/forum/viewtopic.php?t=28097&start=9
Make sure that you:
1. Have SDA on the Slave connected to SDA on the master.
2. Have SCL on the Slave connected to SCL on the master.
3. Have a ground connection between the two boards.
4. Have one pull-up resistor on SDA, and one on SCL.
If it doesn't work, then post your compiler version. |
|
|
domdom
Joined: 06 Sep 2006 Posts: 29
|
|
Posted: Thu Mar 22, 2007 6:18 am |
|
|
Hi PCM programmer,
I program the master.c code in my master PIC , and EX_SLAVE.c in my slave PC.
the output of terminal is like below:
[img]
http://img144.imageshack.us/img144/5611/outputuc3.gif
[/img]
The code seems like not working. May be the baudrate i used?
Can you check again?! |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|