C-08 อินเตอร์รัพท์ Pin Change 2

เป็นการอินเตอร์รัพท์แบบ Pin Change ที่เกิดจากสัญญาณ 2 แหล่ง คือ PD0 (Pin Change Interrupt Request 2) และ PC0 (Pin Change Interrupt Request 1)

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

R10 VDD PD0 10K

K1 PD0 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 IntPort1 PORTC

#define IntDDR1 DDRC

#define IntPort2 PORTD

#define IntDDR2 DDRD

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);

}

}

}

SIGNAL(SIG_PIN_CHANGE2)

{

if(bit_is_clear(PIND,0))

{

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

{

DataPort = 0xff;

_delay_ms(50);

DataPort = 0x00;

_delay_ms(50);

}

}

}

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

// Main program

//

int main(void) {

unsigned char temp;

DataDDR = 0b11111111; // All outputs

IntDDR1 = 0b01111110; // PC0 as input

IntPort1 = 0b11111111; // Set Port C to hight

IntDDR2 = 0b01111110;

IntPort2 = 0b11111111;

PCICR |= _BV(PCIE1)|_BV(PCIE2); //Enable PCINT1 and PCINT2

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

PCMSK2 |= _BV(PCINT16); //Trigger on change of PCINT8 (PD0)

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

}

}