OK
I managed to tweak the code to fit in the direct resistance from the mains without the need for the bridge. The problem of flickering is still there, though not a lot but is still noticeable. Is something wrong with the code? I want to control 2 channels minimum with 1 IC, so I have set timer0 interrupt to happen @ 340uS approximately. This gives me approximately 29 steps for a 10mS interval. I just toggle the trigger edge at every Zero Crossing.
Code:
DEFINE OSC 20 ' OSCCON defaults to 20MHz on reset
DEFINE NO_CLRWDT 1
Include "modedefs.bas"
#CONFIG
ifdef PM_USED
device pic16F676, intrc_osc_noclkout, wdt_on, mclr_on, protect_off
else
__CONFIG _HS_OSC & _WDT_OFF & _MCLRE_OFF & _BODEN & _CP & _CPD
endif
#ENDCONFIG
'----------Other pins Declared Here--------------
Dimmer1 VAR PortA.0 ' dimmer channel 1
Dimmer2 VAR PortC.0 ' dimmer channel 2
'----------Variables declared here----------------
Dimmer var Byte[2] ' timer value for 4 dimmer channels
Count var Byte ' Dimmer working variable
define INTHAND _ISR
Goto main
ISR:
asm
' Context saving happens here
btfsc INTCON, INTF
goto ZeroCrossingInt
T0Over
bcf INTCON, T0IF ; clear the timer overflow flag
movlw 203
movwf TMR0
incf _Count ; Increment Count
; check if channel1 value is reached
movf _Dimmer+0,w
subwf _Count,w
btfsc STATUS,C
bsf _Dimmer1
; check if channel2 value is reached
movf _Dimmer+1,w
subwf _DimrWork,w
btfsc STATUS,C
bsf _Dimmer2
goto EndInt
Set_Bit
bsf OPTION_REG,6
clrf STATUS
Goto EndInt
ZeroCrossingInt
bcf INTCON, INTF ; clear the interrupt
clrf _Count ; Dimmer Working variable
movlw 0
bcf STATUS,Z
addwf _Dimmer+0,w
btfss STATUS,Z
bcf _Dimmer1
movlw 0
bcf STATUS,Z
addwf _Dimmer+1,w
btfss STATUS,Z
bcf _Dimmer2
bsf STATUS,RP0
btfss OPTION_REG,6
Goto Set_Bit
bcf OPTION_REG,6
clrf STATUS
EndInt
; Context Restore here
retfie
endasm
main:
'----------Processor Initialisation---------------
PAUSE 50
TRISA = %001100
TRISC = 0
CMCON = 7
ANSEL = 0
OPTION_REG = %10000100
PORTA=%001100
PORTC=0
INTCON= %10110000
' Global interrupts enabled
' Timer0 interrupt enabled
' Zero Crossing interrupt enabled
' Flags are cleared
DIMMER[0]=0 : DIMMER[1]=25
Pause 3000
Start:
DIMMER[0]=DIMMER[0]+1
DIMMER[1]=DIMMER[1]-1
Pause 3000
If Dimmer[0]=25 then
Dimmer[0]=0 : Pause 3000 : Endif
If Dimmer[1]=0 then
Dimmer[1]=25 : Pause 3000 : Endif
Goto start
END
I am using 16f676 and 4 x 4.7Meg from Mains - Direct to PortA.2.
Bookmarks