CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Communication between two 12f1822 via I2c

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
hamid9543



Joined: 31 Jan 2013
Posts: 63

View user's profile Send private message

Communication between two 12f1822 via I2c
PostPosted: Sun Feb 11, 2018 4:37 pm     Reply with quote

slave does not receive data

Master
Code:

#include <12F1822.h>
#use delay(internal=32000000)
#use i2c(Master,Fast,sda=PIN_A2,scl=PIN_A1,force_hw)

void main()
{
   while(TRUE)
   {
   i2c_start();                           
   i2c_write(0xa0);                         
   i2c_write(0x01);               
   i2c_stop();                               
   }
}


and Slave
Code:

#include <12F1822.h>
#fuses NOPUT,NOWDT,NOBROWNOUT
#use delay(internal=32000000)
#use i2c(Slave,Fast,sda=PIN_A2,scl=PIN_A1,force_hw,address=0xa0)
#use rs232(baud=9600,parity=N,xmit=PIN_A0,rcv=PIN_A3,bits=8,stream=PORT1)
int8 z=0;


#INT_SSP
void ssp_interrupt()
{
output_high(pin_A5);
 //  int state = i2c_isr_state();
   
 //  if(state < 0x80)     // Master is sending data
 //  {
     z=i2c_read();
 //  }
}

void main()
{
   enable_interrupts(GLOBAL);
   enable_interrupts(INT_SSP);
   while(TRUE)
   {
    printf("%u\r",z);
    if (z==1) output_high(pin_A5);
    if (z==0) output_low(pin_A5);
   }
}
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Feb 11, 2018 5:11 pm     Reply with quote

Whenever you're doing I2C, download/compile/run PCM P's "I2C Scanner" program from the code library. It's a 'must run' as it'll show IF the slave is really there, and where.
Have you got the correct I2C bus pullup resistors ? 3 factors, speed, distance, voltage... all play a part in determining the proper value.
Confirm the I2C wires aren't crossed ! Silly mistake like that can spoil your day...

Do the PICs run at their proper speed ?
2nd program to cut/compile/run is a '1Hz LED' program. This will comfirm the hardware is running at the proper speed.

You'll need to confirm these BEFORE trying to debug your code.
Jay
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Feb 12, 2018 12:05 am     Reply with quote

Your i2c slave program has some hidden interrupt overhead. I calculate
that at 32 MHz, the total time to execute the interrupt routine is about
6 usec.

Your master code is a tight loop. You need to put a delay in there to give
the slave's interrupt routine enough time to finish, before you start a new
transmission. Let's be really conservative and stick a 100 usec delay in
there. Try this and see if it works better:
Quote:
while(TRUE)
{
i2c_start();
i2c_write(0xa0);
i2c_write(0x01);
i2c_stop();
delay_us(100);
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Mon Feb 12, 2018 4:42 am     Reply with quote

Also, as written, there is a problem, since the first I2C_read, will return the device address, which is the reason for the I2C_isr_state test, which allows you to know 'where' you are in each transaction....

Last edited by Ttelmah on Mon Feb 12, 2018 7:37 am; edited 1 time in total
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Feb 12, 2018 5:54 am     Reply with quote

As a follow up, I'd make the master main 'delay loop' to be slower, say 1 second. If you've got an LED on the slave's pin A5 then you'll SEE the on/off response. Now if you have a scope, this won't matter as much.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Mon Feb 12, 2018 7:55 am     Reply with quote

and (of course) to actually show anything:

Code:


#include <12F1822.h>
#use delay(internal=32000000)
#use i2c(Master,Fast,sda=PIN_A2,scl=PIN_A1,force_hw)

void main()
{
   int8 toggle=0;
   while(TRUE)
   {

       i2c_start();                           
       i2c_write(0xa0);                         
       i2c_write(toggle);               
       i2c_stop();   
       delay_ms(1000);
       toggle ^=1; //change the toggle                           
   }
}
hamid9543



Joined: 31 Jan 2013
Posts: 63

View user's profile Send private message

PostPosted: Mon Feb 19, 2018 11:18 am     Reply with quote

does i2c code work properly in proteus simulation?
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Mon Feb 19, 2018 12:00 pm     Reply with quote

On _some_ chips it does. However not all.
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Feb 19, 2018 1:03 pm     Reply with quote

probably 'works' without the pullups on the bus ..sigh... Rolling Eyes Crying or Very sad Laughing
hamid9543



Joined: 31 Jan 2013
Posts: 63

View user's profile Send private message

PostPosted: Sun Feb 25, 2018 6:54 am     Reply with quote

Ttelmah wrote:
On _some_ chips it does. However not all.


test 12f1829 and 16f877a and 16f873 but it was not successful Sad Sad Confused Confused Question
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Feb 25, 2018 7:01 am     Reply with quote

re:
Quote:

test 12f1829 and 16f877a and 16f873 but it was not successful

on REAL PICs or just the 'simulation' ?

we need to know that information !
hamid9543



Joined: 31 Jan 2013
Posts: 63

View user's profile Send private message

PostPosted: Tue Feb 27, 2018 9:05 am     Reply with quote

temtronic wrote:
re:
Quote:

test 12f1829 and 16f877a and 16f873 but it was not successful

on REAL PICs or just the 'simulation' ?

we need to know that information !

Not working in simulation! Work in real pic.
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Feb 27, 2018 9:12 am     Reply with quote

Well that's good news !!
Works with real PICs so get rid of the 'simulator' as it is defective !!
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Tue Feb 27, 2018 1:03 pm     Reply with quote

Unfortunately, I2C is one of the things Proteus handles badly.
As a master it is reasonable. As a slave, poor.
It handles simple serial OK.
Basic SPI also usually OK.
Any more complex peripheral it has problems.
Getting people to understand that it doesn't work, seems surprisingly hard... Sad
newguy



Joined: 24 Jun 2004
Posts: 1899

View user's profile Send private message

PostPosted: Tue Feb 27, 2018 1:09 pm     Reply with quote

Ttelmah wrote:
Getting people to understand that it doesn't work, seems surprisingly hard... Sad


I'm struck by what the populace will believe or choose not to believe. Like convincing some people that vaccinating their kids is a good thing = hard, but convincing them that it's a bad thing = easy. Sad
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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