'**************************************************************** '* Name : MX7705 GLCD 2 WIRES * '* Author : Marco D'Onofrio * '* Notice : Copyright (c) 2004 Donomark * '* : All Rights Reserved * '* Date : 10/12/2004 * '* Version : 1.0 * '* Notes : version with 16f877,GLCD and trend plot * '* : 2 wires interface with serial ready flag reading * '**************************************************************** Include "PROTON_G4.INT" ' Demo software for AD7705 (Analog device) and Maxim MX7705 (the chips are pin to pin compatible) 16 bits Sigma delta AD converter with digital filtering ' In this demo 2 pic pins are used. Cs is connected to GND, ready flag is read via serial line ' SI and SO pin are tied togheter with a 4k7 resistor pull-up resistor to VCC. See AD7705 or MX7705 datasheet ' Only Si/SO pin a clock pin are used ' Draw a scale with X and Y lines ' Incremental tick marks are also added ' Code working properly ' Using a GRAPHIC LCD with an external FONT Symbol SCK=PORTA.5 'Clock pin Symbol SI=PORTA.2 ' AD7705 Data in pin Symbol SO=PORTA.2 ' AD7705 Data out pin. Is the same pin of SI 'Variables for scale drawing (the code for scale drawing is from Proton demo file) Dim S_XSTART As Byte Dim S_XEND As Byte Dim S_YSTART As Byte Dim S_YEND As Byte Dim S_YTICK As Byte Dim S_XTICK As Byte Dim XPOS As Byte Dim YPOS As Byte Dim time_unit As Byte 'variable used for ascissa Dim Send As Byte 'byte sent to communication register of AD7705 Dim rec As Byte 'byte received from AD7705:only for communication register, set-up register and clock register (8 bit registers) Dim ready As Byte 'AD7705 Ready BIT Dim I As Byte 'general purpose variable Dim ad As Word 'ad converter value read from AD7705 Dim value As Float 'variable used for Y drawing Dim value_old As Float 'previous value Clear 'clear all variables Cls 'clear LCD S_XSTART = 0 ' XPOS start position S_XEND = 119 ' XPOS end position S_YSTART = 0 ' YPOS start position S_YEND = 59 ' YPOS end position S_XTICK = 10 ' X increment of TICKS S_YTICK = 10 ' Y increment of TICKS start: Print At 0,1,"Mx7705 AD Config" DelayMS 250 Send=$20 ' send command $20 to communication register: channel 1 selected, next operation is a write on clock register rec=$C 'write on clock register: clock division=1, clock bit=1, master clock enabled (bit set to 0), bit output rate set to 50hz (maximum is 500Hz) GoSub AD_write 'call the subroutine for AD write Send=$28 'send command to communication register:next operation is a read from clock register to check if previous command succeded GoSub AD_read calib: 'self-calibration Send=$10 ' send command to communication register:channel 1 selected, next operation is a write on set-up register rec=$46 ' write $46 (bin 01000110) on set-up register:enable self calibration,gain 1, unipolar operation, buffer control on, enable filter synchronization 'note that after the self calibration, AD7705 start free conversion cycle GoSub AD_write 'send command to AD7705 Send=$18 'read back to check command has been accepted GoSub AD_read Send=$8 'send read request to communication register GoSub AD_read ready=rec & %10000000 'check if the MSB BIT is =1 While ready=%10000000 'If MSB bit (bit 8) is 1 AD7705 is busy Print At 4,1,"wait" 'Wait until ready bit is 0 High PORTD.0 GoSub AD_read ready=rec & %10000000 Wend DelayMS 100 Low PORTD.0 'AD7705 set-up finished. Start conversions Cls 'clear LCD GoSub Draw_Scale' Draw the scale loop: Send=$38 'send command to communication register:next operation is reading the 16bit data register where conversion result are stored GoSub AD_read2 Send=$8 'send read request to communication register GoSub AD_read ready=rec & %10000000 'check if the MSB BIT is =1 While ready=%10000000 'wait AD7705 until the conversion is finished Print At 4,1,"wait" High PORTD.0 GoSub AD_read ready=rec & %10000000 Wend Inc time_unit ' draw the trend Line 1,time_unit-1,(60-value_old*10), time_unit,(60-value*10) If time_unit=119 Then 'if time unit is 119, clear GLCD and start again Cls GoSub Draw_Scale ' Draw the scale Clear time_unit EndIf value_old=value If InKey=0 Then GoTo calib 'if SW1 is pressed, perform self-calibration and starts againg conversion If InKey=1 Then 'if SW2 is pressed, put PIC in pause mode Snooze 5 While InKey=16 Snooze 5 Wend EndIf GoTo loop AD_write: 'AD7705 write routine 'first byte is for channel selection and register selection (R/W bit is set to 0) 'second byte is the value to be sent 'this routine is valid only for 8 bit register as communication,clock and set-up register. SHOut SI, SCK, msbfirst, [Send\8] ' Send command to communication register SHOut SI, SCK, msbfirst, [rec\8] ' Send value to the selecte register Print At 2,1,"Send:",HEX Send," Dat:",HEX rec," " 'print command Return AD_read: 'Read from AD7705 'first byte is the register address (R/W bit is set to 1) 'second byte is register content SHOut SI, SCK, msbfirst, [Send\8] 'Send read command and address SHIn SO, SCK, msbpre, [rec\8] ' Read data Print At 3,1,"Rec:",HEX Send," Dat:",HEX rec," " Return AD_read2: 'Read AD7705 conversion result 'A 16 bit value is expected SHOut SI, SCK, msbfirst, [Send\8] ' Send read command and data register address SHIn SO, SCK, msbpre, [ad\16] ' Read 16 bit data Print At 0,1,"Rec:",HEX Send," AD:",Dec ad," " Print At 1,1,"Ad:",BIN ad value= (ad*2.5)/65535 Print At 2,1,"Volts:",DEC8 value," " Return '--------------------------------------------------------------------------- ' Draw_Scale subroutine which draws an XY scale with tick marks ' Assumes values assigned to S_XPOS,S_YPOS,S_XTICK,X_XEND,S_YEND and S_YTICK Draw_Scale: ' Draw vertical Y axis I = 0 YPOS = S_YSTART Repeat Plot YPOS,S_XSTART Inc I Inc YPOS Until YPOS > S_YEND ' Add the tick marks to the Y axis YPOS = S_YSTART Repeat Plot YPOS,S_XSTART + 1 YPOS = YPOS + S_YTICK Until YPOS > S_YEND ' Draw horizontal X axis XPOS = S_XSTART Repeat Plot S_YEND,XPOS Inc XPOS Until XPOS > S_XEND ' Draw X axis tick marks XPOS = S_XSTART Repeat Plot S_YEND - 1,XPOS XPOS = XPOS + S_XTICK Until XPOS > S_XEND Return Include "FONT.INC"