16f628a pwm routine

Discussion in 'Programmer's Corner' started by Dodgydave, Jul 13, 2012.

  1. Dodgydave

    Thread Starter AAC Fanatic!

    Jun 22, 2012
    5,003
    745
    Trying to set up a pwm routine on this pic16f628a, has anyone got a working asm routine. I have one for the16f690, but the files are different. Prefer files is in assembly language.
     
    Last edited: Jul 13, 2012
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
    Microchip's AN1074 is a complete project to slowly change an RGB LED thru all colors. It contains an assembly module to generate 3 PWM signals in assembly.

    The routine mostly lives inside an ISR so all you need do is set the PWM constants and they are automatically controlled.

    It's based on the PIC12HV615 chip, which is a fairly baseline device meaning you probably have whatever resources it needs, such as Timer0.
     
  3. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
  4. Dodgydave

    Thread Starter AAC Fanatic!

    Jun 22, 2012
    5,003
    745
    This is what i have so far, it works but ramps up/dn very slowly, the delay dosent seem to make any difference, what ever time i set it to, is there some thing in the Timer2 thats not set?

    Code ( (Unknown Language)):
    1.  
    2. LIST P=16F628;f=inhx8m
    3. #include "P16F628.INC" ; Include header file
    4. __CONFIG _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT & _BODEN_ON & _LVP_OFF & _CP_OFF & _MCLRE_OFF
    5. ;------------------------------------------------------------
    6. errorlevel -302 ; suppress banksel warning messages during assembly
    7. errorlevel -311 ; suppress HIGH operator warning messages during assembly
    8.  
    9. cblock 0x20 ; Beginn General Purpose-Register
    10. ;-------------------------- counters
    11. d1
    12. d2
    13. d3
    14. ;--------------------------
    15. endc
    16. ;--------------------------
    17. ;--- Reset --------------------------------------------------
    18. org h'00'
    19. goto init ; reset -> init
    20. ;--- Interrupt ----------------------------------------------
    21. org h'04'
    22.  
    23.  
    24.  
    25. init;clrf PORTA
    26. ;clrf PORTB
    27. movlw 0x07 ; Turn comparators off and enable pins for I/O
    28. movwf CMCON
    29. bcf STATUS,RP1
    30. bsf STATUS,RP0 ; Bank 1
    31. movlw 0xFF ; all input
    32. movwf TRISA
    33. movlw 0x00
    34. movwf TRISB ; all output
    35. bcf STATUS,RP0 ; Bank 0
    36. call pause
    37. movlw 0xFF
    38. movwf PORTB
    39. call pause ; wait
    40.  
    41. ;---------------------
    42. clrf T2CON
    43. clrf TMR2
    44. clrf INTCON
    45. bsf STATUS,RP0
    46. clrf PIE1
    47. bcf STATUS,RP0
    48. clrf PIR1
    49. bsf STATUS,RP0
    50. movlw 0xFF
    51. movwf PR2 ; compare with 255
    52. bcf STATUS,RP0
    53. [B][U]movlw b'00000011'[/U][/B]
    54. movwf T2CON ; prescaler 1:16 and postscaler 1:1
    55. movlw 0x3C
    56. movwf CCP1CON
    57. ;************************************************************
    58. Start
    59. movlw .0
    60. movwf CCPR1L
    61. call pause
    62. incfsz CCPR1L,f
    63. goto $-2
    64. call pause
    65. movlw .255
    66. movwf CCPR1L
    67. call pause
    68. decfsz CCPR1L,f
    69. goto $-2
    70. goto Start ; repeat
    71.  
    72. ;************************************************************************
    73. ;
    74. ;************************************************************************
    75. pause
    76.  
    77. ;199998 cycles 2msec
    78. movlw .100
    79. movwf d1
    80. movlw .3
    81. movwf d2
    82. Delay_0
    83. decfsz d1, f
    84. goto $+2
    85. decfsz d2, f
    86. goto Delay_0
    87.  
    88. return
    89. end
    90.  
     
    Last edited: Jul 13, 2012
  5. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    The ramping speed is entirely determined by the delay, changing d1 won't make much difference to the delay, and setting d2 to 0 makes the delay as long as possible, setting it to 1 would be around the shortest delay.
     
  6. Dodgydave

    Thread Starter AAC Fanatic!

    Jun 22, 2012
    5,003
    745
    Fixed it, it was the prescaler setting in Tmr2 not set i have highlighted it
    should be movlw b'00000111' ...Doh!!!
     
  7. MMcLaren

    Well-Known Member

    Feb 14, 2010
    759
    116
    Happy to hear you got it going. If you're still interested, here's an embarrassingly old example (attached).

    Cheerful regards, Mike
     
  8. Dodgydave

    Thread Starter AAC Fanatic!

    Jun 22, 2012
    5,003
    745
    Far Too Complicated!!

    but thanks anyway
     
    Last edited: Jul 14, 2012
  9. MMcLaren

    Well-Known Member

    Feb 14, 2010
    759
    116
    I think a more appropriate response would have been "thank you" (lol).

    Good luck on your projects.

    Cheerful regards, Mike
     
  10. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    Code ( (Unknown Language)):
    1. PR2=0xFF;
    even simpler
     
Loading...