1.目的
先日、制作したPICでのLED点滅制御の回路及びプログラムで、ソースコード上では定期的なdelayを挿入したにも関わらず、実行結果では明らかに不定期な点滅となっていることを確認した。
上記の挙動を確認した後、LEDの点灯プログラムを作成したところ、点灯ではなく不定期な点滅となることを確認された。
これらの結果から、不具合の原因は以下の 点考えられると仮説を立てた。
1.PICの発振子の不具合
内部オシレータの不具合により、不規則なクロックとなり正常にプログラムが作動しなかった可能性。
2.Configuration Bitsの設定ミス
内部発振子の設定や、その他PICの挙動に影響するConfiguration Bitsの定義ミスに依る可能性。
3.利用したPIC自体の個体差、初期不良
利用していたPIC自体の個体差や初期不良により、作成したソースコード通りの挙動が行われなかった。
1と3はめったにないというか、可能性が低いので2から攻めていった結果、Lチカに成功した。
成功した環境と実験回路、ソースコードは以下。
最近はBitConfigurationをGUIで生成できるので便利。
2.実験環境
PicKit3
MPLAB X IDE v3.15
PIC18F14K50仕様USB対応超小型マイコンボード; http://akizukidenshi.com/catalog/g/gK-05499/
LED × 1
3.実験回路
Pickit - PIC18F14K50仕様USB対応超小型マイコンボード、Pickitコネクタ
RCx - LED アノード
GND - LED カソード
4.ソースコード
以下、LED点灯のコード。
/*
* File: newmain.c
* Author: kashiharaakira
*
* Created on December 12, 2015, 11:30 AM
*/
# include <xc.h>
# define _XTAL_FREQ 32000000
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG1L
# pragma config CPUDIV = NOCLKDIV// CPU System Clock Selection bits (No CPU System Clock divide)
# pragma config USBDIV = OFF // USB Clock Selection bit (USB clock comes directly from the OSC1/OSC2 oscillator block; no divide)
// CONFIG1H
# pragma config FOSC = IRC // Oscillator Selection bits (Internal RC oscillator)
# pragma config PLLEN = ON // 4 X PLL Enable bit (Oscillator multiplied by 4)
# pragma config PCLKEN = ON // Primary Clock Enable bit (Primary clock enabled)
# pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor disabled)
# pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)
// CONFIG2L
# pragma config PWRTEN = OFF // Power-up Timer Enable bit (PWRT disabled)
# pragma config BOREN = OFF // Brown-out Reset Enable bits (Brown-out Reset disabled in hardware and software)
# pragma config BORV = 30 // Brown-out Reset Voltage bits (VBOR set to 3.0 V nominal)
// CONFIG2H
# pragma config WDTEN = OFF // Watchdog Timer Enable bit (WDT is controlled by SWDTEN bit of the WDTCON register)
# pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)
// CONFIG3H
# pragma config HFOFST = OFF // HFINTOSC Fast Start-up bit (The system clock is held off until the HFINTOSC is stable.)
# pragma config MCLRE = OFF // MCLR Pin Enable bit (RA3 input pin enabled; MCLR disabled)
// CONFIG4L
# pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
# pragma config LVP = OFF // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
# pragma config BBSIZ = OFF // Boot Block Size Select bit (1kW boot block size)
# pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
// CONFIG5L
# pragma config CP0 = OFF // Code Protection bit (Block 0 not code-protected)
# pragma config CP1 = OFF // Code Protection bit (Block 1 not code-protected)
// CONFIG5H
# pragma config CPB = OFF // Boot Block Code Protection bit (Boot block not code-protected)
# pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM not code-protected)
// CONFIG6L
# pragma config WRT0 = OFF // Table Write Protection bit (Block 0 not write-protected)
# pragma config WRT1 = OFF // Table Write Protection bit (Block 1 not write-protected)
// CONFIG6H
# pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers not write-protected)
# pragma config WRTB = OFF // Boot Block Write Protection bit (Boot block not write-protected)
# pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
// CONFIG7L
# pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 not protected from table reads executed in other blocks)
# pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 not protected from table reads executed in other blocks)
// CONFIG7H
# pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot block not protected from table reads executed in other blocks)
void DelayMs(int dlyms);
void main(void) {
OSCCON = 0b01100000;
TRISC = 0b11111100;
while(1){
PORTC = 0b11111111;
}
}
void DelayMs(int dlyms){
while(dlyms--){
__delay_ms(1);
}
}
5.参考サイト
6.履歴
2015年12月24日 新規記入 樫原 輝
2015年1月3日 参考サイト挿入 樫原 輝
2016年2月7日 少しコメントを追記