Keywords: DS1267, digital potentiometers, digipot, digital pots, Xicor
Related Parts |
;********************************************************************* ;* DS87C520 APPS DEVELOMENT SYSTEM * ;* * ;* Application: Communication with a DS1267, and a PC via * ;* Serial Port 1. * ;********************************************************************* ;* * ;* This program was created to demonstrate using a 3-Wire interface * ;* to communicate with a DS1267 Digital Potentiometer. The program * ;* also talks to PC via Serial Port 1 (19200 Baud). The micro is * ;* operated using DS1077 Oscillator operating at 22.2MHz. * ;* * ;* Software Revision History * ;* * ;* 1.0 01/19/01 - First try at operating a DS1267 using the * ;* generic application engineering generic 8051 * ;* boot loading board. * ;* * ;* Hardware Description * ;* * ;* P1.0 - LED P0.0 - SN74F373N * ;* P1.1 - P0.1 - " * ;* P1.2 - RXD1 - PC P0.2 - " * ;* P1.3 - TXD1 - PC P0.3 - " * ;* P1.4 - CLK P0.4 - " * ;* P1.5 - DQ P0.5 - " * ;* P1.6 - RST P0.6 - " * ;* P1.7 - COUT P0.7 - " * ;* * ;* P3.0 - RXD0 - Not used P2.0 - Upper * ;* P3.1 - TXD0 - Not used P2.1 - Address * ;* P3.2 - P2.2 - Byte * ;* P3.3 - P2.3 - " * ;* P3.4 - P2.4 - " * ;* P3.5 - P2.5 - " * ;* P3.6 - WR\ P2.6 - " * ;* P3.7 - RD\ P2.7 - " * ;* * ;* Window 0 R0 - Used for 3-wire read and write, Do not destroy!; * ;* Window 0 R1 - Used for binasc routine, Do not destroy!; * ;* Window 0 R5-R7 Used for timer/scratch pad, destroy with caution!; * ;********************************************************************* $include (c:\firmware\reg520.inc) ; SFR register defs for compiler ;************* Variable Declarations ************* ;** General Variables ** stack equ 02Fh ; bottom of stack ; stack starts at 30h ;** DS1267 Variables ** stack1 bit 000h ; Read DS1276#1 Stacked? PotData0 equ 042h ; Write Data DS1267#1, pot0 PotData1 equ 043h ; Write Data DS1267#1, pot1 stack2 bit 001h ; Read DS1267#2 Stacked? PotData2 equ 044h ; Write Data DS1267#2, pot0 PotData3 equ 045h ; Write Data DS1267#2, pot1 ;************* SFR Declarations ************* ;** General SFR Names ** smod_1 equ 0DFh ; baud rate doubler bit declared ;** Port 1 Signal Names ** LED equ 90h ; P1.0 is LED ; P1.1 is not used RX1 equ 92h ; P1.2 is Serial Port 1 RX TX1 equ 93h ; P1.3 is Serial Port 1 TX CLK equ 94h ; P1.4 is CLK - 3Wire DQ equ 95h ; P1.5 is DQ - 3Wire RST equ 96h ; P1.6 is RST - 3Wire COUT equ 97h ; P1.7 is COUT - 3Wire ;********************************************************************* ;* Hardware Interrupt Vectors (Table on page 95 of DS databook) * ;********************************************************************* ;* No Interrupts are enabled in this code. If interrupts are to be * ;* enabled they need to have the label initialized here. * ;********************************************************************* org 0000h ; Power up and Reset ljmp start org 0003h ; External Interrupt 0 ljmp start org 000Bh ; Timer 0 Interrupt ljmp start org 0013h ; External Interrupt 1 ljmp start org 001Bh ; Timer 1 Interrupt ljmp start org 0023h ; Serial Port 0 Interrupt ljmp start org 002Bh ; Timer 2 Interrupt ljmp start org 0033h ; PowerFail Interrupt (DS Priority 1) ljmp start org 003Bh ; Serial Port 1 Interrupt (DALLAS) ljmp start org 0043h ; External Interrupt 2 (DALLAS) ljmp start org 004Bh ; External Interrupt 3 (DALLAS) ljmp start org 0053h ; External Interrupt 4 (DALLAS) ljmp start org 005Bh ; External Interrupt 5 (DALLAS) ljmp start org 0063h ; Watchdog Interrupt (DALLAS) ljmp start org 006Bh ; Real-Time Clock (DALLAS) ljmp start ;********************************************************************* ;**** Main Program **** ;**** This program talks to two DS1267s using 3-wire. It also **** ;**** demonstrates reading from the DS1267s using a 4th pin as **** ;**** an input from the Cout of the last DS1267. **** ;********************************************************************* org 0080h; start: clr EA ; Disable Interrupts lcall initSP1 ; Initialize Ser Port 1 & Timer 1/0 lcall intro ; Welcome Message, Serial Port 1 lcall init3wire ; Initialize 3-wire Variables cpl LED ; Complement LED9 - Identifies that the ; has started execution lcall WritePots3 ; write to the pots - writes the values ; stored in the RAM locations by init3wire mov R5, #255 ; set timer up for a 5.0 second delay mov R6, #147 ; " mov R7, #10 ; " lcall wt ; call timer function lcall ReadPots3 ; read data from pots, re-write the data ; back to the part. lcall DisplayPots3 ; display the data from the DS1267s endmain: mov R5, #255 mov R6, #147 ; R5=255,R6=147 => R7 * 0.5 Sec mov R7, #1 ; 0.5 Sec Delay lcall wt cpl LED ; Blink LED once per second sjmp endmain ; Waits forever ;********************************************************************* ;**** 3-Wire Write Pots Routine for DS1267s **** ;**** Writes the 34 bits out to the two DS1267s **** ;********************************************************************* ;* requires WriteBits3 routine * ;* WriteBits3 destroys the R0 and A registers * ;********************************************************************* WritePots3: setb RST ; set RST to begin data xfer mov C, stack2 ; set 1267#2 stack condition=stack2 mov DQ, C setb CLK ; clock 3-wire bus clr CLK ; mov A, PotData3 ; moves PotData3 into A for xmission lcall WriteBits3 ; call WriteBits3 to write serial byte ; out to the DS1267s mov A, PotData2 ; moves PotData2 into A for xmission lcall WriteBits3 ; call WriteBits3 to write serial byte ; out to the DS1267s mov C, stack1 ; set 1267#1 stack condition=stack1 mov DQ, C ; setb CLK ; clock 3-wire bus clr CLK ; mov A, PotData1 ; moves PotData1 into A for xmission lcall WriteBits3 ; call WriteBits3 to write serial byte ; out to the DS1267s mov A, PotData0 ; moves PotData0 into A for xmission lcall WriteBits3 ; call WriteBits3 to write serial byte ; out to the DS1267s clr RST ; clear RST to end data XFER ret ;********************************************************************* ;**** 3-Wire Write Bits Routine for DS1267s **** ;**** Writes 8 bits by toggling the DQ and the CLK pins as **** ;**** required. This is used to write 8-bit potentiometer **** ;**** values out to the parts **** ;********************************************************************* ;* requires no other routines, destroys R0 and A registers * ;********************************************************************* WriteBits3: mov R0, #8 ; 8-bits in transfer WBit3: rlc A ; rotate MSB to carry bit mov DQ, C ; mov C onto DQ pin setb CLK ; clock 3-wire bus clr CLK ; djnz R0, WBit3 ; if all 8-bits not send, keep sending ret ; else return ;********************************************************************* ;**** 3-Wire Read Pot Routine for DS1267s **** ;**** Reads 34 bits of information from the DS1267 by using **** ;**** ReadBits3 and by toggling the RST, DQ and the CLK pins **** ;**** to read the Stack Bits as required. **** ;********************************************************************* ;* requires readbits3 routine * ;* readbits3 destroys R0 and A * ;********************************************************************* ReadPots3: clr PotData0 ; Clear PotDataX registers to guarantee the clr PotData1 ; values displayed are not the values clr PotData2 ; left in the registers from programming clr PotData3 ; the DS1267s setb RST mov C, COUT ; Copy COUT into C mov DQ, C ; Wrap the contents back out onto DQ.. ; ..so the DS1267s will still have the same ; values after the read attempt mov stack2, C ; copy C into stack2 setb CLK ; clock DS1267s clr CLK ; lcall ReadBits3 ; read next byte mov PotData3, A ; mov Data to Storage Byte lcall ReadBits3 ; read next byte mov PotData2, A ; mov Data to Storage Byte mov C, COUT ; Copy COUT into C mov DQ, C ; Wrap Data back into part mov stack1, C ; copy C into stack1 setb CLK ; clock DS1267 clr CLK ; lcall ReadBits3 ; read next byte mov PotData1, A ; mov Data to Storage Byte lcall ReadBits3 ; read next byte mov PotData0, A ; mov Data to Storage Byte clr RST ret ;********************************************************************* ;**** 3-Wire Read Bits Routine for DS1267s **** ;**** Reads 8 bits by toggling the DQ and the CLK pins as **** ;**** required. This is used to read the 8-bit potentiometer **** ;**** values from the parts **** ;********************************************************************* ;* requires no other routines * ;* destroys R0 and A registers * ;********************************************************************* ReadBits3: mov R0, #8 ; 8-bits in transfer RBit3: mov C, COUT ; mov COUT into C mov DQ, C ; Write COUT onto DQ - Wrap Data back into part rlc A ; rotate carry bit to MSB of A setb CLK ; clock 3-wire bus clr CLK ; djnz R0, RBit3 ; if all 8-bits not send, keep sending ret ; else return ;********************************************************************* ;**** Display DS1267 Pots Routine **** ;**** Displays the value of the registers used for all 4 **** ;**** Digital Potentiometers using serial port 1 **** ;********************************************************************* ;* requires binasc and outchar, outstr routines * ;********************************************************************* DisplayPots3: jb stack1, stkd1 ; if not stacked then write "N " mov A, #'N' lcall outchar mov A, #' ' lcall outchar sjmp next1 stkd1: mov A, #'S' ; else write "S " lcall outchar mov A, #' ' lcall outchar next1: mov A, PotData0 ; move data read from RAM @PotData0 to A lcall binasc ; convert data from bin to ascii lcall outchar ; send first byte via Ser. Port1 mov A, R1 ; mov 2nd conversion byte from R1>A lcall outchar ; send second byte via Ser. Port1 mov A, #' ' ; send Space via Ser. Port1 lcall outchar mov A, PotData1 ; Move data read from RAM @PotData1 to A lcall binasc ; convert data from bin to ascii lcall outchar ; send first byte via Ser. Port1 mov A, R1 ; mov 2nd conversion byte from R1>A lcall outchar ; send second byte via Ser. Port1 mov A, #' ' ; send Space via Ser. Port1 lcall outchar jb stack2, stkd2 ; if not stacked then write "N " mov A, #'N' lcall outchar mov A, #' ' lcall outchar sjmp next2 stkd2: mov A, #'S' ; else write "S " lcall outchar mov A, #' ' lcall outchar next2: mov A, PotData2 ; Move data read from RAM @PotData2 to A lcall binasc ; convert data from bin to ascii lcall outchar ; send first byte via Ser. Port1 mov A, R1 ; mov 2nd conversion byte from R1>A lcall outchar ; send second byte via Ser. Port1 mov A, #' ' ; send Space via Ser. Port1 lcall outchar mov A, PotData3 ; Move data read from RAM @PotData3 to A lcall binasc ; convert data from bin to ascii lcall outchar ; send first byte via Ser. Port1 mov A, R1 ; mov 2nd conversion byte from R1>A lcall outchar ; send second byte via Ser. Port1 mov DPTR, #mess2 ; send (2)LF and (2)CR via Ser. Port1 lcall outstr ; ret ;********************************************************************* ;**** 3-Wire Init Routine for DS1267 **** ;**** Initializes 3 wire pin values **** ;********************************************************************* ;* requires no additional routines * ;* destroys no registers * ;********************************************************************* init3wire: clr RST ; clear reset pin clr CLK ; clear clock pin clr DQ ; clear data pin mov PotData0, #0CDh ; set #1, Sout to 2V, 205d if not stack1 mov PotData1, #99h ; set #1, Sout to 4V, 153d if stack1 setb stack1 ; set #1, stack on, set SOUT to 4V mov PotData2, #66h ; set #2, pot0 to 2V, 102d mov PotData3, #99h ; set #2, pot1 to 3V, 153d setb stack2 ; set #2, stack on ret ;********************************************************************* ;**** Wait 1.6 us Function **** ;**** Wastes 1.6us of processor time with call, nop and return **** ;********************************************************************* ;* Requires no other routines or registers * ;********************************************************************* wait16us: nop ; 1 nops @4cc each + lcall @16cc + ret @16cc ; produces approximately 1.6us of delay with a ; 22.22MHz clock ret ;********************************************************************* ;**** Initialize Serial Port 1 for PC interface **** ;**** Set up serial port 1 for use with a 22.1 MHz crystal **** ;**** Uses timer 1 for 19200 baud, Mode 1 **** ;********************************************************************* ;* Uses no other routines or registers * ;********************************************************************* initSP1: setb smod_1 ;enable baud rate doubler mov SCON1, #50h ;Serial Port 0 asynch, 10 bits mov TMOD,#21H ;MSB-T1 on and in 8bit auto-load-mode ;LSB-T0 on and in 16-bit count mode ; T0 is free running 2^16cc ; overflow rate (35.59ms) mov TCON, #50H ;t1/0 enabled, not using ext int ; edge/level select and detect ; flag/reg mov TH1, #0FAH ;set t1 reset val / baud rate=19200 ret ;********************************************************************* ;**** Intro Display Message Routine **** ;**** Sends out a greeting message **** ;********************************************************************* ;* Uses outstr function * ;* Destroys DPTR * ;********************************************************************* intro: mov DPTR, #mess1 ;send welcome message lcall outstr mov DPTR, #mess2 ;send (2) CR and (2) LF lcall outstr mov dptr, #mess3 ;send application specific message lcall outstr mov DPTR, #mess2 ;send (2) CR and (2) LF lcall outstr mov DPTR, #mess4 ;send read header lcall outstr mov DPTR, #mess2 ;send (2) CR and (2) LF lcall outstr ret ;********************************************************************* ;**** Outstring Routine - Serial Port 1 **** ;**** writes a null terminated string to PC via Ser. Port 1 **** ;********************************************************************* ;* Uses outchar routine * ;* Destroys dptr and A * ;********************************************************************* outstr: clr A ; clear A to get data movc A,@A+DPTR ; get data from string at data pointer jz exitstr ; if data zero, eos lcall outchar ; else send character inc dptr ; increment data pointer sjmp outstr ; continue, zero condition will terminate exitstr: ret ;********************************************************************* ;**** Outchar routine - Serial Port 1 **** ;**** writes character in Acc to the PC via serial port 1 **** ;********************************************************************* ;* Uses no routines or registers * ;********************************************************************* outchar: mov SBUF1,A ; place A into Serial Port 1 Buffer waitchar: jnb SCON1.1, waitchar ; wait buffer empty flag is set clr SCON1.1 ; clear buffer empty flag ret ;********************************************************************* ;**** Binary to Ascii conversion routine **** ;**** Converts a binary number in Acc to 2 ascii digits **** ;**** Leaves results in A (upper digit) and R1 (lower digit) **** ;********************************************************************* ;* Uses no routines * ;* Destroys A and R1 * ;********************************************************************* binasc: mov R1, A ; save number in R1 anl A, #0Fh ; convert least significant digit add A, #0F6h ; adjust it jnc noadj1 ; if a-f readjust add A, #07h ; noadj1: add A, #3Ah ; make ascii xch A, R1 ; put result in reg1 swap A anl A, #0Fh ; convert least significant digit add A, #0F6h ; adjust it jnc noadj2 ; if a-f readjust add A, #07h ; noadj2: add A, #3ah ; make ascii ret ;********************************************************************* ;**** General Wait Function **** ;**** Can wait anywhere between 14.42us to 221 sec. **** ;**** Waits R7 * 867.6 ms if R5 = R6 = 255 **** ;**** Waits R6 * 3.4 ms if R5 = 255 and R7 = 1 **** ;**** Waits R5 * 13.34us if R6 = R7 = 1 **** ;********************************************************************* ;* requires wait16us routine * ;* destroys registers R5, R6 and R7 * ;********************************************************************* wt: lcall wait16us ; 12.8us of waits lcall wait16us lcall wait16us lcall wait16us lcall wait16us lcall wait16us lcall wait16us lcall wait16us djnz R5, wt ;Wait = R5 * 13.34us + 1.1us if R6 = R7 = 1 djnz R6, wt ;Wait = R6 * 3.4ms if R5 = 255, R7 = 1 djnz R7, wt ;Wait = R7 * 867.6ms if R5 = R6 = 255 ret ;******************************************************************** ;**** MESSAGES **** ;******************************************************************** org 8000h mess1: db 'Jason''s Proto-board, Rev. 0.2',0Dh,0Ah db 'Now uses a DS1077 for a clock, and a DS1267',0Dh,0Ah db 'for a 3-Wire Demonstration.', 0 mess2: db 0Dh,0Ah,0DH,0AH,0 mess3: db 'This program talks via a 3-wire interface',0Dh,0Ah db 'to a DS1267, and uses serial port 1 to',0Dh,0Ah db 'communicate with the user', 0 mess4: db '-POT#1- -POT#2-',0Dh,0Ah,0Dh,0Ah, db 'S S' ,0Dh,0Ah db 't t' ,0Dh,0Ah db 'k k' ,0Dh,0Ah db '1 P0 P1 2 P2 P3',0 END ;End of program
Jason's Proto-board, Rev. 0.2 Now uses a DS1077 for a clock, and a DS1267 for a 3-Wire Demonstration. This program talks via a 3-wire interface To a DS1267, and uses serial port 1 to communicate with the user -POT#1- -POT#2- S S t t k k 1 P0 P1 2 P2 P3 S CD 99 S 66 99