workshop:통신
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| workshop:통신 [2012/08/13 14:03] – donghee | workshop:통신 [2018/07/18 14:10] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| IC와 통신 하기 | IC와 통신 하기 | ||
| - | 1. UART | + | * [[/UART]] |
| - | 2. | + | * [[/OneWire]] |
| - | + | * [[/I2C]] | |
| - | ====== Uart 통신 ====== | + | * [[/SPI]] |
| - | + | ||
| - | 컴퓨터와 Uart를 통해서 통신을 해보자. UART 디버깅 할때 유용함. | + | |
| - | + | ||
| - | ==== Python 설치 ==== | + | |
| - | | + | |
| - | * Pyserial Win32 (pyserial-2.5.win32.exe) google에서 pyserial win32 검색 | + | |
| - | + | ||
| - | {{http:// | + | |
| - | ==== arduino code ==== | + | |
| - | + | ||
| - | {{{ | + | |
| - | int ledPin = 13; | + | |
| - | int value; | + | |
| - | + | ||
| - | void setup() { | + | |
| - | pinMode(ledPin, | + | |
| - | Serial.begin(9600); | + | |
| - | } | + | |
| - | + | ||
| - | void loop() { | + | |
| - | if (Serial.available() > 0) { | + | |
| - | value = Serial.read(); | + | |
| - | if (value == ' | + | |
| - | digitalWrite(ledPin, | + | |
| - | } else { | + | |
| - | digitalWrite(ledPin, | + | |
| - | } | + | |
| - | } | + | |
| - | } | + | |
| - | }}} | + | |
| - | + | ||
| - | ==== pyserial ==== | + | |
| - | pyserial을 설치하고, | + | |
| - | + | ||
| - | {{{ | + | |
| - | >>> | + | |
| - | >>> | + | |
| - | >>> | + | |
| - | 1 | + | |
| - | >>> | + | |
| - | 1 | + | |
| - | >>> | + | |
| - | }}} | + | |
| - | + | ||
| - | ==== python gui button ==== | + | |
| - | + | ||
| - | {{{ | + | |
| - | from Tkinter import | + | |
| - | import sys | + | |
| - | + | ||
| - | import serial | + | |
| - | port = '/ | + | |
| - | speed = 9600 | + | |
| - | + | ||
| - | def send(char): | + | |
| - | ser = serial.Serial(port, | + | |
| - | ser.setDTR() | + | |
| - | ser.flushInput() | + | |
| - | ser.write(char) | + | |
| - | ser.close() | + | |
| - | + | ||
| - | def toggle_switch(): | + | |
| - | if switch_button[" | + | |
| - | send(' | + | |
| - | switch_button[" | + | |
| - | | + | |
| - | send(' | + | |
| - | switch_button[" | + | |
| - | + | ||
| - | root = Tk() | + | |
| - | root.title(' | + | |
| - | switch_button = Button(root, | + | |
| - | switch_button.pack() | + | |
| - | + | ||
| - | root.mainloop() | + | |
| - | }}} | + | |
| - | + | ||
| - | ---- | + | |
| 칩간에 통신을 실험해보자! | 칩간에 통신을 실험해보자! | ||
| - | ====== WS2801 ====== | ||
| - | |||
| - | LED 드라이버 | ||
| - | |||
| - | 회로도 | ||
| - | {{http:// | ||
| - | |||
| - | Time | ||
| - | |||
| - | {{http:// | ||
| - | |||
| - | {{{ | ||
| - | int SDI = 11; // PIN 11 | ||
| - | int CKI = 12; // PIN 12 | ||
| - | int ledPin = 13; // LED | ||
| - | |||
| - | void setup() { | ||
| - | pinMode(SDI, | ||
| - | pinMode(CKI, | ||
| - | pinMode(ledPin, | ||
| - | | ||
| - | // | ||
| - | // | ||
| - | } | ||
| - | |||
| - | void loop() { | ||
| - | delay(2000); | ||
| - | |||
| - | while(1){ | ||
| - | post_frame(0xFF0000); | ||
| - | |||
| - | // CLK pin keeps low more than 500uS will make the WS2801 internal status register reset | ||
| - | digitalWrite(CKI, | ||
| - | delayMicroseconds(500); | ||
| - | | ||
| - | digitalWrite(ledPin, | ||
| - | delay(250); | ||
| - | digitalWrite(ledPin, | ||
| - | delay(250); | ||
| - | } | ||
| - | } | ||
| - | |||
| - | // this_led_color값의 비트를 ws2801에 CKI에 맞추어 SDI를 밀어 넣는다. | ||
| - | void post_frame (long this_led_color) { | ||
| - | for(byte color_bit = 23 ; color_bit != 255 ; color_bit--) { | ||
| - | // | ||
| - | | ||
| - | digitalWrite(CKI, | ||
| - | | ||
| - | long mask = 1L << color_bit; | ||
| - | //The 1' | ||
| - | | ||
| - | if(this_led_color & mask) | ||
| - | digitalWrite(SDI, | ||
| - | else | ||
| - | digitalWrite(SDI, | ||
| - | | ||
| - | digitalWrite(CKI, | ||
| - | } | ||
| - | } | ||
| - | }}} | ||
| - | |||
| - | 여러개 보낼경우. 이어서 보낸다. | ||
| - | |||
| - | {{{ | ||
| - | post_frame(0xFF0000); | ||
| - | post_frame(0x00FF00); | ||
| - | // CLK pin keeps low more than 500uS will make the WS2801 internal status register reset | ||
| - | digitalWrite(CKI, | ||
| - | delayMicroseconds(500); | ||
| - | | ||
| - | }}} | ||
| - | |||
| - | ====== I2C ====== | ||
| - | I2C직렬 컴퓨터 버스주변기기를 연결하기 위해 사용된다. | ||
| - | |||
| - | {{http:// | ||
| - | |||
| - | 데이터와 클럭은 pull-up | ||
| - | |||
| - | {{http:// | ||
| - | |||
| - | Time | ||
| - | |||
| - | {{http:// | ||
| - | |||
| - | {{http:// | ||
| - | |||
| - | 실제 실험 해보기 | ||
| - | 24LC256 | ||
| - | |||
| - | arduino i2c 관련 핀 | ||
| - | * Analog4 - SDA | ||
| - | * Analog5 - SCL | ||
| - | |||
| - | Time | ||
| - | |||
| - | {{http:// | ||
| - | |||
| - | {{{ | ||
| - | #include < | ||
| - | |||
| - | void i2c_eeprom_write_byte( int deviceaddress, | ||
| - | int rdata = data; | ||
| - | |||
| - | Wire.beginTransmission(deviceaddress); | ||
| - | Wire.write((int)(eeaddress >> 8)); // MSB | ||
| - | Wire.write((int)(eeaddress & 0xFF)); // LSB | ||
| - | Wire.write(rdata); | ||
| - | Wire.endTransmission(); | ||
| - | } | ||
| - | |||
| - | void i2c_eeprom_write_page( int deviceaddress, | ||
| - | Wire.beginTransmission(deviceaddress); | ||
| - | Wire.write((int)(eeaddresspage >> 8)); // MSB | ||
| - | Wire.write((int)(eeaddresspage & 0xFF)); // LSB | ||
| - | |||
| - | byte c; | ||
| - | for ( c = 0; c < length; c++) | ||
| - | Wire.write(data[c]); | ||
| - | |||
| - | Wire.endTransmission(); | ||
| - | } | ||
| - | |||
| - | byte i2c_eeprom_read_byte( int deviceaddress, | ||
| - | byte rdata = 0xFF; | ||
| - | |||
| - | Wire.beginTransmission(deviceaddress); | ||
| - | Wire.write((int)(eeaddress >> 8)); // MSB | ||
| - | Wire.write((int)(eeaddress & 0xFF)); // LSB | ||
| - | Wire.endTransmission(); | ||
| - | Wire.requestFrom(deviceaddress, | ||
| - | |||
| - | if (Wire.available()) rdata = Wire.read(); | ||
| - | |||
| - | return rdata; | ||
| - | } | ||
| - | |||
| - | void setup() { | ||
| - | char data[] = "hello world"; | ||
| - | Wire.begin(); | ||
| - | Serial.begin(9600); | ||
| - | i2c_eeprom_write_page(0x50, | ||
| - | |||
| - | delay(10); //add a small delay | ||
| - | |||
| - | Serial.println(" | ||
| - | } | ||
| - | |||
| - | void loop() { | ||
| - | int addr=0; //EEPROM Address 0 | ||
| - | byte b = i2c_eeprom_read_byte(0x50, | ||
| - | |||
| - | while (b!=0) { | ||
| - | | ||
| - | | ||
| - | b = i2c_eeprom_read_byte(0x50, | ||
| - | } | ||
| - | |||
| - | Serial.println(" | ||
| - | delay(2000); | ||
| - | } | ||
| - | }}} | ||
| - | |||
| - | master가 slave에 데이터를 보낸다. | ||
| - | |||
| - | {{http:// | ||
| - | |||
| - | master가 slave의 데이터를 읽는다. | ||
| - | |||
| - | {{http:// | ||
workshop/통신.1344866585.txt.gz · Last modified: 2018/07/18 14:09 (external edit)