Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Plz help building a voltmeter on PIC12F683

Status
Not open for further replies.

MrSpock

New Member
I'm beginning discovering a PIC world. Trying to build a voltmeter indicator, so it will turn LED on if input voltage drops lower than 3 Volts, and turn it back off it the voltage goes back. It sounds awefully simple, but I'm stuck for couple of day already. I'm using PIC12F683, in MicroCode Studio with PicBasicPro 2.47, and PICKit 2. I'd really appreciate if somebody could give me a code example how to make it work. Thanks in advance.
 
MrSpock said:
I'm beginning discovering a PIC world. Trying to build a voltmeter indicator, so it will turn LED on if input voltage drops lower than 3 Volts, and turn it back off it the voltage goes back. It sounds awefully simple, but I'm stuck for couple of day already. I'm using PIC12F683, in MicroCode Studio with PicBasicPro 2.47, and PICKit 2. I'd really appreciate if somebody could give me a code example how to make it work. Thanks in advance.

Here where I'm so far [see code below]. Conversion seems like working, but I can't make both LEDs go HIGH simultaniously! In other words when Vin somewhere between 86 and 170 both LEDs go into glow or blink. Is there a trick to make both of them go ON at the same time? Or am I doing something wrong?

PIC12F683 connections:
Pin 3 - Input 0-5V
Pin 4 - +5V
Pin 6, 7 - LEDs

Code:

Code:
GrnLED  VAR    GPIO.0   ' Define green pulse-width variable
RedLED  VAR    GPIO.1   ' Define red pulse-width variable
Vin     VAR    BYTE     ' Allocate A/D variable

'A/D Parameters
DEFINE OSC 8
DEFINE ADC_BITS 8		' Use 10-bit A/D as 8-bit A/D

ANSEL.3 = 1				' Set ANS3 as analog input pin
ANSEL.4 = 0				' Set A/D clock
ANSEL.5 = 1
ANSEL.6 = 0
        
ADCON0.0 = 1            ' Turn On A/D
ADCON0.2 = 1			' A/D channel 3
ADCON0.3 = 1            ' ?
ADCON0.6 = 0			' VDD is voltage reference
ADCON0.7 = 0            ' Left Justify result

'GPIO port pins 0, 1 as outputs
TRISIO.0 = 0
TRISIO.1 = 0

PauseUs 100  

start:

    ADCON0.1 = 1        ' Start conversion
    while ADCON0.1 = 1  ' Wait for conversion           
        PauseUs 100   
    wend                    
    Vin = ADRESH
    if Vin < 85 then
        HIGH GrnLED
        'LOW RedLED
    endif
    if Vin > 86 AND Vin < 170 then
        HIGH GrnLED   
        HIGH RedLED
    endif
    if Vin > 171 then
        HIGH RedLED
        'LOW GrnLED    
    endif   
    PauseUs 250
        
goto start

Here is my code:
 
Here are 3 things to check.

Are you sure you have enough power for the circuit? Check this first.
Make sure MCLR is handled properly.
Are interrupts turned off?
 
Looking at your code, you should have both LEDs turned on. If you haven't then I would suspect you are overloading the pic pins and causing a RMW problem. Try placing higher value resistors in series with your LEDs, something like 1K. Alternatively, place a PauseUs(10) after each write to an LED.

Mike.
 
Thank you for response, mramos1.
mramos1 said:
Are you sure you have enough power for the circuit? Check this first.
Yes, more than enough. Tried it in Proteus - same result.
mramos1 said:
Make sure MCLR is handled properly.
Tried connecticting MCLR pin through 10K ressistor, no luck.
mramos1 said:
Are interrupts turned off?
I put DISABLE directive, it should turn interrups off, right? Anyway, still blinking.
 
Pommie said:
Looking at your code, you should have both LEDs turned on. If you haven't then I would suspect you are overloading the pic pins and causing a RMW problem. Try placing higher value resistors in series with your LEDs, something like 1K. Alternatively, place a PauseUs(10) after each write to an LED.

Mike.

Tried both, no luck. I'm using Proteus before testing it on a breadboard, so it shouldn't be a problem with power.
 
How fast do they blink?

Maybe make the PauseuS 250 to say 2000 and see if the blink rate changes.

Also, can you single step? Halt (breakpoint) at the end of the routine and see if the LEDS stay solid.

EDIT: Not sure what Proteus is. Is it a simulator? Or start PIC kit board?
 
Simplified the program to the following, still blinking! Something really wrong with configuration here. Any ideas?

Code:
GrnLED  VAR    GPIO.0   ' Define green pulse-width variable
RedLED  VAR    GPIO.1   ' Define red pulse-width variable

'A/D Parameters
DEFINE OSC 8
DEFINE ADC_BITS 8		' Use 10-bit A/D as 8-bit A/D

ANSEL.3 = 1				' Set ANS3 as analog input pin
ANSEL.4 = 0				' Set A/D clock
ANSEL.5 = 1
ANSEL.6 = 0
        
ADCON0.0 = 1            ' Turn On A/D
ADCON0.2 = 1			' A/D channel 3
ADCON0.3 = 1            ' ?
ADCON0.6 = 0			' VDD is voltage reference
ADCON0.7 = 0            ' Left Justify result

'GPIO port pins 0, 1 as outputs
TRISIO.0 = 0
TRISIO.1 = 0

DISABLE
PauseUs 100  

start:        
    
        HIGH GrnLED
        PauseUs(10)
        HIGH RedLED
        PauseUs(500)
        
goto start
 
mramos1 said:
How fast do they blink?

Maybe make the PauseuS 250 to say 2000 and see if the blink rate changes.

Also, can you single step? Halt (breakpoint) at the end of the routine and see if the LEDS stay solid.

EDIT: Not sure what Proteus is. Is it a simulator? Or start PIC kit board?

They blink about 10 - 50 Hz. The problem is that they can't be ON both. As soon as I put HIGH GPIO.1, GPIO.1 goes LOW. Even with Pauses. Not sure how to use breakpoints. Will research it.

Yes, ISIS Proteus is a simulator.
 
Last edited:
Is the Watch Dog Timer turned off? If yes then it's something to do with your simulator.

Mike.
 
The 12F683 has a comparator module and CVRef, this should be a very simple program.
Comparator mode 3 looks promising, and CVRef can produce very close to 3V
 
Got resistors on your leds to limit current?

When you get that working, in your first program remember that values 85,86,170 & 171 will do nothing.
You want to test your ranges more like:
if <85 ...
if >=85 AND <170 ...
if >=170 ...
 
Pommie said:
Is the Watch Dog Timer turned off? If yes then it's something to do with your simulator.

Mike.

I tried to use @ DEVICE PIC12F683, WDT_ON and @ DEVICE PIC12F683, WDT_OFF
Same effect. Not sure it's a correct way of configuring Watch Dog. I tried to program the chip and put on breadboard - it acts exactly like on simulator. Only one LED goes ON.
 
Last edited:
matc said:
Got resistors on your leds to limit current?
Yes. two 300 Ohm resistors between LED cathods and the ground.

matc said:
When you get that working, in your first program remember that values 85,86,170 & 171 will do nothing.
You want to test your ranges more like:
if <85 ...
if >=85 AND <170 ...
if >=170 ...

The numbers here are for the reference, I'm using a voltage divider potentiometer attached to the input. It changes the input voltage from 0V to 5V. Ideally PIC should turn one LED on at the beginning, both in the middle, and another one at the end of the potentiometer run.
 
blueroomelectronics said:
The 12F683 has a comparator module and CVRef, this should be a very simple program.
Comparator mode 3 looks promising, and CVRef can produce very close to 3V

That is exactly my point! It should be very simple :confused:
I believe I'm missing something in configuration.
 
You want WDT_OFF or that can be another problem.

Can you post the circuit? That will help rather than all guessing.

EDIT: Download screen hunter (windows) or a program you can take a screen shot and post it. And you do have current limiting resistors on the LEDs like asked?
 
Here is the circuit. And yes, I'm using 300 Ohm resistors on LED.
 

Attachments

  • Schematic.JPG
    Schematic.JPG
    55.3 KB · Views: 238
Did you try 1K resistors in place of the 300ohms? And a 1K on /MCLR? If that does not fix it, I would guess it in the software.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top