home    2013/04/14


MPLAB X IDE の勉強、XC8 のCコンパイラーを使ってみました。

シュミレーターを使って動作を確かめました。動かなかったバグは config の設定でした。皆さんもよく確認してください。

PIC18F14K50 の10ビットA/Dコンバーターを使用して電圧計の作成です。

PICマイコンPIC18F14K50−I/P(USB内蔵)   

・20ピンUSBフラッシュマイクロコントローラ
・ナノワットXLPテクノロジー
・USB2.0インターフェース内蔵
・USB用デュアルアクセス256バイトRAM内蔵
・プログラムメモリ:8Kバイト
・SRAM:768バイト
・EEPROM:256バイト
・I/O:最大17ch※内1ピン(MCLR)は、インプット専用
 パッケージ:DIP20ピン

 

回路図  A0,A1につながっているICは無視してください。

使ってみての感想は アセンブラーより簡単、C言語を勉強する必要がある。

PICその物の設定は細かくする必要あり。

実際のプログラムは最後の20行程度なのですが、それまで延々とPIC指定の必要あり。

/*
* File: newmain.c Author: A
*PIC18F14K50 Created on 2013/03/28, 12:23
*/
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#pragma config USBDIV = OFF     //;USB CLOCK SELECTION BIT
#pragma config MCLRE = OFF     //;MCLR PIN ENABLE BIT
#pragma config IESO = OFF     //;Internal/External Oscillator Switchover bit
#pragma config FCMEN = OFF //;Fail-Safe Clock Monitor Enable bit
#pragma config FOSC = IRC     //;OSCILLATOR SELECTION BITS
#pragma config PWRTEN = ON //;Power-up Timer Enable bit
#pragma config PLLEN = OFF     //;X PLL ENABLE BIT(OSCILLATOR MULTIPLIED BY 4)
#pragma config PCLKEN = OFF //;PRIMARY CLOCK ENABLE BIT
#pragma config BOREN = OFF     //;Brown-out Reset Enable bits
#pragma config LVP = OFF     //;Single-Supply ICSP Enable bit
#pragma config CPUDIV=NOCLKDIV //;CPU SYSTEM CLOCK SELECTION BIT
#pragma config CP0 = OFF     //;Code Protection bit
#pragma config CP1 = OFF     //;Code Protection bit
#pragma config CPD = OFF     //;Data EEPROM Code Protection bit
#pragma config CPB = OFF     //;Boot Block Code Protection bit
#pragma config WDTEN = OFF //;Watchdog Timer Enable bit
#pragma config HFOFST = OFF,STVREN = OFF,BBSIZ = OFF,XINST = OFF
#pragma config EBTR0 = OFF,EBTR1 = OFF,EBTRB = OFF
#define _XTAL_FREQ 8000000
const unsigned char seg_data[16] = {0x40, 0x79, 0x24, 0x30, 0x19, 0x12, 0x02, 0x58, 0x00, 0x18,0x08, 0x03, 0x27, 0x21, 0x06, 0x0E };
const unsigned char scan[4] = { 0b11101111,0b11011111,0b10111111,0b01111111};
unsigned char digit_no;     // 表示走査桁指定
unsigned char dsp_buf[4]={0,0,0,0}; // 表示バッファー
int dsp_cnt;                     // 表示用カウント
void main()
{
OSCCON = 0b01101010 ; //8MHz 選択
TRISA = 0b11111111;
TRISB = 0b00000000;
TRISC = 0b00000000;
PORTA = 0x00;                 // ポート初期化。
PORTB = 0b11111111;     //全桁消灯
PORTC = 0b11111111;     // ポート初期化。
digit_no = 0;                     // 走査桁NoをLSDに初期化
dsp_cnt = 0;                     // 表示カウンタを初期化
unsigned int temp1,temp2; // 10進数変換に使用
int i;                             // 10進数変換に使用
int n;                             // 10進数変換に使用
ADCON1 = 0b00001000;     //PVCFG 10 NVCFG 00
ADCON2 = 0b10111111;     //右ADFM1,ACCQT111ADCS111(FRC)
REFCON0= 0b10100000;     //FVR 2.048V
ANSELH = 0b00000000 ;     //ana off
ANSEL = 0b00001000 ;     //RA4アナログ ANSELH=0;
ADCON0 = 0b00001111;     //RA4,ADON 変換開始


while(1){
while(GODONE){ }             // A/D変換済みなら
dsp_cnt =ADRESL+(ADRESH*256) ; //
GODONE = 1;                 //次の A/D変換開始
temp1 = dsp_cnt;             // dsp_cntを4桁の10進数に変換
n = 1000;
for(i = 3; i >= 0; i--){
temp2 = temp1 / n;         // 千、百、十、一の個数を計算
dsp_buf[i] = (unsigned char)temp2; // 表示バッファーに代入
temp1 = temp1 % n;         // 余りを求める
n /= 10;
}
__delay_ms(5) ;
PORTB = 0b11111111;         //全桁消灯
PORTC = seg_data[dsp_buf[digit_no]] ;//7セグDATA出力
PORTB = (scan[digit_no]) ;     // 走査ビット出力
if(++digit_no == 4){digit_no = 0;} // 表示桁を1桁目に設定
}
}
 

バグあったら連絡してください。