$regfile = "AVRX128db64.dat"
$hwstack = 16
$swstack = 40
$framesize = 40
$crystal = 12288000
$initmicro
'Clock config (external HF quartz, CLK out enabled on PORTA.7)
Config Osc = Enabled , XOSCHF=enabled, XOSCHF_RUNMODE=enabled, XOSCHF_SEL_STARTUP=XTAL_4KCLK, XOSCHF_EXT_SRC=EXT_XTAL, XOSCHF_RANGE=MAX_16MHZ
CONFIG SYSCLOCK=EXTERNAL, PRESCALE=1, CLOCKOUT = Enabled, CLOCKOUT_PIN=output
'UART config.
'COMs ports have offset of number by 1. Looking into AVR DB datasheet USARTn n=2, is COMn, where n=n+1.
'Default output port for 2nd USART is PORTF.0 hower you can change routing according to datasheet (marked as alternative pin positions).
'Adding sentence in config TXPIN=ALT1_PF4, Tx will be on PORTF.4 not PORTF.0.
CONFIG COM3 = 9600 , Mode = ASYNCHRONEOUS, Parity=none, Stopbits=1, Databits=8 , Baud_Offset=none , TXPIN=ALT1_PF4
Open "Com3:" For Binary As #3
echo off
Enable Interrupts
'TWI config.
'AVR128DB64 has 2 TWI (TWI0 and TWI1) ports. In this case we would like to use TWI1 (PORTF.2 and PORTF.3)
Config TWI1=400000
Open "twi1" For Binary As #1
Dim Twi_start As Byte
Dim B as byte
'Below we would like to know, which devices are connected to our bus. Results are into terminal (Print #3, because we configured COM3 as #3)
'The same story for TWI. Commands corelated with this TWI must include #1 at the end. For data which need to be send also.
Print #3, "Scan start"
For B = 0 To 254 Step 2 'for all odd addresses
I2cstart #1 'send start
I2cwbyte B , #1 'send address
If Err = 0 Then 'we got an ack
Print #3, "Slave at : " ; B ; " hex : " ; Hex(b) ; " bin : " ; Bin(b)
End If
I2cstop #1 'free bus
Next
'MCP23008-E/SO use 8bit registers.
Dim EXP_IO as byte
'We can divide EXP_IO variable into 8 independent bits. It will be easier (EXP_IO.x = one physical pin)
'Set initial ports values (outputs) to write via I2C to MCP.
EXP_IO.0=0
EXP_IO.1=0
EXP_IO.2=0
EXP_IO.3=0
EXP_IO.4=0
EXP_IO.5=0
EXP_IO.6=0
EXP_IO.7=0
'We can also set aliases
LED1 alias EXP_IO.7
'2 MCP's registers need to be configured to get outputs only.
I2cstart #1
I2cwbyte &H40, #1 'MCP Adress (A0-A2 = 0)
I2cwbyte &H00, #1 'IODIR Register
I2cwbyte &H00, #1 'All pins as output
I2cstop #1
I2cstart #1
I2cwbyte &H40, #1 ' MCP Adress (A0-A2 = 0)
I2cwbyte &H0A, #1 ' OLAT Register
I2cwbyte EXP_IO, #1 'Write EXP_IO to MCP IC.
I2cstop #1
Do
Print #3, "Main loop"
LED1=1
Gosub MCP_Update 'We need to update MCP registers.
waitms 200
LED1=0
Gosub MCP_Update
waitms 200
Loop
Close #3
End
'MCP's register update.
MCP_Update:
I2cstart #1
I2cwbyte &H40, #1 ' MCP Adress (A0-A2 = 0)
I2cwbyte &H0A, #1 ' OLAT Register
I2cwbyte EXP_IO, #1 'Write EXP_IO to MCP IC.
I2cstop #1
Return
_init_micro
Config PortF.4 = output 'TXD
Return
|