purpose:
measuring the timespan between rising/falling-edge (=off/on/off-change) of a digital-output-bank (GPA0…GPA7
)
testing equipment:
-Raspberry Pi 3 Modell B
-I2C-adress of MCP23017-chip = 0x20
-MCP23017 register-adress of 8 digital-outputs: 0x14
-Owon SDS7102 digital ocsilloscope
conclusion:
I²C-baudrate | bash | Python | C |
---|---|---|---|
100.000 | 5,18 ms @ 75 kHz SCL | 0,51 ms @ 62,5 kHz SCL | 0,65 ms Hz @ 62,5 kHz SCL |
400.000 | 4,85 ms @ 300 kHz SCL | 0,13 ms @ 300 kHz SCL | 0,155 ms @ 300 kHz SCL |
1.700.000 | no toggling @ 1.064 kHz SCL |
- baudrate=100.000: clockspeed varies between different codes
- measured clockspeed was always lower than the set baudrate
- the higher the baudrate, the less the on-off-timespan
- baudrate=1.7M: the digital outputs aren’t able to toogle so fast
code examples:
manipulate I²C-baudrate: (e.g. to baudrate=400.000)
- edit:
/boot/config.txt
- change:
dtparam=i2c_arm=on,i2c_arm_baudrate=400000
- reboot (without rebooting it won’t work)
Bash: the following packages must be installed: i2c-dev
#!/bin/bash
i2cset -y 1 0x20 0x00 0x00;
while true;
do
i2cset -y 1 0x20 0x14 0xff;
i2cset -y 1 0x20 0x14 0x00;
done
Python: the following packages must be installed: python-smbus
#!/usr/bin/python
import smbus
mcp23017 = smbus.SMBus(1)
mcp23017.write_byte_data(0x20,0x00,0x00)
while True:
mcp23017.write_byte_data (0x20,0x14,0xff)
mcp23017.write_byte_data (0x20,0x14,0x00)
C: the following packages must be installed: i2c-dev
#include <stdio.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/i2c-dev.h>
#include <i2c/smbus.h>
int main(){
int file = open("/dev/i2c-1", I2C_RDWR);
ioctl(file, I2C_SLAVE, 0x20);
i2c_smbus_write_word_data(file, 0x00, 0x00);
for (;;) {
i2c_smbus_write_word_data(file, 0x14, 0xFF);
i2c_smbus_write_word_data(file, 0x14, 0x00);
}
}