電極に触ると、LEDが光りだし、およそ30秒ごとにLEDの色が変わります。
約3分後にアラームが鳴り響きます。
途中で電極に触ると、時間を待たずにLEDの色が変わっていきます。
フルカラーLED (カソードコモン) OST A5131A-R/PG/B
マイコン PIC 12F1822
カーボン抵抗器 1/4W、150Ω
圧電サウンダ
電線(単線0.5mm)
電池ボックス 単3電池3本型、バッテリースナップ式
バッテリースナップ
(XC8コンパイラ用)
・動作
(1) 電源投入時、ピーッと音が鳴る。
(2) 電極に触るまで待機状態となる。
(3) 電極に触ると、ピッと音が鳴り、緑色に点滅する。
(4) 約30秒ごとに水色→青色→紫色→橙色→赤の順に変化する。
(5) ピーポピーポと警報音が鳴り、(2)待機状態に戻る
/* * File: rgb-led-touch-12f1822.c * Author: kaimu * * Created on 2013/12/27, 10:42 * Modified on 2013/12/29 * Modified on 2013/12/30 Add Timer 0 * Modified on 2013/12/31 New Color Pattern * * * target deveice : PIC12f1822 * I/O * ( INPUT ) * PIN7 : RA0 / CPS3 : Capacitive Sensing Pad * * ( OUTPUT ) * PIN3 : RA4 : Red LED : 1=ON, 0=OFF * PIN5 : RA2 : Blue LED : 1=ON, 0=OFF * PIN6 : RA1 : Green LED : 1=ON, 0=OFF * PIN7 : RA5 : Piezo Speaker */#include <xc.h>#define Red() ( RA4 ^= 1)#define Blue() ( RA2 ^= 1 )#define Green() ( RA1 ^= 1 )#define RedOff() ( RA4 = 0)#define BlueOff() ( RA2 = 0 )#define GreenOff() ( RA1 = 0 )#define RedOn() ( RA4 = 1)#define BlueOn() ( RA2 = 1 )#define GreenOn() ( RA1 = 1 )#define buz() ( RA5 ^= 1 )__CONFIG( FOSC_INTOSC & WDTE_OFF & PWRTE_ON & MCLRE_OFF & CP_OFF & CPD_OFF & BOREN_ON & CLKOUTEN_OFF & IESO_OFF & FCMEN_OFF);__CONFIG(PLLEN_OFF & STVREN_ON & WRT_OFF & BORV_HI & LVP_OFF);volatile unsigned int TouchCounter = 0;
void wait(volatile unsigned long t)
// Delay{while(t-- > 0) NOP();
}volatile unsigned int CPSV[4]; // Capacitive Sensing parameter Threshold value for CPS0,1,2,3
#define CPSPERIOD 10 // Capacitive sensing Counting period
void initCPSV(void)// Initialize Touch ON/OFF threshold value to 95% of Initial CPS value{ volatile unsigned int i;
unsigned int cpsv0;
// CaPacitive Sensing settings CPSCON0 = 0b00001000; // Oscillator is in Medium Range. Charge/Discharge Current is nominally 1.2uA // Timer1 settings T1CON = 0b11000001 ; // TIMER1 clock source is CAPOSC. Prescaler 1:1, TIMER1 ON TMR1H = 0 ; // Initialize TIMER1 TMR1L = 0 ; PEIE = 1 ; // Peripheral Interrupt Enable GIE = 1 ; // General Interrupt Enable CPSON = 1 ; // CPS module ON for (i = 0; i <= 3; i++) { CPSON = 0; // CPS module off CPSCON1 = i; // CPS channel 0,1,2,3 select. CPSON = 1; // CPS module on TMR1H = 0; TMR1L = 0; // Initialize TIMER1 wait(CPSPERIOD); cpsv0 = (TMR1H<<8) + TMR1L; // Get CPS Counted value CPSV[i] = (unsigned int)((float)cpsv0 * 0.95); // When pad is touched, under 95% of initial value
}}int isTouch(unsigned int padno)
// Touch Sensor using Capacitive Sensing module// RETURN VALUE:// 0 : Pad is NOT touched.// -1 : Pad is TOUCHED.{ unsigned int v; // CPS module output value
int touched; CPSON = 0; // CPS module off if (padno > 3) { return 0; // Invalid port no
} else { CPSCON1 = padno; // CPS channel 0,1,2 select. } CPSON = 1; // CPS module on TMR1H = 0; TMR1L = 0; // Initialize TIMER1 wait(CPSPERIOD); v = (TMR1H<<8) + TMR1L; // Get CPS Counted value if (v < CPSV[padno]) { touched = -1; } else { touched = 0; } return touched;}void initIo(void)// Initialize I/O ports{ OSCCON = 0b01111010; // PLL disable, 16MHz internal clock GIE = 1; //Interrupt enable
ANSELA = 0b00000001; // RA0 analog port, others digital port CM1CON0 = 0x00; //Comparator disable // 76543210
TRISA = 0b00001001; //I/O Direction. RA1245:output, RA03 : input
LATA = 0b00000000; // PORTA Latch Settings
}void initTimer0(void){ TMR0 = 0x00; // Initialize TIMER0 TMR0CS = 0; //Internal instruction cycle clock (FOSC/4) PSA = 0; //Prescaler is assigned to the Timer0 module PS2 = 1; PS1=1; PS0 = 1; // Prescaler Rate Select bits set 1:256 TMR0IE = 1; // Timer0 Intterupt Enable PEIE = 1; GIE =1;}void startBeep(void){ volatile unsigned int j;
for ( j = 0; j < 2000; j++) { buz(); wait(10); }}void intervalBeep(void){ volatile unsigned int j;
for ( j = 0; j < 1000; j++) { buz(); wait(10); }}void alarmBeep(void){ volatile unsigned int i, j;
for ( i = 0; i < 5; i++ ) { for ( j = 0; j < 4000; j++) { buz(); wait(8); } for ( j = 0; j < 2000; j++) { buz(); wait(16); } }}void interrupt isr(void)// When Interrupt catched ...{ volatile static long counter = 0;
if (TMR0IF) { // Timer0 Interruption TMR0IF = 0; // Clear Timer0 Interrupt Flag TMR0 = 0; // Clear Timer1 counter counter++; if (counter > 1831) { // about every 30 sec. // ( 1 sec. = 61.03515625 // Interruptted every 0.016384 sec. ) TouchCounter++; counter = 0; } }}void senseTouch(void){ if ( isTouch(0) ) { TouchCounter++; intervalBeep(); while (isTouch(0)); } }void main(void) { initIo(); initCPSV(); initTimer0(); startBeep(); while (1) { senseTouch(); switch (TouchCounter) { case 0:
TMR0IF = 0; TMR0IE = 0;// Timer0 Interrupt Disable RedOff(); GreenOff(); BlueOff(); break; case 1:
TMR0IF = 0; TMR0 = 0; TMR0IE = 1;// Timer0 Interrupt Enable RedOff(); Green(); BlueOff(); wait(10000); break; case 2:
RedOff(); Green(); Blue(); wait(10000); break; case 3:
RedOff(); GreenOff(); Blue(); wait(10000); break; case 4:
Red(); GreenOff(); Blue(); wait(10000); break; case 5:
Red(); Green(); BlueOff(); wait(10000); break; case 6:
Red(); GreenOff(); BlueOff(); wait(10000); break; case 7:
Red(); Green(); Blue(); alarmBeep(); TouchCounter++; break; default: TMR0IF = 0; TMR0IE = 0;// Timer0 Interrupt Disable RedOff(); GreenOff(); BlueOff(); TouchCounter = 0; } } return;}