workshop:통신
This is an old revision of the document!
컴퓨터와 통신 하기 RS232
Rs232
Python 설치
- Python 2.7.3 http://python.org/download/
- Pyserial Win32 (pyserial-2.5.win32.exe) google에서 pyserial win32 검색
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을 설치하고, 파이썬 인터프리터에서 다음과 같이 입력해보자. 어떻게 될지 예상해보자.
>>> import serial >>> s = serial.Serial('/dev/ttyUSB0') >>> s.write('1') 1 >>> s.write('0') 1 >>>
칩간에 통신을 실험해보자!
WS2801
LED 드라이버
Time
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직렬 컴퓨터 버스주변기기를 연결하기 위해 사용된다.
데이터와 클럭은 pull-up
Time
실제 실험 해보기 24LC256
arduino i2c 관련 핀
- Analog4 - SDA
- Analog5 - SCL
Time
#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에 데이터를 보낸다.
master가 slave의 데이터를 읽는다.
workshop/통신.1342741876.txt.gz · Last modified: 2018/07/18 14:09 (external edit)