Hey guys, will this work?


Closed Thread
Results 1 to 27 of 27

Hybrid View

  1. #1
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    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.

  2. #2
    Join Date
    Feb 2004
    Location
    Michigan, USA
    Posts
    305


    Did you find this post helpful? Yes | No

    Default

    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

  3. #3
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    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.

  4. #4
    Join Date
    Feb 2004
    Location
    Michigan, USA
    Posts
    305


    Did you find this post helpful? Yes | No

    Default

    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

  5. #5
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    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.

  6. #6
    Join Date
    Feb 2004
    Location
    Michigan, USA
    Posts
    305


    Did you find this post helpful? Yes | No

    Default

    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?

  7. #7
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by jmgelba View Post
    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?
    assuming a 10 bits results as follow
    Code:
    'Bit number 9876543210
       MyVar = %1111110011
    In red the MSB, in Blue the LSBs for PWM.

    Shift them all two position to the right you have
    Code:
       MyVar = %0011111100
    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 register
    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.

Similar Threads

  1. PortA Doesn't Work
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 11
    Last Post: - 8th September 2015, 18:41
  2. pls help me verify my code not work, why???
    By chai98a in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 18th January 2010, 09:19
  3. Can't get POT work on P12f675 - Newbie
    By berslan in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 26th March 2008, 21:22
  4. How to set ICD-2 clone to work with PBP?
    By G-R-C in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 20th October 2006, 02:50
  5. Pin RA4 doesn't work
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 0
    Last Post: - 15th July 2004, 12:03

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts