Hierbij post ik een voorbeeld voor software PWM in interrupt 8 kanalen in 255 stappen , daar ik zie dat
er toch nogal problemen zijn met hardware PWM.
Voordeel software PWM : ge moet in programma geen rekening houden of leds branden of niet (worden aangestuurd met variabele ‘led0 – led7’)+ meerdere kanalen
Nadeel : Maar 255 Stappen
'**************************************************************** '* Name : Gijsbrechts Danny * '* Author : [select VIEW...EDITOR OPTIONS] * '* Notice : Copyright (c) 2009 [select VIEW...EDITOR OPTIONS] * '* : All Rights Reserved * '* Date : 20/12/2008 * '* Version : 1.5 * '* Notes : * '* : PMW 8 kanalen interrupt gestuurd 255 stappen * '* : kanaal 1 = led0 ,kanaal2 = led1, ... kanaal8 = led7* '* : Leds hangen op Poort b * '* : Subroutine Loop is voorbeeld , dus ledhelderheid * '* : aansturen via variabele le0 - led7 (niet poortb * '***************************************************************** Device 16F628A ' device name XTAL = 20 ' and clock frequency used TRISB = $00 Symbol T0IE = INTCON.5 ' TMR0 Overflow Interrupt Enable Symbol T0IF = INTCON.2 ' TMR0 Overflow Interrupt Flag Symbol GIE = INTCON.7 ' Global Interrupt Enable Symbol T0CS = OPTION_REG.5 ' Timer0 Clock Source Select (0=Internal clock 1=External PORTA.4) Symbol ledpoort = PORTB ' Initiate the interrupt OPTION_REG = %00000001 GIE = 0 ' Turn off global interrupts T0CS = 0 ' Assign TMR0 clock to internal source TMR0 = 200 ' Clear TMR0 initially T0IE = 1 ' Enable TMR0 overflow interrupt GIE = 1 ' Enable global interrupts Dim Value As Byte Dim Result As Byte Dim pwmteller As Byte Dim led0 As Byte ' Helderheid elk RGB kanaal ( 8 kanalen Dim led1 As Byte Dim led2 As Byte Dim led3 As Byte Dim led4 As Byte Dim led5 As Byte Dim led6 As Byte Dim led7 As Byte Dim poortb As Byte Dim teller As Byte Dim r As Byte Dim b As Byte Dim g As Byte Dim blauw As Byte Dim s As Byte On_Interrupt GoTo scan led0 = 0 led1 = 0 led2 = 0 led3 = 0 led4 = 0 led5 = 0 led6 = 0 led7 = 0 GoTo loop ; ********************* interrupt pwmcyclus *************************** scan: TMR0 = 200 ASM incf pwmteller,f ; verhoog pwmteller (helderheidsteller) movf pwmteller,w xorlw 255 ; plaats hier aantal helderheidsstappen (16...255) bnz leds clrf pwmteller ; reset pwmteller( movlw 255 movwf poortb ; alle leds aan zetten (begin nieuwe cyclus van 32) leds: nop nop movf pwmteller,W ; haal huidige pwmcyclus [helderheisstap 16..255] xorwf led0,W ; led(0) zelfde als pwmcyclus skpnz ; nee, volgende instructie bcf poortb,0 ; ja, led(0) uit movf pwmteller,W xorwf led1,W skpnz bcf poortb,1 movf pwmteller,W xorwf led2,W skpnz bcf poortb,2 movf pwmteller,W xorwf led3,W skpnz bcf poortb,3 movf pwmteller,W xorwf led4,W skpnz bcf poortb,4 movf pwmteller,W xorwf led5,W skpnz bcf poortb,5 movf pwmteller,W xorwf led6,W skpnz bcf poortb,6 movf pwmteller,W xorwf led7,W skpnz bcf poortb,7 movf poortb,W movwf ledpoort ; update leds ;clear interrupt,return van interrrupt ENDASM T0IF = 0 ' Clear the TMR0 overflow flag Context Restore ' Restore the registers and exit the interrupt ;******************** dimcyclus ********************************************** loop: For g = 0 To 255 step1 ;groen aan Repeat Until pwmteller = 254 led0 = g led3 = g^255 DelayUS 500 ;vertraging Next g For r = 0 To 255 Step 1 ; rood aan Repeat Until pwmteller = 254 led1 = r led4 = r DelayUS 500 Next r For g = 255 To 0 Step -1 ; groen uit Repeat Until pwmteller = 254 led0 = g led3 = g^255 DelayUS 500 Next g For b = 0 To 255 step1 ;blauw aan Repeat Until pwmteller = 254 led2 = b led5 = b^255 DelayUS 500 Next b For r = 255 To 0 Step -1 ; rood uit Repeat Until pwmteller = 254 led1 = r led4 = r^255 DelayUS 500 Next r For g = 0 To 255 step1 ;groen aan Repeat Until pwmteller = 254 led0 = g led3 = g^255 DelayUS 500 Next g For r = 0 To 255 Step 1 ; rood aan Repeat Until pwmteller = 254 led1 = r led4 = r^255 DelayUS 500 Next r For b = 255 To 0 Step -1 ; blauw uit Repeat Until pwmteller = 254 led2 = b led5 = b^255 DelayUS 500 Next b For r = 255 To 0 Step -1 ; rood uit Repeat Until pwmteller = 254 led1 = r led4 = r^255 DelayUS 500 Next r GoSub flikker ; effekten GoSub looplichtlinks GoSub flikker GoSub looplichtrechts GoSub effekt1 led0=0 led1=0 led2=0 led3=0 led4=0 led5=0 GoTo loop ;******************* looplicht links ************************* looplichtlinks: For s = 1 To 20 led0 = 255 led3 = 255 led2=0 led5=0 DelayMS 30 led0 = 0 led3 = 0 led1 = 255 led4 =255 DelayMS 30 led1=0 led4=0 led2=255 led5=255 DelayMS 30 Next s Return ; **************** looplicht rechts ************************* looplichtrechts: For s = 1 To 20 led4 = 255 led1 = 255 led2=0 led5=0 DelayMS 30 led4 = 0 led1 = 0 led3 = 255 led0 =255 DelayMS 30 led3=0 led0=0 led5=255 led2=255 DelayMS 30 Next s Return ; ******************* flikkeren ************************* flikker: For s = 1 To 20 led1 = 255 led2 =led1 led3=led1 led4=led1 led5=led1 led0=led1 DelayMS 20 led1 = 0 led2 =led1 led3=led1 led4=led1 led5=led1 led0=led1 DelayMS 20 Next s Return ; ************************** effekt 1 ******************** effekt1: led4 =0 led1=0 For s = 1 To 30 led0 = 255 led3 = 255 led2=0 led5=0 DelayMS 50 led0 = 0 led3 = 0 led2 = 255 led5 =255 DelayMS 50 Next s Return
Ingestuurd door: dannymyron