+ All Categories
Home > Documents > การใช้งาน I2C Bus - ecpe.nu.ac.th 08 I2C Bus.pdf · Lecture 8 1...

การใช้งาน I2C Bus - ecpe.nu.ac.th 08 I2C Bus.pdf · Lecture 8 1...

Date post: 24-Mar-2020
Category:
Upload: others
View: 8 times
Download: 0 times
Share this document with a friend
12
Lecture 8 1 การใช้งาน I 2 C Bus ใช้สองสายได้แก่ Serial Data Line (SDA) ในการรับส่งข้อมูลแบบอนุกรมและ Serial Clock Line (SCL) เป็น สายสัญญาณ Clock สามารถรับส่งข้อมูลด้วยความเร็ว 100 kbits/s รูปที่ 1 การเชื่อมต่อด้วยระบบบัสแบบ I 2 C ตารางแสดงสภาวะบนบัส สภาวะบนระบบบัส SDA SCL 1. Bus not busy Hi Hi 2. Start data transfer Hi 3. Stop Hi 4. Data valid การรับส่งข้อมูล1บิตใช้ clock 1ลูก ขณะรับ/ส่ง ข้อมูลบน SDA ต้องคงที่และ SCL Hi ข้อมูลบน SDA เปลี่ยนแปลงได้เมื่อ SCL Lo X Change Hi Lo 5. Acknowledge ตัวรับจะส่งสัญญาณ Ack เมื่อรับข้อมูลครบ 1 byte แล้ว Lo Hi
Transcript
Page 1: การใช้งาน I2C Bus - ecpe.nu.ac.th 08 I2C Bus.pdf · Lecture 8 1 การใช้งาน I2C Bus ใช้สองสายได้แก่ Serial Data Line

Lecture 8

1

การใช้งาน I2C Bus ใช้สองสายได้แก่ Serial Data Line (SDA) ในการรับส่งข้อมูลแบบอนุกรมและ Serial Clock Line (SCL) เป็น สายสัญญาณ Clock สามารถรับส่งข้อมูลด้วยความเร็ว 100 kbits/s

รูปที่ 1 การเชือ่มตอ่ดว้ยระบบบัสแบบ I2C

ตารางแสดงสภาวะบนบัส

สภาวะบนระบบบัส SDA SCL

1. Bus not busy Hi Hi

2. Start data transfer

Hi

3. Stop

Hi

4. Data valid การรับส่งข้อมูล1บิตใช้ clock 1ลูก ขณะรับ/ส่ง ข้อมูลบน SDA ต้องคงที่และ SCL Hi ข้อมลูบน SDA เปล่ียนแปลงได้เมื่อ SCL Lo

X Change

Hi Lo

5. Acknowledge ตัวรับจะส่งสญัญาณ Ack เมื่อรับข้อมูลครบ 1 byte แล้ว

Lo Hi

Page 2: การใช้งาน I2C Bus - ecpe.nu.ac.th 08 I2C Bus.pdf · Lecture 8 1 การใช้งาน I2C Bus ใช้สองสายได้แก่ Serial Data Line

Lecture 8

2

1

รูปที่ 2 Timing Diagram ของการรับส่งข้อมูล

การเขียนข้อมูล 1. ส่งสัญญาณ start เขียน address ของตัว Slave และ bit ต่่าสุดเป็น 0 (เขียน) 2. เขียน byte ควบคุม (ขึ้นอยู่กับอปุกรณ์ว่าจะเป็นค่าอะไร) 3. เขียนข้อมูล 4. ส่งสัญญาณ stop การอ่านข้อมูล 1. ส่งสัญญาณ start เขียน address ของตัว Slave และ bit ต่่าสุดเป็น 0 (เขียน) 2. เขียน byte ควบคุม 3. ส่งสัญญาณ stop 4. ส่งสัญญาณ start เขียน address อีกครั้ง และ bit ต่่าสุดเป็น 1 (อา่น) 5. อ่านข้อมูล 6. ส่งสัญญาณ stop

SDA เปลี่ยนจาก 1 เป็น 0 ขณะที ่SCL เป็น 1

การถ่ายโอนข้อมูล SDA เปลี่ยนจาก 0 เป็น 1 ขณะที่ SCL เป็น

ขณะถ่ายโอนข้อมูล

Page 3: การใช้งาน I2C Bus - ecpe.nu.ac.th 08 I2C Bus.pdf · Lecture 8 1 การใช้งาน I2C Bus ใช้สองสายได้แก่ Serial Data Line

Lecture 8

3

รูปที่ 3 ไบต์ address ของอุปกรณ์

ตัวอย่าง 1 sbit SCL = P1^5; sbit SDA = P1^6; Void I2C_delay(void) { _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); } /*********************/ void SCL_high(void) { SCL = 1; I2C_delay();

}

void SCL_low(void) { SCL = 0; I2C_delay(); }

/**********************/ void I2C_stop(void) // Stop { SDA = 0; SCL_high(); SDA = 1; }

void I2C_start(void) // Start { SDA = 1; SCL_high(); SDA= 0; I2C_delay(); SCL_low(); SDA = 1; }

Page 4: การใช้งาน I2C Bus - ecpe.nu.ac.th 08 I2C Bus.pdf · Lecture 8 1 การใช้งาน I2C Bus ใช้สองสายได้แก่ Serial Data Line

Lecture 8

4

ตัวอย่าง 2

Bit I2C_wrbyte (unsigned dat) // write data , return 0=ok , return 1=error { unsigned char i; bit outbit; for (i=1;i<=8;i++) // send 8 bits { outbit = dat & 0x80; SDA = outbit; // send 1 bit dat = dat <<1; // shift 1 bit SCL_high(); SCL_low(); } SDA = 1; SCL_high(); outbit = SDA; SCL_low(); return(outbit); }

Unsigned char I2C_rdbyte() // read data byte { unsigned char i, dat; bit inbit; dat = 0; for (i=1;i<=8;i++) // read 8 bits { SCL_high(); inbit = SDA; // read 1 bit dat = dat <<1; dat = dat | inbit; SCL_low(); } SDA = 0; SCL_high(); SCL_low(); SDA = 1; return(dat); }

Unsigned char I2C_rdlastbyte() // read data last byte { unsigned char i, dat; bit inbit; dat = 0; for (i=1;i<=8;i++) // read 8 bits { SCL_high(); inbit = SDA; // read 1 bit dat = dat <<1; dat = dat | inbit; SCL_low(); } SDA = 1; SCL_high(); SCL_low(); return(dat); }

Page 5: การใช้งาน I2C Bus - ecpe.nu.ac.th 08 I2C Bus.pdf · Lecture 8 1 การใช้งาน I2C Bus ใช้สองสายได้แก่ Serial Data Line

Lecture 8

5

การใช้งาน PCF8591 ADC/DAC

รูปที่ 4 คุณสมบัตขิอง PCF8591 ADC/DAC

Address byte Control Byte รูปที่ 5 ไบต์ address และ control ของอุปกรณ ์

ตารางแสดงบติต่างๆ ของ Control Byte บิต การใช้งาน รูปแบบ D0 D1

เลือก Channel ของวงจร A/D 00 Channel 1 01 Channel 2 10 Channel 3 11 Channel 4

D2 เพิ่มคา่การอ่าน Channel อัตโนมัต ิ ถ้าใช้งานใหเ้ป็นลอจิก “1” D3 โปรแกรมให้เป็นลอจกิ “0”

Page 6: การใช้งาน I2C Bus - ecpe.nu.ac.th 08 I2C Bus.pdf · Lecture 8 1 การใช้งาน I2C Bus ใช้สองสายได้แก่ Serial Data Line

Lecture 8

6

D4 D5

เลือกสัญญาณอินพุต 4 โหมด แสดงในรูปต่อไป

D6 Enable analogue output ถ้าใช้งานจะเป็นลอจิก “1” D7 โปรแกรมให้เป็นลอจกิ “0”

รูปที่ 6 การท างานของแต่ละ channel ในโหมดต่างๆ

การอ่านข้อมูล

1. ส่งสัญญาณ start เขียน address และ bit ต่่าสุดเป็น 0 (เขียน)

2. เขียน byte ควบคุม

3. ส่งสัญญาณ stop

4. ส่งสัญญาณ start เขียน address อีกครั้ง และ bit ต่่าสุดเป็น 1 (อา่น)

5. อ่านข้อมูลที่แปลงจาก Analogueเขา้มา

Page 7: การใช้งาน I2C Bus - ecpe.nu.ac.th 08 I2C Bus.pdf · Lecture 8 1 การใช้งาน I2C Bus ใช้สองสายได้แก่ Serial Data Line

Lecture 8

7

รูปที่ 7 Timing Diagram แสดงการอ่านข้อมูลจาก A/D conversion

การเขียนข้อมูล

1. ส่งสัญญาณ start เขียน address และ bit ต่่าสุดเป็น 0 (เขียน)

2. เขียน byte ควบคุม 40H

3. เขียนข้อมูลท่ีต้องการแปลงเป็น Analogueออกไป

4. ส่งสัญญาณ stop

รูปที่ 8 Timing Diagram แสดงการอ่านข้อมูลจาก D/A conversion

รูปที่ 9 D/A Conversion Sequence

Page 8: การใช้งาน I2C Bus - ecpe.nu.ac.th 08 I2C Bus.pdf · Lecture 8 1 การใช้งาน I2C Bus ใช้สองสายได้แก่ Serial Data Line

Lecture 8

8

รูปที่ 11 วงจรส าหรับใชง้าน PCF8951 และ แสดงผลด้วย LED ที่ต่อกับ 8255

รูปที่ 10 A/D Conversion Sequence ตัวอย่าง 3 รับข้อมูล analogue แสดงผลทาง LED ที่ต่อกับ port A ของ 8255 #include <reg52.h> #include <intrins.h> #include <acsacc.h> #include <c:i2c.h> #define port_A XBYTE[0x9000] //Port A of 8255 #define port_con XBYTE[0x9003] //Port Control of 8255 unsigned char buf; //ADC input buffer unsigned char ADC_add = 0x90; void delay_1(unsigned int count) { #include <reg52.h> #include <intrins.h> #include <acsacc.h> #include <c:i2c.h>

Page 9: การใช้งาน I2C Bus - ecpe.nu.ac.th 08 I2C Bus.pdf · Lecture 8 1 การใช้งาน I2C Bus ใช้สองสายได้แก่ Serial Data Line

Lecture 8

9

#define port_A XBYTE[0x9000] //Port A of 8255 #define port_con XBYTE[0x9003] //Port Control of 8255 unsigned char buf; //ADC input buffer unsigned char ADC_add = 0x90; void delay_1(unsigned int count) { ตัวอย่าง 4 รับข้อมูล analogue แสดงผลทาง LED ที่ต่อกับ port A ของ 8255 void ADC_READ(unsigned char addr, unsigned char ad_con, unsigned char *dat) { I2C_start(); if(I2C_wrbyte(addr)) //Write address(write) if(I2C_wrbyte(ad_con)) //Write control byte I2C_stop(); I2C_start(); if(I2C_wrbyte(addr|0x01)) //Write address(read) *dat = I2C_rdbyte(); //Read data I2C_stop(); } main() { port_con = 0x88; while(1) { ADC_READ(ADC_add,0x40,&buf) port_A = buf; dalay_1(1000); } }

Page 10: การใช้งาน I2C Bus - ecpe.nu.ac.th 08 I2C Bus.pdf · Lecture 8 1 การใช้งาน I2C Bus ใช้สองสายได้แก่ Serial Data Line

Lecture 8

10

การใช้งาน Real Time Clock DS1307

รูปที่ 12 IC Real Time Clock DS1307

Vcc, GND ใช้ต่อกับแหล่งจา่ยไฟเลี้ยงของระบบ

X1, X2 ต่อกับคริสตอลความถี่ 32.768 kHz และ C12.5 pF

VBAT ต่อกับแบตเตอรี่ส่ารองขนาด 3 V

SDA, SCL ขาส่าหรับส่ือสารแบบ I2C

SQW /OUT เป็นขาส่าหรับสร้างคลื่น square wave บิต 7 บิต 6 บิต 5 บิต 4 บิต 3 บิต 2 บิต 1 บิต 0

00H CH วินาที หลักสิบ วินาท ี 00-59

01H X นาที หลกัสิบ นาท ี 00-59 02H X 12, 24 A/P 10 HR ชั่วโมง 01-12, 00-23

03H X X X X X วัน 1-7

04H X X วันที่ หลกัสิบ วันที ่ 01-31

05H X เดือน หลกัสิบ เดือน 01-12

06H ปี หลักสิบ ปี 00-99

07H OUT X X SQWE X X RS1 RS0

รูปที่ 13 แผนผังหนว่ยความจ าและรีจิสเตอร์ภายใน การเขียนข้อมูลลง chip

1. เขียนไบต์แอดเดรสของชิปโดยให ้R/W =0 2. เขียนแอดเดรสภายในชิปที่ตอ้งการ 3. เขียนข้อมูลลงแอดเดรสนั้น

Page 11: การใช้งาน I2C Bus - ecpe.nu.ac.th 08 I2C Bus.pdf · Lecture 8 1 การใช้งาน I2C Bus ใช้สองสายได้แก่ Serial Data Line

Lecture 8

11

รูปที่ 14 ไบต์ address ของ DS1307

รูปที่ 15 Timing Diagram แสดงการเขียนข้อมูลลง chip

การอ่านข้อมูลจาก chip

1. เขียนไบต์แอดเดรสของชิปโดยให ้R/W =1 2. เขียนแอดเดรสภายในชิปที่ตอ้งการ 3. อ่านข้อมูลจากแอดเดรสนั้น

Page 12: การใช้งาน I2C Bus - ecpe.nu.ac.th 08 I2C Bus.pdf · Lecture 8 1 การใช้งาน I2C Bus ใช้สองสายได้แก่ Serial Data Line

Lecture 8

12

รูปที่ 16 Timing Diagram แสดงการอ่านข้อมูลจาก chip


Recommended