PDA

View Full Version : Idiot, trying to be a programmer!



Roddy Wayne
- 28th August 2009, 22:13
'************************************************* ***************
'* 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=%10000011 ;Sets the Analog to Digital control register to 4 Channels
;with Vref+ and Ground
;ADCON1=%10000010 ;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=%10000010 '?? Setup to read analog inputs
ADCIN 0, A2D_V 'Read channel 0 to A2D_Value
Pause 50
;DEBUG 14, 1, BIN8 A2D_V *
;Pause 1000 *
potpos= 1+(((255-1)*A2D_V)/255) ;*
Debug 14, 1, "Pot Position =", 32, DEC3 potpos, 13 ;*
Pause 50 ;*

************************************************** **********************************************

I wrote the above code a long time ago and then, lost my hard drive
without having it backed up! I'm not sure if the code above ever ran
properly but, it is the only code that was saved.

The problem I'm having with it is that "potpos" should be a Byte wide
but I'm only getting changes in the 0 & 1 bits of the Byte while varying
the input between 0 and + 5 VDC.

Can anyone help this "Idiot, trying to be a programmer"?

mackrackit
- 29th August 2009, 03:16
Which chip are you using?

I do not see the need for the math
potpos= 1+(((255-1)*A2D_V)/255)

The same is
potpos = A2D_V

???

Roddy Wayne
- 29th August 2009, 14:25
Thanks for replying!

The PIC is a 16F877

Acetronics2
- 29th August 2009, 14:49
IF A2DV = 0 THEN Potpos = 1

a small scale translating attempt to avoid value "0" ... probably !


... I know ... he could have written

POTPOS = A2DV MAX 1 ...

Alain

mackrackit
- 29th August 2009, 15:00
Just a guess...
This makes the ADC right justified


ADCON1=%10000010 '?? Setup to read analog inputs

This will make it left justified. Left justified is "normal"for 8 bit.


ADCON1=%00000010

Roddy Wayne
- 29th August 2009, 23:22
Thanks Guys!

I've made it work. I changed ADCON1=%10000010 to ADCON1=%00000010 and this fixed the problem. So, your guess was exactly correct!

mackrackit
- 30th August 2009, 01:31
Well, it was an educated guess....
That is why I asked about the chip being used so I could look at the correct data sheet :)
Have FUN!!!