| 
	
	|  |  |  
	
		| View previous topic :: View next topic |  
		| Author | Message |  
		| isikmurat06 
 
 
 Joined: 08 Jan 2011
 Posts: 7
 
 
 
			    
 
 | 
			
				| i2c --Master read from slave's analog input-- problem |  
				|  Posted: Sun Jan 09, 2011 10:14 am |   |  
				| 
 |  
				| 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
 
 
 
			    
 
 |  |  
		|  |  
		| isikmurat06 
 
 
 Joined: 08 Jan 2011
 Posts: 7
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Mon Jan 10, 2011 2:25 am |   |  
				| 
 |  
				| 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
 
 
 
			    
 
 |  |  
		|  |  
		| isikmurat06 
 
 
 Joined: 08 Jan 2011
 Posts: 7
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Tue Jan 11, 2011 2:15 am |   |  
				| 
 |  
				| My proteus version is 7.6 sp2 CCS C version is 4.093
 |  |  
		|  |  
		| PCM programmer 
 
 
 Joined: 06 Sep 2003
 Posts: 21708
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Tue Jan 11, 2011 12:40 pm |   |  
				| 
 |  
				| 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
 
 
 
			      
 
 | 
			
				| Re: i2c --Master read from slave's analog input-- problem |  
				|  Posted: Tue Feb 15, 2011 5:24 am |   |  
				| 
 |  
				|  	  | 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
 
 
 
			      
 
 | 
			
				| Re: i2c --Master read from slave's analog input-- problem |  
				|  Posted: Tue Feb 15, 2011 5:30 am |   |  
				| 
 |  
				| 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
 |  |  
		|  |  
		|  |  
  
	| 
 
 | 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
 
 |