You are on page 1of 4

'****************************************************************

'* Name : Triac.BAS *


'* Author : *
'* Notice : Copyright (c) 2002 [Free to distribute] *
'* Date : 17/11/2002 *
'* Version : 1.0 *
'* Notes : Lamp Dimmer *
'* Proc : 16F627 *
'* Clock : Internal 4Mhz *
‘* Version: PicBasicPro V2.40 *
'****************************************************************
'
' The rising edge of the ZC circuit corresponds to the ac
' waveform as it passes through zero
'
' PortB.1 = Trig pulse to Opto-triac
' PortB.6 = Brightness Down
' PortB.7 = Brightness Up
'
'
'
'-------------------------------------
' Device configuration
'
@ DEVICE pic16F627, INTRC_OSC_NOCLKOUT
' System Clock Options
@ DEVICE pic16F627, WDT_ON
' Watchdog Timer
@ DEVICE pic16F627, PWRT_OFF
' Power-On Timer
@ DEVICE pic16F627, MCLR_OFF
' Master Clear Options (Internal)
@ DEVICE pic16F627, BOD_OFF
' Brown-Out Detect
@ DEVICE pic16F627, LVP_OFF
' Low-Voltage Programming
@ DEVICE pic16F627, CPD_OFF
' Data Memory Code Protect
@ DEVICE pic16F627, PROTECT_OFF
' Program Code Protection
'
'
' Set up Port Assignments
'-----------------------------------------------
PORTA = 0 ' Clear latches
PORTB = 0 '
TRISA = %00000000 ' PORTA all O/P'S
TRISB = %11111101 ' PORTB.1 Output
'
@ clrf PORTA ; Clear PortA latches

1
@ clrf PORTB ; Clear PortB latches
'
'
'
' Set Variables Options
'
'
CMCON = %00000111 ' Set Port A to all analogue
'
INTF VAR INTCON.1 ' RB0 Interrupt flag
INTE VAR INTCON.4 ' RB0 Interrupt Enable
Angle VAR WORD ' Delay in firing triac

'
'
' Port B
Triac VAR PortB.1 ' Output - to opto-triac
Up VAR PORTB.7 ' Input - Increase brightness
Down VAR PORTB.6 ' Input - Decrease brightness
'
'
' Incon Register Definitions
' -------------------------------
' 7=1 Global Interrupt Enable
' 6=0 Peripheral Interrupts Disabled
' 5=0 TMR0 Interrupt Disabled
' 4=1 RB0 Interrupt Enabled
' 3=1 RB4-RB7 Port change Interrupt Enabled
' 2=0 TMR0 Flag Reset
' 1=0 RB0 Flag Reset
' 0=0 RB4-RB7 Flag Reset
'
' Option Register Definitions
' --------------------------------
OPTION_REG = %11000000
' 7=0 Port B Pullups Enable
' 6=0 Interrupt Edge Select Bit
' 5=0 TMR0 Clock Source Select Bit
' 4=0 TMR0 Source Edge Select Bit
' 3=0 Prescaler Assignment Bit
' 2=0 Prescaler Rate Select Bit
' 1=0 Prescaler Rate Select Bit
' 0=0 Prescaler Rate Select Bit
'
'
'
Triac = 0 ' Triac off
Angle = 0 '
OPTION_REG = %11001001 ' WDT enabled 1:2 (36mS timeout )
'

2
On Interrupt Goto myint ' Define interrupt handler
INTCON = %10010000 'Enable RB0 interrupt
'
Angle = 1000 '
Goto loop ' Start of program
'
'
'
'****************************************
' Interrupt handler
'****************************************
Disable ' No interrupts past this point
myint:
IF INTF = 1 THEN ' If Interrupt flag set
PAUSEUS Angle ' Delay triac firing
HIGH Triac ' Triac ON
PAUSEUS 100 '
LOW Triac ' Triac OFF
ENDIF '
INTCON.1 = 0 ' Clear interrupt flag
Resume ' Return to main program
Enable ' Re-enable interrupts
'
'
'
'----------------------------------------------
' Start of main program
'----------------------------------------------
'Check switch settings
'
Loop:
CLEARWDT ' Reset if timeout
IF (Up = 1) AND (Down = 1) THEN Loop ' Do nothing
IF (Up = 1) AND (Down = 0) THEN Dimit ' Decrease lamp intensity
IF (Up = 0) AND (Down = 1) THEN Upit ' Increase lamp intensity
IF (Up = 0) AND (Down = 0) THEN Loop ' Do nothing
GOTO Loop
'
'
'
Dimit: IF (Angle = 10000) THEN LOOP ' If not at min brightness
Angle = Angle + 2 ' Increase turn on delay
GOTO LOOP '
'
'
Upit: IF (Angle = 1000) THEN LOOP ' If not at max brightness
Angle = Angle - 2 ' decrease turn on delay
GOTO LOOP '
'
END

3
1 2 3 4

1
D1
4 x 1N4007
JP1 +5V
4 2
A 1 A
2
HEADER 2 R1 R2
240V 100K, 1W 10K

3
U2A U2B
1 2 3 4
ZC

74ALS04 74ALS04
U1
K3020
0V

R4 R5 +5V +5V
470 330
U4
240v Live 1 18
B 2 RA2/AN2/VREF RA1/AN1 17 R10 R11 B
3 RA3/AN3/CMP1 RA0/AN0 16 10K 10K
ZC RA4/T0CKI/CMP2 RA7/OSC1/CLKIN
4 RA5/MCLR/THV RA6/OSC2/CLKOUT 15
R3
39 Q1 C2 U3 6 13
47nF, X2, 400V R9 7 RB0/INT RB7/T1OS1 12
TIC226D RB1/RX/DT RB6/T1OSO/T1CKI
330 8 11
RB2/TX/CK RB5
9 RB3/CCP1 RB4/PGM 10

C1 4N25 PIC16F627

0V S1 S2
10nF, X2, 400V
C3
UP DOWN

470nF, X2, 400V


0V 0V
R6 R7 R8
C C

1
470K 470K 47 D2
240V
JP2 JP3LOAD 4 x 1N4007 U5
1 1 78L05
4 2 1 3
2 2 Vin Vout +5V

GND
HEADER 2 HEADER 2
D3
C4 C5
22uF 1uF
3

2
9.1V, 500mW
0V

240v Neutral

Title
D D

Size Number Revision


A4
Date: 18/11/2002 Sheet of
File: C:\Program Files\..\dimmer.Sch Drawn By:
1 2 3 4