Pic12f683 Cmcon0 = 7


Closed Thread
Results 1 to 26 of 26

Hybrid View

  1. #1


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Melanie View Post
    Dave - What is your definition of 'erratic'?

    width = mon*1

    Oh... Can you tell me what is the point of the *1 part of this line?

    Basically your program states that if mon is >2 then your HPWM will be at around 12.5% otherwise it will be solid ON. There's not much else that happens... or at least it would do if you remembered a GOTO MAIN after the last IF statement!!! Because as it is, your code is slamming straight into the OneP section.

    You could rewrite your code as...

    Code:
    Main:
        ADCIN 3, mon ' Read channel AN3 to Mon
        width = mon
        if width > 2 then 
    	hpwm 1,32,2000
    	else
    	hpwm 1,255,2000
    	endif
        pause 1000
        goto main
    I'm also assuming you've set TRISIO and such properly.
    Melanie,

    Thank you for replying.

    Initially, I couldn't get the code to compile without width=mon*1. I've sice removed the *1 and it compiles OK now. No idea why.

    Erratic = while it's executing hpwm 1,255,2000 when "width is less than 2" with either version of code, it cycles randomly back and forth to 1,32,2000. While "width is greater than 2" the output is stable.

    I'm going to try a while wend in there.

    ~ Dave

    Here's what I'm using since your reply:

    Code:
    INTCON = %10001000 'internal oscillator
    OSCCON = %01110000 ' 8mHz
    CMCON0 = 7 'Comparators off
    GPIO = %00000000 'outputs low
    TRISIO = %00010000 'GP4 as input
    ANSEL = %00111000 'AN3 analog
    
    'Define ADCIN parameters
    Define ADC_BITS 10 ' Set number of bits in result
    Define ADC_CLOCK 3 ' Set clock source (3=rc)
    Define ADC_SAMPLEUS 50 ' Set sampling time in uS
    
    ' Variables
    Mon var Word
    Width var byte
    Pause 100
    
    Main:
        ADCIN 3, mon ' Read channel AN3 to Mon
        width = mon
        if width > 2 then 
    	hpwm 1,32,2000
    	else
    	hpwm 1,255,2000
    	endif
        pause 1000
        goto main
     end

  2. #2
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    OK, the penny's dropped...

    mon = 16 bits, whilst width = 8 bits... you can't put a quart into a pint pot...

    try...

    width = mon/4

    In case you're wondering why /4 instead of /8... that's because your ADC reading is 10 bits. So why not read 8-bit ADC to start with?

  3. #3


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Melanie View Post
    OK, the penny's dropped...

    mon = 16 bits, whilst width = 8 bits... you can't put a quart into a pint pot...

    try...

    width = mon/4

    In case you're wondering why /4 instead of /8... that's because your ADC reading is 10 bits. So why not read 8-bit ADC to start with?
    Because I'm such a noob that I have no idea how to do that. Now I'm really confused.

    So with this change, when the IC reads less than 2 volts on "mon" it should execute hpwm 1,255,2000; more than 2 volts and it should execute 1,32,2000?

    ~ Dave
    Last edited by StoneColdFuzzy; - 11th June 2009 at 21:07.

  4. #4
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Looks like we have a lot to learn here...

    No, it WON'T be two volts....

    The ADC returns a number between zero and 1023 (because you are in 10-bit mode), where zero equals 0v on the ADC, and 1023 equals full-scale 5v on the ADC. Each click on the ADC scale represents 1/1023 ie a FRACTIONAL part of 5v.

    So if 5v = 1023, then 2v will be two fifths of 1023 (namely 409).

    So, if you want the trip point to be 2v and with a 10-bit ADC, your code should be...

    Code:
    Main:
        ADCIN 3, mon ' Read channel AN3 to Mon
        if mon > 409 then 
    	hpwm 1,32,2000
    	else
    	hpwm 1,255,2000
    	endif
        pause 1000
        goto main

  5. #5


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Melanie View Post
    Looks like we have a lot to learn here...

    No, it WON'T be two volts....

    The ADC returns a number between zero and 1023 (because you are in 10-bit mode), where zero equals 0v on the ADC, and 1023 equals full-scale 5v on the ADC. Each click on the ADC scale represents 1/1023 ie a FRACTIONAL part of 5v.

    So if 5v = 1023, then 2v will be two fifths of 1023 (namely 409).

    So, if you want the trip point to be 2v and with a 10-bit ADC, your code should be...

    Code:
    Main:
        ADCIN 3, mon ' Read channel AN3 to Mon
        if mon > 409 then 
    	hpwm 1,32,2000
    	else
    	hpwm 1,255,2000
    	endif
        pause 1000
        goto main
    Wow, it's no wonder I was frustrated enough to chew the lips off of a carp!

    OK, where is it that I look up the ins and outs of 8 bit, 10 bit and such? I'd love to find a PBP book for Dummies or something like it.

    Thanks again Melanie!

    ~ Dave
    Last edited by StoneColdFuzzy; - 12th June 2009 at 14:36.

  6. #6


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Melanie View Post
    OK, the penny's dropped...

    mon = 16 bits, whilst width = 8 bits... you can't put a quart into a pint pot...

    try...

    width = mon/4

    In case you're wondering why /4 instead of /8... that's because your ADC reading is 10 bits. So why not read 8-bit ADC to start with?
    So, if I would have used Define ADC_BITS 8, I would have been OK?

    ~ Dave

  7. #7
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Well, in that case with 8 bit ADC, variable mon should be a BYTE rather than a WORD.

    Your ADC range for 0-5v is an ADC reading of 0-255 where 255=5v.

    So 2 volts will be 2/5ths of 255 namely 102...

    So the IF statement then becomes IF mon > 102 THEN...

    The decision to use 8-bit or 10-bit ADC is simply a matter of precision, and you chose whether you need steps of 1/255 or 1/1024 for the ADC span that you are using.

  8. #8


    Did you find this post helpful? Yes | No

    Default

    Melanie,

    1/255 is fine.

    Where can I go to learn the basics of using AD conversions? It's obvious that I have no clue.

    Thanks again for the help.

    ~ Dave

  9. #9
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Posts #20 and #23 of this thread... and you're almost there... it's real simple... add to that the ADCIN section of your PBP Manual and the ADC Section of your chosen PICs Datasheet.

    Lesson 1.

    Get a PIC with ADC Input capability and enough pins to drive an LCD Display. Hook it up so you can send messages to your LCD. Put a POT (10K or 5K) across your 5v supply with the wiper to the PIC ADC pin. Write a small program to display what the ADC value is when reading 8-bit. Wind the POT up and down a few times to see digits change...

    Lesson 2.

    Repeat above with 10-bit ADC input.

    You're an expert!

  10. #10


    Did you find this post helpful? Yes | No

    Default

    Melanie,

    OK, I contacted my supplier rep and he's sampling me some LCD screens and driver ICs. He gasped and said "Wow, you're finally going digital!"

    I also went a step further and ordered the book, Programming PIC Microcontrollers with PICBASIC. Something that I should have done a long time ago.

    Thanks again!

    ~ Dave

Similar Threads

  1. Conway's Game Of Life
    By wellyboot in forum mel PIC BASIC Pro
    Replies: 45
    Last Post: - 28th May 2020, 06:14
  2. Thermo 7 segments - little problem
    By fratello in forum mel PIC BASIC Pro
    Replies: 49
    Last Post: - 27th July 2013, 07:31
  3. RS485 bus - starting probem
    By wurm in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 27th January 2010, 13:35
  4. Working with indivividual bytes of an array
    By J_norrie in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 22nd January 2007, 19:16
  5. code conversion
    By saturnX in forum mel PIC BASIC
    Replies: 19
    Last Post: - 3rd October 2005, 17:17

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts