1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

PIC16f690, initialisation of ports, LEDS,reading from a pin

Discussion in 'Microcontrollers' started by fantabulous68, Aug 24, 2009.

  1. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    5,894
    Likes:
    502
    Location:
    Cleveland, OH, USA
    ONLINE
    The required range is 2 to 50 cm. None of those detectors cover that range. The Vishay device listed first says up to 4 or 5 cm, but the datasheet doesn't seem to support that.

    I don't see how reflection at an angle is going to work with a liquid that potentially has ripples. All of the packaged devices seem to be for solids, like sheets of paper.

    John
     
  2. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,800
    Likes:
    134
    Location:
    morristown,tn
    What you used is not Ir it's a laser ?


    [​IMG]
     
    Last edited: Aug 27, 2009
  3. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    5,894
    Likes:
    502
    Location:
    Cleveland, OH, USA
    ONLINE
    It's all just light. The principle is the same. There are a great number of liquids that are reasonably transparent in the near infrared, at least to 940 nm . The liquid in this case is unspecified.

    My point was that there is a reasonable chance the light will not be reflected from its surface more than from an underlying surface of greater reflectivity, particularly when the liquid level is low. That is basically what my experiment showed. Obviously, there is always reflection at an interface, such as an air-water interface, but if that reflection is about 10% or less, and reflection from a surface a few cm deeper is 60%, the detector is going to read the latter, not the former. I am sure the surface reflected some of the light in my experiment, but its intensity was swamped by the reflection from the basin.

    John
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,800
    Likes:
    134
    Location:
    morristown,tn

    It's not the same a laser is no where the same as IR light. A laser have the same frequencies and identical phase and have narrow wavelength spectrum. The IR light has wide wavelength.So not the same power of a laser. You can get Ir laser but it's not going to work it would shoot thew the water before it bounces back.

    But a Ir led will bounce off the water we done been down that road before. I'll dig up what i found the last time.
     
  6. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    5,894
    Likes:
    502
    Location:
    Cleveland, OH, USA
    ONLINE
    Please do. All of my comments were related to the OP's concept of determining distance using TOF with reflected light from an unspecified liquid's surface. At least that was clearly the focus of my comments.

    I am anxious to see how you did that without a laser and using IR. I hope you can find those experiments.

    I am not denying that light, including IR, reflects off or can be scattered by water. Anyone who is not blind knows that -- actually, blind people experience it too. What I am suggesting is that the OP's projected design, as vague as it is, should have some basic validation done of its underlying assumptions before wasting a lot of effort on building it.

    As for what you imply about laser light grossly deviating from Beer's (Beer-Lambert) law, can you provide any references to that? (Again, I am talking about the sort of intensities that are used for these instruments.)

    John
     
  7. fantabulous68

    fantabulous68 Member

    Joined:
    Feb 20, 2007
    Messages:
    323
    Likes:
    0
    rjenkins from AAC suggested some methods and i think this would be a suitable approach for me. This is what he suggested:

    I want to go with this method. Ok so now that means starting from scratch with my hardware.
     
    Last edited: Aug 28, 2009
  8. fantabulous68

    fantabulous68 Member

    Joined:
    Feb 20, 2007
    Messages:
    323
    Likes:
    0




    The Sharp GP2D12 is an analog distance sensor that uses infrared to detect an object between 10 cm
    and 80 cm away. The GP2D12 provides a non-linear voltage output in relation to the distance an object
    is from the sensor and interfaces easily using any analog to digital converter

    i got the above from the attached parallex datasheet.

    Im not going to angle the sensor. The sensor will be perpendicular to the surface of the liquid

    Will the sensor work on pic micro or the AVR atmel microcontroller?
    Features
    • High immunity to ambient light and color of object
    • No external control circuitry required
    • Sensor includes convenient mounting holes
    • Compatible with all BASIC Stamp® and SX microcontrollers
     

    Attached Files:

    Last edited: Aug 27, 2009
  9. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,800
    Likes:
    134
    Location:
    morristown,tn
    You can use Sharp GP2D12 with any uC. You first said it was not a option
    You first posted you couldn't read a switch on port then later you post it's a Ir module
    If your not careful your going to get options run away here.

    This is some what how the the sharpGP works
     
    Last edited: Aug 28, 2009
  10. fantabulous68

    fantabulous68 Member

    Joined:
    Feb 20, 2007
    Messages:
    323
    Likes:
    0
    Sorry, yeah im just worried now, coz i spent alota time coding for the hardware i tested.

    i just need to decide on something solid to stick with since my other solution is not going to work.
     
  11. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    168
    Location:
    Toronto, Canada
    The Sharp rangefinder needs an A/D input. The BASIC stamp doesn't have one.
     
  12. fantabulous68

    fantabulous68 Member

    Joined:
    Feb 20, 2007
    Messages:
    323
    Likes:
    0
    I want to
    . Its better that i found out now than a week b4 my project is due that my TOF solution would fail. I explained that no microcontroller timer is fast enough to time light and got permission to use the sharpGP2D12.

    any suggestions for a suitable microcontroller in the atmel or pic for doing ADC on sharpGP2D12? the range of the sensor is variable from a couple of centimeters to about 50cm.
     
  13. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,800
    Likes:
    134
    Location:
    morristown,tn
    It will work with the 16f690 it has adc Your using it I would keep using it.
     
  14. birdman0_o

    birdman0_o Active Member

    Joined:
    Feb 23, 2009
    Messages:
    1,370
    Likes:
    18
    Location:
    Montreal, Quebec
    Be careful of the slope the sensor creates, it is not linear.

    They provide the equation on the datasheet or you can use a look up table, the lookup table would be much much easier (less accurate though).
     
  15. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,216
    Likes:
    640
    Location:
    Derbyshire, UK
    I don't see why you would think a lookup table would be any less accurate?, you generate the lookup table from the equation, so it's exactly the same.
     
  16. birdman0_o

    birdman0_o Active Member

    Joined:
    Feb 23, 2009
    Messages:
    1,370
    Likes:
    18
    Location:
    Montreal, Quebec
    You are right, in my mind there would be less values in the lookup table, however since the ADC is limitted to 10 bit, you would just need to make up a big lookup table for it to be just as accurate.
     
    • Like Like x 1
  17. fantabulous68

    fantabulous68 Member

    Joined:
    Feb 20, 2007
    Messages:
    323
    Likes:
    0
    Nigel says:
    So im wondering is it possible for me to modify this code to suit the pic16f690???because i already got my lcd display working on the 16f690. is there any major differences that would affect this code on the 16f690???eg the line with:
    // load ADC value in 16bit math var

    I use hi-tech universal c compiler in mplab. I know i got to configure in mplab compiler.

    Code (text):
    //*********************************************************
    /*  Junebug_Serial4.c    RomanBlack.com 19th July 2009.

        Junebug 18F1320, uses my "zero error 1 second timer"
        system to generate a 2 second interval, then every
        2 seconds it reads an analog voltage from a
        Sharp GP2 distance sensor and converts it to decimal
        distance, then sends that data as a text string to PC
        via bitbanged serial out. It also flashes Junebug LED1
        everytime it takes a distance sample.

        Code for MikroC, config handled by MikroC compiler;
        _INTI02_OSC_1H
        _WDT_OFF_2H
        -MCLRE_ON_3H
        _LVP_OFF_4L
        _PWRT_ON_2L

    */
    //*********************************************************

    // functions declared
    void calc_distance(void);
    void send_serial_message(void);
    void send_serial_byte(unsigned char);

    #define PIN_SER_OUT LATA.F3     // which pin for serial out (PORTA.F3)
    #define SER_BAUD 51             // TMR3 (1Mhz/19200 baud) = 52
                                    // tested; works from 49 to 53, using 51

    unsigned char i;        // used for looping

    unsigned char cm10;     //
    unsigned char cm;       //

    unsigned int math;      // used for voltage calculations
    unsigned long bres;     // for bresenham 2-second timer system


    //---------------------------------------------------------
    void main()
    {
        // setup the PIC 18F1320
        OSCCON = 0x72;          // internal osc, 8MHz

        PORTA = 0;
        TRISA = 0b10000010;     // RA7 high imp, RA3 is serial out,
                                // RA1 is ADC input measuring VR1
        PORTB = 0;
        INTCON2 = 0;            // PORTB pullups ON
        TRISB = 0b00000000;     // PORTB not used

        ADCON0 = 0b00000101;    // ADC ON, RA1 is ADC input
        ADCON1 = 0b01111101;    // AN1 is ADC input
        ADCON2 = 0b10100010;    // right justify, 8Tad, 32Tosc

        T1CON = 0b00010001;     // TMR1 is ON, 1:2 prescale, =1MHz

        T3CON = 0b00010001;     // TMR3 is ON, 1:2 prescale, =1MHz

        // main loop here;
        while(1)
        {
            // wait for 2 seconds, uses TMR1 free running at 1Mhz
            while(!PIR1.TMR1IF);    // wait for TMR1 overflow
            PIR1.TMR1IF = 0;        // clear overflow flag
            LATA = 0;               // all LEDs off
           
            bres += 65536;          // add 65536uS to bres value
            if(bres >= 2000000)     // if reached 2 seconds!
            {
                bres -= 2000000;    // subtract 2 seconds, keep error
           
                // flash Junebug LED1,  RA0=1 RA6=0 RA7=hiimpedance
                LATA.F0 = 1;            // LED on

                // read the ADC voltage RA1 (Sharp GP2 sensor)
                ADCON0.F1 = 1;      // set GO bit, start doing ADC
                while(ADCON0.F1);   // wait until ADC done
                calc_distance();        // convert ADC value to distance
                send_serial_message();  // send message back to PC!
            }

        }
    }



    //---------------------------------------------------------
    void calc_distance(void)
    {
        // from the Sharp datasheet the analog voltage is
        // the inverse of distance, so distance can be calculated
        // d = (1 / volts) then just scaled to suit the sensor

        [B]// load ADC value in 16bit math var[/B]
        math = ADRESH;
        math = (math * 256);
        math += ADRESL;

        // now invert it; (1 / volts) use (6050 / volts) for scaling
        math = (6050 / math);
        if(math >= 2) math -=2;     // fix linear error (-2)
        if(math > 99) math = 99;    // max limit at 99cm

        // convert from 0-99 to 2 decimal digits, 0-99cm
        cm10=0;
        while(math >= 10)
        {
            cm10++;
            math -= 10;
        }
        cm = math;
    }


    //---------------------------------------------------------
    void send_serial_message(void)
    {
        // send message and number to serial port
        send_serial_byte('J');          // send ascii text
        send_serial_byte('u');
        send_serial_byte('n');
        send_serial_byte('e');
        send_serial_byte('b');
        send_serial_byte('u');
        send_serial_byte('g');
        send_serial_byte(' ');

        send_serial_byte('G');
        send_serial_byte('P');
        send_serial_byte('2');
        send_serial_byte('=');

        send_serial_byte('0'+ cm10);    // send number as ascii text 0-9
        send_serial_byte('0'+ cm);  // send number as ascii text 0-9
        send_serial_byte('c');
        send_serial_byte('m');

        send_serial_byte(13);           // send carriage return/linefeed pair
        send_serial_byte(10);           //  to start a new text line

    }


    //---------------------------------------------------------
    void send_serial_byte(unsigned char data)
    {
        // this manually sends a serial byte out any PIC pin.
        // NOTE! serial is inverted to connect direct to PC serial port.
        // baud timing is done by using TMR3L and removing
        // timer error after each baud.

        i=8;                        // 8 data bits to send

        PIN_SER_OUT = 1;            // make start bit
        TMR3L = (256 - SER_BAUD);   // load TMR3 value for first baud;
        while(TMR3L.F7);            // wait for baud

        while(i)            // send 8 serial bits, LSB first
        {
            if(data.F0) PIN_SER_OUT = 0;    // invert and send data bit
            else        PIN_SER_OUT = 1;

            data = (data >> 1);         // rotate right to get next bit
            i--;
            TMR3L -= SER_BAUD;      // load corrected baud value
            while(TMR3L.F7);        // wait for baud
        }

        PIN_SER_OUT = 0;        // make stop bit
        TMR3L -= SER_BAUD;      // wait a couple of baud for safety
        while(TMR3L.F7);
        TMR3L -= SER_BAUD;
        while(TMR3L.F7);
    }
     
     
    Last edited: Aug 29, 2009
  18. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,800
    Likes:
    134
    Location:
    morristown,tn
    That shouldn't be to hard to use with hi-tech
     
  19. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    168
    Location:
    Toronto, Canada
    I would test the sensor before proceeding. A multimeter, a sink with water and a 6v lantern battery should do the trick.
     
  20. fantabulous68

    fantabulous68 Member

    Joined:
    Feb 20, 2007
    Messages:
    323
    Likes:
    0
    yeah ill do that as soon as the component arrives Bill. Would someone pls tell me how would the the 16-bit math var in the code above be affected if i use the pic16f690 which is 14-bit?
     
  21. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,216
    Likes:
    640
    Location:
    Derbyshire, UK
    The 16F690 is an 8 bit processor, it's the instruction width that's 14 bit.

    16 bit maths just means you use two 8 bit registors instead of one 16 bit one.
     

Share This Page