マイコンを使って、決められた時間が経つとブザが鳴るタイマ。
ブレッドボード
PICマイコン PIC12F1822P
カーボン抵抗器 150Ω (茶緑茶金)
LED
圧電ブザ
バッテリスナップ
電池ボックス(単3×3本用)
※プログラムを書き込んだ後には、一度5本のケーブルを外し、電源を入れ直す必要がある。
最初にLEDが数回点滅し、ブザが鳴る。
次に、センサを触ると、3分間LEDが点滅し、3分経過するとブザが鳴る。
(1ファイル版)bc17-05-1file-led-timer-capsensor-pic12f1822.zip
(分割ファイル版)bc17-05-led-timer-capsensor-pic12f1822.zip
以下は分割ファイル版のコード
File: user_def.h
/*
* User customizing Definition
*/
#define FIRSTFLASHBLINK 1 // When system boot, First flash "FIRSTFLASH" times blink
#define TIMELIMIT 180L // Time limitation 3 minutes (180sec.)
File: main_led_timer_capsensor_12f1822.c
/*
* target device : PIC12f1822
* XC8 compiler
*
* I/O
* ( INPUT )
* PIN7 : RA0 / CPS3 : Capacitive Sensing Module
*
* ( OUTPUT )
* PIN3 : RA4 : Buzzer
* PIN5 : RA2 : LED : 1=ON, 0=OFF
*/
#include <xc.h>
#include "kaimu_config.h"
#include "kaimu_io.h"
#include "capsensor.h"
#include "kaimu_timer0.h"
#include "kaimu_interrupt.h"
#include "kaimu_sound.h"
#include "user_def.h"
void wait(volatile unsigned long t)
// Delay
{
while(t-- > 0) NOP();
}
volatile unsigned long T0Counter;
void main(void) {
int i;
initIo();
initCPSV();
initTimer0();
// first flash
for ( i = 0; i < FIRSTFLASHBLINK * 2; i++ ) {
led();
wait(20000);
}
// first sound
bootBeep();
while (1) {
if (isTouch(0)) {
startBeep();
while (isTouch(0)); // wait until pad untouched
ledOn();
T0Counter = 0L;
while (T0Counter < T0UNIT * TIMELIMIT) {
led(); wait(100 + (T0UNIT * TIMELIMIT - T0Counter) * 10);
}
alarmBeep();
} else {
ledOff();
}
}
}
File : kaimu_config.h
/*
* Configuration Bit
* for pic12f1822
*/
// Configuration 1
#pragma config FOSC = INTOSC // Internal Clock
#pragma config WDTE = OFF // NO Watchdog timer
#pragma config PWRTE = ON // Program start at Power on after 64ms
#pragma config MCLRE = OFF // No Ext Reset, Enable RA3 input pin
#pragma config CP = OFF // No Code protect
#pragma config CPD = OFF // No Data Protect
#pragma config BOREN = ON // Brown out enable
#pragma config CLKOUTEN = OFF // CLKOUT as RA4
#pragma config IESO = OFF // No Internal External clock
#pragma config FCMEN = OFF // No FCM
// Configuration 2
#pragma config WRT = OFF // No Write Protect
#pragma config PLLEN = OFF // No PLL
#pragma config STVREN = ON // Reset on Stack over / under flow
#pragma config BORV = HI // Watch Voltage drop (2.5V = HI)
#pragma config LVP = OFF // No Low Voltage Programming
File : kaimu_io.h
/*
* I/O port control macros.
* Target device : PIC12F1822
*/
#define led() ( RA2 ^= 1)
#define ledOff() ( RA2 = 0)
#define ledOn() ( RA2 = 1)
#define buz() ( RA4 ^= 1 )
void initIo(void);
File : kaimu_io.c
/*
* I/O port settings
* Target device : PIC12F1822
*/
#include <xc.h>
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
}
File : capsensor.h
/*
* Capacitive Sensing Module
* Target device : PIC12F1822
*/
void cps_wait(volatile unsigned long );
void initCPSV(void);
int isTouch(unsigned int );
File : capsensor.c
/*
* Capacitive Sensing Module
* Target device : PIC12F1822
*/
#include <xc.h>
#include "capsensor.h"
void cps_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
cps_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
cps_wait(CPSPERIOD);
v = (TMR1H<<8) + TMR1L; // Get CPS Counted value
if (v < CPSV[padno]) {
touched = -1;
} else {
touched = 0;
}
return touched;
}
File : kaimu_timer0.h
/*
* Timer0 functions prototype
* Target device : PIC12F1822
*/
#define T0UNIT 61 // Timer0Counter about 1sec ( 1 / (1 / ((Clock 16MHz / 4) / prescaler 256) * 256)
void initTimer0(void);
FIle : kaimu_timer0.c
/*
* Timer0 setting function
* Target device : PIC12F1822
* XC8Compiler
*
*/
#include <xc.h>
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;
}
File : kaimu_sound.h
/*
* Sound functions prototype
*/
void sound_wait(volatile unsigned long );
void bootBeep(void);
void startBeep(void);
void alarmBeep(void);
File : kaimu_sound.c
/*
* Sound functions
* for XC8 compiler
*/
#include <xc.h>
#include "kaimu_io.h"
#include "kaimu_sound.h"
void sound_wait(volatile unsigned long t)
// Delay
{
while(t-- > 0) NOP();
}
void bootBeep(void)
{
volatile unsigned int i, j;
for ( i = 0; i < 2; i++) {
for (j = 0; j < 1000; j++) {
buz();
sound_wait(10);
}
sound_wait(10000);
}
}
void startBeep(void)
{
volatile unsigned int j;
for (j = 0; j < 1000; j++) {
buz();
sound_wait(10);
}
}
void alarmBeep(void)
{
volatile unsigned int i, j;
for ( i = 0; i < 5; i++ ) {
for ( j = 0; j < 4000; j++) {
buz();
sound_wait(8);
}
for ( j = 0; j < 2000; j++) {
buz();
sound_wait(16);
}
}
}
File : kaimu_interrupt.h
/*
* Interrupt
* Target device : PIC12F1822
* XC8 compiler
*/
void interrupt isr(void);
File : kaimu_interrupt.c
/*
* Interrupt function
* Target device : PIC12F1822
* XC8 compiler
*/
#include <xc.h>
#include "kaimu_interrupt.h"
extern volatile unsigned long T0Counter; // declare in main_***.c
void interrupt isr(void)
// When Interrupt catched ...
{
if (TMR0IF) {
// Timer0 Interruption
TMR0IF = 0; // Clear Timer0 Interrupt Flag
TMR0 = 0; // Clear Timer1 counter
T0Counter++;
}
}