PWMPIN EQU P1.0  ;PWM output pin

SJMP PWM_SETUP

ORG 000Bh
LJMP TIMER0_INTERRUPT

PWM_SETUP:MOV TMOD,#00h  ;Timer 0 in mode 0
        MOV R7,#160   ;set pulse width control   ;this is a random value
        SETB EA    ;enable interrupts
        SETB ET0   ;enable timer 0 interrupt
        SETB TR0   ;start timer 0
        RET

TIMER0_INTERRUPT:JB F0,HIGH_DONE;  if F0=1 then we just finished the
                                     ;high section of the cycle so jump to
                                       ;HIGH_DONE
LOW_DONE:       SETB F0    ;make F0=1 to indicate start of high section
        SETB PWMPIN     ;make PWM output pin HIGH
        MOV TH0,R7     ;load high byte of timer with R7        
        CLR TF0      ;clear timer 0 interrupt flag
        RETI

HIGH_DONE:      CLR F0   ;make F0=0 to indicate start of low section
        CLR PWMPIN   ;make PWM output pin LOW
        MOV A, #0FFh   ;load A with 255
        CLR C    ;clear Carry flag so it does not affect the subtraction
        SUBB A,R7 ;A=255-R7
        MOV TH0,A   ;low high byte with the value of A
        CLR TF0  ;clear timer 0 interrupt flag
        RETI 

END