+ Reply to Thread
Page 1 of 2
1 2 Last
Results 1 to 15 of 22

Thread: How to Use a PIC16F873A for IR proximity, school project,simple but help needed

  1. #1
    MoRoH Newbie
    Join Date
    Nov 2008
    Location
    Corner Brook, Newfoundland, Canada
    Posts
    61

    Question How to Use a PIC16F873A for IR proximity, school project,simple but help needed

    Hi all,

    I am an electronics engineering technology student doing my final project.

    I need to use a PIC16F873A to take an analog voltage and turn motor A on if it is less then 4 and motor B on if it more than a 4.

    This is a very simple task, I have some ideas but i have no experience with PIC's.


    Thanks in advance


  2. #2
    Super Moderator Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent
    Join Date
    Nov 2003
    Location
    Derbyshire, UK
    Posts
    29,775

    Default

    Check my tutorials, which show how to use the analogue inputs.
    PIC programmer software, and PIC Tutorials at:
    http://www.winpicprog.co.uk

  3. #3
    MoRoH Newbie
    Join Date
    Nov 2008
    Location
    Corner Brook, Newfoundland, Canada
    Posts
    61

    Default

    Thanks!

    I'll take a look at that

  4. #4
    MoRoH Newbie
    Join Date
    Nov 2008
    Location
    Corner Brook, Newfoundland, Canada
    Posts
    61

    Default

    Ok,

    I will use a reference voltage of 5v and if V>2.5 motor A will come on and if v < 2.5 V motor B will come on? Is this correct

    I am also wondering what type of code is best to use?

    What ports should i use for input and output?



    Thanks, any help it greatly appriciated

  5. #5
    Noggin Good Noggin Good
    Join Date
    Jul 2003
    Location
    Austin, TX
    Posts
    436

    Default

    Virtually any port that has the peripheral you need. If you want to read an analog voltage, use one of the analog pins. If you want to output a signal, use virtually any pins. If you want to output a PWM signal to vary the speed of the motor, use an OC pin.

    The best code to use is the kind you know.
    A rectangular bear is just a polar bear after a coordinate transform. -- I dunno who.
    A recent study shows that research causes cancer in rats. -- I dunno who said that one either.

  6. #6
    MoRoH Newbie
    Join Date
    Nov 2008
    Location
    Corner Brook, Newfoundland, Canada
    Posts
    61

    Default hmm ADC code

    ok,

    1) Can someone clarify an OC pin, is that an oscillator pin

    2)So i've looked into this a bit more...I'm going to use an analog input (RA0) and output a 8bit binary word on ports (RB0-RB7) for LED. I will also like to output a PWM signal so the stronger the analog signal is the slower the motor will spin.

    3) For the ADC it seems pretty simple...from examples i've gotten this code, but it comes with errors. Help would be awesome.

    line/column message no. message text
    a) 1.3 11 ';' expected by ADCON1 found
    b) 1.5 4 specifier needed
    c) 1.5 12 internal error




    Code:
    unsigned short adc_val  ;
    void main ( )
    
    ADCON1 = 0x00  ;
    TRISA  = 0xff  ;
    TRISB  = 0x00  ;
    while (1)
    (
    adc_val = adc_read(2)    ;
    portb = adc_val     ;
    )
    
    )
    

  7. #7
    Noggin Good Noggin Good
    Join Date
    Jul 2003
    Location
    Austin, TX
    Posts
    436

    Default

    OC is Output Compare, the name doesn't seem all that obvious to me unless you know how the OC pin works. In general, an output compare works by comparing a period timer to a counter value.

    First, you set up a timer to run at some frequency, a good rule of thumb for a motor is 20kHz.
    Set your period register to some value
    The hardware will compare the timer value to the period register and set/clear the output pin based on if the timer is greater than, equal to, or less than the period register.
    When you want to change the duty cycle, you just change the period register.

    I've only used OC on PIC24 and PIC32 so I can't help with absolute specifics on PIC16, but it should work the same way. It may be called something other than OC on PIC16.

    As for the errors in your code, you may need to post more of the code. The only thing I see wrong with what you have is that you're missing a '{' after void main(). Also, ADCON1 I think should be set to 0xFE as you only need RA0 to be analog. TRISA should be set to 0x01 to make RA0 an input and the rest an output.

    With a digital input that is floating, if the pin is say 2.5v, then both the PFET and the NFET on the input gate will be partially on, which will cause additional current to flow through the chip. It won't hurt anything, but in general, it is good practice to set all unused pins to output high/low or input with pull-ups or pull-downs.

    Of course, read your datasheet and make sure I'm not telling you wrong about this

    Hmm.... I don't use names like "unsigned short" so I don't know how many bits wide it is (I know I should as that is fairly basic...) but I think it is 8 bit. The ADC's in PICs are generally 10 bits, which means if your function adc_read() returns a 16 bit value, you are probably losing the 2 most significant bits. What you should do is change the ADC setup to left adjust the ADC result and return only the high byte of the value (if you don't need 10 bits of resolution). This could also be done by simply shifting the result to the right by 2, but it is more efficient to have the ADC module left adjust the result and read only one byte. The datasheet will have more information on this and explain it in detail.
    Last edited by Noggin; 1st February 2009 at 03:38 PM.
    A rectangular bear is just a polar bear after a coordinate transform. -- I dunno who.
    A recent study shows that research causes cancer in rats. -- I dunno who said that one either.

  8. #8
    Torben Excellent Torben Excellent Torben Excellent Torben Excellent Torben Excellent Torben Excellent Torben Excellent Torben Excellent Torben Excellent
    Join Date
    Oct 2006
    Location
    B.C., Canada
    Posts
    2,469

    Default

    Quote Originally Posted by MoRoH View Post
    3) For the ADC it seems pretty simple...from examples i've gotten this code, but it comes with errors. Help would be awesome.

    line/column message no. message text
    a) 1.3 11 ';' expected by ADCON1 found
    b) 1.5 4 specifier needed
    c) 1.5 12 internal error

    Code:
    unsigned short adc_val  ;
    void main ( )
    
    ADCON1 = 0x00  ;
    TRISA  = 0xff  ;
    TRISB  = 0x00  ;
    while (1)
    (
    adc_val = adc_read(2)    ;
    portb = adc_val     ;
    )
    
    )
    
    Hi there,

    Where did you get that code? And is that all the code? Assuming it's supposed to be C, and assuming that that's all the code there is, there is a lot wrong with it.

    As Noggin pointed out, it doesn't have an opening curly brace for the main function. Actually, it doesn't have a closing brace either; the closing "brace" is actually a right parenthesis (meaning it's a ")" when it should be a "}").

    Same with the while() loop. It should look like

    Code:
    // Right.
    while (1) 
    {
        // loop contents go here
    }
    
    . . .instead of:

    Code:
    // Wrong.
    while (1) 
    (
        // loop contents go here
    )
    
    The code uses ADCON1, TRISA, TRISB, adc_read(), and portb without any of these variables and functions having been defined. There should be an #include directive to include the header for the chip you're using.

    What compiler are you using?

    (BTW: No offense intended if you wrote that code; everybody has to start somewhere. I'm just trying to point out reasons it might not be working.)


    Regards,

    Torben

    [Edit: Hey, you're from Corner Brook! I had a very good night followed by a very bad morning in Corner Brook a few years back. Newfs have to be the friendliest folk I've ever met.]
    Last edited by Torben; 1st February 2009 at 06:29 PM.
    Curiosity was framed. Ignorance killed the cat.

  9. #9
    MoRoH Newbie
    Join Date
    Nov 2008
    Location
    Corner Brook, Newfoundland, Canada
    Posts
    61

    Default

    HEy


    First of all glad you had a good night here, not so glad you had a bad morning but the better the night the worse the morning.


    I'm new to PIC's, and im not an engneering student, my schooling is mostly focused on troubleshooting, and the hardware stuff. I have very little design and programming altough ive used the 8255 and assembly a fair bit and a small course on C.


    To get more knowledge ive been doing tutorials on simple programs like blinking LED and this ADC i found on youtube, but i couldint get it working. but now that i got this one working i will adapt it to my PIC and move on.


    The { vs ( was my main problem! thanks for the hints guys. I will update you on my progress after some more research.


    Thanks for the replys, I really appriciate it!

  10. #10
    Noggin Good Noggin Good
    Join Date
    Jul 2003
    Location
    Austin, TX
    Posts
    436

    Default

    I feel silly I didn't notice that they were ( and ) instead of { and }
    A rectangular bear is just a polar bear after a coordinate transform. -- I dunno who.
    A recent study shows that research causes cancer in rats. -- I dunno who said that one either.

  11. #11
    MoRoH Newbie
    Join Date
    Nov 2008
    Location
    Corner Brook, Newfoundland, Canada
    Posts
    61

    Default

    Hahaa man don't worry, i didin't even know they were supposed to be { } ha.

  12. #12
    MoRoH Newbie
    Join Date
    Nov 2008
    Location
    Corner Brook, Newfoundland, Canada
    Posts
    61

    Default

    ok,

    so i got my code adapted to my 16F873A now inputs A0 (A1-A4 are off) B0-b7 are set to outputs.. so I get my output on the simulator using this code:

    Nogin, you were right about ADCON1 and TRISA thanks

    Code:
    unsigned short adc_val  ;
    void main ( )
    {
    
    ADCON1 = 0xFE  ;
    TRISA  = 0x01  ;
    TRISB  = 0x00  ;
    while (1)
    {
    adc_val = adc_read(0)    ;
    portb = adc_val     ;
    }
    
    }
    


    Next is how to get PWM output.

  13. #13
    MoRoH Newbie
    Join Date
    Nov 2008
    Location
    Corner Brook, Newfoundland, Canada
    Posts
    61

    Default

    Question : i'm using MikroC for my programming...the #include code includes libraries from which i will use code right? how do i add libraries?

    Answer : unknown

  14. #14
    demestav Newbie
    Join Date
    Aug 2003
    Posts
    128

    Default

    Hi there!

    Just a quick note: Make sure you setup the acquisition time correctly to assure correct readings... This is explained very well in the datasheet.

  15. #15
    MoRoH Newbie
    Join Date
    Nov 2008
    Location
    Corner Brook, Newfoundland, Canada
    Posts
    61

    Default

    ok, thanks, ive been reaidng the data sheets and commands for PWM, when i get some code made up, ill post it with the errors (99.999% sure there will be errors)

    Thanks

    PS: i got the library thing sorted out, just a misunderstanding

+ Reply to Thread
Page 1 of 2
1 2 Last

Similar Threads

  1. Help needed for school project
    By tucka20 in forum Electronic Projects Design/Ideas/Reviews
    Replies: 2
    Latest: 12th May 2008, 09:12 PM
  2. Ei, i nid some simple electronic projects for school..
    By mori38 in forum Electronic Projects Design/Ideas/Reviews
    Replies: 4
    Latest: 30th June 2007, 11:59 AM
  3. Simple project: audio cable tester (assistance needed)
    By Pixie in forum Electronic Projects Design/Ideas/Reviews
    Replies: 1
    Latest: 29th September 2005, 07:32 PM
  4. Help needed to design a simple robotics project
    By sci-lover in forum Electronic Projects Design/Ideas/Reviews
    Replies: 2
    Latest: 18th August 2003, 09:14 AM
  5. Help: School Project
    By less_than_jake_2002 in forum General Electronics Chat
    Replies: 0
    Latest: 5th July 2003, 11:33 PM

Tags for this Thread