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

    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

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

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

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

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

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    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.

  7. #7
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: 12F683 - basic code not working

    OK, I've removed the breadboard connection from the EasyPic5 and configured the board to use the 10K pot on the EasyPic5. I have the USB/Serial breakout board connected to +5v, GND and GPIO.5 and confirmed the communications were OK in the serial monitor window. I've added the correct setting for the comparators (must of removed the 0 at the end of the statement when editing the file- my bad ).

    Loading the code I can get 0 - 1023 by moving the wiper on the EasyPic5 board, so the chip is reading 0-5v and converting that to a digital value.

    I then added back the lines for the motor control, making them GPIO.2 and 4, and hooked up the H-Bridge on the breadboard - I added the trigger of 100 in the line "if number >100 then turn on the motor" and watched what happened when I turned the pot on the easyPic5 board and low and behold, when the value hit 101 the motor came on......

    I'm going to pull all the connections off the breadboard and re-check the wiring - seems that the issue is external and not the code. One possible cause could be the resistance / capacitance nature of the breadboard causing the issue ??

    Anyway, thanks guys for your comments and input.... as always it's nice to receive assistance and helps with the learning curve

    Malcolm

  8. #8
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: 12F683 - basic code not working

    Hi again,

    I'm stumped on what's happening when I run the following code, and having gone over it time and time again I might be overlooking the obvious.

    I have a motor connected to the H-bridge module that arrived yesterday - making pins 1 high and 2 low makes the motor run one way and reversing which pin is high and low makes the motor run the opposite way. I'm using a separate 9v supply for the motor.

    When first turned on, the threshold set means that the code is set to night mode. I then shine a light on the LDR and the motor runs until I connect GPIO.5 to 5v to simulate the door closing the micro-switch at the end of its travel. So far so good. However, if I remove the connection between GPIO.5 and +5v to simulate the door in the close state GPIO.5 remains latched high. Grounding the pin momentarily has no affect, and disconnecting the H-bridge has no affect. Only way to clear this is to turn off and on the EasyPic5 board.

    Code:
    ASM
     __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF 
    endasm
    
    DEFINE ADC_BITS 10              ' ADC resolution
    DEFINE ADC_CLOCK 3
    DEFINE ADC_SAMPLEUS 50
    number VAR word                 ' Variable to store ADC result
    day var byte                    ' Variable to indicate day or night
    
    CMCON0=7
    CCP1CON=0
    ANSEL = %00000001               ' GPIO.0 A/D in, rest digital
    ADCON0.7 = 1                    ' Right justify for 10-bit
    GPIO = %00000000                ' Initialize outputs
    TRISIO = %101011                ' GPIO 0,1,3,5 = input, 2 & 4 outputs
    
    
    Motor1          var gpio.2      ' H-Bridge pin 1
    Motor2          var gpio.4      ' H-Bridge pin 2
    SW1             VAR gpio.5      ' Door open limit switch
    
    
    low sw1                         ' Ensure limit switch is 0 (door is closed)
    low Motor1                      ' Turn off H-Bridge pin 1
    low Motor2                      ' Turn off H-Bridge pin 2
    day=0                           ' Initialise unit to check if day time
    
    main:
    if sw1=0 then day = 0           ' If limit switch is open, and mode is night then
    adcin 0,number                  ' Read AN0
    if day=0 then                   ' Day 0 = night, Day 1 = Day
      If number >660 then           ' If ACD result is above a threshold
      low SW1                       ' Set state of limit switch to off
      high Motor1                   ' Turn on H-Bridge pin 1
      Low Motor2                    ' Turn off H-Bridge pin 2
    endif
    endif
    if SW1 = 1 then                 ' Door closes limit switch and applies 5v to GPIO.5
      low Motor1                    ' Turn off H-Bridge pin 1 }  Stop motor
      low Motor2                    ' Turn off H-Bridge pin 2 }
      day = 1                       ' Set variable to day to indicate the door is open
    endif
    
    goto main
    The logic behind this project is that when the light level increases at dawn past the threshold it drives the motor which by use of a threaded rod slides open the door on the coop. When the door closes a micro-switch at the end of the travel the power to the motor is turned off and the unit placed in day mode. At the end of the day a switch will be pressed directly applying 5v to pin2 of the H-Bridge thus reversing the motor, opening the door and opening the switch, changing its state from 1 to 0.

    Any ideas ??

    Malcolm

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