PWM on PORTG pin 3 on a 18F67k22


Closed Thread
Results 1 to 13 of 13
  1. #1
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838

    Default PWM on PORTG pin 3 on a 18F67k22

    Hi guys ,

    i am trying the get PWM (CCP4) output to work on PortG.3 of the 18F67k22 series

    what i am seeing so far is that the port seems to put out some pulses then stops , so it seems that i have part of the setup correct

    i am thinking that i am also effecting other ports with the setting as other activity on portF , and G seem to sometimes have an effect , but i cant seem to isolate exactly what it is so far

    the output pulses rates etc dont matter at this point just as long as it outputs all the time for a test of the PWM function on this pin only

    what am i missing


    cheers

    sheldon


    http://ww1.microchip.com/downloads/e...Doc/39960d.pdf


    Code:
    '    -------   Setup port G Variables  ----------     
       ' port G has 6 I/O only , G5 - Always input ( uses CONFIG3H - MCLRE = OFF for I/O use)  
        CM3CON = 0                 ' Disable comparator 3 on PORTG
        
        ANCON2.0 = 0               ' Set PortG:4 Analog / Digital allocation - 0 = digital ,1 analog - ANSEL16 
        ANCON2.1 = 0               ' Set PortG:3 Analog / Digital allocation - 0 = digital ,1 analog - ANSEL17
        ANCON2.2 = 0               ' Set PortG:2 Analog / Digital allocation - 0 = digital ,1 analog - ANSEL18
        ANCON2.3 = 0               ' Set PortG:1 Analog / Digital allocation - 0 = digital ,1 analog - ANSEL19
        
        TRISG = %00100000          ' ports Output , Row_1 output  portG.5 always input ( internal pullup) VPP/MCLR ,CCP4 output
        FLASH_CE     var PORTG.2   ' FLASH CE - active low - uses SPI bus 
        PWM_TEST VAR PORTG.3   ' CCP4 - PWM outout to control the contrast on glcd       
      
      
       PORTG  =  %00100000        ' Clear Outputs , 
       flash_ce = 1               ' set high as active low 
       PWM_TEST = 0          ' active high

    Code:
    '---------- PWM OUTPUT REGISTER SETUP --------------------- 
        ' setup CCP4 - PORTG.3 for PWM_CONTRAST control
        ' PWM FRQ = 1/PWM period
        ' When TMR2 = PR2 = start of duty cycle(on Time ) , start of period length
        ' when TMR2 = CCPR4L:CCP4CON5:4 = End of duty cycle , START OF OFF TIME  
        ' when TMR2 = PR2 = End of period , start of duty cycle AGAIN 
       
       
        CCPTMRS1 = %00000000   ' select TM1/TM2 for CCP4 use ( TIMER2 used for PWM)                      
        CCP4CON  = %00001100   ' Setup CCP4 single output for PWM , bits 3,2 , bits 5,4 = 9th and 10 bit of duty cycle ,
        CCPR4L   = 100         ' Lower Byte register for DUTY CYCLE OF PWM  - 8 bits of duty cycle 
        ODCON2.1  = 0          ' set CCP4OD pin not enabled FOR OPen drain  
        PR2      = 100         ' set the PWM period = [(PR2+1)x 4xTosc / TM2 prescale value  
        T2CON    = %00000101   ' Timer 2 ON - POSTSCALLER SETTING NOT USED BY PWM , OLY PRESCALE SETTING - PRESCALE: 00 =1 ,01 = 4,1x = 16
                               ' ( bit 7 = x, bit6-3 postscale, bit2=0 timer off ,bit1-0 = prescale )
        TMR2     =  200

  2. #2
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: PWM on PORTG pin 3 on a 18F67k22

    This is a HPWM pin?
    Why not use HPWM you must have your reasons I guess.

  3. #3
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: PWM on PORTG pin 3 on a 18F67k22

    steve this is the first time i need a hardware based pwm , so dont assume i got it right

  4. #4
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: PWM on PORTG pin 3 on a 18F67k22

    i did use darrel software based one before , but i need this pwm to be set and forget , and not use up cpu time if it were based as software

  5. #5
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: PWM on PORTG pin 3 on a 18F67k22

    and from what i read HPWM command has a speed limitations and the settings are often best set in via the registers directly

  6. #6
    Join Date
    May 2013
    Location
    australia
    Posts
    2,389


    Did you find this post helpful? Yes | No

    Default Re: PWM on PORTG pin 3 on a 18F67k22

    hpwm is set and forget
    the only limitations are max freq = 32767hz min freq depends on fosc , and the pwm resolution is 8 bits , should be adequate for lcd contrast control.

    in your example depending on fosc the tmr2 prescale value is way to low try 16 the pwm resolution might only be about 4 bits (read the data sheet)

  7. #7
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: PWM on PORTG pin 3 on a 18F67k22

    i think i found my mistake steve, a solder bridge on the output to another pin that was being set , which caused an issue when the PWM was output

    d

  8. #8
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: PWM on PORTG pin 3 on a 18F67k22

    ok thanks richard i am tryng the to get a contrast control working for neg voltage , came across this example , see attached

    i still want to have a trim pot , which will set the default value so the lcd is viewable regardless , as well a setting it up via software , to get better viewable / allow temperature change

    have you come across other working examples for negative bios contrast controls
    Attached Images Attached Images  

  9. #9
    Join Date
    May 2013
    Location
    australia
    Posts
    2,389


    Did you find this post helpful? Yes | No

    Default Re: PWM on PORTG pin 3 on a 18F67k22

    I use something like this , note drive fet pin high at reset and gradually decrease the pwm till the contrast is good
    Attached Images Attached Images

  10. #10
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: PWM on PORTG pin 3 on a 18F67k22

    that example looks like the display contrast control only swing between vdd and vss , not a negative voltage of vee ???

  11. #11
    Join Date
    May 2013
    Location
    australia
    Posts
    2,389


    Did you find this post helpful? Yes | No

    Default Re: PWM on PORTG pin 3 on a 18F67k22

    its actually a switch mode power supply that creates the 0-12v contrast voltage , the output voltage is inversely proportional to the pwm

    http://www.digikey.com/en/articles/t...-input-voltage

  12. #12
    Join Date
    May 2013
    Location
    australia
    Posts
    2,389


    Did you find this post helpful? Yes | No

    Default Re: PWM on PORTG pin 3 on a 18F67k22


  13. #13
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: PWM on PORTG pin 3 on a 18F67k22

    thanks richard , the second link helped clear why it works ,

    i happened to have LM324 onhand and the first cct seems to work well , changing the r4, r9 in the diagram to 200k pot allows easy way to set the best display level as a dephalt setting for the display that is in use for testing , and will go back to a set resistors now that it has been found

    the display i am using has a -9.5v VEE generated output , seems best display with -7v ( relative to GND) applied to to V0

    for anyone using this cct the OP amp series LM321( single) LM358(duel),LM324(quad) , has a voltage range of upto 32v ( combined 5v + VEE = 32v( VEE upto -27v))

    ill do further testing , but i found the best results to be for a freq of 1300 , which gave a smooth voltage output from the op amp , a duty of 140 , ranging from 80 - 200 gave a voltage shift of +/- 2V from pot setting

    in the end it was alot easier to use the HPWM command , as the freq required was well below 32767Hz no direct chip register setting up was required

    cheers guys

Similar Threads

  1. PULSIN ON PORTF and PORTG ON 16F1947
    By longpole001 in forum mel PIC BASIC Pro
    Replies: 25
    Last Post: - 9th December 2013, 00:54
  2. 16-48 pin Multi slow PWM
    By krohtech in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 28th March 2009, 03:28
  3. Strange behavior - PORTG.2, 18F8720
    By Charles Linquis in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 11th January 2009, 23:30
  4. Multiplexing PWM pin with other outputs
    By bcd in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 19th September 2006, 11:24
  5. pwm to pin using variable
    By wsmrite in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 7th April 2006, 17:08

Members who have read this thread : 1

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