PDA

View Full Version : Question on LampDim.BAS



FromTheCockpit
- 5th August 2010, 00:33
Can someone please
1) Explain the calculation behind MAXDELAY in the following program
2) Confirm if in Dimmers only Positive half is controlled (and WHY)?


' Lamp dimmer
' ===========
'
' File name : LampDim.bas
' Company : Mister E
' Programmer : Steve Monfette
' Date : 23-12-2004
' Device : PIC12F675
'
'
' This program is use to dim intensity of an AC line load
' like lamp, motor and other. Developped for 60 Hz line.
'
' This allow to increase or decrease intensity.
' If the user doesn't held at least 0.5 sec:
' 1. "Increase" : we will set the output for full brightness
' 2. "Decrease" : we will turn off the output
'
'
' The software need :
' 1. A full wave signal from the AC line on GP4
'
' The software use :
' 1. TIMER1 overflow to check if pushbutton are hold for
' more than 0.5 Sec
' 2. Interrupt on GP4 (AcLine input) to synchronise Triac
'
'
' Device programming mode and hardware definition
' ===============================================
' Using Internal Clock, no clock out
' Enable Watch dog timer
' Disable MCLR pin
' Enable Power-up timer
' Enable Brown-out detect
'
@ __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_OFF & _PWRTE_ON & _BODEN_ON
'
'
TRISIO = %11111011
OPTION_REG.7=0 ' Enable pull-ups
WPU=%00000011 ' Enable pull-ups on GP0, GP1
CMCON=7 ' Disable analog comparator
ANSEL=0 ' Disable analog converter

Pb_Inc var GPIO.0 ' Input for INCREMENT/ON push button
Pb_Dec var GPIO.1 ' Input for DECREMENT/OFF push button
Triac var GPIO.2 ' Output to TRIAC gate
ACLine var GPIO.4 ' Input for the FullWave rectify AC line
'
' Software definition
' ===================
'
'
'
MaxDelay var word
TriacDelay var Word
Debounce_AutoRepeatDelay var word
FullBright var bit
'
'
' Interrupts definition
' =====================
'
'
'
INTCON=%10001000 ' Enable interrupt on GPIO change
IOCB.4=1 ' Enable interrupt on GP4 change
PIE1.0=0 ' disable TMR1 overflow interrupt
T1CON=%00110100 ' Set TIMER1
' prescaler 1:8
' internal clock (Fosc/4) 1MHZ
' synchro internal
' we will use TIMER1 overflow
' $ffff * 8* (1/(4MHZ / 4)) = 0.524 Sec
ON INTERRUPT GOTO ACDetect
'
' Hardware and variable initialisation
' ====================================
'
'
Maxdelay=6000 ' Set Maximum delay (set to 8000 for 50Hz)
FullBright=0 ' disable Full Brightness flag
triac=0 ' disable Triac Gate
triacdelay=0 ' Set delay to minimum
gosub ResetTimer1 ' reset Timer1
'
' Main
' ====
' Get entry from user to Increment of Decrement intensity
'
'
start:
'
' Test Increment push button
' --------------------------
' If hold more than .5 Sec, increment triac gate delay by 500 uSec
' case else Full brightness at output
'
while Pb_inc=0
gosub TestTimer1 ' test status of TIMER1
while (Pb_inc==0) AND (PIR1.0==0) 'loop while holding push button
'and no TIMER1 overflow

wend

if PIR1.0==1 then ' If timer overflow (pushbutton hold for > 0.5 sec),
T1CON.0=0 ' disable TMR1
gosub Debounce_AutoRepeat
if (triacdelay<Maxdelay) then
triacdelay=triacdelay+500 'increment Triac gate delay
else
triacdelay=maxdelay ' if triacDelay>MaxDelay,
fullbright=1 ' set the full brightness Flag
endif
else
triacdelay=maxdelay ' If pushButton was hold less than .5 sec
Fullbright=1 ' Set the full brightness flag
endif
wend
gosub ResetTimer1
'
' Test Decrement push button
' --------------------------
' If hold more than .5 Sec, increment triac gate delay by 500 uSec
' Case else, turn off output
'
while Pb_dec=0
gosub TestTimer1 ' Test status of TIMER1
while (Pb_dec==0) AND (PIR1.0==0) 'loop while holding push button
'and no TIMER1 overflow

wend

if PIR1.0==1 then ' If timer overflow (pushbutton hold for > 0.5 sec),
T1CON.0=0 ' disable TIMER1
gosub Debounce_AutoRepeat
if (triacdelay>0) then
Fullbright=0 ' Reset TRIAC always ON flag
triacdelay=triacdelay-500
endif
else
Fullbright=0 ' Reset TRIAC always ON flag
triacdelay=0
endif
wend
gosub ResetTimer1
goto start
'
'
' TestTimer1
' ----------
'
' Enable TIMER1 if :
' not enable and not in overflow
'
TestTimer1:
if (T1CON.0==0) AND (PIR1.0==0) then 'if TIMER1 not enable
'and TIMER1 not overflow
T1CON.0=1 'enable TIMER1
endif
return
'
'
' ResetTimer1
' -----------
'
' Subroutine to clear Timer1
' 1. Overflow flag
' 2. Disable Timer
' 3. Clear counter
'
ResetTimer1:
PIR1.0=0 'clear timer overflow
T1CON.0=0 'disable timer
TMR1L=$00 'clear counter
TMR1H=$00 '
return
'
'
' Debounce_AutoRepeat
' -------------------
'
' Subroutine to debounce push button.
' Also provide kind of auto-repeat when push button
' are held down.
'
' each delay = 20 mSec
' Use of PAUSEUS to be sure getting ACLine interrupt
'
Debounce_AutoRepeat:

for Debounce_AutoRepeatDelay=1 to 2000
pauseus 10
next
return
'
'
' ACDetect
' --------
'
' Interrupt routine called by ACLine (GP4) pin state change
'
disable
ACDetect:
if ACline==1 then ' Check for rising edge of AC signal
if triacdelay > 0 then
Triac=1 ' Activate TRIAC
if FullBright==0 then ' In case Brightness flag is not set
pauseus triacdelay ' do the selected delay
triac=0 ' Disable TRIAC
endif
else
triac=0
endif
endif
INTCON.0=0 ' Clear GPIF (interrupt on GP4 change)
resume
enable

FromTheCockpit
- 6th August 2010, 14:01
Anyone.........?

mackrackit
- 6th August 2010, 14:15
Looks like Steve was going for a 10 second period in the MAXDELAY . Triggering with the AC line at 60 Hz 6000 cycles = 10 second.

DIMMERS? Catching the positive side of the AC signal?

Acetronics2
- 6th August 2010, 14:33
Looks like Steve was going for a 10 second period in the MAXDELAY . Triggering with the AC line at 60 Hz 6000 cycles = 10 second.



Hi, Dave

Maxdelay value is used for a PAUSEUS command ( bottom of program ) ... soooooo ...

looks Steve has left 2 ms time for the triac to de-activate @ zero approaching and get a good zero detection for next cycle, maiy be executing some code lines in between ( :rolleyes: )

these 2 ms also set the minimum time the lamp glows ( may be THE reason :rolleyes: ) to effectively produce some light ...

Alain

mackrackit
- 6th August 2010, 15:23
So that is what the US after PAUSE is for :D

FromTheCockpit
- 7th August 2010, 00:14
I am trying to experiment and adjust this code for 16F676 BUT with an external oscillator (_XT_OSC).
Can I still use "T1CON=%00110100"?

aratti
- 7th August 2010, 09:12
Can I still use "T1CON=%00110100"?

You have to set bit 2 to 0 T1CON = %00110000 for external oscillator.




gosub Debounce_AutoRepeat
if (triacdelayMaxDelay,
fullbright=1 ' set the full brightness Flag
endif


The above has been copied from the code you have posted. This code, without correction, will not compile

Al.

FromTheCockpit
- 7th August 2010, 10:51
You have to set bit 2 to 0 T1CON = %00110000 for external oscillator.


I checked the datasheet for Bit2, will timer1 be running at 4MHz & TMR1 interrupts be happening at 16.383mS?

Also can bit2=0 run timer1 or it won't work at all?

financecatalyst
- 7th August 2010, 14:55
OK, I have now changed the code to work on 16F676. Code is attached.
I have tried different values for triacdelay from 8000 & downwards as in lampdim.bas to as high as 15000 & downwards, but I am not getting satisfactory results for my fan speed.
It woks fine at full speed but when values start reducing, the fan speed reduces quiet a lot at the first step only. Any ideas as to what values I should try with?

Acetronics2
- 7th August 2010, 18:02
Any ideas as to what values I should try with?

Hi,

IF my old memory is right ... your motor is not one of the so called " universal " type ... AND modifying its speed is not so simple as for your electric drill ...

as speed is based upon the mains FREQUENCY ... :rolleyes:

BTW ... This:


2) Confirm if in Dimmers only Positive half is controlled (and WHY)?


Shows you did not understand at all the way steve's DIMMER ( never been a motor speed controller !!! ) works ...

Alain

financecatalyst
- 7th August 2010, 19:03
Hi,

IF my old memory is right ... your motor is not one of the so called " universal " type ... AND modifying its speed is not so simple as for your electric drill ...

as speed is based upon the mains FREQUENCY ... :rolleyes:

BTW ... This:


Shows you did not understand at all the way steve's DIMMER ( never been a motor speed controller !!! ) works ...

Alain

It does say it can be used for motors in the code lampdim.bas
Also, I am not using this code for previous motor control project, it is just for controlling speed of my ceiling fan, not any stepper motor or anything like that.

I am trying to understand how fan/light Dimmers work & what is the benefit and use of half wave & full wave. Which one can be used for inductive load & which is better for resistive load?

Acetronics2
- 7th August 2010, 19:11
It does say it can be used for motors in the code lampdim.bas
Also, I am not using this code for previous motor control project, it is just for controlling speed of my ceiling fan, not any stepper motor or anything like that.

?

Soooo ... if you already know the things ... why do you ask such questions and why doesn't it want to work properly. :rolleyes:

excuse me if I have disturbed you ...

*** smiles ***

Alain

financecatalyst
- 7th August 2010, 19:16
Soooo ... if you already know the things ... why do you ask such questions and why doesn't it want to work properly. :rolleyes:

excuse me if I have disturbed you ...

*** smiles ***

Alain

A little explanation would have been more appreciated than this. I said it because I read it in the lampdim.bas " ' This program is use to dim intensity of an AC line load
' like lamp, motor and other. Developped for 60 Hz line."

FromTheCockpit
- 7th August 2010, 22:28
hey finance, have a look here:
http://www.picbasic.co.uk/forum/showthread.php?t=2674&p=92337

Read this thread from start and you may get more idea.