C-07 การอินเตอร์รัพท์ Pin Change

เป็นการอินเตอร์รัพท์ที่เกิดเมื่อมีการเปลี่ยนแปลงของสัญญาณที่ พอร์ท ซึ่งมีอยู่ 3 แบบคือ

ทั้งนี้ทั้งนั้นต้องดูด้วยว่า AVR เบอร์นั้นๆ มีอยู่หรือเปล่า

1. สร้างโปรเจคใหม่ และเพิ่มคำสั่งจำลองการทำงานของสวิทช์และ LED

D1 VDD D1_NODE

D2 VDD D2_NODE

D3 VDD D3_NODE

D4 VDD D4_NODE

D5 VDD D5_NODE

D6 VDD D6_NODE

D7 VDD D7_NODE

D8 VDD D8_NODE

R1 D1_NODE PB0 0.62K

R2 D2_NODE PB1 0.62K

R3 D3_NODE PB2 0.62K

R4 D4_NODE PB3 0.62K

R5 D5_NODE PB4 0.62K

R6 D6_NODE PB5 0.62K

R7 D7_NODE PB6 0.62K

R8 D8_NODE PB7 0.62K

R9 VDD PC0 10K

K0 PC0 VSS Latched

2. โปรแกรม

#include <avr\io.h> // Most basic include files

#include <avr\interrupt.h> // Add the necessary ones

#include <avr\signal.h> // here

#include <util/delay.h>

// Define here the global static variables

//

#define DataPort PORTB // Using PortC as our Dataport

#define DataDDR DDRB

#define IntPort PORTC

#define IntDDR DDRC

SIGNAL(SIG_PIN_CHANGE1)

{

if(bit_is_clear(PINC,0))

{

for(unsigned char i=0;i<5;i++)

{

DataPort = 0x0f;

_delay_ms(50);

DataPort = 0xf0;

_delay_ms(50);

}

}

}

// ***********************************************************

// Main program

//

int main(void) {

unsigned char temp;

DataDDR = 0b11111111; // All outputs

IntDDR = 0b01111110; // PC0 as input

IntPort = 0b11111111;

PCICR |= _BV(PCIE1); //Enable PCINT1

PCMSK1 |= _BV(PCINT8); //Trigger on change of PCINT18 (PC0)

sei();

temp = 0x01;

while(1) { // Infinite loop; define here the

DataPort = ~temp;

if(temp==0x80) temp = 0x01;

else temp<<=1;

_delay_ms(80); // Software debouncing control

}

}