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

i2c --Master read from slave's analog input-- problem

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



Joined: 08 Jan 2011
Posts: 7

View user's profile Send private message

i2c --Master read from slave's analog input-- problem
PostPosted: Sun Jan 09, 2011 10:14 am     Reply with quote

hi,
I have 3 pic16F877A. One of them is master and the others is slave.
The Slave pics read analog data from their own I/O.

The Master pic will read analog data from slave pics by i2c. And show it an LCD that is connected to the master pic.
I use 1k resistor to pull-up for SCL and SDA line

unfortunately, I even couldn't connect one slave to one master.
I don't know if there is any error with my codes or Proteus doesn't show anything. Please help...

MASTER CODE:
Code:

#include <16f877a.h>
#fuses HS,NOLVP,NOWDT,NOPROTECT
#use delay(clock=4000000)
#use I2C(FAST, SCL=PIN_C3, SDA=PIN_C4, FORCE_HW)

#define use_portb_lcd True
#include <lcd.c>

float data;

void main(void)
{
   lcd_init();
   set_tris_c(0xFF);
   while(TRUE)
   {
      i2c_start();
         delay_ms(2);

      i2c_write(0xE0);  //send the slave address
         delay_ms(2);

      i2c_write(0xE1);  //send R/W bit for reading
         delay_ms(2);

      i2c_start();      //make the slave to send data
         delay_ms(2);

      data=i2c_read(); //Read  the data
         delay_ms(2);
      i2c_stop();
           
      printf(lcd_putc,"\f data=%f",data);
      delay_ms(5000);
   }
}


SLAVE-1 CODE:
Code:

#include <16f877A.h>
#device adc=8
#fuses HS,NOLVP,NOWDT,NOPROTECT
#use delay(clock=4000000)

#use i2c(SLAVE, FAST, SCL=PIN_C3, SDA=PIN_C4, address=0xE0, FORCE_HW)
#use fast_io(c)
#use fast_io(d)

BYTE state;
int8 adc_result;

#INT_SSP    //Interrupt for I2C activity
void sspinterupt()
{
   disable_interrupts(INT_SSP);
   
   state = i2c_isr_state();
   
   if(state == 0x80)                //master is requesting data
   {
      i2c_write (adc_result);  //send requested data
   }

}

void main(void)
{
   setup_psp(PSP_DISABLED);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_CCP1(CCP_OFF);
   setup_CCP2(CCP_OFF);
   
   set_tris_c(0xFF);
   
   enable_interrupts(INT_SSP);
   enable_interrupts(GLOBAL);
   
   setup_adc(adc_clock_div_32);
   setup_adc_ports(all_analog);
   
   while(TRUE)
   {
      set_adc_channel(0);
      delay_us(10);
     
      adc_result=read_adc();
      delay_us(10);
     
      enable_interrupts(INT_SSP);
   }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jan 09, 2011 1:35 pm     Reply with quote

I think your program is based on my code in this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=39242&start=6
But you have made many, many changes to the code.
Try it with the sample code without any changes.
isikmurat06



Joined: 08 Jan 2011
Posts: 7

View user's profile Send private message

PostPosted: Mon Jan 10, 2011 2:25 am     Reply with quote

I tried without any changes, but I couldn't make it work. So, I have made a little changes with your codes.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jan 10, 2011 12:01 pm     Reply with quote

Quote:
I don't know if there is any error with my codes or Proteus doesn't show anything.

Read this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=44199&start=5
isikmurat06



Joined: 08 Jan 2011
Posts: 7

View user's profile Send private message

PostPosted: Tue Jan 11, 2011 2:15 am     Reply with quote

My proteus version is 7.6 sp2
CCS C version is 4.093
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jan 11, 2011 12:40 pm     Reply with quote

I don't have Proteus, so I can't test it for you. I can't help you any more
on this problem.
matrix86



Joined: 15 Feb 2011
Posts: 2

View user's profile Send private message MSN Messenger

Re: i2c --Master read from slave's analog input-- problem
PostPosted: Tue Feb 15, 2011 5:24 am     Reply with quote

isikmurat06 wrote:
hi,
I have 3 pic16F877A. One of them is master and the others is slave.
The Slave pics read analog data from their own I/O.

The Master pic will read analog data from slave pics by i2c. And show it an LCD that is connected to the master pic.
I use 1k resistor to pull-up for SCL and SDA line

unfortunately, I even couldn't connect one slave to one master.
I don't know if there is any error with my codes or Proteus doesn't show anything. Please help...

MASTER CODE:
Code:

#include <16f877a.h>
#fuses HS,NOLVP,NOWDT,NOPROTECT
#use delay(clock=4000000)
#use I2C(FAST, SCL=PIN_C3, SDA=PIN_C4, FORCE_HW)

#define use_portb_lcd True
#include <lcd.c>

float data;

void main(void)
{
   lcd_init();
   set_tris_c(0xFF);
   while(TRUE)
   {
      i2c_start();
         delay_ms(2);

      i2c_write(0xE0);  //send the slave address
         delay_ms(2);

      i2c_write(0xE1);  //send R/W bit for reading
         delay_ms(2);

      i2c_start();      //make the slave to send data
         delay_ms(2);

      data=i2c_read(); //Read  the data
         delay_ms(2);
      i2c_stop();
           
      printf(lcd_putc,"\f data=%f",data);
      delay_ms(5000);
   }
}


SLAVE-1 CODE:
Code:

#include <16f877A.h>
#device adc=8
#fuses HS,NOLVP,NOWDT,NOPROTECT
#use delay(clock=4000000)

#use i2c(SLAVE, FAST, SCL=PIN_C3, SDA=PIN_C4, address=0xE0, FORCE_HW)
#use fast_io(c)
#use fast_io(d)

BYTE state;
int8 adc_result;

#INT_SSP    //Interrupt for I2C activity
void sspinterupt()
{
   disable_interrupts(INT_SSP);
   
   state = i2c_isr_state();
   
   if(state == 0x80)                //master is requesting data
   {
      i2c_write (adc_result);  //send requested data
   }

}

void main(void)
{
   setup_psp(PSP_DISABLED);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_CCP1(CCP_OFF);
   setup_CCP2(CCP_OFF);
   
   set_tris_c(0xFF);
   
   enable_interrupts(INT_SSP);
   enable_interrupts(GLOBAL);
   
   setup_adc(adc_clock_div_32);
   setup_adc_ports(all_analog);
   
   while(TRUE)
   {
      set_adc_channel(0);
      delay_us(10);
     
      adc_result=read_adc();
      delay_us(10);
     
      enable_interrupts(INT_SSP);
   }
}


you should write;

i2c_start();
i2c_write(0xe1);//master is requesting data from the slave
data=i2c_read(0);//to stop the reading from the slave NACK will be sent before stop
i2c_stop();
_________________
hayat cekilmeye deger
matrix86



Joined: 15 Feb 2011
Posts: 2

View user's profile Send private message MSN Messenger

Re: i2c --Master read from slave's analog input-- problem
PostPosted: Tue Feb 15, 2011 5:30 am     Reply with quote

My problem is to read data from 3 different slaves. When I request data from one slave it responds correctly and there is no problem but then when I want to request datas from other slaves one by one, they do not respond. I did not understand why.
_________________
hayat cekilmeye deger
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