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

Please help me

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







Please help me
PostPosted: Fri Nov 22, 2002 7:49 pm     Reply with quote

Dear all,

I am currently communicating PIC 16f876 with Agilent
Technologies optical sensor (ADNS2051) using
synchronous, half duplex serial port.

However, I can't write or read data from the sensor
after trying for so many times.

The communication protocol of the sensor is given in
the datasheet below
( <a href="http://literature.agilent.com/litweb/pdf/5988-4289EN.pdf" TARGET="_blank">http://literature.agilent.com/litweb/pdf/5988-4289EN.pdf</a>)-
page 17-20

I have connected a 18MHz ceramic resonator to the
optical sensor (as recommended by the Agilent) and a
20MHz (with 2 33pF capacitors) to the PIC chip.

Below is the program that I've written:

#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_C6

#byte TRIS_A=0x85
#byte TRIS_B=0x86
#byte TRIS_C=0x87

void init_sensor();
void write_address (byte register_address);
byte read_data ();

void main()
{
byte address=0x02;
byte data;

set_tris_b(0);
set_tris_c(0);

init_sensor();

do
{
write_address(address);

read_data();
if(bit_test(data,7))
{
output_high(PIN_C0);
delay_ms(5000);
}
else
{
output_low(PIN_C0);
delay_ms(500);
}
}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(700);
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()
{
int i;
byte data=0;

bit_set(TRIS_C,4);

for(i=0;i<8;i++)
{
output_low(SCLK);
delay_us(3);
output_high(SCLK);
delay_us(3);
shift_left(&data,1,input(SDIO));
}

bit_clear(TRIS_C,4);
delay_us(200);

return data;

}

In the program, I intend to read bit 7(MSB) of
register 0x02 to determine whether motion has occured.
If motion occurs, Pin_C0 will be made 1. Else, PIN_C0
will be made 0.

I have connected SCLK to PIN C3, SDIO to PIN C4, and
PD to PIN C6.

I don't have any idea to make the thing work. Can you
help me?

Thanks.

Einly
___________________________
This message was ported from CCS's old forum
Original Post ID: 9302
R.J.Hamlett
Guest







Re: Please help me
PostPosted: Sat Nov 23, 2002 9:13 am     Reply with quote

:=Dear all,
:=
:=I am currently communicating PIC 16f876 with Agilent
:=Technologies optical sensor (ADNS2051) using
:=synchronous, half duplex serial port.
:=
:=However, I can't write or read data from the sensor
:=after trying for so many times.
:=
:=The communication protocol of the sensor is given in
:=the datasheet below
:=( <a href="http://literature.agilent.com/litweb/pdf/5988-4289EN.pdf" TARGET="_blank"> <a href="http://literature.agilent.com/litweb/pdf/5988-4289EN.pdf" TARGET="_blank">http://literature.agilent.com/litweb/pdf/5988-4289EN.pdf</a></a>)-
:=page 17-20
:=
:=I have connected a 18MHz ceramic resonator to the
:=optical sensor (as recommended by the Agilent) and a
:=20MHz (with 2 33pF capacitors) to the PIC chip.
:=
:=Below is the program that I've written:
:=
:=#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_C6
:=
:=#byte TRIS_A=0x85
:=#byte TRIS_B=0x86
:=#byte TRIS_C=0x87
:=
:=void init_sensor();
:=void write_address (byte register_address);
:=byte read_data ();
:=
:=void main()
:={
:= byte address=0x02;
:= byte data;
:=
:= set_tris_b(0);
:= set_tris_c(0);
:=
:= init_sensor();
:=
:= do
:= {
:= write_address(address);
:=
:= read_data();
:= if(bit_test(data,7))
:= {
:= output_high(PIN_C0);
:= delay_ms(5000);
:= }
:= else
:= {
:= output_low(PIN_C0);
:= delay_ms(500);
:= }
:= }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(700);
:= 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);
:= }
:=}
There is a glaring problem here, in that you are calling 'write_address', with the value 2, and handing this to the shift, which requires an address. Hence you need the '&' to get the memory address of the variable containing the actual address byte.
Personally I'd code something like:
void write_address(byte register_address)
{
int mask=128;
do
{
output_low(SCLK);
output_bit(SDIO,register_address & mask);
delay_us(3);
output_high(SCLK);
delay_us(3);
mask=mask>>1;
}
while (mask!=128);
}

:=byte read_data()
:={
:= int i;
:= byte data=0;
:=
:= bit_set(TRIS_C,4);
:=
:= for(i=0;i<8;i++)
:= {
:= output_low(SCLK);
:= delay_us(3);
:= output_high(SCLK);
:= delay_us(3);
:= shift_left(&data,1,input(SDIO));
:= }
:=
:= bit_clear(TRIS_C,4);
:= delay_us(200);
:=
:= return data;
:=
:=}
:=
:=In the program, I intend to read bit 7(MSB) of
:=register 0x02 to determine whether motion has occured.
:=If motion occurs, Pin_C0 will be made 1. Else, PIN_C0
:=will be made 0.
:=
:=I have connected SCLK to PIN C3, SDIO to PIN C4, and
:=PD to PIN C6.
:=
:=I don't have any idea to make the thing work. Can you
:=help me?
:=
:=Thanks.

Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 9316
Einly
Guest







Re: Please help me
PostPosted: Sun Nov 24, 2002 8:44 pm     Reply with quote

Dear sir,

Thanks for your reply.

I've tried using your code but it still can't work. I've checked the output pin (SCLK & SDIO) with a digital oscilloscope. I get signal that I want from the SCLK but no signal coming out from the SDIO pin.

May I know what happen when all 8 bits of 128 has been shifted right? (mask = mask >>1)

10000000 ---- 01000000 ---- 00100000 ---- 00010000 ---- 00001000 ---- 00000100 ---- 00000010 ---- 00000001 ---- (?)

After that, will it be 00000000 / 10000000?

Thanks for your help.

Yours sincerely,

Einly

:=:=Dear all,
:=:=
:=:=I am currently communicating PIC 16f876 with Agilent
:=:=Technologies optical sensor (ADNS2051) using
:=:=synchronous, half duplex serial port.
:=:=
:=:=However, I can't write or read data from the sensor
:=:=after trying for so many times.
:=:=
:=:=The communication protocol of the sensor is given in
:=:=the datasheet below
:=:=( <a href="http://literature.agilent.com/litweb/pdf/5988-4289EN.pdf" TARGET="_blank"> <a href="http://literature.agilent.com/litweb/pdf/5988-4289EN.pdf" TARGET="_blank"> <a href="http://literature.agilent.com/litweb/pdf/5988-4289EN.pdf" TARGET="_blank">http://literature.agilent.com/litweb/pdf/5988-4289EN.pdf</a></a></a>)-
:=:=page 17-20
:=:=
:=:=I have connected a 18MHz ceramic resonator to the
:=:=optical sensor (as recommended by the Agilent) and a
:=:=20MHz (with 2 33pF capacitors) to the PIC chip.
:=:=
:=:=Below is the program that I've written:
:=:=
:=:=#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_C6
:=:=
:=:=#byte TRIS_A=0x85
:=:=#byte TRIS_B=0x86
:=:=#byte TRIS_C=0x87
:=:=
:=:=void init_sensor();
:=:=void write_address (byte register_address);
:=:=byte read_data ();
:=:=
:=:=void main()
:=:={
:=:= byte address=0x02;
:=:= byte data;
:=:=
:=:= set_tris_b(0);
:=:= set_tris_c(0);
:=:=
:=:= init_sensor();
:=:=
:=:= do
:=:= {
:=:= write_address(address);
:=:=
:=:= read_data();
:=:= if(bit_test(data,7))
:=:= {
:=:= output_high(PIN_C0);
:=:= delay_ms(5000);
:=:= }
:=:= else
:=:= {
:=:= output_low(PIN_C0);
:=:= delay_ms(500);
:=:= }
:=:= }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(700);
:=:= 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);
:=:= }
:=:=}
:=There is a glaring problem here, in that you are calling 'write_address', with the value 2, and handing this to the shift, which requires an address. Hence you need the '&' to get the memory address of the variable containing the actual address byte.
:=Personally I'd code something like:
:=void write_address(byte register_address)
:={
:= int mask=128;
:= do
:= {
:= output_low(SCLK);
:= output_bit(SDIO,register_address & mask);
:= delay_us(3);
:= output_high(SCLK);
:= delay_us(3);
:= mask=mask>>1;
:= }
:= while (mask!=128);
:=}
:=
:=:=byte read_data()
:=:={
:=:= int i;
:=:= byte data=0;
:=:=
:=:= bit_set(TRIS_C,4);
:=:=
:=:= for(i=0;i<8;i++)
:=:= {
:=:= output_low(SCLK);
:=:= delay_us(3);
:=:= output_high(SCLK);
:=:= delay_us(3);
:=:= shift_left(&data,1,input(SDIO));
:=:= }
:=:=
:=:= bit_clear(TRIS_C,4);
:=:= delay_us(200);
:=:=
:=:= return data;
:=:=
:=:=}
:=:=
:=:=In the program, I intend to read bit 7(MSB) of
:=:=register 0x02 to determine whether motion has occured.
:=:=If motion occurs, Pin_C0 will be made 1. Else, PIN_C0
:=:=will be made 0.
:=:=
:=:=I have connected SCLK to PIN C3, SDIO to PIN C4, and
:=:=PD to PIN C6.
:=:=
:=:=I don't have any idea to make the thing work. Can you
:=:=help me?
:=:=
:=:=Thanks.
:=
:=Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 9351
R.J.Hamlett
Guest







Re: Please help me
PostPosted: Mon Nov 25, 2002 5:41 am     Reply with quote

:=Dear sir,
:=
:=Thanks for your reply.
:=
:=I've tried using your code but it still can't work. I've checked the output pin (SCLK & SDIO) with a digital oscilloscope. I get signal that I want from the SCLK but no signal coming out from the SDIO pin.
:=
:=May I know what happen when all 8 bits of 128 has been shifted right? (mask = mask >>1)
:=
The CCS compiler seems normally to encode the shift as a rotation. Hence the bit arrives in the top bit again, and is seen by the 'mask !=128' test. This is a bodge depending on the behaviour of the compiler (which does change between versions), so it'd probably be safer to code as a rotation, using:
rotate_right(&mask,1);

:=10000000 ---- 01000000 ---- 00100000 ---- 00010000 ---- 00001000 ---- 00000100 ---- 00000010 ---- 00000001 ---- (?)
:=
:=After that, will it be 00000000 / 10000000?
:=
The version I did this on gave 10000000, which then makes the test at the end of the loop drop through, but I'd try again with the rotate (the behaviour when you rotate through the end of a byte, is undefined in the original C paperwork, but in Ansi C is required to be a shift, not a rotation - it may be that CCS have changed to the latter form.

:=Thanks for your help.
:=
:=Yours sincerely,
:=
:=Einly

Hope you crack it. :-)

:=:=:=Dear all
:=:=:=
:=:=:=I am currently communicating PIC 16f876 with Agilent
:=:=:=Technologies optical sensor (ADNS2051) using
:=:=:=synchronous, half duplex serial port.
:=:=:=
:=:=:=However, I can't write or read data from the sensor
:=:=:=after trying for so many times.
:=:=:=
:=:=:=The communication protocol of the sensor is given in
:=:=:=the datasheet below
:=:=:=( <a href="http://literature.agilent.com/litweb/pdf/5988-4289EN.pdf" TARGET="_blank"> <a href="http://literature.agilent.com/litweb/pdf/5988-4289EN.pdf" TARGET="_blank"> <a href="http://literature.agilent.com/litweb/pdf/5988-4289EN.pdf" TARGET="_blank"> <a href="http://literature.agilent.com/litweb/pdf/5988-4289EN.pdf" TARGET="_blank">http://literature.agilent.com/litweb/pdf/5988-4289EN.pdf</a></a></a></a>)-
:=:=:=page 17-20
:=:=:=
:=:=:=I have connected a 18MHz ceramic resonator to the
:=:=:=optical sensor (as recommended by the Agilent) and a
:=:=:=20MHz (with 2 33pF capacitors) to the PIC chip.
:=:=:=
:=:=:=Below is the program that I've written:
:=:=:=
:=:=:=#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_C6
:=:=:=
:=:=:=#byte TRIS_A=0x85
:=:=:=#byte TRIS_B=0x86
:=:=:=#byte TRIS_C=0x87
:=:=:=
:=:=:=void init_sensor();
:=:=:=void write_address (byte register_address);
:=:=:=byte read_data ();
:=:=:=
:=:=:=void main()
:=:=:={
:=:=:= byte address=0x02;
:=:=:= byte data;
:=:=:=
:=:=:= set_tris_b(0);
:=:=:= set_tris_c(0);
:=:=:=
:=:=:= init_sensor();
:=:=:=
:=:=:= do
:=:=:= {
:=:=:= write_address(address);
:=:=:=
:=:=:= read_data();
:=:=:= if(bit_test(data,7))
:=:=:= {
:=:=:= output_high(PIN_C0);
:=:=:= delay_ms(5000);
:=:=:= }
:=:=:= else
:=:=:= {
:=:=:= output_low(PIN_C0);
:=:=:= delay_ms(500);
:=:=:= }
:=:=:= }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(700);
:=:=:= 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);
:=:=:= }
:=:=:=}
:=:=There is a glaring problem here, in that you are calling 'write_address', with the value 2, and handing this to the shift, which requires an address. Hence you need the '&' to get the memory address of the variable containing the actual address byte.
:=:=Personally I'd code something like:
:=:=void write_address(byte register_address)
:=:={
:=:= int mask=128;
:=:= do
:=:= {
:=:= output_low(SCLK);
:=:= output_bit(SDIO,register_address & mask);
:=:= delay_us(3);
:=:= output_high(SCLK);
:=:= delay_us(3);
:=:= mask=mask>>1;
:=:= }
:=:= while (mask!=128);
:=:=}
:=:=
:=:=:=byte read_data()
:=:=:={
:=:=:= int i;
:=:=:= byte data=0;
:=:=:=
:=:=:= bit_set(TRIS_C,4);
:=:=:=
:=:=:= for(i=0;i<8;i++)
:=:=:= {
:=:=:= output_low(SCLK);
:=:=:= delay_us(3);
:=:=:= output_high(SCLK);
:=:=:= delay_us(3);
:=:=:= shift_left(&data,1,input(SDIO));
:=:=:= }
:=:=:=
:=:=:= bit_clear(TRIS_C,4);
:=:=:= delay_us(200);
:=:=:=
:=:=:= return data;
:=:=:=
:=:=:=}
:=:=:=
:=:=:=In the program, I intend to read bit 7(MSB) of
:=:=:=register 0x02 to determine whether motion has occured.
:=:=:=If motion occurs, Pin_C0 will be made 1. Else, PIN_C0
:=:=:=will be made 0.
:=:=:=
:=:=:=I have connected SCLK to PIN C3, SDIO to PIN C4, and
:=:=:=PD to PIN C6.
:=:=:=
:=:=:=I don't have any idea to make the thing work. Can you
:=:=:=help me?
:=:=:=
:=:=:=Thanks.
:=:=
:=:=Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 9382
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