Download - บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น
![Page 1: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/1.jpg)
ETE183การเขยนโปรแกรมคอมพวเตอรส าหรบ
วศวกรรมไฟฟา
ผศ.ดร.วชราพรรณ แกวประพนธอ.ชนกชนม สงวรโยธน
![Page 2: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/2.jpg)
บทท 9การตอวงจรไมโครคอนโทรลเลอรและเขยนโปรแกรมควบคมดวย
บอรด Arduino
![Page 3: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/3.jpg)
บทท 9
1. การตอวงจรไมโครคอนโทรลเลอรเบองตน
2. อปกรณอเลกทรอนกสเบองตน
3. การอานคา/สงคา การหนวงเวลา
4. การวนรอบชดค าสง
5. การสงคาใหแสดงผล 7seg ชนด BCD
![Page 4: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/4.jpg)
แนะน าบอรดArduino
![Page 5: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/5.jpg)
Arduino UNO R3บอรด Arduino มความ สามารถ
ในการรกษาระดบแรงดนไฟฟาใหคงทได โดยระดบแรงดนไฟฟานนอยท 5โวลต
![Page 6: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/6.jpg)
Arduino UNO R3
![Page 7: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/7.jpg)
อปกรณอเลกทรอนกส
เบองตน(ทใชในคาบ)
![Page 8: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/8.jpg)
หนาท• จ ากดการไหลของกระแสไฟฟาในวงจร
หนวย• โอหม (Ω)
ตวตานทาน (Resistor)
![Page 9: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/9.jpg)
หนาท•เปนป มส าหรบ ตด/ตอ วงจร
สวตซ (Button)
![Page 10: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/10.jpg)
หนาท
• หลอดไฟชนดหนงทกนกระแสนอยมาก
• แตใหแสงสวางมาก
ไดโอดเปลงแสง(Light Emitting Diode : LED)
![Page 11: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/11.jpg)
หนาท
• เปนบอรดอ านวยความสะดวกในการทดลองทางวงจรไฟฟา
Bread Board
(คนไทยเรยกวาPhotoBoard)
![Page 12: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/12.jpg)
หนาท
• ตวความตานทานนสามารถเปลยนสภาพทางความน าไฟฟา ไดเมอมแสงมาตกกระทบ
LDR(PhotoResistor)
![Page 13: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/13.jpg)
หนาท• คออปกรณไฟฟาซงท าหนาทสงเสยงสญญาณเตอน เปรยบเสมอนกรงไฟฟา มขวลบและขวบวก เชนเดยวกบหลอด LED
Buzzer
![Page 14: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/14.jpg)
Digital และ Analog
![Page 15: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/15.jpg)
Digital
• 1/0
• HIGH/LOW
• 5V/0V
• สง/ต า
Analog
• ตงแต 0 ถง 1023
• ตงแต 0V ถง 5V
![Page 16: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/16.jpg)
การตอวงจรเบองตน
![Page 17: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/17.jpg)
การตอใชงาน LED เพอแสดงผล แบบ Source
![Page 18: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/18.jpg)
การตอใชงาน LED เพอแสดงผล แบบ Source
![Page 19: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/19.jpg)
การตอใชงานสวตซแบบ กดตด-ปลอยดบ (Button)การตอ แบบ เมอกดสวตซแลวไดคา logic 1
![Page 20: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/20.jpg)
การตอใชงานสวตซแบบ กดตด-ปลอยดบ (Button)การตอ แบบ เมอกดสวตซแลวไดคา logic 0
![Page 21: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/21.jpg)
การน าคาจาก SENSOR
เขามาใชใน Arduino
![Page 22: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/22.jpg)
การตอใชงานสวตซแบบ กดตด-ปลอยดบ (Button)การตอ แบบ เมอกดสวตซแลวไดคา logic 0
![Page 23: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/23.jpg)
การน าภาษาซมาใชควบคมการท างาน
ของบอรด Arduino
![Page 24: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/24.jpg)
การเขยนโปรแกรมเพอใชงานบน Arduino
![Page 25: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/25.jpg)
C Language for Micro Controller
Variable
Input
OutputDefine
Process
![Page 26: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/26.jpg)
void setup ()
//ค าสงทใชในการตงคา เชน การก าหนดพอรต คาเรมตน …void loop ()
// ค าสงทใชในการสงให Arduino ท างานตามทเรตองการ โดยโปรแกรมจะวน loop ฟงกชนนใหอตโนมตเมอท าเสรจสนครบรอบ
เรมตนเขยนโปรแกรม
![Page 27: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/27.jpg)
เมอเรมตนท างาน Arduino จะท าตามค าสงตางๆทอยในฟงกชน “setup” เปนจ านวน1 รอบ โดยค าสงตางๆทจะเขยนในฟงกชนน สวนมากจะเปนการก าหนดคาเรมตน การก าหนดหนาทของแตละขา หรอค าสงตางๆทตองการเรยกใชเพยงแคครงแรกครงเดยว หลงจากทจบฟงกชน “setup” จะไมมการยอนกลบมาท าค าสงในนอก สวนฟงกชน loop จะท างานตอจาก setup โดยใน loop นจะเปนการท าตามค าสงแบบวนซ า คอ ท างานตามค าสงบรรทดแรกไปเรอยๆจนถงบรรทดสดทาย แลววนกลบมาเรมท าทบรรทดแรกใหมอกครง
เรมตนเขยนโปรแกรม
![Page 28: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/28.jpg)
การก าหนดพอรต
![Page 29: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/29.jpg)
การก าหนดพอรต
pinMode(พอรต,INPUT หรอ OUTPUT);
* ท ำใน Function Setup เทำนน
![Page 30: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/30.jpg)
การก าหนดพอรต OUTPUT
void setup ()
pinMode(12,OUTPUT);
pinMode(12,OUTPUT); หมายความวาก าหนดให pin หมายเลข12 เปน OUTPUTซงสญญาณทสงออกมานนเปนสญญาณแบบดจตอล ทมอย 2 สถานะคอ HIGH และ LOW
![Page 31: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/31.jpg)
การก าหนดพอรต INPUT
void setup ()
pinMode(13,INPUT);
pinMode(13,INPUT); หมายความวาก าหนดให pin หมายเลข13 เปน INPUTซงสญญาณทรบเขามานนเปนสญญาณแบบดจตอล ทมอย 2 สถานะคอ HIGH และ LOWเรามกจะรบคาแลวน าไปเกบไวในตวแปร
![Page 32: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/32.jpg)
Digitalการอานคาการสงคา
การหยดชวคราว
![Page 33: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/33.jpg)
การสงคา HIGH และ LOW
digitalWrite(พอรต,HIGH หรอ LOW);
![Page 34: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/34.jpg)
การสงคา HIGH และ LOWvoid setup ()
pinMode(12,OUTPUT);
void loop ()
digitalWrite(12,HIGH);
digitalWrite(12,HIGH); เปนค าสงท สงให pin หมายเลข 12สงแรงดนขนาด 5 โวลตออกมา เพอท าให LED ไฟสวาง
digitalWrite(12,LOW); หมายถงหยดการจายแรงดนไฟฟาออก หรอท าใหมนเชอมตอกบ GND แทน จะท าให LED ไฟดบ
![Page 35: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/35.jpg)
การอานคาจากพอรต INPUT
digitalRead(พอรต)
![Page 36: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/36.jpg)
การอานคาจากพอรต INPUT
int button = 0;void setup ()
pinMode(13,INPUT);
void loop ()
button = digitalRead(13);
button = digitalRead(13); เปนค าสงท สงให pin หมายเลข 13 อานคาทได (ไดจากการกดปม switch จากรป ถากด logic = 1 ถาไมกด logic = 0) แลวน าคาทอานได ไปเกบไวในตวแปร button
![Page 37: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/37.jpg)
Analogการอานคาการสงคา
การหยดชวคราว
![Page 38: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/38.jpg)
การอานคาจากพอรต Analog INPUT
analogRead(พอรต)
int LDRpin = A0;int LDR;void setup ()
void loop () LDR = analogRead(LDR_pin);
![Page 39: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/39.jpg)
การอานคาจากพอรต AnalogINPUT
int button = 0;void setup ()
pinMode(13,INPUT);
void loop ()
button = digitalRead(13);
button = digitalRead(13); เปนค าสงท สงให pin หมายเลข 13 อานคาทได (ไดจากการกดปม switch จากรป ถากด logic = 1 ถาไมกด logic = 0) แลวน าคาทอานได ไปเกบไวในตวแปร button
![Page 40: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/40.jpg)
delay(คามลลวนาท);เชน
delay(1000); มคาเทากบ 1 วนาทโปรแกรมจะหยดชวคราว 1 วนาท
การหยดพกชวคราว
การหนวงเวลาคอการสงงานให ไมโครคอนโทรลเลอร หยดท างานตามชวงเวลาทเราก าหนด และเมอครบตามก าหนดนนแลว ไมโครคอนโทรลเลอรกจะเรมท างานตอไปตามค าสงทเราเขยน เราสามารถสงงานให Arduino ท าการหนวงเวลาไดดวยฟงกชน
![Page 41: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/41.jpg)
การหยดพกชวคราว
int led = 12;void setup() pinMode(led, OUTPUT); void loop()
digitalWrite(led, HIGH); delay(1000); digitalWrite(led, LOW); delay(500);
![Page 42: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/42.jpg)
ตวอยางการเขยนโปรแกรมควบคม
Arduino
![Page 43: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/43.jpg)
การประยกตกบภาษาซ
ทไดเรยนมา
![Page 44: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/44.jpg)
ประกาศตวแปร1
ค านวณคา2
การเกบคาไวในตวแปร3
การเรยกใชคาจากตวแปร4
If else5
For6
Array7
![Page 45: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/45.jpg)
-
QuizQuiz บทท 9 และ 8.2กอนเขาเรยนครงหนา
![Page 46: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/46.jpg)
ประเมนการสอน
เขาไปประเมนการสอนในระบบสารสนเทศ
![Page 47: บทที่ 1 ภาษาซีเบื้องต้นete.tido.tech/ete-programming/student/tutorials/... · บทที่ 1 ภาษาซีเบื้องต้น](https://reader034.vdocuments.mx/reader034/viewer/2022050221/5f66546642b07d0726326e7d/html5/thumbnails/47.jpg)
Mini Project
Homework
1. กลมทหวขอยงไมผาน ใหมาสอบใหม2. ไฟลงานน าเสนอมนโปรเจคทแกไขแลว
(ZIP)ภายใน 16/10/2560 เวลา 16.30 น.