' ********************************************************************** ' ** Program: gmbi2ce.BAS - Version : 1.1 - 14 June 2005 ** ' ** Compiler : Bascom AVR IDE - LIB 1.11.7.4 ** ' ** Board : GMB HR168 and GMM AM32 ** ' ** 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 ** ' ********************************************************************** ' ' 14.06.05 - Rel 1.1 By Graziano Gaiba ' These demos allow to communicate to I2C BUS devices connected to CN8. ' It is possible to read or write bytes at any slave address and location ' address input by console. In detail, when reading the byte read is visualized ' and when writing the byte input is sent. ' ' ' !!!!!!!!!!!!!!!!!!!!!!!!!!! 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 ' ' ********************* Constans declarations ************************* ' Const Cret = 13 ' Codice di ritorno di carrello Const Nl = 10 ' Codice nuova linea Const Clrscr = 12 ' codice di clear screen Const Bell = 7 ' codice di Bell ' Time out in case a TWI command receives no response Const Ee_timeout = 50000 ' ' '****************** Variables declaration ********************** ' Dim A As Eram Byte At 1023 ' '****************** Procedures declartion ********************** ' Declare Sub Clrscr() ' Clears the screen Declare Sub Errore() ' Error message Declare Function Leggi_char() As Byte ' Reads one character (blocking) Declare Sub Demo_twi() ' Send a command to TWI device, waits for a response and returns TWI status Declare Function Twi_cmd(byval Cmd As Byte) As Byte Declare Sub Check_ready() ' '************************** Main program ****************************** ' Main: Print Chr(clrscr); ' Clears the screen Print "Demo 1.1 for GMM AM32 + GMB HR168" Print Call Check_ready() Do Call Demo_twi() Loop Do Loop End ' '************************ Program end *************************** ' ' ' **************************** Procedures ****************************** ' ' ' ************************* Sends 25 Line Feed *************************** ' Generic console clear screen function. ' Parameters: ' Input : nothing ' Output : nothing ' ************************************************************************ ' Sub Clrscr Local I As Byte For I = 1 To 25 Print Next I End Sub ' ' ' Prints "Error: " ' Sub Errore() Print "Error: "; End Sub ' ' ' Reads a character in blocking way,converts it in capital letter and prints it ' Function Leggi_char() As Byte Local C As Byte C = Waitkey() Print Chr(c) C = C And &HDF ' Converts ASCII character in capital letter Leggi_char = C End Function ' ' ' 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 ' ' ' Demo of hardware two wires serial interface (TWI) usage. ' Sub Demo_twi() Local S_addr As Word , Time_out As Word Local Dato As Byte , Char As Byte Ddrc = Ddrc And &HCF Portc = Portc Or &H30 ' Enables pull-up Twsr = Twsr And &HFC ' No prescaler Twbr = 72 ' Clock rate 46080 Hz Time_out = 0 ' Time-out for stop Print "Demo TWI" Print Print "Actions:" Print Do Print "A) Write" Print "B) Read" Print "C) Exit" Print Print "Select: "; Char = Leggi_char() If Char <> "C" Then Print Inputhex "Slave and data address in hex (e.g. A804): " , S_addr Select Case Char Case "A" : Inputhex "Data in hex (e.g. 01): " , Dato Print If Twi_cmd(&Ha4) = &H08 Then ' Start Twdr = High(s_addr) ' Send SLA+W If Twi_cmd(&H84) = &H18 Then Twdr = Low(s_addr) ' Send address If Twi_cmd(&H84) = &H28 Then Twdr = Dato ' Send data If Twi_cmd(&H84) = &H28 Then Twcr = &H94 ' Stop ' Wait for stop Do ' completed or Incr Time_out ' Time Out Loop Until Twcr.twsto = 0 Or Time_out = Ee_timeout Else Call Errore() Print "data" End If Else Call Errore() Print "address" End If Else Call Errore() Print "SLA+W" End If Else Call Errore() Print "start" End If Case "B" : If Twi_cmd(&Ha4) = &H08 Then ' Start Twdr = High(s_addr) ' Send SLA+W If Twi_cmd(&H84) = &H18 Then Twdr = Low(s_addr) ' Send address If Twi_cmd(&H84) = &H28 Then If Twi_cmd(&Ha4) = &H10 Then ' Repeat start Twdr = High(s_addr) Or 1 ' Send SLA+R If Twi_cmd(&H84) = &H40 Then ' Use Twi_cmd(&HC4) to perform two or more consecutive read. ' Intercept code &H50 if read performed is the last one of ' the sequence, otherwise use the code below. If Twi_cmd(&H84) = &H58 Then ' Read data Dato = Twdr Twcr = &H94 ' Stop ' Wait for stop Do ' completed or Incr Time_out ' Time Out Loop Until Twcr.twsto = 0 Or Time_out = Ee_timeout Print "Read: " ; Hex(dato) Print Else Call Errore() Print "data" End If Else Call Errore() Print "SLA+R" End If Else Call Errore() Print "start" End If Else Call Errore() Print "address" End If Else Call Errore() Print "SLA+W" End If Else Call Errore() Print "start" End If End Select Twcr.twen = 0 ' Disables TWI peripheral End If Loop Until Char = "C" End Sub ' ' ' Check for hardware ready ' Sub Check_ready() Test: If A = &H55 Then Goto L2 Else Goto Test End If L2: End Sub