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 CCS Technical Support

Using UART for SDI-12 interface

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



Joined: 16 Sep 2005
Posts: 138
Location: The Netherlands

View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

Using UART for SDI-12 interface
PostPosted: Wed Jun 10, 2009 7:50 am     Reply with quote

Hello,

I'm trying to connect a PIC18F8722 to a SDI-12 Sensor. For this I need a UART running on 1200 baud and with 1 start bit, 7 data bits, 1 parity bit (even) and 1 stop bit.
Before sending I also need to make the data line high for at least 12 ms.

I have hardware that converts the UART to SDI-12 interface, but the software doesn't seem to be working.

I have configured my uart with the following code:
Code:
#use rs232(baud=1200, BRGH1OK, parity=e, bits=7, stop=1, xmit=PIN_G1, rcv=PIN_G2, ERRORS, stream=SDI12)


Now sending works as expected (checked it with a oscilloscope). But I am unable to get the break that I need to send in front of any communication to work. What I need is to let PIN_G1 high for at least 12 ms and then low for at least 9 ms.

What I have tried is disabling the UART via setup_uart(0, SDI12); and disabling it via the SPEN bit of the RCSTA2 register. But both didn't work.

I have already search this forum for any comments on the SDI-12 protocol and on using RX as output, but have so far no answer.

Does someone have any idea on what might be going wrong?
Or does someone have an example of working code for SDI-12 communication?

I use compiler version 4.068.


Thanks in advance,
Jos
Ttelmah
Guest







PostPosted: Wed Jun 10, 2009 8:38 am     Reply with quote

High, is where the pin sits, when RS232 is idle. It should be there all the time you are not sending characters.
Low, is the problem.
Two basic choices:

1) Turn off the UART. You now have control of the pin, and can use an 'output_low'. Then wait for 9mSec, & turn the UART back on. Turning it off, on it's own, will not drive the pin low. You need to do this.

2) My 'preferred' method. Use the 'set_uart_speed' function, to turn the baud rate down, so a character, takes the required 'low' time. Send a character that is all zeros. Once this has sent, turn the baud rate back up.
In your case, using 'even' parity, the start bit will be low, and if you send a 'zero', the eighth parity bit will also be low, giving 9 low bits. So 9mSec, requires a baud rate of 1000bps or less. There is nothing to stop you selecting 'odd' baud rates like this.

Code:

//Assume you are starting with the UART 'idle'
delay_ms(12); //leave line high for 12mSec
set_uart_speed(1000);
putc(0);
delay_ms(9); //ensure character has sent.
set_uart_speed(1200);


Best Wishes
Foppie



Joined: 16 Sep 2005
Posts: 138
Location: The Netherlands

View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

PostPosted: Thu Jun 11, 2009 12:58 am     Reply with quote

Thank you very much Ttelmah!

I overlooked the fact that an UART pin normally goes high. (My hardware already changed the polarity for me and I measured only at the SDI-12 side.

I made the choice for option 1 after trying them both. Turning the UART off has no drawbacks for me since I am the master of the SDI port.

I now have the basics working and am starting to implement the protocol. :)

Thanks again!
Guest








PostPosted: Thu Oct 15, 2009 9:52 pm     Reply with quote

What particular tri-state buffer did you use? Thank you very much. I'm interfacing an SDI-12 Hydra sensor to a PIC16F877A MCU. Thank you in advance. (n_n)
vasiliok



Joined: 17 May 2011
Posts: 19
Location: Kaunas, Lithuania

View user's profile Send private message

PostPosted: Wed Jun 09, 2021 11:21 pm     Reply with quote

Hello!

I've tried software UART and it works perfectly!

But I use Rx line and interrupt from hardware UART (to get Rx interrupt working), while Tx line is from software UART

Code piece in .h file:
Code:

// PC interface to send data from sensor
#use rs232(uart1,baud=1200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=PC, Errors)

// We use Rx pin only to get bytes by isr_interrupt
#use rs232(uart2, baud=1200,stop=1, parity=E,xmit=PIN_D6,rcv=PIN_D7,bits=7,stream=SENSOR, Errors)

// We use software uart PIN_D5 to send messages to SDI-12 sensor and to control this pin like GPIO at the same time
#use rs232(force_sw, baud=1200,stop=1, parity=E,xmit=PIN_D5,rcv=PIN_D4,bits=7,stream=TRANSMIT, Errors)


This piece of code in main function:

Code:

      output_low(PIN_D5); // software UART
      delay_ms(12);
      output_high(PIN_D5);
      delay_ms(9);
      set_uart_speed(1200);
      fprintf(TRANSMIT, "1MC!");
     
     
     
      output_high(PIN_D5);
     
      delay_ms(3000);



Here our interrupt code. We get reply from SDI-12 sensor and send it to PC:
Code:

#int_rda2
void rda_isr2(void){


   // Read BYTE from RxD buffer
   data = fgetc(SENSOR); // hardware uart Rx pin
   fputc(data, PC);

}



Hope this will be useful for someone!

Code tested on PIC18F46K22 mcu ;)

P.S. circuit diagram for converter: https://forum.pycom.io/assets/uploads/files/1568117082805-sdi12-shifter-2.jpg
temtronic



Joined: 01 Jul 2010
Posts: 9225
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Jun 10, 2021 5:33 am     Reply with quote

Well I'm confused ! As a 'dinosaur', I downloded the SDI-12 'specs' and it's a bidirectional, one wire interface..similar to 'RS-485', so I have to ask, are the TX and RX pins of your PIC tied together or is there some kind of 'interface module' between them and the SDI-12 devices ?
vasiliok



Joined: 17 May 2011
Posts: 19
Location: Kaunas, Lithuania

View user's profile Send private message

PostPosted: Thu Jun 10, 2021 5:36 am     Reply with quote

Yes. Rx and Tx connected together via opto coupler circuit.
You receive what you send + you receive sensor reply.
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