weird 12F629/675 problem


Closed Thread
Results 1 to 10 of 10
  1. #1

    Default weird 12F629/675 problem

    Hello Melanie. Hello Everyone. I have to apologize to you Melanie. I started a project using your Olympic Timer as the "heart" of a background timer, and before I got the chance to even TRY to get it to work, the project was killed. I will go back to it for educational purposes soon. I do have something strange going on now with a simple piece of code. Has this happened to anyone and does anyone have an explanation? I have encountered the same problem with both the 12F629 and the 12F675. In this case I'm using an ADC on the 'F675. Here is the code:

    ANSEL = 0 'all inputs digital
    CMCON = 7 'comparators off
    trisio = %11111111 'all inputs for now
    b0 VAR BYTE ' define names for variables
    b1 VAR BYTE

    start: ' this line is the key for fixing the problem!
    ADCIN 0,b1
    IF b1 = 000 Then start
    LET b0 = b1
    NAP 3
    ADCIN 0,b1
    IF b0 > b1 + 007 Then play
    IF b0 < b1 - 007 Then play
    GoTo start

    play:
    High GPIO.4
    Pause 5000
    Low GPIO.4
    Pause 100
    GoTo start

    The ADC is detecting a voltage change during the NAP 3 period. If detected, GPIO.4 turns on a FET which powers about 10 LED's. The problem is that it works intermittently. Sometimes it's fine, sometimes GPIO.4 doesn't shut off. Here is what's making me crazy; the exact same program compiled for a 16LF818 (replacing GPIO with PORTB) works perfectly every time! On the 12F675, this is how I fixed it: I moved "start" to the first line of the program above the ANSEL = 0 line and the problem went away. Is this a "quirk" of 8-pin PIC's or am I missing something here? The circuit runs on a 3V battery and brownout detect is off. And with this, I have another question about the 'F675: ADCIN 0, ADCIN 1, ADCIN 2 all work perfectly. The 4th ADC input "ADCIN 4" does not work. If this is because of the way PBP defaults when the program is compiled, is there a source of information to find out exactly what features will be available on the PIC without specifying them in the header file? On the 16LF818, all 4 ADC's work after compiling. Thank you.

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


    Did you find this post helpful? Yes | No

    Default

    A few things strike me here...

    1. Why are you setting ANSEL=0 (Analog Inputs to OFF) and then using ADCIN for Analog Input? Surely you would set ANSEL for Analog per your requirements. This might cause you some anomalies.

    2. There are four ADC inputs on the 12F675, but wouldn't you access the fourth one as ADCIN3 (counting up from zero) rather than ADCIN4.

    3. The pause turning GPIO.4 Low is only 100mS, (compared up to 5 Seconds High) does it actually happen but you haven't noticed and it gets turned on again immediately?

    4. And do set TRISIO to Output for GPIO.4...

  3. #3


    Did you find this post helpful? Yes | No

    Default

    Hi Melanie. 1 - I have always set ANSEL=0 at the beginning of the program. The ADCIN command always converts the digital inputs back to analog as long as you use the command ADCIN 1,B1 and NOT use ADCIN PORTA.1,B1 or ADCIN GPIO.1,B1. It works for me every time. This is what is so confusing to me when I download sample programs - people have all sorts of header files for ADCON0, ADCON1, etc. They are not needed for 256 & 1024 bit results, ADCIN does it automatically. I have more examples if you want to see the header files.
    2 - Your right, I typed the wrong port pin. AN0 is on GP0, AN1 is on GP1, AN2 is on GP2 and AN3 is on GP4. I tried ADCIN 3 and still no response from AN3 on GP4.
    3 - Turns on again immediately? Yes possibly. I will delay the off and repeat the experiment. However, again the 16LF818 works every time.
    4 - Regardless of how I set the trisio ports (all input or all output), it seems PBP automatically reconfigures the pin on demand in the program. GPIO.4 does indeed become an output when called on in my program. Thank you for such a quick response.

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


    Did you find this post helpful? Yes | No

    Default

    Personally I never use ADCIN (not a fan of any of those kinds of Compiler commands) always setting the registers myself because then I know what's going on (having been bitten in the past by the strange way some compiler authors go about implimenting things)... but I'll try the permutations later in the day and let you know.

    The trouble is thre is NO CONSISTENCY in the way people write programs. Contrary to popular belief, there are RULES to programming. Things get done a certain way in a certain sequence. Then along comes a high-level compiler that claims to do some of it for you... but I've noticed it DOESN'T always happen consistently across all PICs accross all compiler versions. So I've ALWAYS predefined all the I/O environment as one of the first things that I do as soon as my code starts running. As a consequence, I don't ever experience the majority of anomalies and problems that people post on the support forum.

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


    Did you find this post helpful? Yes | No

    Default

    Just as an aside... you are aware that using ADCIN for channel 3, clashes with your LED on GPIO.4?

    I can confirm that ADCIN 3 equates to the GP4 pin... and it works just fine (naturally not with an LED outputting on it!). All four ADC pins on the 12F675 work as expected with ADCIN (0-3).

  6. #6


    Did you find this post helpful? Yes | No

    Default

    Hi Melanie. My ADC experiments were not in this code or circuit using GPIO.4 as an output. Do I need an extra header file to get AN3 to work or did you do it the way I did with the other 3?

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


    Did you find this post helpful? Yes | No

    Default

    Full Test Code that I threw together to see if it would work with your ANSEL and TRISIO settings...

    Code:
    	'
    	'	PIC Defines
    	'	-----------
    	@ DEVICE pic12F675, INTRC_OSC_NOCLKOUT
    		' System Clock Options (Internal)	
    	@ DEVICE pic12F675, WDT_ON
    		' Watchdog Timer
    	@ DEVICE pic12F675, PWRT_ON
    		' Power-On Timer
    	@ DEVICE pic12F675, MCLR_OFF
    		' Master Clear Options (Internal)
    	@ DEVICE pic12F675, BOD_ON
    		' Brown-Out Detect
    	@ DEVICE pic12F675, CPD_OFF
    		' Data Memory Code Protect
    	@ DEVICE pic12F675, PROTECT_OFF
    		' Program Code Protection
    
    	'
    	'	Hardware Defines
    	'	----------------
    	LED var GPIO.5
    	GP3Pin var GPIO.3
    
    	'
    	'	Software Defines
    	'	----------------
    	B1 var Byte
    
    	'
    	'	Initialise PIC
    	'	--------------
    	CMCON=%00000111
    	ANSEL=%00000000
    	TRISIO=%11111111
    
    	'
    	'	Main Program Loop
    	'	-----------------
    Loop:
    	ADCIN 0,b1
    	If B1>127 then goto LEDOn
    	ADCIN 1,b1
    	If B1>127 then goto LEDOn
    	ADCIN 2,b1
    	If B1>127 then goto LEDOn
    	ADCIN 3,b1
    	If B1>127 then goto LEDOn
    	If GP3Pin=1 then goto LEDOn
    	High LED
    	Goto Loop
    LEDOn:
    	Low LED
    	Goto Loop
    
    	end
    I just grounded the inputs that I wasn't playing with and used a 10K pot betweedn Vdd and 0v with the wiper to the PIC to play with the pin of my choice. My LED was connected between Vdd and the PIC (via 330R Resistor), so a LOW turned it ON and a HIGH turned it OFF.

    Melanie

  8. #8


    Did you find this post helpful? Yes | No

    Default

    Thank you Melanie. I must be missing something. Your code is almost the same as my test code & I couldn't get AN3 to work. In fact it acted strangely. It seemed to work only part of the pot's rotation. I figured PBP defaulted this port as Vref or something. I'll try again as soon as I clear my workbench. - Peter

  9. #9


    Did you find this post helpful? Yes | No

    Default

    Hi Melanie. I tried the 4th ADC port on the 'F675 and it worked! Thank you. I guess I missed something.
    Now, I guess what you are saying is that it is the "proper" thing to do to setup the ports before hand and define the inputs & outputs rather than change them in the program? I usually start with all inputs & convert them to outputs if and when I need them in the program. This could be the reason for some anomolies? Thank you.

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


    Did you find this post helpful? Yes | No

    Default

    there' nothing wrong in flipping between Input and Output on a pin mid-program... just keep track of what you're doing and ensure nothing clashes. I often have LED's and Buttons on the same pin for example.

Similar Threads

  1. Weird code problem
    By Navaidstech in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 16th March 2009, 11:16
  2. Weird Problem
    By isaac in forum General
    Replies: 9
    Last Post: - 22nd September 2008, 19:30
  3. PIC18F got some weird problem?
    By NatureTech in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 19th February 2007, 16:18
  4. Weird Oscillator Problem
    By eoasap in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 17th December 2005, 14:21
  5. Weird problem with 12F629
    By martarse in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 28th May 2005, 07:15

Members who have read this thread : 1

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