From c0aad15b28eefba7d4982d7848786cae48bc9839 Mon Sep 17 00:00:00 2001 From: Daniel Faust Date: Wed, 2 Oct 2013 16:08:43 +0200 Subject: [PATCH] Initial dump. --- .gitattributes | 22 ++++++ .gitignore | 163 +++++++++++++++++++++++++++++++++++++++++ I2C_Slave_Tiny.ino | 179 +++++++++++++++++++++++++++++++++++++++++++++ rpi.py | 48 ++++++++++++ 4 files changed, 412 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 I2C_Slave_Tiny.ino create mode 100644 rpi.py diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..412eeda --- /dev/null +++ b/.gitattributes @@ -0,0 +1,22 @@ +# Auto detect text files and perform LF normalization +* text=auto + +# Custom for Visual Studio +*.cs diff=csharp +*.sln merge=union +*.csproj merge=union +*.vbproj merge=union +*.fsproj merge=union +*.dbproj merge=union + +# Standard to msysgit +*.doc diff=astextplain +*.DOC diff=astextplain +*.docx diff=astextplain +*.DOCX diff=astextplain +*.dot diff=astextplain +*.DOT diff=astextplain +*.pdf diff=astextplain +*.PDF diff=astextplain +*.rtf diff=astextplain +*.RTF diff=astextplain diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ebd21a --- /dev/null +++ b/.gitignore @@ -0,0 +1,163 @@ +################# +## Eclipse +################# + +*.pydevproject +.project +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.classpath +.settings/ +.loadpath + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# CDT-specific +.cproject + +# PDT-specific +.buildpath + + +################# +## Visual Studio +################# + +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.sln.docstates + +# Build results +[Dd]ebug/ +[Rr]elease/ +*_i.c +*_p.c +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.vspscc +.builds +*.dotCover + +## TODO: If you have NuGet Package Restore enabled, uncomment this +#packages/ + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf + +# Visual Studio profiler +*.psess +*.vsp + +# ReSharper is a .NET coding add-in +_ReSharper* + +# Installshield output folder +[Ee]xpress + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish + +# Others +[Bb]in +[Oo]bj +sql +TestResults +*.Cache +ClientBin +stylecop.* +~$* +*.dbmdl +Generated_Code #added for RIA/Silverlight projects + +# Backup & report files from converting an old project file to a newer +# Visual Studio version. Backup files are not needed, because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML + + + +############ +## Windows +############ + +# Windows image file caches +Thumbs.db + +# Folder config file +Desktop.ini + + +############# +## Python +############# + +*.py[co] + +# Packages +*.egg +*.egg-info +dist +build +eggs +parts +bin +var +sdist +develop-eggs +.installed.cfg + +# Installer logs +pip-log.txt + +# Unit test / coverage reports +.coverage +.tox + +#Translations +*.mo + +#Mr Developer +.mr.developer.cfg + +# Mac crap +.DS_Store diff --git a/I2C_Slave_Tiny.ino b/I2C_Slave_Tiny.ino new file mode 100644 index 0000000..18ac5da --- /dev/null +++ b/I2C_Slave_Tiny.ino @@ -0,0 +1,179 @@ + +#define F_CPU 8000000 + +//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +#include "TinyWireS.h" +//#include "usiTwiSlave.h" + +//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +#define I2C_SLAVE_ADDR 0x2F +#define PIN_2__PB_3 3 // ATtiny Pin 2 // used by adc3 +#define PIN_3__PB_4 4 // ATtiny Pin 3 // used as pwm +#define PIN_6__PB_1 1 // ATtiny Pin 6 + +//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +bool bTrigger[] = {false, false}; +unsigned int uiPwm[] = {0, 0}; +uint8_t u8Received = 0; +uint8_t u8Command = 0; + +//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +void initADC() { + + ADCSRA = (1 << ADPS2) | // set prescaler to 64, bit 2 ~~ for 8MHz below is valid + (1 << ADPS1) | // set prescaler to 64, bit 1 ~~ 1 1 0 = ps64 = 125kHz + (0 << ADPS0); // set prescaler to 64, bit 0 ~~ 1 1 1 = ps128 = 62.5kHz + + ADCSRA |= (1 << ADATE); // set auto trigger enable + ADCSRA |= (0 << ADTS2) | // set free running mode, bit 2 + (0 << ADTS1) | // set free running mode, bit 1 + (0 << ADTS0); // set free running mode, bit 0 + + + ADMUX = (1 << REFS2) | // Sets ref. voltage to VCC, bit 3 ~~ i2c makes use of ADREF pin, only internal vref can be used + (1 << REFS1) | // Sets ref. voltage to VCC, bit 1 ~~ 0 1 0 Internal 1.1V Voltage Reference. + (0 << REFS0); // Sets ref. voltage to VCC, bit 0 ~~ 1 1 0 Internal 2.56V Voltage Reference without external bypass capacitor, disconnected from PB0 (AREF) + + ADMUX = (0 << MUX3) | // MUX bit 3 ~~ 0 0 0 0 = ADC0 --> PB4 | Pin3, needs RESET fuse disabled, mcu can then not be reprogrammed. + (0 << MUX2) | // MUX bit 2 ~~ 0 0 0 1 = ADC1 --> PB2 | Pin7 + (1 << MUX1) | // MUX bit 1 ~~ 0 0 1 0 = ADC2 --> PB4 | Pin3 + (1 << MUX0); // MUX bit 0 ~~ 0 0 1 1 = ADC3 --> PB3 | Pin2 + + ADCSRA |= (1 << ADEN); // Enable ADC + + ADCSRA |= (1 << ADSC); // start ADC measurement + ADCL;ADCH; // The first ADC conversion result after switching voltage reference source may be inaccurate, and the user is advised to discard this result. +} + +//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +void initPWM() { + + //DDRB |= (1 << PB4) | (1 << PB1); // same as pinMode(PIN_3__PB_4, OUTPUT); ?? + + // PWM pins are 3 (OC1B), 5 (OC0A), and 6 (OC0B) + + TCCR0A = + (0 << COM0A1) | // set OC0A on compare match, clear at BOTTOM + (0 << COM0A0) | // set OC0A on compare match, clear at BOTTOM + (1 << COM0B1) | // set OC0B on compare match, clear at BOTTOM + (0 << COM0B0) | // set OC0B on compare match, clear at BOTTOM + //(-) + //(-) + (1 << WGM01) | // 0 1 1 = Fast PWM mode (1/3) + (1 << WGM00); // 0 1 1 = Fast PWM mode (2/3) + + // 1001 0001 + + TCCR0B = + (0 << FOC0A) | + (0 << FOC0B) | + //(-) + //(-) + (0 << WGM02) | // 0 1 1 = Fast PWM mode (3/3) + (1 << CS02) | + (0 << CS01) | + (0 << CS00); + // 0 0 1 -- No prescaling) + // 0 1 0 -- 8 (From prescaler) + // 0 1 1 -- 64 (From prescaler) + // 1 0 0 -- 256 (From prescaler) + // 1 0 1 -- 1024 (From prescaler) + + TCCR1 = 0< + /* //<--- Free Running Conversion + ADMUX = (0 << MUX3) | // MUX bit 3 ~~ 0 0 0 0 = ADC0 --> PB4 | Pin3, needs RESET fuse disabled, mcu can then not be reprogrammed. + (0 << MUX2) | // MUX bit 2 ~~ 0 0 0 1 = ADC1 --> PB2 | Pin7 + (1 << MUX1) | // MUX bit 1 ~~ 0 0 1 0 = ADC2 --> PB4 | Pin3 + (1 << MUX0); // MUX bit 0 ~~ 0 0 1 1 = ADC3 --> PB3 | Pin2 + ADCSRA |= (1 << ADSC); // start ADC measurement, needed when auto trigger enable is 0 (= single conversion) + */ //<--- Free Running Conversion + // <--- Single Conversion on ADC 2 + + /* + TinyWireS.send(ADCL); // read lower byte + TinyWireS.send(ADCH); // read upper byte + */ + + TinyWireS.send(lowByte(uiPwm[0])); + TinyWireS.send(highByte(uiPwm[0])); + } + //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + else if (u8Command == 0x04) { + /* + // single conversion on ADC 0 ---> + / * //<--- Free Running Conversion + ADMUX = (0 << MUX3) | // MUX bit 3 ~~ 0 0 0 0 = ADC0 --> PB4 | Pin3, needs RESET fuse disabled, mcu can then not be reprogrammed. + (0 << MUX2) | // MUX bit 2 ~~ 0 0 0 1 = ADC1 --> PB2 | Pin7 + (0 << MUX1) | // MUX bit 1 ~~ 0 0 1 0 = ADC2 --> PB4 | Pin3 + (0 << MUX0); // MUX bit 0 ~~ 0 0 1 1 = ADC3 --> PB3 | Pin2 + ADCSRA |= (1 << ADSC); // start ADC measurement, needed when auto trigger enable is 0 (= single conversion) + * / //<--- Free Running Conversion + // <--- single conversion on ADC 0 + + TinyWireS.send(ADCL); // read lower byte + TinyWireS.send(ADCH); // read upper byte + */ + + TinyWireS.send(lowByte(uiPwm[1])); + TinyWireS.send(highByte(uiPwm[1])); + } + //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + } +} diff --git a/rpi.py b/rpi.py new file mode 100644 index 0000000..510a4e9 --- /dev/null +++ b/rpi.py @@ -0,0 +1,48 @@ + + +if 'counter' not in self.storage: + self.storage['counter'] = 0 + +import smbus +bus = smbus.SMBus(1) +addr = 0x2F + +try: + + responses = [] + + print 'writing to', addr + + increment = 1 + #self.storage['counter'] = 0 + if self.storage['counter'] > 255: + self.storage['counter'] = 32 + + for i in range(1): + time.sleep(0.01) + val1 = self.storage['counter'] + self.storage['counter'] += increment + ################################################################################### + bus.write_byte(addr, 0x01) + bus.write_byte(addr, val1) + bus.write_byte(addr, val1 >> 8) + ################################################################################### + bus.write_byte(addr, 0x02) + bus.write_byte(addr, val1) + bus.write_byte(addr, val1 >> 8) + ################################################################################### + time.sleep(0.01) + adc0 = bus.read_word_data(addr, 0x03) + responses.append(str(adc0)) + ################################################################################### + time.sleep(0.01) + adc1 = bus.read_word_data(addr, 0x04) + responses.append(str(adc1)) + ################################################################################### + + return 'ok
' + ' | '.join(responses) + +except: + + traceback.print_exc() + return 'err: ' + str('')