12F683 - basic code not working


Closed Thread
Results 1 to 40 of 57

Hybrid View

  1. #1
    Join Date
    Sep 2010
    Location
    Las Vegas, NV
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: 12F683 - basic code not working

    I think you need a voltage divider network for the LDR. As it is the voltage on your ADC input line will always be 5VDC.

    I also wonder about the capacity of your transistors. The stall current on the some of the motors I found was 46 Amps. If the motor were to stall, your transistors would burn out with unknown consequences. If you want to use this motor I'd put a fuse in the supply line to limit current during stalls.

    Typically motors make alotta noise when running. I think it's a great idea to separate the power supplies. Just make sure to make the low side connected between the two systems.

    My intimation of TRISIO is relevant to PBP2.6 also. I'm not sure if PBP starts with LSB when reading the TRISIO and quits after 6 bits or starts at the MSB and quits after 6 bits. I do know the 12F683 only has 5 I/O pins and one I only pin so your TRISIO statement should be 6 bits wide.

  2. #2
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: 12F683 - basic code not working

    Thanks for the comments:

    I've tried using the LDR in series with a 10K pot between 5v and GND, connecting GPOI.0 to the junction between them and still can't get anything meaningful for the value of the variable that stores the ADC readings.

    I've corrected the code thus:

    Code:
    ASM
     __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF 
    endasm
    
    ' Hardware configuration
    ' ======================
    
    
    
    TRISIO = %001111 
    ADCON0 = %10000001 
    ANSEL = %00110011 
    DEFINE ADC_BITS 8
    DEFINE ADC_SAMPLEUS 50 
    CMCON0=7
    
    Motor1 var GPIO.4
    Motor2 var GPIO.5
    
    ADChannel0 var byte
    
    GPIO = 0 ' clear all output
    Pause 50 ' internal oscillator settle time
    
    main:
    low Motor1
    low Motor2
    adcin 0,adchannel0 ' Read AN0
    
    If adchannel0 >10 then
      high Motor1
      Low Motor2
    pause 5000
    low Motor1
    low Motor2
    
    endif
    
    
    goto main

  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

    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

  4. #4
    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 ?

  5. #5
    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

  6. #6
    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 ??

  7. #7
    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

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