johnnyprimavera
Newbie level 4
- Joined
- Mar 21, 2013
- Messages
- 6
- Helped
- 1
- Reputation
- 2
- Reaction score
- 1
- Trophy points
- 1,283
- Activity points
- 1,382
Code Basic4GL - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 program Candle_NOT_PWM Dim pink as byte sub procedure InitTimer() T1CON = 0x01 'Sets Timer1 Prescaler @ 1:1 OPTION_REG = 0x81 'Sets Timer0 Prescaler @ 1:4 PIR1.TMR1IF = 0 'Resets TMR1 TMR1H = 0xFC 'Preloads TMR1 value: 64512; generates 0.512ms timer. TMR1L = 0x00 TMR0 = 0 PIE1.TMR1IE = 1 'Activates TMR1 INTCON.T0IE = 1 'Activates TMR0 INTCON = 0xC0 'Sets GIE and PEIE. end sub sub procedure Interrupt() if (PIR1.TMR1IF) then PIR1.TMR1IF = 0 'Resets TMR1 TMR1H = 0xFC TMR1L = 0x00 TMR0 = 255 - pink 'Pink is the variable (0 to 255) that controls duty cycle of PWM. INTCON.T0IE = 1 'Activates TMR0 GPIO.4 = 1 'Output is HIGH end if if (INTCON.T0IF) then INTCON.T0IF = 0 'Resets TMR0 GPIO.4 = 0 'Output is LOW end if end sub main: CMCON = 0x07 'All pins configured as Digital I/O TRISIO = %001000 'Only GPIO.3 is input. GPIO = %000000 'Clears all outputs. InitTimer() while(TRUE) 'Performs a linear increase of duty cycle to test its function. ' for pink = 0 to 255 ' delay_ms(100) ' next pink pink = 128 'Fixed value on pink. wend end.
Code ASM - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 _InitTimer: ;Candle_NOT_PWM.mbas,4 :: sub procedure InitTimer() ;Candle_NOT_PWM.mbas,5 :: T1CON = 0x01 'Sets Timer1 Prescaler @ 1:1 MOVLW 1 MOVWF T1CON+0 ;Candle_NOT_PWM.mbas,6 :: OPTION_REG = 0x88 'Sets Timer0 Prescaler @ 1:1 MOVLW 136 MOVWF OPTION_REG+0 ;Candle_NOT_PWM.mbas,8 :: PIR1.TMR1IF = 0 'Resets TMR1 BCF PIR1+0, 0 ;Candle_NOT_PWM.mbas,10 :: TMR1H = 0xFF 'Preloads TMR1 value: 65280; generates 0.256ms timer. MOVLW 255 MOVWF TMR1H+0 ;Candle_NOT_PWM.mbas,11 :: TMR1L = 0x00 MOVLW 16 MOVWF TMR1L+0 ;Candle_NOT_PWM.mbas,12 :: TMR0 = 0 CLRF TMR0+0 ;Candle_NOT_PWM.mbas,14 :: PIE1.TMR1IE = 1 'Activates TMR1 BSF PIE1+0, 0 ;Candle_NOT_PWM.mbas,15 :: INTCON.T0IE = 1 'Activates TMR0 BSF INTCON+0, 5 ;Candle_NOT_PWM.mbas,16 :: INTCON = 0xC0 'Sets GIE and PEIE. MOVLW 192 MOVWF INTCON+0 ;Candle_NOT_PWM.mbas,17 :: end sub L_end_InitTimer: RETURN ; end of _InitTimer _Interrupt: MOVWF R15+0 SWAPF STATUS+0, 0 CLRF STATUS+0 MOVWF ___saveSTATUS+0 MOVF PCLATH+0, 0 MOVWF ___savePCLATH+0 CLRF PCLATH+0 ;Candle_NOT_PWM.mbas,19 :: sub procedure Interrupt() ;Candle_NOT_PWM.mbas,20 :: if (PIR1.TMR1IF) then BTFSS PIR1+0, 0 GOTO L__Interrupt3 ;Candle_NOT_PWM.mbas,21 :: PIR1.TMR1IF = 0 'Resets TMR1 BCF PIR1+0, 0 ;Candle_NOT_PWM.mbas,22 :: TMR1H = 0xFF MOVLW 255 MOVWF TMR1H+0 ;Candle_NOT_PWM.mbas,23 :: TMR1L = 0x00 MOVLW 16 MOVWF TMR1L+0 ;Candle_NOT_PWM.mbas,24 :: TMR0 = 255 - pink + 3 'Pink is the variable (0 to 255) that controls duty cycle of PWM. MOVF _pink+0, 0 SUBLW 255 MOVWF R0+0 MOVLW 3 ADDWF R0+0, 0 MOVWF TMR0+0 ;Candle_NOT_PWM.mbas,25 :: INTCON.T0IE = 1 'Activates TMR0 BSF INTCON+0, 5 ;Candle_NOT_PWM.mbas,26 :: GPIO.4 = 1 'Output is HIGH BSF GPIO+0, 4 L__Interrupt3: ;Candle_NOT_PWM.mbas,29 :: if (INTCON.T0IF) then BTFSS INTCON+0, 2 GOTO L__Interrupt6 ;Candle_NOT_PWM.mbas,30 :: INTCON.T0IF = 0 'Resets TMR0 BCF INTCON+0, 2 ;Candle_NOT_PWM.mbas,31 :: GPIO.4 = 0 'Output is LOW BCF GPIO+0, 4 L__Interrupt6: ;Candle_NOT_PWM.mbas,33 :: end sub L_end_Interrupt: L__Interrupt16: MOVF ___savePCLATH+0, 0 MOVWF PCLATH+0 SWAPF ___saveSTATUS+0, 0 MOVWF STATUS+0 SWAPF R15+0, 1 SWAPF R15+0, 0 RETFIE ; end of _Interrupt _main: ;Candle_NOT_PWM.mbas,35 :: main: ;Candle_NOT_PWM.mbas,36 :: CMCON = 0x07 'All pins configured as Digital I/O MOVLW 7 MOVWF CMCON+0 ;Candle_NOT_PWM.mbas,37 :: TRISIO = %001000 'Only GPIO.3 is input. MOVLW 8 MOVWF TRISIO+0 ;Candle_NOT_PWM.mbas,38 :: GPIO = %000000 'Clears all outputs. CLRF GPIO+0 ;Candle_NOT_PWM.mbas,39 :: InitTimer() CALL _InitTimer+0 ;Candle_NOT_PWM.mbas,40 :: while(TRUE) L__main10: ;Candle_NOT_PWM.mbas,45 :: pink = 165 MOVLW 165 MOVWF _pink+0 ;Candle_NOT_PWM.mbas,46 :: wend GOTO L__main10 L_end_main: GOTO $+0 ; end of _main
void ISR ()
{
if ( pwm_counter == 0 ) // checks the counter variable
{
PORTA=&0b00000001; // if zero, pull output high
}
if ( pwm_counter = duty_cycle ) // check counter against duty_cycle ( a number from 0-99 )
{
PORTA=^0b00000001; // if counter equals duty_cycle, pull output low
}
if ( pwm_counter < 99 ) // check to see if counter reached 99 yet
{
pwm_counter=pwm_counter++; // if not, increment it
}
else
{
pwm_counter=0; // if yes, reset it
}
}
void ISR ()
{
if ( pwm_counter == 0 ) // checks the counter variable 1
{
PORTA=&0b00000001; // if zero, pull output high 2
}
if ( pwm_counter = duty_cycle ) // check counter against duty_cycle ( a number from 0-99 ) 3
{
PORTA=^0b00000001; // if counter equals duty_cycle, pull output low 2
}
if ( pwm_counter < 99 ) // check to see if counter reached 99 yet 6
{
pwm_counter=pwm_counter++; // if not, increment it 1
}
else
{
pwm_counter=0; // if yes, reset it 1
}
} -------------
16 inst. cycles.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?