; USES PIC12F675 AS A DDS GENERATOR. PHASE ACCUMULATOR IS 24 BITS
; AND PHASE INCREMENT is 16 BITS. GPIO 0-2 IS THE 3 BIT OUPUT TO
; THE CRUDE RESISTOR DAC.
; THIS CODE USES A 256 BYTE (8 BIT) LOOKUP TABLE BUT ONLY A
; 3 BIT OUTPUT VALUE. THIS IS FOR VERSATILITY. IT ALLOWS UP
; TO 8 BIT OUTPUT WITH A DIFFERENT MICRO AND A SIMPLE CHANGE
; TO THE TABLE DATA.
; THE CODE IS 'HARD WIRED" TO PRODUCE 88.5 HZ BUT ANY OTHER
; FREQUENCY CAN BE PRODUCED WITH A DIFFERENT PHASE INCREMENT.
; SEE THE EXPLANATORY TEXT BELOW AND THE 2ND LAST LINE OF
; CODE FOR THE PRESET VALUE WHICH IS STORED IN EEPROM
;*************************************************​********************

;*************************************************​**
; In this design only 2 bytes are used to store the phase
; increment. This is to maximise the loop frequency and hence
; minimise the phase resolution. This limits the range of
; frequencies which can be generated to the range .0019 Hz to
; 127 Hz
;
; Instead of hard-coding the phase increment calculated above,
; it is instead stored in EEPROM as 2 hexadecimal digits.
; This is to make a change of frequency a simpler process.
; Instead of re-assembling the source code, the 2 bytes at the
; end of the hex file can simply be be edited with a text editor
; to change the phase increment to the required value.
;
; Read the 2 phase increment bytes from EEPROM. In the case of
; the 88.5 Hz calculation above, the first byte 1 = B3h and
; byte 2 = CCh
;*************************************************​**
movlw 0
call EEPROMread ; read first byte
movwf phaseincr1
movlw 1
call EEPROMread ; read second byte
movwf phaseincr0
;*************************************************​**
; START OF DDS LOOP. NOTE THAT THE NOP'S ARE TO GIVE
; A CONSTANT 31uS LOOP TIME REGARDLESS OF WHETHER THERE
; ARE 0, 1 or 2 CARRYS
;*************************************************​**
main
movf phaseincr0,w ;Add byte 0
addwf phaseacc0,f
bc carry1 ;jump if carry into byte 1
nop
nop
nop
nop
nop
nop
b addbyte2
carry1 movlw 1
addwf phaseacc1,f
bc carry2
nop
b addbyte2
carry2
movlw 1
addwf phaseacc2,f

addbyte2
movf phaseincr1,w ;Add byte phaseincr1
addwf phaseacc1,f ;to phasecc1
bc carry3 ;jump if carry into byte 2
nop
b lookup
carry3 movlw 1
addwf phaseacc2,f
;*************************************************​**
; THE ABOVE CODE HAS ADDED THE PHASE INCREMENT TO THE
; PHASE ACCUMULATOR. NOW USE THE VALUE OF THE MOST
; SIGNIFICANT 8 BITS OF THE PHASE ACCUMULATOR TO ADDRESS
; A 256 BYTE TABLE WHICH CONTAINS THE DIGITAL OUTPUT TO
; BE PRESENTED TO THE DAC AT THIS PHASE OF THE SINE WAVE
;*************************************************​**

;*************************************************​
; Preset first 2 bytes of EEPROM to the required
; phase increment. These will be B3CCh for 88.5 Hz
;*************************************************​
org 2100h

;*************************************************​
; Preset first 2 bytes of EEPROM to the required
; phase increment. These will be D246h for 103.5 Hz
;*************************************************​
org 2100h