0

I have this code here for generating a square wave of 50% duty cycle and 0.1 Hz frequency with 8 MHz Fosc:

BCF TRISD,7
MOVLW   0x07
MOVWF   T0CON
HERE    MOVLW   0x67
MOVWF   TMR0L
MOVLW   0x69
MOVWF   TMR0H
BCF INTCON,TMR0IF
BTG PORTD,7
BSF T0CON,TMROON
AGAIN   BTFSS   INTCON,TMR0IF
BRA AGAIN
BCF T0CON,TMR0ON
BRA HERE

How can I modify this code in order to perform rectangular wave instead of square wave and with 30% duty cycle instead of 50%?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • Have two loops, one after the other, with different timer values. One controls the high output, the other controls the low output. Their sum gives the required period and their ratio gives the required duty cycle. So 50% would be the special case where the two timer counts are equal. – Weather Vane May 17 '16 at 08:12
  • Thanks for your reply **Mr Weather Vane**, I didn't get your exact point... would you mind to show me what you mean by code? –  May 17 '16 at 16:14
  • This looks similar to PIC code, which I haven't written for some time. But suppose the waveform period requires 10000 timer counts, based on the configuration of the timer settings. For a 30% duty cycle you write the output high, and use a timer to delay 3000 counts. Then you write the output low, and use the timer to delay 7000 counts (total period 10000). Now repeat. If that's your code you shouldn't find it too difficult, having managed a 50% duty cycle for a given waveform period. – Weather Vane May 17 '16 at 17:05

1 Answers1

0

Thanks for the help Mr Weather Vane I have edited the code to perform the 30% duty cycle but the delay part I couldn't solve it so far....

Here is the update..

BCF TRISD,2
MOVLW   0x07
MOVWF   T0CON
HERE:   MOVLW   0xE5
MOVWF   TMR0L
MOVLW   0x48
MOVWF   TMR0H
BCF INTCON,TMR0IF
BTG PORTD,2
BSF T0CON,TMR0ON
AGAIN:  BTFSS   INTCON,TMR0IF
BRA AGAIN
BCF T0CON,TMR0ON
BRA HERE