  | 
	  | 
		 
	 
	
		| View previous topic :: View next topic   | 
	 
	
	
		| Author | 
		Message | 
	 
	
		
			Einly Guest
 
 
 
 
  
			
			
			
			
			
			
			
			
			
			
  
		  | 
		
			
				|  Communicating pic16f876 to sensor4 | 
			 
			
				 Posted: Sun Nov 17, 2002 10:13 am     | 
				     | 
			 
			
				
  | 
			 
			
				Dear sir,
 
 
I've written another program using the bit-bang method. It is much longer. Communication between the pic and the sensor is Mode A. 
 
 
May I know does the program below make sense?
 
 
#include <16f876.h>
 
#use delay (clock=20000000)
 
#fuses noprotect, nowdt, hs, nolvp
 
 
#byte PORTA = 5
 
#byte PORTB = 6
 
#byte PORTC = 7
 
 
#define SCLK PIN_C3
 
#define SDIO PIN_C4
 
#define PD PIN_C2
 
 
void init_sensor();
 
void write_address (byte register_address);
 
byte read_data (byte register_address);
 
 
void main()
 
{
 
  byte address=0x02;
 
  byte data;
 
  
 
  set_tris_b(0);
 
  set_tris_c(0);
 
  
 
  init_sensor();
 
  
 
  do
 
  {
 
    write_address(address);
 
    delay_us(200);
 
    read_data(address);
 
    if(bit_test(data,7))
 
      output_high(PIN_C0);
 
    else
 
      output_low(PIN_C0);
 
  }while(1);
 
}
 
 
void init_sensor()
 
{
 
  output_low(SCLK);
 
  output_low(SDIO);
 
  output_low(PD);
 
  delay_ms(4);
 
  output_high(SCLK);
 
  output_high(SDIO);
 
  output_high(PD);
 
  delay_us(200);
 
  output_low(PD);
 
  delay_ms(4);
 
}
 
 
void write_address(byte register_address)
 
{
 
  int i;
 
  for(i=0;i<8;i++)
 
  {
 
    output_low(SCLK);
 
    output_bit(SDIO,shift_left(register_address,1,0));
 
    delay_us(3);
 
    output_high(SCLK);
 
    delay_us(3);
 
  }
 
}
 
 
byte read_data(byte register_address)
 
{
 
  int i;
 
  byte data=0;
 
  for(i-0;i<8;i++)
 
  {
 
    output_low(SCLK);
 
    delay_us(3);
 
    shift_left(data,1,input(SDIO));
 
    output_high(SCLK);
 
    delay_us(3);
 
    return data;
 
  }
 
}
 
 
Thanks for your help.
 
 
Einly
 
___________________________
 
This message was ported from CCS's old forum
 
	Original Post ID: 9042 | 
			 
		  | 
	 
	
		  | 
	 
	
		
			johnpcunningham Guest
 
 
 
 
  
			
			
			
			
			
			
			
			
			
			
  
		  | 
		
			
				| Re:  Communicating pic16f876 to sensor4 | 
			 
			
				 Posted: Mon Nov 18, 2002 9:44 am     | 
				     | 
			 
			
				
  | 
			 
			
				What is sensor4?  Who makes it and what is the part number? Without that information, no one can help you becaus no one knows what the communication protocal should be.
 
 
Also, what problem are you having?
 
 
JC 
 
___________________________
 
This message was ported from CCS's old forum
 
	Original Post ID: 9057 | 
			 
		  | 
	 
	
		  | 
	 
	
		
			Einly Guest
 
 
 
 
  
			
			
			
			
			
			
			
			
			
			
  
		  | 
		
			
				| Re:  Communicating pic16f876 to sensor4 | 
			 
			
				 Posted: Mon Nov 18, 2002 10:28 am     | 
				     | 
			 
			
				
  | 
			 
			
				:=What is sensor4?  Who makes it and what is the part number? Without that information, no one can help you becaus no one knows what the communication protocal should be.
 
:=
 
:=Also, what problem are you having?
 
:=
 
:=JC 
 
 
Sensor4 is an optical sensor manufactured by Agilent Technologies (Optical navigation), i.e. ADNS2051.
 
 
The problem that I have is I am not sure whether I can make 1 pin  from the PIC chip output (to send address to the sensor) and after a very short period, changing it to input pin (to receive data sent by the sensor) by just changing "set_tris_b(0) to set_tris_b(0xff)"? Must I use FAST_IO?
 
___________________________
 
This message was ported from CCS's old forum
 
	Original Post ID: 9062 | 
			 
		  | 
	 
	
		  | 
	 
	
		
			johnpcunningham Guest
 
 
 
 
  
			
			
			
			
			
			
			
			
			
			
  
		  | 
		
			
				| Re:  Communicating pic16f876 to sensor4 | 
			 
			
				 Posted: Mon Nov 18, 2002 11:21 am     | 
				     | 
			 
			
				
  | 
			 
			
				I could not find the datasheet so I dont know if this is I2C, SPI, or a specific protocal for this chip.
 
 
Anyway, to answer your question; You can change the output to an input in enough time.  Remember, you are controling the clock so you can pulse the clock and then get the serial data.  I think you need to pulse the clock low then high; the data should be stable - more than likely the data is output on a falling edge so when you go low then high, the data has time to stabilize on the line.  Probably something like this; (after sending address)
 
 
#byte PORTA = 0x05 
 
#byte PORTB = 0x06
 
#byte PORTC = 0x07
 
 
#byte TRIS_A = 0x85 
 
#byte TRIS_B = 0x86
 
#byte TRIS_C = 0x87
 
 
#define SCLK PIN_C3 
 
#define SDIO PIN_C4 
 
#define PD PIN_C2
 
 
#define  bit0  0
 
#define  bit1  1
 
#define  bit2  2
 
#define  bit3  3
 
#define  bit4  4
 
#define  bit5  5
 
#define  bit6  6
 
#define  bit7  7
 
 
 
char read_data() 
 
{ 
 
int i; 
 
byte data=0x00;
 
 
 
//MAKE SDIO AN INPUT
 
bit_set(tris_C, pin4);
 
delay_us(100);
 
 
for(i = 0;i < 8; i++) 
 
{ 
 
output_low(SCLK); 
 
delay_us(3); 
 
output_high(SCLK); 
 
delay_us(3);
 
shift_left(data,1,input(SDIO)); 
 
}
 
 
 
//MAKE SDIO AN output (default)
 
bit_clear(tris_C, pin4);
 
delay_us(100);
 
 
return data; 
 
}
 
___________________________
 
This message was ported from CCS's old forum
 
	Original Post ID: 9065 | 
			 
		  | 
	 
	
		  | 
	 
	
		 | 
	 
 
  
	 
	    
	   | 
	
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
  
		 |