C-04.Switch Monostable

การเขียนโปรแกรมภาษา 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 MONOSTABLE(0.1m)

4. จำลองการทำงานของสวิทช์ด้วย Control Panet

5. Run โปรแกรม ขณะ Run Program ลองคลิกเมาส์ที่สวิทช์ 0

6. แก้ไข Project file โดยเพิ่มคำสั่ง .PLOT V(PB0) V(PD0)

D1 VDD D1_NODE

R1 D1_NODE PD0 0.68K

R9 VDD PB0 5K

K0 PB0 VSS MONOSTABLE(0.1m)

.PLOT V(PB0) V(PD0)

7. ในเมนู View เปิด Scope

8. เมื่อ Run Run Program ให้คลิกเมาส์ที่สวิทช์ 0 แล้วดูรูปคลื่นบน Scope