明るくなると音楽が流れ出す。
音楽が流れているときにLEDが点滅する。
1曲終わると、SLEEPモードに入って電池の消耗をおさえる。
明るくなるとSLEEPモードから抜け出て、再び曲を流す。
ブレッドボード
マイコン Microchip PIC12F1822
カーボン抵抗器 1/4W 150Ω、1/4W 100kΩ
フォトトランジスタ NJL7502L
ジャンパ線 3本
圧電サウンダ (13mm)PKM13EPYH4
LED φ5mm
電池ボックス(単3型×3本用)、バッテリースナップ
XC8 Compiler用
/*
* File: melody-box-sleep-12f1822.c
* Author: kaimu
*
* Created on 2014/02/16
*/
/*
* Input
* INT/RA2 [PIN5] : Opt-transistor ( Dark...1, Blight...0 )
* Output
* RA4 [PIN3] : LED (1...OFF, 0...ON)
* CCP1 (RA5) [PIN2] : piezo buzzer
*/
#include <xc.h>
#define buz() ( RA5 ^= 1 )
#define ledOn() ( RA4 = 0 )
#define ledOff() ( RA4 = 1 )
#define led() ( RA4 ^= 1 )
#define isBright() ( RA2 == 0 )
__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); // VDD High
__CONFIG(PLLEN_OFF & STVREN_ON & WRT_OFF & BORV_LO & LVP_OFF); // VDD LOW
void wait(volatile unsigned long t)
// Delay
{
while(t-- > 0) NOP();
}
void initIo(void)
{
//OSCCON = 0b01111010; // PLL disable, 16MHz internal clock
//OSCCON = 0b01100010; // PLL disable, 8MHz internal clock
OSCCON = 0b01101010; // PLL disable, 4MHz internal clock
//OSCCON = 0b01011010; // PLL disable, 1MHz internal clock
//OSCCON = 0b00111010; // PLL disable, 500kHz internal clock
INTEDG = 0; // Interrupt on falling edge of RB0/INT pin
INTE = 1; // External Interrupt Enable
GIE = 1; //Interrupt Enable
ANSELA = 0b00000001; //RA0 Analog port, others digital port
CM1CON0 = 0x00; //Comparator disable
TRISA = 0b00001101; //I/O Direction. RA1,4-5 : output, RA0,2,3 : Input
LATA = 0b00000000; // PORTA Latch Settings
}
void initPwm(void)
{
//Timer2 settings for PWM standard mode
PR2 = 0; // PWM Period = [(PR2) +1 ]*4*(1/FOSC)*(Timer2 Prescale value)
T2CON = 0b00000010; //00 = Prescaler is 1, 01 = Prescaler is 4 10 = Prescaler is 16 11 = Prescaler is 64
//APFCON &= ~(1<<CCP1SEL);//CCP1 function is on RA2
CCP1SEL = 1;//CCP1 function is on RA5
CCP1CON = 0b00001100; //PWM mode: P1A, P1C active-high; P1B, P1D active-high
CCPR1L = 64; // PWM Duty Cycle (CCPR1L:CCP1CON<5:4>)*(1/FOSC)*(TMR2 Prescale Value)
//Start PWM
TMR2 = 0;
TMR2ON = 1;
}
void soundLed(unsigned char melody)
// Melody format
/*
* (example) 0x12 ... first '1' is 'C', second '2' is length of sound
* first digit : 1 2 3 4 5 6 7 8 0
* sound name : C D E F G A B C rest
*/
{
unsigned int freq[] = {0, 238, 212, 188, 178, 158, 141, 125, 118};
volatile unsigned int oto, nagasa;
oto = (int)(melody & 0xf0)>>4;
nagasa = (int)( melody & 0x0f);
PR2 = freq[oto]; led(); wait((long)nagasa * 10000);
}
void interrupt intr(void)
{
int i;
if (INTF == 1) {
INTF = 0;
}
}
void main(void)
{
volatile unsigned int i;
// "Fujisan"
const unsigned char melody[]
= { 0x53, 0x51, 0x62, 0x52,
0x32, 0x11, 0x21, 0x32, 0x02,
0x23, 0x51, 0x52, 0x41, 0x31,
0x25, 0x02,
0x53, 0x51, 0x32, 0x12,
0x63, 0x71, 0x82, 0x62,
0x53, 0x61, 0x51, 0x41, 0x31, 0x21,
0x15, 0x02,
0x23, 0x21, 0x22, 0x22,
0x11, 0x21, 0x31, 0x41, 0x52, 0x02,
0x63, 0x71, 0x82, 0x62,
0x55, 0x02,
0x84, 0x62, 0x42,
0x34, 0x62, 0x52,
0x42, 0x32, 0x23, 0x11,
0x15, 0x02,
0x04, 0xff };
//Initialization
initIo();
initPwm();
while(1){
ledOff();
SLEEP();
// Play Melody
for (i = 0; melody[i] != 0xff; i++) {
soundLed( melody[i] );
}
}
}