Yes it works now, thank you Alain
Changed the ADC resolution to match with HPWM and
I forgot to put PORTB = 0to clear the PORTB
As for the ADC & HPWM , why is 8 bit better than 10 bit ?
Johan
Yes it works now, thank you Alain
Changed the ADC resolution to match with HPWM and
I forgot to put PORTB = 0to clear the PORTB
As for the ADC & HPWM , why is 8 bit better than 10 bit ?
Johan
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
************************************************** ***********************
Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
************************************************** ***********************
IF there is the word "Problem" in your question ...
certainly the answer is " RTFM " or " RTFDataSheet " !!!
*****************************************
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?
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
************************************************** ***********************
Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
************************************************** ***********************
IF there is the word "Problem" in your question ...
certainly the answer is " RTFM " or " RTFDataSheet " !!!
*****************************************
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
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
************************************************** ***********************
Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
************************************************** ***********************
IF there is the word "Problem" in your question ...
certainly the answer is " RTFM " or " RTFDataSheet " !!!
*****************************************
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
Bookmarks