PICkit3
ICSPCLK
ICSPDAT VSS Ground
VDD MCLR/VPP
Breadboard Challenge 2017
[
BC17-
04]
マイコン・センサ・ライト
チャレンジ1 マイコンにプログラムを書き込む
実体配線図
+
-部品
PIC 12F1822 PIC
12F1822
マイコン
/*
* LED1 : PIN 7 (RA0) : 1-ON, 0-off *
*
* ANALOG PORT : PIN 3 (AN3) *
*/
#include <xc.h> #include <stdint.h>
#define ledOn() (RA0 = 1) #define ledOff() (RA0 = 0)
#define led() (RA0 ^= 1) // LEDを点滅する
// 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
void wait(volatile uint32_t i) {
while (i-- > 0); }
void initIO(void) // I/Oポートの初期化 {
OSCCON = 0b01110010 ; // Internal clock 8MHz
OPTION_REG = 0b00010000 ; // Weak Pull up Resistor RA4-OFF, others ON
ANSELA = 0b00010000 ; // Analog PORT AN3(RA4) TRISA = 0b00011000 ; // Input : RA34, Output: others WPUA = 0b00001000 ; // Weak pullup : RA3
PORTA = 0b00000000 ; // Initialize PORTA }
void main(void) {
int i;
uint8_t adc_value; // 明るさを表す数値
initIO(); // I/Oポートの初期化 initAdc8(); // A/Dコンバータの初期化 initTimer0(); // Timer0の初期化
// LEDが3回点滅 for (i = 0; i < 6; i++) { led();
wait(10000); }
ledOff();
while (1) {
adc_value = getAdc8(); // A/Dコンバータで明るさを検出 if (adc_value < 220) { // 明るいときは LED消灯 ledOff();
} else {
// 暗いときは60秒間点灯する
for (T0count = 0 ; T0count < TIMER0_1SEC * 60; ) { ledOn();
}
// 明るくなるまでLEDを消灯して待つ while (adc_value >= 220) { ledOff();
adc_value = getAdc8(); // A/Dコンバータで明るさを検出 }
} } }
Breadboard Challenge 2017
[
BC17-
04]
マイコン・センサ・ライト
マイコンに書き込むプログラムのソースコード
void initAdc8(void) // A/Dコンバータの初期化 {
ANSELA = 0b00010000; //RA4 Analog port, others digital port ADCON0 = 0b00001101; //0:unimp, 00011:AN3,
0:GOnDone, 1:ADC enable ADCON1 = 0b01000000; //0:Left jutified (8bit res.), //100:Fosc/4 ,0:Reserved, // 00:AVDD=VREF CM1CON0 = 0x00; //Comparator disable }
uint8_t getAdc8(void) // 精度8bitでA/D変換 {
uint8_t d; //ADC wait(10); GO_nDONE = 1; while (GO_nDONE); d = ADRESH; return d; }
void initTimer0(void) // タイマの初期化 {
OPTION_REG &= 0b11111000; // Clear Prescale OPTION_REG |= 0b00000111; // Prescaler 1:256
TMR0 = 0; // initialize Timer0
TMR0IF = 0; // Timer0 interrupt enable TMR0IE = 1; // Enable Timer0 Interrupt GIE = 1; // Enable Interrupt
}
/*
* 1 interrupt
* => 1/8 * 4 (instruction exec clock) * 256(prescaler) * 256 * = 32768 us ,
* 1 sec = 1 / 32768e-6 = 30.517 counts */
static volatile unsigned int T0count; #define TIMER0_1SEC 30
void interrupt InterFunction( void ) // 割り込み関数
{
// Timer0 Interrupt
if (TMR0IF == 1) { // Timer0割り込みが発生したとき TMR0 = 0;
T0count++; TMR0IF = 0; }
A
A
150Ω
茶緑茶金
部品
A
-+4.5V
PIC 12F1822
Breadboard Challenge 2017
[
BC17-
04]
マイコン・センサ・ライト
E
E
100kΩ
茶黒黄金
部品
PIC 12F1822