PDA

View Full Version : Pretty New and need help please



Roddy Wayne
- 30th August 2009, 18:42
Hi Everyone,

I'm using a PIC16F877 chip in this project.

I'm still pretty new at PIC programming and there's alot I don't know or understand!

About 34 or 35 lines below, you will see *** ;ADCON1=%00000011 and ;ADCON1=%00000010 ***
The reason these lines are marked as notes (;) is because I can not get the results I expected!

I'm trying to use A2D Channels 0 and 1. Channel 0's input ranges from 0 to + 5 VDC.
Channel 1's input ranges from 0 to + 1 VDC.

I'm trying to use a very small input from a current shunt (in Millivolts)
100 Millivolts Max, and display this current on the LCD. With the Vref set
at + 1 VDC, I think I should be able to see current changes of 4 Amps.

Is there a minumum Vref voltage? I've looked over the Datasheet but
have not found anything about Vref minimums. Ideally, I would set
Vref at .255 VDC.

**** Is what I'm trying to do even possible?****

****Any help will really be appreciated!****



'************************************************* ***************
'* Name : New_PWM.bas *
'* Author : Roddy Wayne *
'* Notice : Copyright (c) 2008 *
'* : All Rights Reserved *
'* Date : 08/09/2008 *
'* Version : 1.0 *
'* Notes: *
'* *
'* A PWM for running a Hydrogen Generator *
'************************************************* ***************


CLEAR ;always start with clear
DEFINE OSC 4 ;define oscillator speed
DEFINE ADC_BITS 8 ; Sets number of bits in result
DEFINE ADC_CLOCK 3 ; Sets clock source (3=rc)
DEFINE ADC_SAMPLEUS 50 ; Sets sampling time in uS
define DEBUG_REG PORTC
DEFINE DEBUG_BIT 6
define DEBUG_BAUD 2400
DEFINE DEBUG_MODE 0
DEFINE DEBUG_PACING 1000
Low PORTE.2 ;makes low for write only
TRISA = %00001111 ;sets 0, 1, 2, and 3 as porta inputs (A to D's & ref's)
TRISB = %00111111 ;sets bits 6 & 7 portb pins as outputs, 0 thru 5 as inputs
TRISC = %10111111 ;sets port c.6 for serial output
TRISD = %00000011 ;sets PORTD 0,1, lines to inputs, rest to outputs
TRISE = %00000000 ;sets all PORTE lines to outputs
potpos var BYTE ;potpos is 8 bit
cellamps VAR BYTE ;cellamps is 8 bit
A2D_V VAR BYTE ;Created A2D_V Value to store result
A2D_V2 Var BYTE ;Created A2D_V2 Value to store result
;ADCON1=%00000011 ;Sets the Analog to Digital control register to 4 Channels
;with Vref+ and Ground as the references!
;This Vref is + 1 VDC
;ADCON1=%00000010 ;Sets the Analog to Digital control register to 5 Channels
;with +5 VDC and Ground as the references!


Start:


If portc.0 = 0 then portb.6 = 0 'If pin 15 is low, make pin 39 low
If Portc.0 = 1 then portb.6 = 1 'If pin 15 is high, make pin 39 high
'When Pin 39 goes high, this turns the
'pump on! I'm using a Darlington pair
'to do this (Part # TIP120) It also turns
'on the "Pump On" LED.
If portc.3 = 0 then portb.7 = 0 'If pin 18 is low, make pin 40 low
If portc.3 = 1 then portb.7 = 1 'If pin 18 is high, make pin 40 high
'When Pin 40 goes high, the "Low Tank
'Water" LED is turned on!

ADCON1=%00000010 '?? Setup to read analog inputs
ADCIN 0, A2D_V 'Read channel 0 to A2D_Value
Pause 50
potpos= A2D_V
Debug 14, 1, "Pot Position =", 32, DEC2 potpos, 13
Pause 50
;ADCON1=%00000011
Pause 50
ADCIN 1, A2D_V2 'Read channel 1 to A2D_V2 Value
cellamps= A2D_V2
Debug "Cell Amps =", 32, DEC2 cellamps, 13
Pause 50
HPWM 2, 127, 17500 'Output is a 17.5 Khz, 50% duty cycle (pin 16)
;ADCON1=%0000010
Pause 100
ADCIN 0, A2D_V ' Read channel 0 to A2D_Value
potpos= A2D_V
If (potpos <=%00011100)and(potpos <=%00111111) then percent_40

ADCIN 0, A2D_V ' Read channel 0 to A2D_Value
potpos= A2D_V
If (potpos <=%01000000)and(potpos <=%01011001) then percent_60

ADCIN 0, A2D_V ' Read channel 0 to A2D_Value
potpos= A2D_V
If (potpos <=%01011010)and(potpos <=%01110011) then percent_80

ADCIN 0, A2D_V ' Read channel 0 to A2D_Value
potpos= A2D_V
If (potpos <=%01110100)and(potpos <=%01111110) then percent_100

If (potpos >=%01011110) Then percent_100
'This assures that any potpos >= 11110110, will keep the output
'at 100%, where it used to go back to 50%!!!



percent_40: 'Asking for 40% On / 60% Off
HPWM 1, 102, 17500 'Output is a 17.5 Khz, 40% duty cycle (pin 17)
HPWM 2, 127, 17500 'Output is a 17.5 Khz, 50% duty cycle (pin 16)
Pause 100
DEBUG "PWM = 40%High/60%Low", 32, 32, 1
Pause 400
GoTO start


percent_60: 'Asking for 60% On / 40% Off
HPWM 1, 153, 17500 'Output is a 17.5 Khz, 60% duty cycle (pin 17)
HPWM 2, 127, 17500 'Output is a 17.5 Khz, 50% duty cycle (pin 16)
Pause 100
DEBUG "PWM = 60%High/40%Low", 32, 32, 1
Pause 400
GoTO start


percent_80: 'Asking for 80% On / 20% Off
HPWM 1, 204, 17500 'Output is a 17.5 Khz, 80% duty cycle (pin 17)
HPWM 2, 127, 17500 'Output is a 17.5 Khz, 50% duty cycle (pin 16)
Pause 100
DEBUG "PWM = 80%High/20%Low", 32, 32, 1
Pause 400
GoTO start


percent_100: 'Asking for 99% On / 1% Off
HPWM 1, 253, 17500 'Output is a 17.5 Khz, 100% duty cycle (pin 17)
HPWM 2, 127, 17500 'Output is a 17.5 Khz, 50% duty cycle (pin 16)
Pause 100
DEBUG "PWM = 99%High/01%Low", 32, 32, 1
Pause 400
GoTO start



End

mackrackit
- 30th August 2009, 18:56
It looks doable...

ADCON1 only needs setup in one place of your code. Normally near the beginning.
You may want to read the separate ADC channels into separate variables. The deal with them later.

Read this page, and everything else on Bruce's site for that matter, the VREF is talked about here.
http://www.rentron.com/PicBasic/LM34.htm

Roddy Wayne
- 30th August 2009, 19:42
Thanks for responding Mackracit!

Boy, I sure do not understand this!

I've looked at this link before

http://www.rentron.com/PicBasic/LM34.htm


and thought that ADCON1=%00000011 is used when using an external reference voltage and ground with up to 4 analog input channels.

In looking at the datasheet, it looks to me that ADCON1=%00000010 can be used for up to 5 input channels
using + 5 VDC and ground as the reference voltage.

So, I guess what I am asking you is, how can ADCON1=%00000011 be used to read both channels accurately?

It seems to me that I'd have to switch between ADCON1=%00000011 and ADCON1=%00000010 to be able to do this????

I think I have two separate variables to store the two A2D channels, do I not?

Now I am totally confused!

mackrackit
- 30th August 2009, 23:16
Confusion is good :)

I missed the second 2 on one of your variables.... ooppsss. You do have two separate.

ADCON1 is used to set the pins to be either digital or analog and if and external VREF is used. The data sheet gives the different options, sometimes the option you want is not an option...

So no switching after it is set. You have to decide on the pins ahead of time.