View Full Version : Newbie HPWM / ADC question
Johan
- 23rd June 2007, 08:37
Hello,
I am new to PICBASIC and I want to read from a pot and output to HPWM with PIC 16 F 88
OSCCON = $60 ' Internal oscillator frequency 4 Mhz
TRISA = %11111111 ' Set PORTA to all input
TRISB = %00000000 ' Set PORTB to all output
ANSEL = %01111011 ' Analog channel 0,1,3-7 Digital channel 2,8
ADCON1= %10000000 ' VDD & VSS as VREF
DEFINE ADC_BITS 10 ' ADC resolution 10 bit
DEFINE ADC_CLOCK 3 ' Set clock source (rc = 3)
DEFINE ADC_SAMPLEUS 50 ' Sampling time 50ms
define OSC 4 ' Clock 4 Mhz
analogport VAR PORTA.0 ' Analog port pin RA.0
analogvalue VAR word ' Value digital 0 - 1024
pwmduty VAR word
main:
ADCIN analogport,analogvalue
pwmduty = analogvalue / 2
hpwm 1,pwmduty,7800
goto main
is the code correct, since I am not satisfied with the pwm output: the pwm turn off at 2.5v and portb.0 & portb.7 always on
btw fuse is set on mikroe programmer
Thanks
Acetronics2
- 23rd June 2007, 11:33
Hi,
First ...
your ADC gives 1024 steps ...
HPWM has 256 steps
your PWMDuty has 512 possible values ...
sooooo .... pwmduty = analogvalue / 4 ...
OR .... DEFINE ADC_BITS 8 ' ADC resolution 8 bit
which is ... much better !!! ( try and see why ...)
Now, for PORTB.0 and B.7 ...
did you define their initial state somewhere ????
nooooo, you didn't !!! ... and you should had !!!
Have a Good day
Alain
Johan
- 24th June 2007, 16:43
Yes it works now, thank you Alain
Changed the ADC resolution to match with HPWM and
I forgot to put PORTB = 0 :) to clear the PORTB
As for the ADC & HPWM , why is 8 bit better than 10 bit ?
Johan
Acetronics2
- 24th June 2007, 18:01
Hi, Johan
For what you do at this time ... no great difference !!!
Note HPWM Always use 8 bits...
but why use 10 bits resolution to divide the result afterwards ... ( note here it's a division by 4 or two times right shifting !!! ).
Here, with 8 bits, ADC result is faster given, no need to add complementary formatting and no need to use an intermediate variable ( "pwmduty" can be replaced by "analogvalue" )
add to this your variables can be only BYTES ... so you spare lots of room !!!
Note also ADCON1.7 must be set to LEFT Justification ( "0" for BYTE result )
One habit I have is NOT to change HPWM if pwmduty doesn't change ... that saves also time for other stuff ... and response to pot change is faster.
Also always give initial values to variables ( CLEAR, if all = 0 )
so, gives at last ...
DEFINE OSC 4 ' Clock 4 Mhz
DEFINE ADC_BITS 8 ' ADC resolution 10 bit
DEFINE ADC_CLOCK 3 ' Set clock source (rc = 3)
DEFINE ADC_SAMPLEUS 50 ' Sampling time 50ms
OSCCON = $60 ' Internal oscillator frequency 4 Mhz
ANSEL = %01111011 ' Analog channel 0,1,3-7 Digital channel 2,8
ADCON1 = %00000000 ' VDD & VSS as VREF
PORTA = %00000000
PORTB = %00000000
TRISA = %11111111 ' Set PORTA to all input
TRISB = %00000000 ' Set PORTB to all output
analogport VAR PORTA.0 ' Analog port pin RA.0
analogvalue VAR BYTE ' Value digital 0 - 255
oldanalogvalue VAR BYTE
CLEAR
main:
ADCIN analogport,analogvalue
IF analogvalue = oldanalogvalue THEN Jump
hpwm 1,analogvalue,7800
oldanalogvalue = analogvalue
Jump:
goto main
Hope I didn't forget anything ... LOL
Alain
Johan
- 25th June 2007, 08:47
Hi Alain,
I got another problem :
If I try to read from PORTA.1 and change the code accordingly ( analogport var porta.1 )
HPWM does not function, it just send output at 50% regardless the position of the pot :(
Anything I set wrong?
Acetronics2
- 25th June 2007, 09:25
Hi, Johan
When happening ... the first reflex is to open the datasheet and look to the functions connected to the "faulty" pins ...
have a look to pict $ 5.1 ...
Here we also find an ANALOG Comparator ...
so, let's disable it !!!
CMCON = 7 ( from the table $13.1 )
By the way, we don't need the voltage ref nor ...
CVRCON = 0
So, I did forget some littles things ??? ...
Alain
Johan
- 25th June 2007, 10:35
Hi Alain,
On table 13.1 : The value on POR, register CMCON is 7 & CVRCON is 0, do we really need to change ?
I did put CMCON = 7 and CVRCON =0 , but there was no difference
The strange thing: If I put 2 pots ( one on RA0, one on RA1), the PIC read value on PORTA.0 only ( How come ??? I did not even put ADC command on RA0 )
Johan
Acetronics2
- 25th June 2007, 12:36
Hi,Johan,
You're right for CMCON ... there's some overhead here, but Melabs recommends to set it in the program beginning ...
and we all trust in Melabs !!!
WOW ... Got it for non-working pins !!!
VAR Statements do not work with ADCIN ( I just wrote a post about some days ago ... shame on me !!! )
http://www.picbasic.co.uk/forum/showthread.php?t=6522
So, Instead of :
"analogport VAR PORTA.0
WRITE in the Ports definitions :
"analogport0 CON 0 ' Analog port pin RA.0"
"analogport1 CON 1 ' Analog port pin RA.1"
and so on ...
but take care that AN5 is different from ... RA.5 !!!
Here we are !!!
Alain
Johan
- 25th June 2007, 12:52
Thank you Alain,
I spent all day trying to figure out how to solve this problem.
Once again, thank you, merci .... it was a great help
Johan
Powered by vBulletin® Version 4.1.7 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.