' ********************************************************************** ' ** Program: gmbi2ce.BAS - Version : 1.1 - 20 June 2005 ** ' ** Compiler : Bascom AVR IDE - LIB 1.11.7.4 ** ' ** Board : GMB HR168 and GMM AM128 ** ' ** 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 ' 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: M128 ' HW Stack: at least 64 ' Soft Stack: at least 32 ' Framesize: at least 64 ' ' $regfile = "m128def.dat" $crystal = 7372800 $baud = 19200 ' ' ********************* Constans declarations ************************* ' Const Cret = 13 ' Carriage return Const Nl = 10 ' New line Const Clrscr = 12 ' Clear screen Const Bell = 7 ' Bell ' Time out in case a TWI command receives no response Const Ee_timeout = 50000 ' ' '****************** Variables declaration ********************** ' ' No global variables used ' '****************** Procedures declartion ********************** ' Declare Sub Init() ' Initialization 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() Declare Function Check_ready_2() As Byte ' '************************** Main program ****************************** ' Main: Call Init() ' Initialize module ' Turn OFF relays Portf.4 = 1 Portf.5 = 1 Portf.6 = 1 Portf.7 = 1 Portb.3 = 1 Portb.2 = 1 Portb.1 = 1 Porte.2 = 1 Print Chr(clrscr); ' Clears the screen Print "Demo 1.1 for GMM AM128 + GMB HR168" Print Call Check_ready() Do Call Demo_twi() Loop Do Loop End ' '************************ Program end *************************** ' ' '****************************************************************************** '* Procedures to manage I2C BUS hardware * '****************************************************************************** ' ' ' ' 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 specific procedures * '****************************************************************************** ' ' ' Initializes demo ' Sub Init() ' Initializes directionality of ports that drive optocouped inputs and relay ' outputs: ' IN1-1 - Portb.7 ' IN2-1 - Portb.6 ' IN3-1 - Porte.4 ' IN4-1 - Porte.5 ' IN5-1 - Portd.7 ' IN6-1 - Porte.6 ' IN7-1 - Portb.4 ' IN8-1 - Portb.0 Ddrb.7 = 0 Ddrb.6 = 0 Ddre.4 = 0 Ddre.5 = 0 Ddrd.7 = 0 Ddre.6 = 0 Ddrb.4 = 0 Ddrb.0 = 0 ' IN1-2 - Portc.0 ' IN2-2 - Portc.1 ' IN3-2 - Portc.2 ' IN4-2 - Portc.3 ' IN5-2 - Portc.4 ' IN6-2 - Portc.5 ' IN7-2 - Portc.6 ' IN8-2 - Portc.7 Ddrc = 0 ' OUT A1 - Portf.4 ' OUT A2 - Portf.5 ' OUT B1 - Portf.6 ' OUT B2 - Portf.7 ' OUT C1 - Portb.3 ' OUT C2 - Portb.2 ' OUT D1 - Portb.1 ' OUT D2 - Porte.2 (by Default , that is When J10 is connected in 3-4) Ddrf.4 = 1 Ddrf.5 = 1 Ddrf.6 = 1 Ddrf.7 = 1 Ddrb.3 = 1 Ddrb.2 = 1 Ddrb.1 = 1 Ddre.2 = 1 End Sub ' ' ' 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 ' '****************************************************************************** '* Generic 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 ' ' ' 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