Jump to content

Recommended Posts

  • 10 years later...
Posted

Hallo, ich bin neu bei dieses Forum.

Ich habe vor 3-4 Monaten angefangen mit Tinkerforge und Raspi zu arbeiten und möchte ein Sensor Simulator entwickeln als I2C Slave und wäre sehr interessiert an der gelinkte Seite. Leider ist der oben hinzugefügte Thread-link schon gelöscht worden.

Kann jemand mir helfen?

 

 

Posted
#!/usr/bin/env python
# -*- coding: utf-8 -*-  

HOST = "localhost"
PORT = 4223
uid_io16 = 'xxx';

import time

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_io16 import IO16

i2c_io16_port='a'
#SCL = Port0
scl_port=0
#SDA = Port1
sda_port=1

#Adresse: 01110000 bis 01110111

def PortWert(bitstring=0, port=0):
    #ermittelt den Wert eines(!) Input-Ports anhand der kompletten Bitmaske
    wert =bitstring & (1<<port)
    wert =wert>>port
    return wert

def send_start():
    #Start: SDA und SCL = high, SCL geht auf low
    io.set_port(i2c_io16_port,(1 << scl_port) | (1 << sda_port))
    io.set_port(i2c_io16_port,(1 << scl_port) | (0 << sda_port))
    io.set_port(i2c_io16_port,(0 << scl_port) | (0 << sda_port))        
    #Start Ende
    
def send_stop():
    #Stop: SDA und SCL = high, SCL geht auf low
    io.set_port(i2c_io16_port,(1 << scl_port) | (0 << sda_port))
    io.set_port(i2c_io16_port,(1 << scl_port) | (1 << sda_port))
    #Stop Ende
    
def send_ack():
    #Acknowledge
    io.set_port(i2c_io16_port,(1 << scl_port) | (1 << sda_port))
    io.set_port(i2c_io16_port,(0 << scl_port) | (0 << sda_port))        
    #Acknowledge Ende

def send_dat(daten):
    #Daten
    for i in range(7,-1,-1): 
        #Bit
        io.set_port(i2c_io16_port,(1 << scl_port) | (PortWert(daten, i) << sda_port))
        io.set_port(i2c_io16_port,(0 << scl_port) | (0 << sda_port))
        #Daten Ende                
    send_ack()

if __name__ == "__main__":
    ipcon = IPConnection() # Create IP connection
    io = IO16(uid_io16, ipcon) # Create device object
    ipcon.connect(HOST, PORT) # Connect to brickd   
    # Don't use device before it is added to a connection
    
    #Ports als Ausgang definieren und erstmal low
    io.set_port_configuration(i2c_io16_port, (1 << scl_port) | (1 << sda_port), 'o', False)
    
    send_start()   
    #Adresse 1110000r/w (r/w=1 für Write) ===> Adresse: 0b11100001
    send_dat(0b11100001)    
    #send_dat(0xD9)          #TestMode
    send_dat(0x21)          #turn on oscillator
    send_dat(0x7F)
    send_dat(0b11101111)    #Display volle Helligkeit
    send_stop()
    
    pause=0.05
    #time.sleep(pause)

    ipcon.disconnect()

Das ist der Code von damals mit python. SDA und SCL werden einfach als Ausgang am IO bedient.

Der Code müsste noch angepasst werden: neues IO-Bricklet, Adresse, etc...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...