การเขียนโปรแกรมภาษา C รับข้อมูลจากสวิทช์ แล้วส่งออกแสดงผลที่ LED จำลองการทำงานของ LED และสวิทช์ แบบ เปิด-ปิด (Latch)
1. สร้างโปรเจคใหม่
2. เขียนโปรแกรม
#include <avr\io.h> // Most basic include files
#include <avr\interrupt.h> // Add the necessary ones
#include <avr\signal.h> // here
int main(void) {
unsigned int a;
DDRD = 0xff; //Port D as output
DDRB = 0xfe;
PORTB = 0xff;
while(1) {
a = PINB;
PORTD = a;
}
}
3. จำลองการทำงานของ LED และสวิทช์ ด้วย Control Panel แก้ไขไฟล์โปรเจค เพื่อจำลองการทำงานของ LED และสวิทช์ขึ้นใน Control Panel โดยใส่ข้อความดังนี้
; Following lines are optional; if not included
; exactly these values are taken by default
; ------------------------------------------------------------
.POWER VDD=5 VSS=0 ; Power nodes
.CLOCK 1meg ; Micro clock
.STORE 250m ; Trace (micro+signals) storage time
; Micro nodes: RESET, AREF, PB0-PB7, PC0-PC6, PD0-PD7, ACO, TIM1OVF, ADC6, ADC7
; Define here the hardware around the micro
; ------------------------------------------------------------
D1 VDD D1_NODE
R1 D1_NODE PD0 0.68K
R9 VDD PB0 5K
K0 PB0 VSS LATCHED
4. จำลองการทำงานของสวิทช์ด้วย Control Panet
5. Run โปรแกรม ขณะ Run Program ลองคลิกเมาส์ที่สวิทช์ 0
------------------------------------------------------------------------------------------
โปรแกรมจำลองการทำงานเหมือนกับวงจรลอจิกเกต ตามรูป โดยสัญญาณอินพุท ป้อนเข้าทาง PD0 PD1 และ PD2 สัญญาณเอาท์พุทออกทาง PD7
แนวทางทำงาน เนื่อจากคำสังทางด้านโลจิก จะทำกับบิตที่ตรงกัน ดังนั้น ก่อนจะกระทำทางโลจิก จะเลื่อนบิตอินพุท ให้ตรงกับบิตเอาท์พุท
โปรแกรมไฟล์ เพิ่มเติมการจำลองการทำงานของ LED และะสวิทช์
R3 VDD PD0 10K
K0 PD0 VSS LATCHED
R2 VDD PD1 10K
K1 PD1 VSS LATCHED
R1 VDD PD2 10K
K2 PD2 VSS LATCHED
D1 VDD D1_NODE
R4 D1_NODE PD7 0.68K
โปรแกรม
#include <avr\io.h> // Most basic include files
#include <avr\interrupt.h> // Add the necessary ones
#include <avr\signal.h> // here
// ***********************************************************
// Main program
//
int main(void) {
int a,b,c,y;
DDRD = 0b11110000; //pd7-pd4 as output pd3-pd0 as input
PORTD = 0xff;
while(1) { // Infinite loop; define here the
a = PIND;
b = a;
c = a;
a = a<<7;
b = b<<6;
c = c<<5;
y = a & b;
c = c^b;
y = y|c;
y = ~y;
PORTD = y;
}
}