++++++++++ BASCOM Quellcode +++++++++++++++++++++++ $regfile = "M32def.dat" $crystal = 16000000 $lib "i2c_twi.lbx" Config Scl = Portc.0 Config Sda = Portc.1 I2cinit Config Twi = 100000 I2cstart I2cwbyte &H40 I2cwbyte &H0F I2cstop Do Loop End ++++++++++ Assembler Quellcode +++++++++++++++++++ .DEVICE ATmega32 .INCLUDE "m32def.inc" ; *** Konstanten *** .equ SCL = PC0 .equ SDA = PC1 .equ dev_read = 0b01000001 .equ dev_write = 0b01000000 .equ data = 0b00001111 .equ bit_anz = 0x08 ; *** Variablen *** .def temp = R18 .def counter = R17 .def buffer = R16 ; *** Programm *** .cseg .org 0x00 rjmp main ; +++ I2C-Init +++ init: sbi PORTC,SDA sbi PORTC,SCL ldi temp,0b00000000 ; laden mit 0b00000011 ändert auch nichts out DDRC,temp ret ; +++ I2C-Start-Sequenz +++ ; ; Anfangszustand: SDA = High, SCL = High ; Endzustand: SDA = Low, SCL = Low pstart: sbi PORTC,SDA sbi PORTC,SCL rcall wait5 cbi PORTC,SDA rcall wait5 cbi PORTC,SCL ret ; +++ I2C-Stop-Sequenz +++ ; ; Anfangszustand: SDA = Low, SCL = Low ; Endzustand: SDA = High, SCL = High pstop: cbi PORTC,SCL cbi PORTC,SDA rcall wait5 sbi PORTC,SCL rcall wait5 sbi PORTC,SDA ret ; +++ I2C-RX-ACK Polling +++ ; ; Anfangszustand: SDA = Low, SCL = Low ; Endzustand: SDA = Low, SCL = Low rx_ack: cbi PORTC,SCL sbi PORTC,SDA nop nop sbi PORTC,SCL warte: sbic PINC,SDA rjmp warte nop nop cbi PORTC,SCL cbi PORTC,SDA ret ; +++ I2C-Byte-Out Sequenz +++ byte_out: ldi counter,bit_anz cbi PORTC,SCL next_out: sbi PORTC,SDA rol buffer brcs wr_one cbi PORTC,SDA wr_one: sbi PORTC,SCL rcall wait5 cbi PORTC,SCL rcall wait5 dec counter brne next_out cbi PORTC,SDA ret ; +++ Pause 5u Sec +++ wait5: push temp ldi temp,23 wdh1: dec temp brne wdh1 pop temp ret ; +++ Hauptprogramm +++ main: ldi temp,HIGH(RAMEND) ; Stackpointer setzen out SPH,temp ldi temp,LOW(RAMEND) out SPL,temp rcall init ; Initialisierung rcall pstart ; Start-Sequenz ldi buffer,dev_write ; Adressierung rcall byte_out rcall rx_ack ldi buffer,data ; Datenausgabe rcall byte_out rcall rx_ack rcall pstop ; Stop-Sequenz endloop: rjmp endloop .exit