RS-232 to PWM - Page 2


+ Reply to Thread
Page 2 of 2 FirstFirst 12
Results 16 to 17 of 17

Thread: RS-232 to PWM2217 days old

  1. #16
    Kamikaze
    Guest Kamikaze's Avatar

    0 Not allowed!

    Default Re: RS-232 to PWM

    Quote Originally Posted by Les View Post
    That's why you need to read the datasheet for the device being used.

    Manipulating SFRs (Special Function Register) is fundamental when programming microcontrollers, in any language. Each peripheral is explained within the device's datasheet.

    What you're looking for, and what the HPWM command uses, is contained in the CCP section of the datasheet. In particular PWM.

    This topic has been covered many times on the forum, and I even created a set of routines that manipulate the CCP peripheral with more flexability than the HPWM command. However, they are for an 18F device, but they should give clues as to CCP use because both 14-bit devices and 18F devices have some commonality with peripherals.

    Flexible-Hpwm-routines-for-18F-devices
    Okay, so the instructions set in the Datasheet (p. 64) for setting up a PWM operation are the following:
    1). Set the PWM period by writing to the PR2 register.
    2). Set the PWM duty cycle by writing to the CCPR1L register and CCP1CON<5:4> bits.
    3). Make the CCP1 pin an output by clearing the TRISC<2> bit.
    4). Set the TMR2 prescale value and enable Timer2 by writing to T2CON.
    5). Configure the CCP1 module for PWM operation.

    http://www.futurebots.com/16f877.pdf

    Could I base my work in the information given in this page?
    http://www.piclist.com/techref/microchip/16F877/pwm.htm
    1. Set the PWM period (1000Hz = 0.001S) by writing to the PR2 register.
      PWM Period = [PR2 + 1]*4*Tosc*TMR2 PreScale valueUse 4 for TMR2 prescale. You can try other values but make sure it works with the duty cycle calcs as well.

      Tosc = 1/4000000 = 2.5*10-7Rearranging the equation gives...

      PR2 = (Period/(4 * Tosc * TMR2 Prescale)) - 1Therefore...

      PR2 = (0.001/(4 * 2.5 * 10-7 * 4)) - 1 = 249 bsf STATUS,RP0 movlw d'249' ; when TMR2 = 249 = end of Period movwf PR2 bcf STATUS,RP0
    2. Set the PWM Duty Cycle by writing to the CCPR1L register and CCP1CON<5:4> bits.CCPR1L contains the upper 8 bits of the 10 bit Duty Cycle value CCP1CON<5:4> contain the lower 2 bits

      PWM Duty Cycle = (CCPR1L:CCP1CON<5:4>)*Tosc*TMR2 Prescale ValueCCPR1L:CCP1Con<5:4> = PWM Duty Cycle / (Tosc * TMR2 Prescale)PWM Duty Cycle = 50% of PeriodPWM Duty Cycle = 50% of 0.001 = 0.0005CCPR1L:CCP1Con<5:4> = 0.0005 / (2.5 * 10-7 * 4)CCPR1L:CCP1Con<5:4> = 500500 in 10 bit binary = 0111110100CCPR1L = 01111101 CCP1Con<5:4> = 00Write the 10 bit value

      movlw b'01111101' ; set bits 9 - 2 movwf CCPR1L bcf CCP1CON,CCP1X ; set bit 1 bcf CCP1CON,CCP1Y ; set bit 0
    3. Make the CCP1 pin an output by clearing the TRISC<2> bit
      bsf STATUS,RP0 movlw b'11111011' andwf TRISC bcf STATUS,RP0
    4. Set The TMR2 prescale value and enable TMR2 by writing to T2CON.We decided previously to set the prescale value to 4.
      TMR2 prescale are bits 1 and 0 in T2CON
      TMR2 enable is bit 2

      movlw b'00000101' ; TMR2 = on, prescale = 1:4 movwf T2CON
    5. Configure the CCP1 module for PWM
      movf CCP1CON,W andlw b'00110000' ; mask all but previously set Duty Cycle bits iorlw b'00001111' ; and enable PWM mode movwf CCP1CON
    Thank you very much again!

  2. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  3. #17
    Prolific Poster pic-ignorant's Avatar
    Join Date
    Oct 2007
    Posts
    2,942
    Thumbs Up
    Received: 25
    Given: 31
    Total Downloaded
    926.61 MB

    0 Not allowed!

    Default Re: RS-232 to PWM

    This is cut from some code for 38Khz pwm with a 12F683, so might need tweaking for your device

    TRISIO.2 = 0 'Set as output. (Your Pic will probably need this changed to TRISC.2 = 0)
    PR2 = 100 'SET PERIOD VAL
    CCP1CON = %00001100 'PWM MODE ACTIVE LOW
    CCPR1L = 127 'INITIAL DUTY VALUE
    TMR2 = %00000100 'NO PRE/POSTSCALE TIMER ON

    This should get you PWM running on CCP1 if you check your pics registers, and change accordingly. To adjust duty cycle in 8 bit mode, vary CCPR1L from 1 to 254 in a loop. If you want to use 10 bit you will need to look in the datasheet

  4. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. [SOLVED !] PWM, dithered PWM and Tmr1 as a (variable bit) PWM source.
    By david in forum The Lounge
    Replies: 8
    Last Post: 2nd August 2013, 12:57
  2. RS-232 Drivers/Receivers
    By Richard in forum The Lounge
    Replies: 2
    Last Post: 14th May 2009, 17:36
  3. RS 232 comunication
    By joesaliba in forum Proton Plus Compiler v3
    Replies: 21
    Last Post: 7th February 2007, 20:55
  4. RS 232 protocol
    By mikedownunder in forum The Lounge
    Replies: 1
    Last Post: 21st January 2005, 14:16
  5. Rs 232 baud rate
    By jackjay2 in forum Wish List / Product Feedback
    Replies: 4
    Last Post: 19th September 2004, 10:14

Tags for this Thread

Posting Permissions

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