📄 sinewave.asm
字号:
;**********************************************************************************
; sinewave.asm
;
; Version 1.02 9th September 1999
;
; Software to time slice a 50Hz Mains Sinewave to give even power increments.
; Input from 4 Pins:
; Up - Increases power by 1%
; Down - Decreases power by 1%
; On - Sets Power to 100%
; Off - Sets Power to 0%
;
; Software designed for use with 12C50x Using internal 4MHz Oscillator giving
; an instruction cycle time of 1uS.
; This software has carefully timed loops that act as delays as well as checking
; for input from the user.
;
; Author: Michael Pearce
; Chemistry Department, university Of Canterbury
;
; Started: 16 July 1998
;
;*********************************************************************************
; VERSION INFORMATION
; (Current Version On top)
; Version 1.02 - 9 September 1999
; - Attempt to correct the timing values by re-including the 100% check
;
; Version 1.01 - 23 July 1998
; - Added Calibration of Xtal to overcome timing problems using different
; devices. The OTP I used was different to the EPROM device timing wise.
; - May Need to alter Timing values to suit (Haven't Done Yet)
;
;
; Version 1.00 - 23 July 1998
; - All found bugs removed and testing was sucessful
; - First OTP device Programmed and Used (Found Timing difference)
;
;**********************************************************************************
;
; UpButton ; Input PIN - Increments Power in 1% steps (Lowest Priority)
; DownButton ; Input PIN - Decrements Power in 1% steps (Second Lowest Priority)
; OnPin ; Input PIN - Switches Power to 100% (Second Highest Priority)
; OffPin ; Input PIN - Turns Power OFF (Highest Priority)
; Output ; Output PIN - Strikes the LED to Start the TRIAC
; ZeroCross ; Input PIN - Indicates zero cross - used for wakeup!!
;
; UpFlag ; Indicator Flag - Indicates that Up Pin previously "High"
; DownFlag ; Indicator Flag - Indicates that Down Pin previously "Low"
; NoPower ; Indicator Flag - Indicates that Zero Power Required
;
; Power ; Data Byte - Power Level Percentage Setting Variable
; Count ; Num Of Cycles to wait before switching Power ON
; CountTotal ; Total Num of loops Per Cycle
;*******************************************************************
; REQUIRED SET UP in Hardware!!
;
; Low Going Edge 400uS before actual zero of 4ns Minimum low time!!
;
; Inputs are active LOW, pulled high externally by 10K resistors
;
;*******************************************************************
; LIST P=12C508,F=INHX8M
; INCLUDE "picreg.equ"
INCLUDE "P12C508.inc"
;******* I/O Port Addresses ******
#define UpButton GPIO,5
#define DownButton GPIO,4
#define OnPin GPIO,3
#define OffPin GPIO,2
#define ZeroCross GPIO,0
#define Output 0x06,1 ;-- Toggle TRIS to toggle pin
;****** Timing Info *******
#define TIMEOUT 0xF1 ;-- Longest time alowed 0xf1 * 38
;****** BIT FLAGS ******
#define UpFlag 0x07,0
#define DownFlag 0x07,1
#define NoPower 0x07,3
#define MASK_SLEEP b'00110111' ;-- Set TRIS so GP3 won't cause Wake from sleep
#define MASK_ON b'00111101' ;-- Turn Output ON
#define MASK_OFF b'00111111' ;-- Turn Output OFF
;****** DATA Locations *****
Power EQU 0x08
Count EQU 0x09
CountTotal EQU 0x0A
Delay1 EQU 0x0B ;-- Used to generate 3mS delay...
Delay2 EQU 0x0C ; .... till actual zero cross.
;**********************************************************************************
; Equates for this program
;**********************************************************************************
TEMP equ 0x10
adif equ 1
adgo equ 2
;**********************************************************************************
; Reset address and interupts
;**********************************************************************************
org 0x00
;----- Load Calibration Settings for XTAL ------
MOVWF OSCCAL ;-- W is loaded with value on RESET
;-----------------------------------------------
goto start
org 0x04
return ;interupt vector
;**********************************************************************************
; Start - Begining of main program - Initialise then Run.
;**********************************************************************************
ORG 0x10
start NOP
NOP
NOP
NOP
CLRF GPIO ;-- Set all OUTPUT states to Zero
MOVLW 0xFF ;-- All as Inputs - Toggle Bit For GP6 to change output
TRIS GPIO ;-- Load the TRIS Register
MOVLW 0x4F ;-- Wake up on change + disable pull ups
OPTION
CLRF Power ;-- Clear the Power Setting
;**********************************************************************************
; MainLoop - This is the main Loop of the program
;**********************************************************************************
MainLoop
;******** Check if POwer > 100 %
MOVLW 0x64 ; Load 100% into Reg for checking if > 100%
SUBWF Power,W ; Subtract 100 from Power Rating
BTFSS STATUS,C ; Check if result +ve of -ve
GOTO MaxEnd1 ; result was positive so no need to adjust the setting
MOVLW 0x64 ; Result -ve so adjust the power
MOVWF Power ;
GOTO MaxEnd
MaxEnd1
MaxEnd
;**********
BCF NoPower ;-- Enable The Power Bit
MOVLW TIMEOUT ;-- Load Total Numer of timing loops to Do
MOVWF CountTotal ;-- MAX Before Coming back to here
MOVF Power,W ;-- Load Power Rating to use as Offset
CALL PowerTable ;-- Get the data from the table
MOVWF Count ;-- Store in Count Register
MOVF Power,W
BTFSC STATUS,Z ;-- Test the zero flag
BSF NoPower ;-- Indicate that are not to switch lamp on!!
MOVLW MASK_OFF ;-- Set up GPIO so GP3 won't cause a Wake from sleep
TRIS GPIO ;-- But so Zero Cross (GP0) will Wake things Up
;**********************************************************************************
; WAIT FOR ZERO CROSS INTERRUPT
;**********************************************************************************
; MOVF GPIO,W ;-- Read the Pins ready for sleep
; SLEEP ;-- Wait for interrupt
SleepLoop
BTFSC ZeroCross
GOTO SleepLoop
; MOVLW 0xFD ;-- Output Short Pulse FOR DEBUG #################
; TRIS GPIO
; MOVLW MASK_OFF ;-- On Wake Up from Sleep
; TRIS GPIO ;-- Set all Ports to INPUT
;**** Wait 3ms Until actual zero cross ****
; MOVLW 0x0A
; MOVWF Delay1
Dly3ms
MOVLW 0x85
MOVWF Delay2
Dly3ms_1
DECFSZ Delay2 ;-- 3 cycles per loop * 133 = approx 400us
GOTO Dly3ms_1
; DECFSZ Delay1
; GOTO Dly3ms
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -