12F683 - basic code not working


Closed Thread
Results 1 to 40 of 57

Hybrid View

  1. #1
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default Re: 12F683 - basic code not working

    Check the risistence of your LDR in both condition (dark and light). Since the lowest resistance should be found in light, use this value for your devider network. In this way you should have a value in volts near to zero in dark and near to Vdd/2 in light, more than enough to be sure to trip the system at the right time.

    By the way the schematics of your Hbridge is wrong! NPN should be connected on the low side, while PNP should be connected on the high side.

    Follow the wise suggestion given above, always use freewheel diodes when using inductors!

    Cheers

    Al.
    Last edited by aratti; - 19th January 2014 at 20:43.
    All progress began with an idea

  2. #2
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: 12F683 - basic code not working

    Al,

    My fault, I drew the schematic the wrong way round (I was rushing ). I've now placed diodes between the collector and emitters of each transistor as suggested.

    Measuring the LDR I get a few hundred ohms when the torch is directly into the face of the LDR and around 2.1m ohms when I place my finger over it. Under the ambient light in the room it was averaging 16K - 18K. I've tried placing the LDR and an 18k resistor in series between +ve and GND and then connecting GPOI.0 to the junction between the two. The LED on the development board was lit quite bright and covering the LDR had no effect. I then used a 200 ohm resistor based on the fact that in light the resistance was a few hundred ohms when tested.

    With the 200 ohm resistor in place as a voltage divider with the LDR, under normal room light with a value of adchannel0 >2 in the if then statement the motor runs. If I change that to adchannel0 >3 and re-compile the motor won't run regardless of bright light or darkness.

    With the divider made up as the LDR between +5v and GPOI.0, and the 18K between GPIO.0 and GND I get 0.6v on GPIO.0 when the ldr is covered, and around 3.5v with the torch shone directly at the ldr. So I would assume this voltage range of 3v should be enough for the ADC to read ? so why is it that the ADC is acting more like a switch with a value of 1 rather than 255 steps ?

  3. #3
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default Re: 12F683 - basic code not working

    Add the following defines on top of your code:

    DEFINE ADC_BITS 8
    DEFINE ADC_CLOCK 2
    DEFINE ADC_SAMPLEUS 50

    Cheers

    Al.
    All progress began with an idea

  4. #4
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: 12F683 - basic code not working

    Hi Al,

    I've modded the code so I can see what's going on.

    Code:
    ASM
     __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF 
    endasm
    
    TRISIO = %001111 
    ADCON0 = %10000001 
    ANSEL = %00110011 
    DEFINE ADC_BITS 8
    DEFINE ADC_SAMPLEUS 50 
    DEFINE ADC_CLOCK 2
    CMCON0=7
    
    
    Tx              var GPIO.5         
    ADChannel0 var byte
    
    GPIO = 0 ' clear all output
    Pause 50 ' internal oscillator settle time
    
    main:
    adcin 0,adchannel0 ' Read AN0
    Serout2 Tx,84, ["val: ",#adchannel0,10]
    pause 5000
    
    goto main
    The divider made up as the LDR between +5v and GPOI.0, and the 18K between GPIO.0 and GND.

    Compiled the code and loaded it to the chip. Opening the serial tool in Micro-studio I get a value of 1 when the LRD is in ambient light or covered and 2 with the torch shone directly on it.

    Ideas ??

  5. #5
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: 12F683 - basic code not working

    As a postscript, i wired a 10K pot between +5v and GND with the wiper to GPIO.0 and got the same results - seems the ADC isn't working

  6. #6
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: 12F683 - basic code not working

    OK I'm going to throw this thing out the window in a moment

    I downloaded a sample code from this forum written by Bruce

    Code:
    ASM
     __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF 
    endasm
    
    DEFINE ADC_BITS 10
    DEFINE ADC_CLOCK 3
    DEFINE ADC_SAMPLEUS 50
    number VAR word
    
    'CMCON = 7 ' Comparators off
    ANSEL = %00000001 ' GPIO.0 A/D in, rest digital
    ADCON0.7 = 1 ' Right justify for 10-bit
    GPIO = %00000000 ' Initialize outputs
    TRISIO = %00000001 ' GPIO.0 = input, rest outputs
    Tx              var GPIO.5
    
    
    main:
    adcin 0,number ' Read AN0
    Serout2 Tx,84, ["val: ",#number,10]
    pause 5000
    goto main

    With the 10K pot wired between the supply with the wiper to GPIO.0 and get readings between 437 and 440 !!!!!!!!!!!!!! - still just three digits between min and max.
    I had to rem out the CMCON line - presumably the 12F683 has no comparators, would that be the case.

  7. #7
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Re: 12F683 - basic code not working

    Hi Malcolm,
    Comparator register for this chip is CMCON0 set CMCON0=7 to turn off as per data sheet 8.3
    It compiles. You might have to set the CCP1CON to zero too , not sure about that though.
    Last edited by Archangel; - 20th January 2014 at 11:18.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  8. #8
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,621


    Did you find this post helpful? Yes | No

    Default Re: 12F683 - basic code not working

    Hi,
    I haven't been following this thread but why do you say presumably the 12F683 has no comparators, would that be the case ?

    Why not simply open the datasheet and have a look? Sure enough, the 12F683 DOES have a comparator and it's control register is CMCON0 - which you had got correct in your previous snippets of code but not in the latest one. I'm not saying this is the problem though.

    Verify that ALL your grounds/commons are connected, as you move the wiper of the pot measure the voltage right at the PIC, between pins 8 (Vss) and 7 (AN0) to verify that it's not a loose connection or floating ground etc.

    /Henrik.

Similar Threads

  1. 12F683 - Pin1 not working
    By ruijc in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 8th February 2014, 17:38
  2. 12F683 Maths and code genius reqd.
    By retepsnikrep in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 1st January 2014, 08:44
  3. please help with 12f683 pwm code
    By haidar in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 22nd May 2013, 21:25
  4. Problem converting 12F683 code to 12F1840
    By RossWaddell in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 14th March 2013, 01:55
  5. Working code but my layman approach uses too much code space
    By Christopher4187 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 14th December 2012, 20:44

Members who have read this thread : 2

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