PDA

View Full Version : New and I'm having problems!



Roddy Wayne
- 11th April 2009, 19:58
'************************************************* ***************
'* 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 Hydrogen / Oxygen Generators *
'* with serial output to a Serial LCD Display *
'* (Using an old circuit to do new things!) *
'************************************************* ***************


CLEAR ;always start with clear
DEFINE OSC 4 ;define oscillator speed
DEFINE ADC_BITS 8 ; Sets number of bits in result (Tried 10 but this screwed up
' the LCD display)
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 = %00000111 ;sets porta 0, 1, & 2, as an inputs (A to D'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 output
potpos var BYTE ;this had been a VAR WORD
cellamps VAR word
A2D_V VAR BYTE ; Create A2D_V Value to store result
A2D_V2 Var word ;Create A2D_V2 Value to store result
ADCON1=%00000111 ;Sets the Analog to Digital control register
'************************************************* **************
' I'm fairly new to PIC programming and I DEFINATELY NEED HELP PLEASE!
'
'Above, I have 2 Var Bytes (potpos and A2D_V) and 2 Var Words (cellamps
'and A2D_V2). I would like to display "potpos" as a Decimal number ranging from
'1 to 255 on a serial LCD. I would also like to display the cellamps on this
'serial LCD display. The problem I'm having with this is, the cellamps input
'only ranges from zero to 60 millivolts, with 1 millivolt representing 1 Amp of
'current. The shunt I'm using is a 50 MV / 50 Amp shunt. I've tried changing
'the "DEFINE ADC_BITS" to 10, but this screws up the LCD display's reading on
'both the "potpos" and "cellamps".
'
'
' ANY HELP WILL BE GREATLY APPRECIATED!!!


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!
ADCIN 0, A2D_V 'Read channel 0 to A2D_Value
potpos= (((255-1)*A2D_V)/255)
'potpos= 1+(((255-1)*A2D_V)/255)
PAUSE 100
Debug 14, 1, "Pot Position = ", DEC2 potpos, 13
ADCIN 1, A2D_V2 'Read channel 1 to A2D_V2 Value
cellamps= (((255-1)*A2D_V2)/255)
pAUSE 100
Debug "Cell Amps = ", dec2 cellamps, 13


HPWM 2, 127, 17500 'Output is a 17.5 Khz, 50% duty cycle (pin 16)
ADCIN 0, A2D_V ' Read channel 0 to A2D_Value
potpos= (((255-1)*a2d_v)/255)
If (potpos <=%00011100)and(potpos <=%00111111) then percent_40

'Serout on pin 25 of 16F877)
'Pause 200 '
'Debug 1, 14, 1 'Send curser home (Top left of screen)
'if potpos <= 28 and <= 63, then goto 10%
ADCIN 0, A2D_V ' Read channel 0 to A2D_Value
potpos= (((255-1)*a2d_v)/255)
If (potpos <=%01000000)and(potpos <=%01011001) then percent_60

'Pause 200 '
'Debug 1, 1 'Send curser home (Top left of screen)
'if potpos <= 64 and <= 89, then goto 20%
ADCIN 0, A2D_V ' Read channel 0 to A2D_Value
potpos= (((255-1)*a2d_v)/255)
If (potpos <=%01011010)and(potpos <=%01110011) then percent_80

'Pause 200 '
'Debug 1, 1 'Send curser home (Top left of screen)
'if potpos <= 90 and <= 115, then goto 30%
ADCIN 0, A2D_V ' Read channel 0 to A2D_Value
potpos= (((255-1)*a2d_v)/255)
If (potpos <=%01110100)and(potpos <=%01111110) then percent_100

'Pause 200 '
'Debug 1, 1 'Send curser home (Top left of screen)
'if potpos <= 116 and <= 126 4, then goto 40%
If (potpos >=%01011110) Then percent_100



percent_40: 'Throttle is depressed 40%
HPWM 1, 102, 17500
HPWM 2, 127, 17500
Pause 100
DEBUG "PWM = 40%High/60%Low", 32, 32, 1
Pause 400
GoTO start

'percent_50: 'Throttle is depressed <=50%
'HPWM 1, 127, 17500 'Output is a 17.5 Khz, 50% duty cycle (pin 17)
'HPWM 2, 127, 17500 'Output is a 17.5 Khz, 50% duty cycle (pin 16)
'Pause 10
'GoTO start '

percent_60: 'Throttle is depressed 60%
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_70: 'Throttle is depressed 70%
'HPWM 1, 178, 17500 'Output is a 17.5 Khz, 70% duty cycle (pin 17)
'HPWM 2, 127, 17500 'Output is a 17.5 Khz, 50% duty cycle (pin 16)
'Pause 10
'GoTO start

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

'percent_90: 'Throttle is depressed 90%
'HPWM 1, 230, 17500 'Output is a 17.5 Khz, 90% duty cycle (pin 17)
'HPWM 2, 127, 17500 'Output is a 17.5 Khz, 50% duty cycle (pin 16)
'Pause 10
'GoTO start

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



End

Archangel
- 11th April 2009, 20:53
Hi Roddy,
Welcome to the forum !
A couple of things . . . #1. please tell us which PIC you are using. Please use code tags so as to separate your text from your code. To do so put the word CODE between these brackets [ ] at the beginning of the code and put /code inside [] brackets after.Like so
DEFINES must be all UPPERCASE. You stated, "I've tried changing the "DEFINE ADC_BITS" to 10, but this screws up the LCD display's reading on both the "potpos" and "cellamps". " I do not have a lot of experience with ADC but I think it is dependent upon which PIC you are using. Your project looks interesting. You are throttling an H/O generator ? Could you elaborate on the issue surrounding the shunt so I/We can better understand it ?

Roddy Wayne
- 12th April 2009, 02:11
I am using a PIC16F877. I'm not sure about re-posting my code. Does this forum allow this? I'm not sure why a couple of my DEFINE's appeared the way they did but, I've checked my code and they appear correctly in it. If I can re-post my code, I will use the
, examples you've given.

Yes, I am controlling a PWM driven HHO Generator with this code and it seems to work very well. I decided to try to display my cell's current draw so, bought this current shunt to put in series between frame ground and my cell. I've read the millivoltage coming out of this shunt with my Fluke Meter and it does in fact put out 23 millivolts with 23 Amps being drawn by the cell.

I am also using a new version of the "PicBasic Pro" Compiler. Please let me know about re-posting and if this is acceptable.

Thanks very much for your help!
Roddy

Archangel
- 12th April 2009, 06:07
Please let me know about re-posting and if this is acceptable.

I think it is ok either way, it looks like The Admins moved this post from the FAQ area and may have deleted the duplicate.

Archangel
- 12th April 2009, 06:38
Damn Edit function is inoperative ! OK I'm 3 beers into a 12 pack right now, UM Firewater's got my N/A blood flowing now. From your code . . .
<b><I>ADCON1=%00000111 ;Sets the Analog to Digital control register</I></b> seems to me ADCON1 = 7 makes all ports digital.