User Tools

Site Tools


workshop:통신

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
workshop:통신 [2012/08/13 14:03] dongheeworkshop:통신 [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 설치 ==== +
-  Python 2.7.3 http://python.org/download+
-  Pyserial Win32  (pyserial-2.5.win32.exe) google에서 pyserial win32 검색 +
- +
-{{http://i.imgur.com/1wwVJ.png}} +
-==== arduino code ==== +
- +
-{{{ +
-int ledPin = 13; +
-int value; +
- +
-void setup() { +
-  pinMode(ledPin, OUTPUT);    +
-  Serial.begin(9600); +
-+
- +
-void loop() { +
-  if (Serial.available() > 0) { +
-    value = Serial.read(); +
-    if (value == '1') {  +
-    digitalWrite(ledPin, HIGH); +
-    } else { +
-    digitalWrite(ledPin, LOW); +
-    } +
-  } +
-+
-}}} +
- +
-==== pyserial ==== +
-pyserial을 설치하고, 파이썬 인터프리터에서 다음과 같이 입력해보자. 어떻게 될지 예상해보자. +
- +
-{{{ +
->>> import serial +
->>> s = serial.Serial('/dev/ttyUSB0'+
->>> s.write('1'+
-+
->>> s.write('0'+
-+
->>>  +
-}}} +
- +
-==== python gui button ==== +
- +
-{{{ +
-from Tkinter import * +
-import sys +
- +
-import serial +
-port = '/dev/ttyUSB0'  # TODO: change port name +
-speed = 9600 +
- +
-def send(char): +
-    ser = serial.Serial(port,speed) +
-    ser.setDTR() +
-    ser.flushInput() +
-    ser.write(char) +
-    ser.close() +
- +
-def toggle_switch(): +
-    if switch_button["text"] == "Turn On": +
-        send('1'+
-        switch_button["text"= "Trun Off" +
-    else: +
-        send('0'+
-        switch_button["text"= "Turn On" +
- +
-root = Tk() +
-root.title('Switch'+
-switch_button = Button(root, text="Turn On",command=toggle_switch) +
-switch_button.pack() +
- +
-root.mainloop() +
-}}} +
- +
-----+
  
 칩간에 통신을 실험해보자! 칩간에 통신을 실험해보자!
  
-====== WS2801 ====== 
- 
-LED 드라이버 
- 
-회로도 
-{{http://i.imgur.com/v1Tol.png?300}} 
- 
-Time 
- 
-{{http://i.imgur.com/zG6jm.png?400}} 
- 
-{{{ 
-int SDI = 11; // PIN 11 
-int CKI = 12; // PIN 12 
-int ledPin = 13; // LED 
- 
-void setup() { 
-  pinMode(SDI, OUTPUT); 
-  pinMode(CKI, OUTPUT); 
-  pinMode(ledPin, OUTPUT); 
-   
-  //Serial.begin(9600); 
-  //Serial.println("Hello!"); 
-} 
- 
-void loop() { 
-  delay(2000); 
- 
-  while(1){  
-    post_frame(0xFF0000); //현재 칼라를 WS2801에 적용한다. 
- 
-    // CLK pin keeps low more than 500uS will make the WS2801 internal status register reset 
-    digitalWrite(CKI, LOW); 
-    delayMicroseconds(500); 
-     
-    digitalWrite(ledPin, HIGH);     
-    delay(250);                   
-    digitalWrite(ledPin, LOW);    
-    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--) { 
-      //23비트가 처음! 빨간색 (red data MSB) 
-       
-      digitalWrite(CKI, LOW); //클럭이 LOW일때 데이터를 받아 들인다. 
-       
-      long mask = 1L << color_bit; 
-      //The 1'L' forces the 1 to start as a 32 bit number, otherwise it defaults to 16-bit. 
-       
-      if(this_led_color & mask)  
-        digitalWrite(SDI, HIGH); 
-      else 
-        digitalWrite(SDI, LOW); 
-   
-      digitalWrite(CKI, HIGH); //클럭이 HIGH일때 데이터를 적용(latch) 한다. 
-  } 
-} 
-}}} 
- 
-여러개 보낼경우. 이어서 보낸다. 
- 
-{{{ 
-post_frame(0xFF0000); //첫번째 프레임 
-post_frame(0x00FF00); //두번째 프레임 
-// CLK pin keeps low more than 500uS will make the WS2801 internal status register reset 
-digitalWrite(CKI, LOW); 
-delayMicroseconds(500); 
-   
-}}} 
- 
-====== I2C ====== 
-I2C 직렬 컴퓨터 버스 주변기기를 연결하기 위해 사용된다. 
- 
-{{http://i.imgur.com/99a3Z.png?300}} 
- 
-데이터와 클럭은 pull-up 
- 
-{{http://i.imgur.com/U3al8.png?300}} 
- 
-Time 
- 
-{{http://i.imgur.com/NtRbM.png?300}} 
- 
-{{http://i.imgur.com/dgtdQ.png?300}} 
- 
-실제 실험 해보기 
-24LC256 
- 
-arduino i2c 관련 핀 
-  * Analog4 - SDA 
-  * Analog5 - SCL 
- 
-Time 
- 
-{{http://i.imgur.com/VQtTJ.png?300}} 
- 
-{{{ 
-#include <Wire.h> //Include the Arduino I2C Library 
- 
-void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data ) { 
-  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, unsigned int eeaddresspage, byte* data, byte length ) { 
-  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, unsigned int eeaddress ) { 
-  byte rdata = 0xFF; 
- 
-  Wire.beginTransmission(deviceaddress); 
-  Wire.write((int)(eeaddress >> 8)); // MSB 
-  Wire.write((int)(eeaddress & 0xFF)); // LSB 
-  Wire.endTransmission(); 
-  Wire.requestFrom(deviceaddress,1); 
- 
-  if (Wire.available()) rdata = Wire.read(); 
- 
-  return rdata; 
-} 
- 
-void setup() { 
-  char data[] = "hello world"; 
-  Wire.begin(); //Start I2C connections 
-  Serial.begin(9600); 
-  i2c_eeprom_write_page(0x50, 0, (byte *)data, sizeof(data)); // write to EEPROM 
- 
-  delay(10); //add a small delay 
- 
-  Serial.println("Memory written"); 
-} 
- 
-void loop() { 
-  int addr=0; //EEPROM Address 0 
-  byte b = i2c_eeprom_read_byte(0x50, 0); // access the first address from the memory 
- 
-  while (b!=0) { 
-   Serial.print((char)b); //print content to serial port 
-   addr++; //increase address 
-   b = i2c_eeprom_read_byte(0x50, addr); //access an address from the memory 
- } 
- 
-  Serial.println(" "); 
-  delay(2000); 
-} 
-}}} 
- 
-master가 slave에 데이터를 보낸다. 
- 
-{{http://i.imgur.com/lHqBZ.png?300}} 
- 
-master가 slave의 데이터를 읽는다. 
- 
-{{http://i.imgur.com/NE9xJ.png?300}} 
workshop/통신.1344866585.txt.gz · Last modified: 2018/07/18 14:09 (external edit)