持ち物
- PIC Kit 5
- 2024年時点で最新の公式の書き込み器。18000円くらいして震える。
- Mac(M1 Max)
- MPLAB X IDE v6.20
- XC8(v2.46)
- 8ビットマイコン用のコンパイラ
- PIC16F1827
- 抵抗
- 10k x 1: プルアップ抵抗。MCLRのピンとVDDの間に設置
- 330 x 1: LEDに直列接続で繋げる抵抗
- LED
- 5mm青色LED。基本手元にあるもので良い。
最初何から始めればいいかわからなかったけど結局できたからそのメモ。
MPLAB X IDEをインストールして新規でプロジェクトを作成。Userフォルダ「MPLABXProjects
」直下に
L-chika PIC16F1827の名前でプロジェクトを作った。

勝手に中に色々ファイル作られる。main.cは自分で今回のプログラム用に作ったC言語ファイル。この中に以下のコードを記述
// PIC16F1827 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1
#pragma config FOSC = HS // Oscillator Selection (HS Oscillator, High-speed crystal/resonator connected between OSC1 and OSC2 pins)
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled)
#pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled)
#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = ON // Internal/External Switchover (Internal/External Switchover mode is enabled)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)
// CONFIG2
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
#pragma config PLLEN = ON // PLL Enable (4x PLL enabled)
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config DEBUG = OFF // In-Circuit Debugger Mode (In-Circuit Debugger disabled, ICSPCLK and ICSPDAT are general purpose I/O pins)
#pragma config LVP = ON // Low-Voltage Programming Enable (Low-voltage programming enabled)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#define _XTAL_FREQ 4000000 //4MHz
void main(void) {
TRISA=0x00;
//PORTAbits.RA0 = 1;
while(1){
__delay_ms(5);
RA0 = 1;
__delay_ms(5);
RA0 = 0;
}
return;
}
5msでLEDをチカチカさせよ、と。細かいマイコンの設定情報が大部分を占めているが他の参考サイトから引っ張ってきたもので詳しくよくわかってない。一旦そのままでも問題なかろう。
ビルドする

プロダクション用に真ん中のClean and Build Projectを実行。
うまくいけば下記のような表示に。
CLEAN SUCCESSFUL (total time: 56ms)
make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf
make -f nbproject/Makefile-default.mk dist/default/production/L-chika_PIC16F1827.X.production.hex
make[2]: Entering directory '/Users/kilian/MPLABXProjects/L-chika PIC16F1827.X'
"/Applications/microchip/xc8/v2.46/bin/xc8-cc" -mcpu=16F1827 -c -mdfp="/Applications/microchip/mplabx/v6.20/packs/Microchip/PIC12-16F1xxx_DFP/1.7.242/xc8" -fno-short-double -fno-short-float -O0 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -DXPRJ_default=default -msummary=-psect,-class,+mem,-hex,-file -ginhx32 -Wl,--data-init -mno-keep-startup -mno-osccal -mno-resetbits -mno-save-resetbits -mno-download -mno-stackcall -mno-default-config-bits -std=c99 -gdwarf-3 -mstack=compiled:auto:auto -o build/default/production/main.p1 main.c
make[2]: Leaving directory '/Users/kilian/MPLABXProjects/L-chika PIC16F1827.X'
make[2]: Entering directory '/Users/kilian/MPLABXProjects/L-chika PIC16F1827.X'
"/Applications/microchip/xc8/v2.46/bin/xc8-cc" -mcpu=16F1827 -Wl,-Map=dist/default/production/L-chika_PIC16F1827.X.production.map -DXPRJ_default=default -Wl,--defsym=__MPLAB_BUILD=1 -mdfp="/Applications/microchip/mplabx/v6.20/packs/Microchip/PIC12-16F1xxx_DFP/1.7.242/xc8" -fno-short-double -fno-short-float -O0 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -msummary=-psect,-class,+mem,-hex,-file -ginhx32 -Wl,--data-init -mno-keep-startup -mno-osccal -mno-resetbits -mno-save-resetbits -mno-download -mno-stackcall -mno-default-config-bits -std=c99 -gdwarf-3 -mstack=compiled:auto:auto -Wl,--memorysummary,dist/default/production/memoryfile.xml -o dist/default/production/L-chika_PIC16F1827.X.production.elf build/default/production/main.p1
Memory Summary:
Program space used 20h ( 32) of 1000h words ( 0.8%)
Data space used 3h ( 3) of 180h bytes ( 0.8%)
EEPROM space used 0h ( 0) of 100h bytes ( 0.0%)
Configuration bits used 2h ( 2) of 2h words (100.0%)
ID Location space used 0h ( 0) of 4h bytes ( 0.0%)
make[2]: Leaving directory '/Users/kilian/MPLABXProjects/L-chika PIC16F1827.X'
BUILD SUCCESSFUL (total time: 689ms)
Loading code from /Users/kilian/MPLABXProjects/L-chika PIC16F1827.X/dist/default/production/L-chika_PIC16F1827.X.production.hex...
Program loaded with pack,PIC12-16F1xxx_DFP,1.7.242,Microchip
Loading completed
ここでマイコンの種類と書き込みに使うデバイスを指定しておく。アプリ画面左側のプロジェクトツリーの該当プロジェクトを右クリックしてPropertiesをクリック


接続
PIC Kit 5 調べる限り以下の8端子あるうちの右側の5端子の使用だけでよかった。

これをマイコンの下記に接続する。

バッテリーは5V電源を安定化電源から持ってきてVDDとVSSにそれぞれ接続した。
MCLRとVDDの間はプルアップ抵抗を入れた方が安定するそうなので10kΩを間に入れた。
出力ピンは17番ピンのRA0としてそこからLED(アノードとカソードの向き注意)と抵抗330Ωを入れた。

マイコンにプログラムを書き込む
真ん中のProgram Device for Productionをクリックする

次の表示がでたら「OK」で。

うまく書き込まれたら下記のような表示になって

無事に動く!
というわけでひとまず開発の環境が整ったことは確認できた。