Need HELP with a program code

Discussion in 'Programmer's Corner' started by ashwinsinha, Feb 5, 2013.

  1. ashwinsinha

    Thread Starter New Member

    Feb 5, 2013
    i am doing a project on obstacle detection for blind using 4 ultrasonic sensors.
    we are using 4 sensors, each of it is gonna give direct value of distance in ascii format in form of 6 bytes serially
    i want help in getting that value in a single register say reg0 for sensor 1.....reg1 for sensor2...reg2 for sensor 3 and so on
    so what i really need is a code for getting data from this sensor through UART and store it in a REG
    Please Not:- First two sensors are connected to normal serial pin (RX1,RX2) and the other two r connected to two i/o pins say portA.1 and port B.1
    so we will get data from sensor 1&2 through UART...and from sensor 3&4 through soft UART.
    For your reference i have attached a datasheet link of the ultrasonic echo sensor which we are using Distance Sensor Serial Output-User Manual.pdf
    FYI we are using pic18f8722
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    stdlib.h has the atoi() function (ASCII to Integer) function that can do that conversion for you, assuming you first strip off any formatting characters that maybe there (I did not read the device spec sheet).

    Now asfar as running 4 uarts... The hardware uarts should be fine bu I've never sees 2 soft uarts together. Not saying it is impossible just that it may need some crafty coding to work properly.
  3. tshuck

    Well-Known Member

    Oct 18, 2012
    Could you not multiplex the UART TX(from the sensors) to the hardware UART on the PIC?

    As Ernie points out, you may run into problems with two SW UARTS... perhaps another device would be beneficial?

    I know TI's Stellaris has 8 UARTs:)
  4. takao21203

    AAC Fanatic!

    Apr 28, 2012
    You can normally multiplex hardware ports with an extra chip select line. Each device needs one, and only one must be active.

    Not all devices have chip select, but it is often possible to build a small circuit for this.

    When you don't need fullspeed transfer all the time you can always multiplex.
  5. tshuck

    Well-Known Member

    Oct 18, 2012
    Your solution is only valid if the sensors can put their output into a high-z state. can input all 4 sensors into a 4-to-1 mux and select which sensor you want to listen to. Problems will arise in timing using this cheapy sensor, though, so forget what I said. the sensor takes a reading and outputs every 500ms! That means waiting for one sensor's response could be up to 1s!

    You could even use multiple uCs to gather the data and update a master to do the processing, but that may be a tangled mess of an option...

    My advice: get some better sensors, or get crafty with your design/coding.
  6. takao21203

    AAC Fanatic!

    Apr 28, 2012
    1x SOT23 MOSFET + 1x 1/8W resistor each will do I think.

    Think in negatory so you cancel out something you don't want or you don't use now.
  7. Markd77

    Senior Member

    Sep 7, 2009
    If you have to write your own software UART because you need more than one channel, it should be just about possible.
    You have to have an interrupt at three times the baud rate (only 104 instruction cycles at 4MHz oscillator). Test the pin for a start bit and if so wait for four interrupts so that it happens between 1/3 and 2/3 of the way through the first data bit, store, wait 3 interrupts, etc until the whole byte is received.
    I've done a single UART in software (PIC16 assembler) and the general interrupt code took 14 cycles and the worst case for the UART reception part was 18 cycles. I think adding another channel would add about 18 cycles in the worst case, but in the 500ms between readings it's only about 6.
    It could be possible to do all four channels in software if the PIC isn't doing much else. If your oscillator frequency is 8MHz it leaves over 50 percent of the processing power available.