View previous topic :: View next topic |
Author |
Message |
vinc Guest
|
synchronous communication (without USART and I2C) |
Posted: Wed May 12, 2004 12:44 am |
|
|
Hi,
i need some advises. I am trying to make a synchronous bidirectional communication between a sensor and my microcontroller using a simple I/O pin. I can not use the USART or I2C (they are already used for another application).
It is my very first microcontroller program so any help would be really appreciated.
Vince |
|
|
andyfraser
Joined: 04 May 2004 Posts: 47 Location: UK
|
|
Posted: Wed May 12, 2004 5:19 am |
|
|
Vince,
If your sensor is an I2C device then you can use #use I2C specifying the two port pins you have available. If the device is SPI then it is a bit more tricky and you may have to bit-bang the ports. The basic principle of synchronous serial communications is to generate a clock on one port pin while either setting the state of the serial data out pin and/or reading the state of the serial data in pin. Some example pseudo code follows.
Sending/Receiving a byte
Code: |
Set clock port pin to idle level.
Loop:
Is MSB of byte to send set ?
Yes: Set serial data out pin high.
No: Set serial data out pin low.
Set clock port pin to active level.
Wait a small period of time.
Set clock port pin to idle level.
Wait a small period of time.
*Read serial data in pin.
*Set LSB of receive byte to state of serial data in pin.
*Shift receive byte left one bit.
Shift byte to be sent left one bit.
Loop 8 times.
Return byte read.
|
The above pseudo code assumes that the byte is sent MSB first.
*In SPI data transfer is full duplex so while data is being clocked out, the sensor may return data to the microcontroller at the same time. To cater for this condition, after you have taken the clock back to the idle state, you need to read the serial data in pin, place this bit into the LSB of your receive byte then shift the receive byte left one as shown above. If the sensor only returns data, you can simply send a 0 which will generate the necessary clock cycles to allow the sensor to return it's data.
Hope this helps
Andy |
|
|
vinc Guest
|
i ll try it |
Posted: Wed May 12, 2004 5:48 am |
|
|
thx u Ansy 4 the fast answer. i ll try 2 do what u suggest me. hope i ll get good results
Vinc |
|
|
vinc Guest
|
|
Posted: Wed May 12, 2004 5:49 am |
|
|
sorry i spell ur name wrongly |
|
|
|