News:

;) This forum is the property of Proton software developers

Main Menu

5 channel pwm with 18F25K22

Started by OG, Feb 20, 2021, 12:22 AM

Previous topic - Next topic

OG

25k22.JPG

25k22_2.JPG


8 bit pwm

;-------------------------------------------------------------------------------
;**** Added by Fuse Configurator ****
; Use the Fuse Configurator plug-in to change these settings

Device = 18F25K22

Declare Reminders Off
@ CONFIG_REQ = 0 ; Override Compiler's configuration settings
Asm-
__Config Config1H, 0x28 ;FOSC_INTIO67 & PLLCFG_OFF & PRICLKEN_ON & FCMEN_OFF & IESO_OFF
__Config Config2L, 0x19 ;PWRTEN_OFF & BOREN_OFF & BORV_190
__Config Config2H, 0x3F ;WDTEN_ON & WDTPS_32768
__Config Config3H, 0x39 ;CCP2MX_PORTC1 & PBADEN_OFF & CCP3MX_PORTC6 & HFOFST_ON & T3CMX_PORTC0 & P2BMX_PORTB5 & MCLRE_INTMCLR
__Config Config4L, 0x80 ;STVREN_OFF & LVP_OFF & XINST_OFF & DEBUG_OFF
__Config Config5L, 0x00 ;CP0_ON & CP1_ON & CP2_ON & CP3_ON
__Config Config5H, 0x80 ;CPB_ON & CPD_OFF
__Config Config6L, 0x0F ;WRT0_OFF & WRT1_OFF & WRT2_OFF & WRT3_OFF
__Config Config6H, 0xE0 ;WRTC_OFF & WRTB_OFF & WRTD_OFF
__Config Config7L, 0x0F ;EBTR0_OFF & EBTR1_OFF & EBTR2_OFF & EBTR3_OFF
__Config Config7H, 0x40 ;EBTRB_OFF
Endasm-
Declare Reminders On

;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------

Declare Xtal = 16  
Declare All_Digital = True

OSCCON = %01110000       ''' Osc 16Mhz

PORTA = 0
PORTB = 0
PORTC = 0
TRISA = %00000000  '
TRISB = %00000000  '
TRISC = %00000000  '
TRISE.3 = 1
PORTA = 0
PORTB = 0
PORTC = 0

CM1CON0.7=0     ''' Comparator is disabled
CM2CON0.7=0     ''' Comparator is disabled

INTCON = 0

ANSELA = 0
ANSELB = 0
ANSELC = 0


;;; *********  PWM SETTINGS  ********  ;;;;;;;
''' Timer4 selected for PWM 
    ''' page 201 REGISTER 14-3: CCPTMRS0: PWM TIMER SELECTION CONTROL REGISTER 0
    ''' page 201 REGISTER 14-4: CCPTMRS1: PWM TIMER SELECTION CONTROL REGISTER 1
CCPTMRS0 = %01001001    ' 
CCPTMRS1 = %00000101    '

''' see page 181 for PWM PERIOD (frq),  DUTY CYCLE
''' PWM Period = [(PRx) + 1] * 4 * TOSC * (TMRx Prescale Value)
    ''' TOSC = 1/FOSC  ,  PWM FRQ = 1 / PWM Period

''' PWM FRQ = 976,563 Hz @ 16MHz Fosc (TMRx Prescale Value = 16)
    ''' PRx = 255
T4CON    = %00000110    ' TIMER4 PRESCALAR = 16 ( page 171, for Timer2/4/6 Control)   

'''' PWM FRQ = 3906,25 Hz @ 16MHz Fosc (TMRx Prescale Value = 4)
'    ''' PRx = 255
'T4CON    = %00000101    ' TIMER4 PRESCALAR = 4 ( page 171, for Timer2/4/6 Control)


'''' PWM FRQ = 15625 Hz @ 16MHz Fosc (TMRx Prescale Value = 1)
'    ''' PRx = 255
'T4CON    = %00000100    ' TIMER4 PRESCALAR = 1 ( page 171, for Timer2/4/6 Control)

PR4 = 255
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;  CCPxCON
'''  page 198 CCPxCON: STANDARD CCPx CONTROL REGISTER
    '''  bit 5-4 DCxB<1:0>: PWM Duty Cycle Least Significant bits
        '''    PWM mode:
        '''    These bits are the two LSbs of the PWM duty cycle. The eight MSbs are found in CCPRxL.
    ''' bit 3-0 CCPxM<3:0>: ECCPx Mode Select bits
        '''   11xx =: PWM mode
       
CCP1CON = %00001100                     ' ENABLE PWM1
CCP2CON = %00001100                     ' ENABLE PWM2
CCP3CON = %00001100                     ' ENABLE PWM3
CCP4CON = %00001100                     ' ENABLE PWM4
CCP5CON = %00001100                     ' ENABLE PWM5

'' Duty = 0 (pwm MSB 8bit's)
CCPR1L = 0
CCPR2L = 0
CCPR3L = 0
CCPR4L = 0
CCPR5L = 0


Dim pwm_1 As Byte
Dim pwm_1_memory As Byte
 
Dim pwm_2 As Byte
Dim pwm_2_memory As Byte
 
Dim pwm_3 As Byte
Dim pwm_3_memory As Byte
 
Dim pwm_4 As Byte
Dim pwm_4_memory As Byte

Dim pwm_5 As Byte
Dim pwm_5_memory As Byte

Dim my_byte As Byte

'''''''''''''''

''    OSCCON bit 2 HFIOFS: HFINTOSC Frequency Stable bit
''        1 = HFINTOSC frequency is stable
''        0 = HFINTOSC frequency is not stable
Main:
    Clrwdt
    If OSCCON.2 = 0 Then Main
   
    GoSub close_all
   
    Clrwdt
    DelayMS 100
    Clrwdt

loops:   
    For my_byte = 0 To 255
        pwm_1 = my_byte
        pwm_2 = my_byte
        pwm_3 = my_byte
        pwm_4 = my_byte
        pwm_5 = my_byte
       
        GoSub out_pwm
        DelayMS 7
    Next
   
    DelayMS 1000
    Clrwdt
   
    For my_byte = 255 To 0 Step -1
        pwm_1 = my_byte
        pwm_2 = my_byte
        pwm_3 = my_byte
        pwm_4 = my_byte
        pwm_5 = my_byte
       
        GoSub out_pwm
        DelayMS 7
    Next
   
    DelayMS 1000
    Clrwdt
GoTo loops


''''''''''''''''
out_pwm:

''' 5. pwm
out_5:
    Clrwdt
    If pwm_5 <> pwm_5_memory Then    
        CCPR5L = pwm_5          
        pwm_5_memory = pwm_5
    EndIf

''' 4. pwm
out_4:
    Clrwdt
    If pwm_4 <> pwm_4_memory Then    
        CCPR4L = pwm_4       
        pwm_4_memory = pwm_4
    EndIf


''' 3. pwm
out_3:
    If pwm_3 <> pwm_3_memory Then   
        CCPR3L = pwm_3     
        pwm_3_memory = pwm_3
    EndIf  

''' 2. pwm
out_2:
    If pwm_2 <> pwm_2_memory Then   
        CCPR2L = pwm_2      
        pwm_2_memory = pwm_2
    EndIf

''' 1. pwm
out_1:
    If pwm_1 <> pwm_1_memory Then
        CCPR1L = pwm_1     
        pwm_1_memory = pwm_1
    EndIf

    Clrwdt   
Return


''''''''''''
close_all:
    CCPR1L = 0
    CCPR2L = 0
    CCPR3L = 0
    CCPR4L = 0
    CCPR5L = 0
   
    pwm_1_memory = 0
    pwm_2_memory = 0
    pwm_3_memory = 0
    pwm_4_memory = 0
    pwm_5_memory = 0
Clrwdt
Return
'''''''''''