In theory it should work. Theory is always theory
PicMultiCalc download
http://www.picbasic.co.uk/forum/atta...7&d=1225550328
Probably you can get some inspiration of the following
http://www.picbasic.co.uk/forum/show...0&postcount=10
In theory it should work. Theory is always theory
PicMultiCalc download
http://www.picbasic.co.uk/forum/atta...7&d=1225550328
Probably you can get some inspiration of the following
http://www.picbasic.co.uk/forum/show...0&postcount=10
Last edited by mister_e; - 11th May 2009 at 20:42.
Steve
It's not a bug, it's a random feature.
There's no problem, only learning opportunities.
Ok, so using the calc I get PR2 = 9, duty register = 20 and with a 1:1 prescaler.
Is this about right?:
Code:OSCCON = $70 define osc 8 CSET VAR WORD 'CURRENT SET CSEN VAR WORD 'CURRENT SENCE DUTY VAR wORD 'HPWM DUTY CYCLE ADCON0 = %00011001 ADCON1 = %00001111 ADCON2 = %10111111 TRISA = %00000000 TRISB = %00110011 Define ADC_BITS 10 ' Set number of bits in result Define ADC_CLOCK 3 ' Set clock source (3=rc) Define ADC_SAMPLEUS 10 ' Set sampling time in uS 'DUTY = 30 'SET INITIAL OUTPUT CURRENT HIGH TO ENABLE FAST STARTUP CSET = 0 CSEN = 0 duty = 20 ' duty value for 50% duty cycle PR2 = 9 ' T2CON = %00000100 ' timer2 on, prescale 1:1 CCPR1L = duty>>2 ' MSB of duty cycle value CCP1CON=%00001100 | (dUTY<<5) ' set PWM mode and store the ' 2 LSB of duty LOOP: ADCIN 6, CSET 'READ VALUE OF CURRENT SET POT ADCIN 4, CSEN 'READ VALUE OF CURRENT SENCE IF CSEN > CSET THEN LET DUTY = DUTY - 1 IF CSEN < CSET THEN LET DUTY = DUTY + 1 CCPR1L = duty>>2 ' MSB of duty cycle value CCP1CON=%00001100 | (dUTY<<5) ' set PWM mode and store the ' 2 LSB of duty 'HPWM 1,DUTY,32767 'OUTPUTS THE DUTYCYCLE AT 32.767KhZ GOTO LOOP
Almost... but no cigar. Check CCP1CON for YOUR particular PIC. CCP1CON is a bit different on this one (and I think my example was false anyway ... dooh) as usual, MicroChip can't be consistent between all their PIC model. Good idea to release a datasheet for each and every different model though
Code:CCP1CON=%00001100 | (Duty<<4)
Last edited by mister_e; - 11th May 2009 at 21:38.
Steve
It's not a bug, it's a random feature.
There's no problem, only learning opportunities.
I did, it looks the same to me. I must have missed something.
bit 7 and 6 are 00 = Single output; P1A modulated; P1B, P1C, P1D assigned as port pins
bit 5 and 4 are the two LSbs of the PWM duty cycle. The eight MSbs are found in CCPR1L. I guess I do not know what to put in here or how to find out what should go in here. I thought duty>>2 took care of this.
and 3 - 0 are 1100 = PWM mode; P1A, P1C active-high; P1B, P1D active-high
The change is above... <<4 instead of <<5... but as i said... <<5 was wrong anyway... not sure why, but wrong anyways... few years after... no one complaint
It is more code efficient... and obviously safer to use
CCPR1L = duty>>2
CCP1CON.4=Duty.0
CCP1CON.5=Duty.1
unless you'll screw up the CCP module on that 18F (think about the bit <7:6> of CCP1CON when duty value change). This was my major point here.
Last edited by mister_e; - 11th May 2009 at 22:01.
Steve
It's not a bug, it's a random feature.
There's no problem, only learning opportunities.
Ok, I think I understand all except how did you fill in bits 4 and 5? Why a 0 and 1 in the location you put them in?
The lsb's of the duty cycle go in there. Its a 10 bit duty cycle. The top 8 are shifted away leaving to 2 lsb's. In decimal the duty cycle is 20. Is the binary representaion of 20 used? Is that how you get the last 2 bits?
20 = 00010100 so the lsb's should be 00, no?
assuming a 10 bits results as follow
In red the MSB, in Blue the LSBs for PWM.Code:'Bit number 9876543210 MyVar = %1111110011
Shift them all two position to the right you have
Now, where are the LSB now... gone... you need to extract them 2 from the original 10bit value, so Bit0 and BIT1, and transfer them to Bit4 and BIT5 of CCP1CON registerCode:MyVar = %0011111100
CCP1CON.4=MyVar.0 ' bit0 to bit4 of CCP1CON
CCP1CON.5=Myvar.1
Steve
It's not a bug, it's a random feature.
There's no problem, only learning opportunities.
Bookmarks