' ********************************************************************** ' ** Program: k51dae.BAS - Version : 1.1 - 15 June 2005 ** ' ** Compiler : Bascom AVR IDE - LIB 1.11.7.4 ** ' ** Board : GMB HR168 and GMM AM32 plus K51-AVR ** ' ** Society: grifo(r) ITALIAN TECHNOLOGY ** ' ** Via Dell' Artigiano 8/6 40016 San Giorgio di Piano (BO) ** ' ** Tel.+39 051 892 052 Fax +39 051 893 661 ** ' ** http://www.grifo.com http://www.grifo.it ** ' ** Written by: Graziano Gaiba ** ' ********************************************************************** ' ' 15.06.05 - Rel 1.1 By Graziano Gaiba ' This demo allows to drive an I2C BUS peripheral on board of K51-AVR, PCF8591, ' through mini-BLOCK module GMB HR168 and grifo(r) mini module. ' PCF 8591 is provided with one analog output for D/A conversions with 8 bits ' resolution, all the operations of activation are performed through ' synchronous serial interface I2C BUS. ' This demo allows to set the voltage output level through PC keyboard, and ' writes 'da' on the 7 segments display of K51-AVR to indicate that demo is ' running. ' ' Compiled file is smaller than 2048 bytes, so it can becompliled with free ' demo of BASCOM AVR. ' ' ' !!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ' In menu Options | Compiler | Chip, set: ' ' Chip: M32 ' HW Stack: at least 64 ' Soft Stack: at least 32 ' Framesize: at least 64 ' ' $regfile = "m32def.dat" $crystal = 7372800 $baud = 19200 Config Sda = Portc.1 ' I2C serial DATA Config Scl = Portc.0 ' I2C serial CLK ' '********************* Constants declaration *************************** ' Const Saa1064 = &H38 ' Slave address SAA1064 Const Wsaa1064 = &H70 ' Slave address SAA1064 in Write Const Rsaa1064 = &H71 ' Slave address SAA1064 in Read '****************** Addresses list for Saa1064 ************************* Const Ctb = 0 ' Address Control byte Const Dig1 = 1 ' Address Digit 1 Const Dig2 = 2 ' Address Digit 2 Const Dig3 = 3 ' Address Digit 3 Const Dig4 = 4 ' Address Digit 4 '*********************************************************************** ' Const Pcf8591 = &H48 ' Slave address PCF8591 Const Wpcf8591 = &H90 ' Slave address PCF8591 in Write Const Rpcf8591 = &H91 ' Slave address PCF8591 in Read ' '*********************** Variables declaration ************************* ' ' General purpose Dim A As Eram Byte At 1023 ' PWM output voltage Dim Pwm As Byte ' Number to send to 7-segments display Dim Cifh As Byte ' Most significant nibble Dim Cifl As Byte ' Least significant nibble ' '********************* Procedures declaration ************************** ' Declare Sub Check_ready() Declare Sub Iniz() ' Peripherals initialization Declare Sub Canale ' Channel number management Declare Sub Cifre(byval Valore As Byte ) ' converts a number in 2 HEX figures Declare Function Digit(byval Dig As Byte ) As Byte ' Converts a number in range ' 0-F in 7 segments Declare Sub Vis_num(byval N1 As Byte) ' Shows a byte in HEX Declare Sub Dac(byval Valore As Byte) ' D/A converter management Declare Sub Valore() ' Output value selection ' '*************************** Main program ****************************** ' Main: Print Chr(12) ' Clears the screen Print "Demo PWM of PCF 8591" Print Call Check_ready() Call Iniz() ' initializations Pwm = 0 Print Print "Press + to increment output voltage level" Print "Press - to decrement output voltage level" Print Do Call Dac(pwm) ' D/A converter management Call Vis_num(pwm) ' Show byte in HEX Call Valore() Loop End ' '**************************** Program end ****************************** ' ' '**************************** Procedures ******************************* ' ' ' ********************** Peripherals initialization ******************** ' This procedure performs all the system initializations. ' Parameters: ' Input : nothing ' Output : nothing ' ********************************************************************** ' Sub Iniz() ' Peripherals initialization Local Valore As Byte Do I2creceive Rsaa1064 , Valore ' read status register Loop Until Valore = 0 ' wait for SAA1064 to turn on I2cstart ' Start sequence for I2CBUS I2cwbyte Wsaa1064 ' Communicate the Slave address I2cwbyte Ctb ' point to control register I2cwbyte &B00100111 ' bit0 =1 dynamic mode ' bit1 =1 digit 1+3 not blanked ' bit2 =1 digit 2+4 not blanked ' bit3 =0 no test segment ' bit4 =0 no 3mA segment current ' bit5 =1 6mA segment current ' bit6 =0 no 12mA segment current ' bit7 =0 indifferent I2cwbyte 94 ' set DY1 'd' I2cwbyte 119 ' set DY2 'a' I2cwbyte 0 ' set DY3 off I2cwbyte 0 ' set DY4 off I2cstop End Sub ' ' ' ******************** D/A converter management **************************** ' This procedure allows to set an output value on the D/A converter, ' available on IC2 (PCF8591). ' Parameters: ' Input : Valore as byte, value from 0 to 255 (0v ..+Vref) ' Output : ' ************************************************************************ ' Sub Dac(byval Valore As Byte) ' D/A converter management I2cstart ' Start sequence for I2CBUS I2cwbyte Wpcf8591 ' Communicate the Slave address I2cwbyte &H40 ' D/A activation command I2cwbyte Valore ' value for D/A I2cstop End Sub ' ' ' ******** Converts a figure in range from 0 to F in 7 segments ******** ' This procedure converts a figure in range from 0 to F in 7 segments ' format, if value is greater than F the display is off. ' Parameters: ' Input : dig as byte, value in the range from 0 to F ' Outout : byte, value in 7 segments format. ' ********************************************************************** ' Function Digit(byval Dig As Byte ) As Byte ' Converts a number in range Local Valore As Byte ' 0-F in 7 segments If Dig < 16 Then ' if number is lower than 16 Valore = Lookup(dig , Tab_7seg) ' read value from table Else Valore = 0 ' if = 16 or greater reset End If Digit = Valore End Function ' ' ***************** Converte 1 byte in due cifre HEX ******************** ' This procedure converts 1 byte in range from 0 to 255 in two 4 bits ' hexadecimal figures. ' Parameters: ' Input : Valore as byte, value in the range from 0 to 255 ' Output : Cifh as byte, figure of high nibble ' Cifl as byte, figure of low nibble ' ************************************************************************ ' Sub Cifre(valore As Byte ) ' converts a number in two HEX figures Cifh = Valore And &HF0 ' mask 4 high bits Cifh = Cifh / 16 ' shift to low the 4 high bits Cifl = Valore And &H0F ' mask the 4 low bits End Sub ' ' ' *********************** Visualizes a byte in HEX ********************* ' The first display visualizes the number of the channel being ' converted, the second display is off. ' This procedure allows to show a byte in hexadecimal format ' in the two displays on the right. E.g.: 255= FFH, 32= 20H etc. ' Parameters: ' Input : N1 as byte, number to visualize in HEX ' Output : nothing ' ********************************************************************** ' Sub Vis_num(n1 As Byte) ' visualizes a byte in HEX Local Valore As Byte I2cstart ' START sequence I2cwbyte Wsaa1064 ' slave address I2cwbyte Dig3 ' point to display 3 (DY3) Call Cifre(n1 ) ' convert numero1 in 2 figures Valore = Digit(cifh) ' convert tenth in 7 segments I2cwbyte Valore ' write to display DY3 Valore = Digit(cifl) ' convert units in 7 segments I2cwbyte Valore ' write to display DY4 I2cstop ' Stop sequence End Sub ' ' ' Output level managemet ' Sub Valore() Local Valore As Byte Valore = Inkey() If Valore = "+" Then Incr Pwm End If If Valore = "-" Then Decr Pwm End If End Sub ' ' ' Check for hardware ready ' Sub Check_ready() Test: If A = &H55 Then Goto L2 Else Goto Test End If L2: End Sub ' ' ** Conversion table for 7 segments figure in the range from 0 to 15 ** Tab_7seg: ' num. 0 1 2 3 4 5 6 7 8 9 Data &H3F , &H06 , &H5B , &H4F , &H66 , &H6D , &H7D , &H07 , &H7F , &H6F ' num. A b C d E F Data &H77 , &H7C , &H39 , &H5E , &H79 , &H71 ' ********************************************************************** '