4 channel software PWM on P16

General discussion on mikroPascal.
Author
Message
BaCaRdi
Posts: 236
Joined: 21 Feb 2008 02:19

#16 Post by BaCaRdi » 07 Mar 2008 06:06

Cheers m8, will do definitely!

-Marc

showtime
Posts: 2
Joined: 25 Oct 2005 21:47

#17 Post by showtime » 09 Sep 2009 23:41

Hi
I have compiled the code but there is some errors:

30 313 Invalid ASM instruction: "BSF PORTB, ___Red" aa.mpas

What i can i do ?

Thank you

Gianca00
Posts: 1
Joined: 18 Nov 2009 23:39

16F628A

#18 Post by Gianca00 » 18 Nov 2009 23:42

xor wrote:Here is a simple little 135 byte program that will vary 8 LED's on PORTB with 256 Resolution. It's tested on a 16F877A @ 20MHz.

Sorry... it's in mikroBasic. Conversion to Pascal should be easy.

Code: Select all

' 8 x 256 PWM by Warren Schroeder 11/25/2007
' tested on 16F877A @ 20MHz using mikroBasic 6.0


dim pwm as byte[8]
dim i, cnt as byte

sub procedure interrupt
   If dec(cnt) = 0 Then PORTB = 255 End If  ' occurs every 13ms
   ASM
      movlw     0
      decfsz    _pwm,1
      iorlw     1
      decfsz    _pwm+1,1
      iorlw     2
      decfsz    _pwm+2,1
      iorlw     4
      decfsz    _pwm+3,1
      iorlw     8
      decfsz    _pwm+4,1
      iorlw     16
      decfsz    _pwm+5,1
      iorlw     32
      decfsz    _pwm+6,1
      iorlw     64
      decfsz    _pwm+7,1
      iorlw     128
      andwf     PORTB,1
   End ASM
   PIR1.TMR2IF = 0                ' clear interrupt flag
end sub

sub procedure timer_init
   INTCON.GIE = 1                 ' enable GIE
   INTCON.PEIE = 1                ' enable PEIE
   T2CON = 0                      ' prescaler=0; postscaler=0; Timer2=off
   TMR2 = 0                       ' clear timer2
   PIE1.TMR2IE = 1                ' interrupt enabled
   PIR1.TMR0IF = 0                ' interrupt flag cleared
   T2CON.TMR2ON = 1               ' start Timer0
end sub

main:
   PORTB = 255                    ' turn all LED's ON
   TRISB = 0
   ADCON1 = 6
   CMCON = 7
   timer_init
   cnt = 0
   
' valid values are 0 to 255 .. 0 is OFF and 255 is Full ON
' preload pwm channels with some values for demonstration

   pwm[1] = 1
   pwm[7] = 30
   pwm[4] = 60
   pwm[6] = 90
   pwm[3] = 120
   pwm[2] = 150
   pwm[5] = 180
   pwm[0] = 210
   
' demo routine to vary the pwm duty-cycle of each channel every 40ms

   Do
      FSR = @pwm
      For i = 0 to 7
         inc(INDF)
         inc(FSR)
      Next i
      delay_ms(40)
   Loop Until 0=1
   
end.
Dear,
I'm new on this forum. Can you help me for use the above code into a 16F628A with 6 channel and internal clock (if the 4mhz internal clock It's OK for this application)?
Thank You.

Post Reply

Return to “mikroPascal General”