スイッチやセンサの入力された回数をカウントアップする。
センサとして、焦電型赤外線センサ Panasonic PaPIRs EKMC1601111 を使用して、人が近づくとカウントアップするようにしてある。
マイコン AVR ATmega168P
焦電型赤外線センサ Panasonic PaPIRs EKMC1601111
トランジスタ NPN形 2SC1815
7セグメントLED アノードコモンタイプ 3個
積層セラミックコンデンサ 0.1uF×2個
タクトスイッチ2個
スライドスイッチ
カーボン抵抗器 150Ω×7本、10kΩ×2本
ユニバーサル基板 PICOK200 94V-D
ICソケット 28ピン幅狭(300mil)タイプ
電池ボックス(単3×3本用、バッテリスナップタイプ)、バッテリスナップ
ピンヘッダ、ピンソケット
焦電型赤外線センサとして使用したPanasonic PaPIRsの出力は、オープン・ドレインであるため、トランジスタ(2SC1815)を通してマイコンに入力している。
/* * 7segled_mega168p.c * * Created: 2017/06/10 15:11:27 * Author : shokuin */ /* * PD0, 1 : Push Button Switch : OFF=1, ON=0 * PD2 : Infrared detecotr : OFF=1, ON=0 */#include <avr/io.h>#define isSw0On() !(PIND & 1<<PD0)#define isSw1On() !(PIND & 1<<PD1)//#define isIrsDetect() (PIND & 1<<PD2) // infrared detector (Detetcted 1, no detection 0)#define isIrsDetect() !(PIND & 1<<PD2) // infrared detector (Detetcted 0, no detection 1)#define MASK_7SEG_PORTC 0b11111101 //Mask for 7-segmnet LED (-A-) control #define MASK_7SEG_PORTB 0b11000000#define MASK_ANODE_PORTC 0b11000011static inline void wait(volatile uint32_t i)// wait for a while// Arguments :// i : waiting length{ while ( i-- > 0 );}/* * initialize */static inline void initIo(void)// initialize I/O ports.{ PORTD = (1<<PD5) | (1<<PD3) | (0<<PD2) | (1<<PD1) | (1<<PD0); // weak pull up resistor enable on PD53210 DDRD = 0b11010000; // PD53210 as Input, others Output DDRC = 0b11111111; // PORTC All Output DDRB = 0b11111111; // PORTB All Output}void choose_7seg_digit(uint8_t digit)/* * PORTC-5432 : 7seg anode ( digit ) 0123 : 0-OFF, 1-ON * digit 0123 * PORTC 0bxx0000xx */{ uint8_t dataC; // choose digit switch (digit) { case 0 : // PORTC = 0bxx1000xx dataC = 0b00100000; PORTC &= MASK_ANODE_PORTC; PORTC |= dataC; break; case 1 : // PORTC = 0bxx0100xx dataC = 0b00010000; PORTC &= MASK_ANODE_PORTC; PORTC |= dataC; break; case 2 : // PORTC = 0bxx0010xx dataC = 0b00001000; PORTC &= MASK_ANODE_PORTC; PORTC |= dataC; break; case 3 : // PORTC = 0bxx0001xx dataC = 0b00000100; PORTC &= MASK_ANODE_PORTC; PORTC |= dataC; break; default : PORTC &= (0b11000011); } }void off_7seg(void){ PORTB = (0b11111111); PORTC |= (0b00000010); PORTC &= (0b11000010);}void show_7seg_1digit(uint16_t number)/* * arguments : * digit : 3210 * number : 0 ~ 9 * * PORT * * digit: * 3 2 1 0 * -a- -a- -a- -a- * | | | | | | | | * f b f b f b f b * | | | | | | | | * -g- -g- -g- -g- * | | | | | | | | * e c e c e c e c * | | | | | | | | * -d- -d- -d- -d- * * * PORTC-1 : 7seg -a- : 1-OFF, 0-ON * PORTC 0bxxxxxx0x * * PORTB-543210 : 7seg -b-c-d-e-f-g- : 1-OFF, 0-ON * PORTB : 0bxx 0 0 0 0 0 0 * * xxbcdefg * PORTB 0bxx000000 * PORTC 0bxx0000xx * */{ uint8_t dataB, dataC; switch (number) { case 0 : // a-b-c-d-e-f dataB = 0b00000001; dataC = 0b00000000; PORTB &= MASK_7SEG_PORTB; // clear data bit PORTB |= dataB ; // set data bit 1 PORTC &= MASK_7SEG_PORTC; PORTC |= dataC; break; case 1 : // b-c dataB = 0b00001111; dataC = 0b00000010; PORTB &= MASK_7SEG_PORTB; // clear data bit PORTB |= dataB; // set data bit 1 PORTC &= MASK_7SEG_PORTC; PORTC |= dataC; break; case 2 : // a-b-d-e-g dataB = 0b00010010; dataC = 0b00000000; PORTB &= MASK_7SEG_PORTB; // clear data bit PORTB |= dataB; // set data bit 1 PORTC &= MASK_7SEG_PORTC; PORTC |= dataC; break; case 3 : // a-b-c-d-g dataB = 0b00000110; dataC = 0b00000000; PORTB &= MASK_7SEG_PORTB; // clear data bit PORTB |= dataB ; // set data bit 1 PORTC &= MASK_7SEG_PORTC; PORTC |= dataC; break; case 4 : // b-c-f-g dataB = 0b00001100; dataC = 0b00000010; PORTB &= MASK_7SEG_PORTB; // clear data bit PORTB |= dataB ; // set data bit 1 PORTC &= MASK_7SEG_PORTC; PORTC |= dataC; break; case 5 : // a-c-d-f-g dataB = 0b00100100; dataC = 0b00000000; PORTB &= MASK_7SEG_PORTB ; // clear data bit PORTB |= dataB ; // set data bit 1 PORTC &= MASK_7SEG_PORTC; PORTC |= dataC; break; case 6 : // a-c-d-e-f-g dataB = 0b00100000; dataC = 0b00000000; PORTB &= MASK_7SEG_PORTB; // clear data bit PORTB |= dataB ; // set data bit 1 PORTC &= MASK_7SEG_PORTC; PORTC |= dataC; break; case 7 : // a-b-c dataB = 0b00001111; dataC = 0b00000000; PORTB &= MASK_7SEG_PORTB; // clear data bit PORTB |= dataB ; // set data bit 1 PORTC &= MASK_7SEG_PORTC; PORTC |= dataC; break; case 8 : // a-b-c-d-e-f-g dataB = 0b00000000; dataC = 0b00000000; PORTB &= MASK_7SEG_PORTB; // clear data bit PORTB |= dataB ; // set data bit 1 PORTC &= MASK_7SEG_PORTC; PORTC |= dataC; break; case 9 : // a-b-c-d-f-g dataB = 0b00000100; dataC = 0b00000000; PORTB &= MASK_7SEG_PORTB; // clear data bit PORTB |= dataB ; // set data bit 1 PORTC &= MASK_7SEG_PORTC; PORTC |= dataC; break; default : // All segments OFF dataB = 0b00111111; dataC = 0b00000010; PORTB &= MASK_7SEG_PORTB; // clear data bit PORTB |= dataB ; // set data bit 1 PORTC &= MASK_7SEG_PORTC; PORTC |= dataC; }}void show_7seg_4digit( uint16_t number ){ uint8_t i; for ( i = 0; i < 4 ; i++) { choose_7seg_digit( -1 ); // 7SEG ALL DIGIT OFF show_7seg_1digit( number % 10 ); choose_7seg_digit( i ); wait(100); number /= 10; }}void getDigitNum(uint16_t num, uint16_t *num1000, uint16_t *num100, uint16_t *num10, uint16_t *num1){ *num1000 = num / 1000; *num100 = ( num % 1000 ) / 100; *num10 = (num % 100 ) / 10; *num1 = (num % 10 ); }/* Blink LEDs */#define SENSING_WAIT_TIME 20#define SENSIG_BLINK_TIME 5void blink_7seg_4digit(uint16_t num, uint8_t blink_number){ uint8_t i; while (blink_number-- > 0) { off_7seg(); wait(SENSIG_BLINK_TIME * 1000); for (i = 0; i< SENSING_WAIT_TIME; i++) { show_7seg_4digit( num ); } }}/* * main */int main(void){ uint16_t num = 0; initIo(); // Warming-UP Infrared detection module for ( num = 99; num > 0; num --) { blink_7seg_4digit(num, 1); } while (1) { show_7seg_4digit( num ); if (isIrsDetect() && num < 999) { num++; while (isIrsDetect()) { // wait for no detection blink_7seg_4digit(num, SENSIG_BLINK_TIME); } } if (isSw0On() && num < 999){ num++; while (isSw0On()) { show_7seg_4digit( num ); } } if (isSw1On() && num > 0){ num--; while (isSw1On()) { show_7seg_4digit( num ); } } } return 0;}# AVR-GCC MakefilePROJECT=mainSOURCES=$(PROJECT).cCC=avr-gccOBJCOPY=avr-objcopyMMCU=atmega168TARGETDEV=m168pCFLAGS=-mmcu=$(MMCU) -Wall$(PROJECT).hex: $(PROJECT).out$(OBJCOPY) -j .text -O ihex $(PROJECT).out $(PROJECT).hex$(PROJECT).out: $(SOURCES)$(CC) $(CFLAGS) -I./ -o $(PROJECT).out $(SOURCES)program: $(PROJECT).hexsudo avrdude -p $(TARGETDEV) -P usb -c avrispmkII -e -U flash:w:$(PROJECT).hexclean:rm -f $(PROJECT).outrm -f $(PROJECT).hex