Electoday 2025

ไมโครคอนโทรลเลอร์ => AVR and Arduino => Topic started by: tha on November 06, 2017, 03:17:08 PM

Title: Atmega328P Datasheet 18. I/O-Ports
Post by: tha on November 06, 2017, 03:17:08 PM
ข้ามไปก่อนก็แล้วกัน เอาบทง่ายๆที่ใช้งานบ่อยขึ้นก่อนก็แล้วกัน ดาต้าชีตผมว่าไม่ค่อยจะเรียงลำดับสักเท่าไหร่ วันนี้อากาศเย็นเลยแปลตอนกลางวันได้ เลยไปได้เรื่อยๆก็เพลินดีมีงานอดิเรกทำ

18. I/O-Ports

18.1. Overview(ภาพรวม)

AVR ports ทั้งหมดมีฟังก์ชัน Read-Modify-Write ที่แท้จริง เมื่อใช้เป็น general digital I/O ports. นี้หมายความว่าทิศทางของขาพอร์ตหนึ่งสามารถถูกเปลี่ยนได้โดยปราศจากการเปลี่ยนที่ไม่ตั้งใจของทิศทางของขาอื่น ๆ ด้วยคำสั่ง SBI และ CBI. เช่นเดียวกันกับเมื่อเปลี่ยน drive value (ถ้ากำหนดเป็น output) หรือ enabling/disabling of pull-up resistors (ถ้ากำหนดเป็น input). Each output buffer แต่ละตัวมีลักษณะของการไดรฟ์แบบสมมาตรกันโดยมีทั้งความสามารถในการดึงลงกราวน์(sink)และเป็นแหล่งจ่ายไฟ(source)สูง. The pin driver มีความแข็งแรงเพียงพอที่จะไดรฟ์ LED displays ได้โดยตรง. port pins ทั้งหมดมี pull-up resistors ที่เลือกได้เฉพาะของแต่ละตัวด้วย a supply-voltage ที่มีความต้านทานที่คงที่. I/O pins ทั้งหมดมี protection diodes ทั้ง VCC and Ground ดังแสดงในรูปต่อไปนี้.

(https://i.imgur.com/juMQ3dZ.png)

registers และ bit ทั้งหมดที่อ้างอิงใน section นี้ถูกเขียนในรูปแบบทั่วไป. ตัวพิมพ์เล็ก "x" หมายถึงหมายเลขตัวอักษรสำหรับ the port นั้นๆ, และตัวพิมพ์เล็ก "n" หมายถึง bit number นั้นๆ อย่างไรก็ตามเมื่อใช้ the register หรือ bit กำหนดลงใน a program, รูปแบบที่ถูกต้องต้องถูกใช้ ตัวอย่างเช่น, PORTB3 สำหรับ bit no. 3 ในเอกสารนี้โดยทั่วไปอ้างเป็น PORTxn.

I/O memory address locations ถูกจัดแบ่งสำหรับแต่ละ port, แต่ละพอร์ตสำหรับ the Data Register – PORTx, Data Direction Register – DDRx, and the Port Input Pins – PINx. The Port Input Pins I/O location ถูกอ่านได้เพียงอย่างเดียว ขณะที่ the Data Register และ the Data Direction Register ถูกอ่าน/เขียนได้. อย่างไรก็ตามการเขียน '1' ไปยังบิตหนึ่งใน PINx Register จะทำให้เกิดการสลับในบิทที่ตรงกันใน the Data Register. นอกจากนี้, การปิดการใช้งาน the Pull-up Disable – PUD bit ใน MCUCR จะปิดการใช้งาน the pull-up function สำหรับทุก pins ใน
ทุก ports เมื่อเซท.

การใช้ the I/O port เป็น General Digital I/O ถูกอธิบายในส่วนถัดไปนี้  port pins โดยมากแล้วใช้ร่วมกันกับหน้าที่ทางเลือกอื่น (alternate functions) สำหรับอุปกรณ์ต่อพ่วงต่างๆ บน the device. วิธีที่แต่ละ alternate function ยุ่งเกี่ยวกับ the port pin ถูกอธิบายใน Alternate Port Functions section ในบทนี้.  โปรดดูที่ the individual module sections สำหรับการคำอธิบายแบบเต็มของ the alternate functions

การเปิดใช้งาน the alternate function ของบาง the port pins ไม่มีผลกระทบต่อการใช้ pins อื่นๆใน the port ที่ใช้เป็น general digital I/O.
Title: Re: Atmega328P Datasheet 18. I/O-Ports
Post by: tha on November 08, 2017, 09:37:46 AM
18.2. Ports เป็น General Digital I/O
The ports เป็น bi-directional I/O ports พร้อมด้วยมีทางเลือก pull-ups ภายใน. รูปต่อไปนี้แสดงการอธิบายหน้าที่ของ I/O-port pin อันหนึ่ง, ในที่นี้เรียกว่า Pxn.

(https://i.imgur.com/lxh4RoJ.png)

หมายเหตุ: 1. WRx, WPx, WDx, RRx, RPx, and RDx ใช้ร่วมกันทุก pins ภายใน port เดียวกัน. clkI/O, SLEEP, and PUD ใช้ร่วมกันทุก ports.

18.2.1. Configuring the Pin ( การกำหนดค่า Pin)
แต่ละ port pin ประกอบด้วย three register bits: DDxn, PORTxn, and PINxn. ดังแสดงใน the Register Description, the DDxn bits ถูกเข้าถึงที่ the DDRx I/O address, the PORTxn bits ที่ the PORTx I/O address, และ the PINxn bits ที่ the PINx I/O address.

The DDxn bit ใน the DDRx Register จะเลือกทิศทางของ pin นี้. ถ้า DDxn ถูกเขียนเป็น '1', Pxn ถูกกำหนดเป็น output pin. ถ้า DDxn ถูกเขียนเป็น '0', Pxn ถูกกำหนดเป็น input pin.

ถ้า PORTxn ถูกเขียนเป็น '1' เมื่อ pin นั้นถูกกำหนดเป็น input pin, the pull-up resistor จะถูกเปิดใช้งาน. เพื่อเปลี่ยน the pull-up resistor เป็นปิดใช้งาน, PORTxn ต้องถูกเขียนเป็น '0' หรือ the pin นั้นต้องถูกกำหนดเป็น output pin. The port pins จะเป็น tri-stated เมื่อ reset condition ทำงาน, ถึงแม้ว่าสัญญานนาฬิกาไม่ได้รันอยู่ก็ตาม.

ถ้า PORTxn ถูกเขียนด้วย '1' เมื่อ the pin นั้นถูกกำหนดเป็น output pin, the port pin นั้นจะถูกขับให้เป็นสูง(ให้เป็น1). ถ้า PORTxn ถูกเขียนด้วย logic zero เมื่อ the pin นั้นถูกกำหนดเป็น output pin, the port pin นั้นจะถูกขับให้เป็นต่ำ(ให้เป็น 0).

18.2.2. Toggling the Pin (การสลับค่า PIN)
การเขียน '1' ไปยัง PINxn จะสลับค่า PORTxn, โดยเป็นอิสระจากค่าของ DDRxn. คำสั่ง The SBI instruction สามารถถูกใช้สลับค่าของหนึ่ง bit เดี่ยวใน a port.
Title: Re: Atmega328P Datasheet 18. I/O-Ports
Post by: tha on November 08, 2017, 12:17:26 PM
18.2.3. Switching Between Input and Output(การสลับระหว่าง  Input และ Output)
เมื่อสลับระหว่าง tri-state ({DDxn, PORTxn} = 0b00) และ output high ({DDxn, PORTxn} = 0b11), สถานะระหว่างกลางที่ทั้ง pull-up enabled {DDxn, PORTxn} = 0b01) หรือ output low ({DDxn, PORTxn} = 0b10) ต้องเกิดขึ้น. โดยปกติ, the pull-up enabled state ได้รับการยอมรับอย่างเต็มที่, เป็นดังที่ a highimpedance environment จะไม่แจ้งให้ทราบถึงความแตกต่างระหว่าง a strong high driver และ a pull-up. หากไม่เป็นกรณีนี้, the PUD bit ใน the MCUCR Register สามารถถูกเซทเพื่อปิดใช้งานl pull-ups ทั้งหมดในทุก ports.

การสลับระหว่าง input with pull-up และ output low จะเกิดปัญหาเดียวกัน. ผู้ใช้ต้องใช้ทั้ง the tri-state ({DDxn, PORTxn} = 0b00) หรือ the output high state ({DDxn, PORTxn} = 0b11) เป็นลำดับระหว่างกลาง.

ตารางต่อไปนี้จะสรุปรวบยอด the control signals สำหรับ the pin value.

(https://i.imgur.com/hXykItG.png)
Title: Re: Atmega328P Datasheet 18. I/O-Ports
Post by: tha on November 09, 2017, 01:31:45 PM
18.2.4. Reading the Pin Value(การอ่านค่า Pin Value)
ความเป็นอิสระของการเซทของ Data Direction bit DDxn, the port pin สามารถถูกอ่านผ่านทาง the PINxn Register bit. ดังแสดงใน Ports as General Digital I/O, the PINxn Register bit และ the preceding(ก่อนหน้า) latch ประกอบเป็น a synchronizer. นี้เป็นสิ่งจำเป็นเพื่อหลีกเลี่ยง metastability(ความไม่เสถียร ใช่ป่าว) ถ้า the physical pin เปลี่ยนค่าใกล้กับขอบของ the internal clock, แต่มันก็ทำให้เกิดการ delay อีกด้วย. รูปต่อไปนี้แสดง a timing diagram ของ the synchronization เมื่ออ่าน an externally applied pin value. The maximum and minimum propagation(การแพร่กระจาย) delays ถูกแสดงเป็น tpd,max และ tpd,min ตามลำดับ

(https://i.imgur.com/eP0c39j.png)
Title: Re: Atmega328P Datasheet 18. I/O-Ports
Post by: tha on November 09, 2017, 09:37:04 PM
พิจารณาที่ the clock period เริ่มต้นไม่นานหลังจากขอบขาลงแรกของ the system clock. The latch จะปิดเมื่อ the clock เป็นดึงลงต่ำ(ขอบขาลง), และจะเปิดเผยเมื่อ the clock ขึ้นสูง(ขอบขาขึ้น), ดังแสดงเป็นส่วนแรงเงาของ the "SYNC LATCH" signal. The signal value จะถูก latched(ค้าง) เมื่อ the system clock ลงต่ำอีก. มันจะ clocked เข้าไปยัง the PINxn Register ที่ขอบขาขึ้น (the succeeding positive clock edge).ดังแสดงโดยสองลูกศรของ tpd,max และ tpd,min, การเปลี่ยนผ่านของสัญญานหนึ่งบน the pin จะถูกทำให้ล่าช้าไประหว่าง ½ และ 1½ system clock period ขึ้นอยู่กับ the time of assertion
Title: Re: Atmega328P Datasheet 18. I/O-Ports
Post by: tha on November 09, 2017, 10:25:38 PM
เมื่ออ่านค่า a software assigned pin value กลับ, a nop instruction ต้องถูกใส่เข้าไปดังแสดงในรูปต่อไปนี้. คำสั่ง out จะเซท the "SYNC LATCH" signal ที่ขอบขาขึ้นของ the clock. ในกรณีนี้, the delay tpd ที่ผ่าน the synchronizer คือ 1 system clock period

(https://i.imgur.com/e9F2w8f.png)
Title: Re: Atmega328P Datasheet 18. I/O-Ports
Post by: tha on November 10, 2017, 12:23:21 PM

ตัวอย่าง code ต่อไปนี้แสดงวิธีการเซท port B pins 0 and 1 high(เป็นสูง), 2 and 3 low(เป็นต่ำ), และกำหนด the port pins from 4 to 7 เป็น input พร้อมทั้งกำหนด pull-ups ไปยัง port pins 6 and 7. The resulting pin values ถูกอ่านกลับอีกครั้ง แต่ตามที่กล่าวไว้ก่อนหน้านี้, a nop instruction ถูกเพิ่มเข้ามาเพื่อให้สามารถอ่านค่ากลับคืนมาได้ทันทีที่เพิ่งที่กำหนดถึงพินบางพิน (some of the pins).

(https://i.imgur.com/TA2vzqh.png)

หมายเหตุ: 1. สำหรับ the assembly program, two temporary registers ถูกใช้เพื่อให้ใช้เวลาให้น้อยลงจาก pull-ups ถูกเซทบน pins 0, 1, 6, and 7, จนกระทั้งถึง the direction bits ถูกเซทอย่างถูกต้อง, กำหนด 2 and 3 เป็น low และกำหนดซ้ำ bits 0 and 1 เป็น strong high drivers.

(https://i.imgur.com/BiVjMcs.png)
(https://i.imgur.com/F3hlnQX.png)

Title: Re: Atmega328P Datasheet 18. I/O-Ports
Post by: tha on November 17, 2017, 04:17:22 AM
http://www.mediafire.com/file/6rgq3sijdifor1a/io.rar (http://www.mediafire.com/file/6rgq3sijdifor1a/io.rar)
บอร์ด atmega328p ลองทำโปรแกรมตามตัวอย่าง ต่อ LED เข้ากับ PD0-PD5  มีคำสั่ง asm volatile ("nop" :: ); กับไม่มีคำสั่ง asm volatile ("nop" :: ); ให้ผลต่างกันครับ
Title: Re: Atmega328P Datasheet 18. I/O-Ports
Post by: tha on November 17, 2017, 04:44:59 AM
PORTB ปล่อยลอยๆไว้ครับ ส่วน asm volatile ("nop" :: ); มาได้อย่างไร ผมหาในเน็ต search google "avr nop in C" ครับ
Title: Re: Atmega328P Datasheet 18. I/O-Ports
Post by: tha on November 20, 2017, 12:38:21 PM
18.2.5. Digital Input Enable and Sleep Modes
ตามที่แสดงในรูปของ General Digital I/O, the digital input signal สามารถถูกต่อลงกราวด์ที่อินพุทของ the Schmitt Trigger. The signal ที่แสดงเป็น SLEEP ในรูปนั้น, ถูกเซทโดย the MCU Sleep Controller ใน Power-down mode และ Standby mode เพื่อหลีกเลี่ยงการกินพลังงานที่สูง ถ้าบาง input signals ถูกปล่อยลอย (floating), หรือมี an analog signal level ใกล้ถึง VCC/2.

SLEEP ถูกข้ามไปสำหรับ port pins ที่เปิดใช้งานเป็น external interrupt pins. ถ้า the external interrupt request ไม่ถูกเปิดใช้งาน, SLEEP ยังทำงานกับ pins เหล่านี้ได้อีกด้วย. SLEEP ยังถูกข้ามโดย alternate functions อื่นๆ ดังอธิบายใน Alternate Port Functions section ในบทนี้อีกด้วย

ถ้า a logic high level เกิดขึ้นบน an asynchronous external interrupt pin ที่กำหนดเป็น "Interrupt on Rising Edge, Falling Edge, หรือ Any Logic Change on Pin" ในขณะที่ the external interrupt นั้นไม่ถูกเปิดใช้งาน, the corresponding(ที่ตรงกัน) External Interrupt Flag จะถูกเซท เมื่อกลับมาทำงานใหม่จากโหมด Sleep ที่กล่าวมาข้างต้น, เนื่องจากการจับยึดใน sleep mode เหล่านี้ทำให้เกิดการเปลี่ยนลอจิกที่ต้องการ (the requested logic change.)
Title: Re: Atmega328P Datasheet 18. I/O-Ports
Post by: tha on June 27, 2019, 06:56:28 PM
อันนี้ยังไม่จบ จะไปต่อนะ ตอนนี้ทบทวนที่แปลมาแล้วก่อน สักพักหนึ่งนะครับ