' ********************************************************************** ' ** Program: k51ppoe.BAS - Version : 1.1 - 20 June 2005 ** ' ** Compiler : Bascom AVR IDE - LIB 1.11.7.4 ** ' ** Board : GMB HR168 and GMM AM128 + 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 ** ' ********************************************************************** ' ' 20.06.05 - Rel 1.1 By Graziano Gaiba ' This demo allows to drive an I2C BUS peripheral on board of K51-AVR, PCF8574, ' through mini-BLOCK module GMB HR168 and grifo(r) mini module. ' PCF 8574 is an I/O expander that allows to drive 8 TTL digital lines, both as ' input and as output, all the operations of read and write are performed ' through synchronous serial interface I2C BUS. ' This demo activates in sequence one line at a time, creating the classic bit ' shift to the right and to the left, and writes 'out' on the 7 segments ' display of K51-AVR, near the characters 'out'. ' ' ' ' !!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ' In menu Options | Compiler | Chip, set: ' ' Chip: M128 ' HW Stack: at least 64 ' Soft Stack: at least 32 ' Framesize: at least 64 ' ' $regfile = "m128def.dat" $crystal = 7372800 $baud = 19200 ' '********************* Constants declaration *************************** ' ' Time out in case a TWI command receives no response Const Ee_timeout = 50000 ' TWI reading options Const Twi_ack = 1 Const Twi_nack = 0 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 ' ********************************************************************** ' Comment the declaration of the component not used, uncomment the other ' declaration ' 'Const Pcf8574 = &H3C ' Slave address PCF8574A 'Const Wpcf8574 = &H78 ' Slave address PCF8574A in Write 'Const Rpcf8574 = &H79 ' Slave address PCF8574A in Read Const Pcf8574 = &H24 ' Slave address PCF8574P Const Wpcf8574 = &H48 ' Slave address PCF8574P in Write Const Rpcf8574 = &H49 ' Slave address PCF8574P in Read ' '*********************** Variables declaration ************************* ' ' General purpose Dim I2cstatus As Byte Dim Pout As Byte ' Value to set in output Dim X As Byte ' Generic use Dim Dato As Byte ' Counter ' '********************* Procedures declaration ************************** ' ' ' Send a command to TWI interface, wait for it to complete and returns status Declare Function Twi_cmd(byval Cmd As Byte) As Byte Declare Sub Twiinit(byval Clockrate As Byte) ' Initialize TWI hardware interface Declare Function Twistart() As Byte ' Generate a start by TWI hardware interface Declare Function Twiwbyte(byval Dato As Byte) As Byte ' Send a byte by TWI hardware interface Declare Function Twirbyte(twi_data As Byte , Byval Assert_ack As Byte) As Byte ' Read a byte from TWI hardware interface Declare Function Twistop() As Byte ' Generate a stop by TWI hardware interface Declare Sub Iniz() ' Peripherals initialization Declare Sub Check_ready() Declare Function Check_ready_2() As Byte ' '*************************** Main program ****************************** ' Main: ' Delay for signals settling Print Chr(12) Print "Demo output of 8 TTL signals of PCF8574." Print Call Check_ready() Print "One TTL signal shifts from left to right and viceversa." Call Iniz() ' initializations Do Dato = 1 ' starting value For X = 1 To 7 ' cycle initialization Pout = Not Dato ' complement the value I2cstatus = Twistart() ' Start sequence for I2CBUS I2cstatus = Twiwbyte(wpcf8574) I2cstatus = Twiwbyte(pout) I2cstatus = Twistop() 'I2csend Wpcf8574 , Pout ' set the outputs Rotate Dato , Left ' left shift of bits Waitms 255 ' delay Next X For X = 1 To 7 ' cycle initialization Pout = Not Dato ' complement the value I2cstatus = Twistart() ' Start sequence for I2CBUS I2cstatus = Twiwbyte(wpcf8574) I2cstatus = Twiwbyte(pout) I2cstatus = Twistop() 'I2csend Wpcf8574 , Pout ' set the outputs Rotate Dato , Right ' right shift of the bits Waitms 255 ' delay Next X Loop End ' '*************************** Programma end ****************************** ' ' '**************************** Procedures ******************************** ' ' ********************** Peripherals initialization ******************** ' This procedure performs all the system initializations. ' Parameters: ' Input : nothing ' Output : nothing ' ************************************************************************ ' Sub Iniz() ' Inizializzazione periferiche Local Valore As Byte ' Initialize hardware I2C BUS interface at about 72 kHz Call Twiinit(72) Do I2cstatus = Twistart() ' Start sequence for I2CBUS I2cstatus = Twiwbyte(rsaa1064) I2cstatus = Twirbyte(valore , Twi_nack) ' Read the status register I2cstatus = Twistop() 'I2creceive Rsaa1064 , Valore Loop Until Valore = 0 ' Wait for SAA1064 to turn on I2cstatus = Twistart() ' Start sequence for I2CBUS I2cstatus = Twiwbyte(wsaa1064) ' Communicate the Slave address I2cstatus = Twiwbyte(ctb) ' Point to control register I2cstatus = Twiwbyte(&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 I2cstatus = Twiwbyte(92) ' set DY1 "o" I2cstatus = Twiwbyte(28) ' set DY2 "u" I2cstatus = Twiwbyte(120) ' set DY3 "t" I2cstatus = Twiwbyte(0) ' set DY4 off I2cstatus = Twistop() End Sub ' '****************************************************************************** '* I2C BUS hardware management procedures * '****************************************************************************** ' ' ' ' Writes command passed in parameter cmd into control register TWI and waits ' for the completion of operation, then reads the status of TWI interface and ' returns it. ' Function Twi_cmd(byval Cmd As Byte) As Byte Local Time_out As Word , I As Byte Twcr = Cmd ' Writes the command Time_out = 0 ' Timeout counter Do Incr Time_out ' Increment counter Loop Until Twcr.twint = 1 Or Time_out = Ee_timeout ' Reads status and masks not significant bits I = Twsr I = I And &HF8 Twi_cmd = I End Function ' ' ' Initializes hardware TWI interface ' Sub Twiinit(byval Clockrate As Byte) Twsr = Twsr And &HFC ' No prescaler Twbr = Clockrate ' Set clock rate End Sub ' ' ' Send start for hardware TWI interface ' Function Twistart() As Byte Twistart = Twi_cmd(&Ha4) ' Start End Function ' ' ' Send byte to hardware TWI interface ' Function Twiwbyte(byval Dato As Byte) As Byte Twdr = Dato ' Write data Twiwbyte = Twi_cmd(&H84) End Function ' ' ' Read byte from hardware TWI interface ' Function Twirbyte(twi_data As Byte , Byval Assert_ack As Byte) As Byte If Assert_ack = 1 Then Twirbyte = Twi_cmd(&Hc4) Else Twirbyte = Twi_cmd(&H84) End If Twi_data = Twdr End Function ' ' ' Send stop on hardware TWI interface. ' Returns 0 if there are no errors, a value different from 0 in case of ' timeout. ' Function Twistop() As Byte Local Time_out As Word Twcr = &H94 ' Stop ' Waits for the stop completer or timeout Time_out = 0 Do Incr Time_out Loop Until Twcr.twsto = 0 Or Time_out = Ee_timeout If Time_out = Ee_timeout Then Twistop = 1 Else Twistop = 0 End If End Function ' '****************************************************************************** '* Generic procedures * '****************************************************************************** ' ' ' ' Supporting function for Check_ready(). ' Function Check_ready_2() As Byte Local Time_out As Word , I As Byte Time_out = 0 ' Timeout counter Do Incr Time_out ' Increment counter Loop Until Twcr.twint = 1 Or Time_out = Ee_timeout I = Twsr I = I And &HF8 Check_ready_2 = I End Function ' ' ' Check for card ready ' Sub Check_ready() Local Check As Byte , Test As Byte Do ' Loop to check for card ready Twsr = Twsr And &HFC Check = Twdr Or &HF4 Test.4 = Not Check.3 Twbr = 72 Twcr = &HA4 Check = Check_ready_2() Test = Check Or Twcr If Check = &H09 Then Test = &H67 Else Test.1 = Not Test.1 End If Twdr = &HA0 Twcr = &H84 Check = Check_ready_2() Twcr = &H94 Waitms 27 Loop Until Check = &H18 Or Test <> 1 Twcr.twen = 0 End Sub