見出し画像

PIC18F27Q43 プログラミング1 内蔵オシレータFosc=32Mhzで、LEDを点滅させる。


PIC18F27Q43を使って、Lチカさせるまでの内容。レジスタを直接叩いて、マイコンを制御するのも面白い。今回は、内臓オシレータを使って、Fosc=32Mhz に設定し、LEDを点滅させます。


1.PIC18F27Q43 主なスペック

電源 1.8v~5.5v
プログラムメモリ 128kbytes
Data SRAM      8192bytes
Data EEPROM     1024
IO 25本 PPSで端子機能の任意振り分け可能。
CPUクロック 最速64Mhz
DMA 6個
割り込み ベクターテーブルで、各割り込みごとに独自処理、
     もしくは、従来のPICマイコンと同じく単一ベクタで処理。

従来の18Fより機能が追加。16ビット、32ビットマイコンに標準搭載されていたDMAや割り込みベクタが採用されています。その分、少し戸惑いますが、少しずつ理解していきましょう。

7.1 System Arbitration ハードアクセスメモリシステム調停

データシートP.28 
The system arbiter resolves memory access
System Level Section ( Main, Interrupt Service Routine )
peripheral selection ( DMA and Scanner)
システムレベルセクションと周辺機能セクションで、メインメモリアクセス占有権の調停システム。あらかじめ、優先度をそれぞれのセクションに割り振ることで、メモリの同時アクセスを防ぐ。

7.3 8x8 Hardware Multiplie 乗算器

データシートP.30 
8x8 hardware multiplier 
乗算器 1cycleで計算してくれるので、使えればとても処理速度が上がりそう、なのですが、よくわからない。確実に実行するには、アセンブラで直接、乗算器で計算したほうがいいみたいです。コンパイラがこのハードウェア乗算器をどのように使っているか、分かっていないのではっきりしたことが言えません。
たとえば、

int val=100;
int val1 = 200;
int answer, answer1;
answer = val * val1;
answer1 = val1 * val;

この場合、answer1よりanswerのほうが、処理速度が速くなるらしい。
大量に乗算を使うことはあまりないので、深追いしません。

7.4 PIC18 Instruction Cycle 命令サイクル数

データシートP.33
7.4.1 Instruction Flow/Pipelining An “Instruction Cycle” consists of four cycles of the oscillator clock.
オシレータクロック4つで、1命令を実行。従来の16F,18Fと同じ。

9.1 Program Memory Organization プログラムメモリ概要

データシートP.61
PIC18 microcontrollers implement a 21-bit Program Counter, which is capable of addressing a 2 Mbyte program memory space.

Program Flash Memory(64kW)
0x00000
    |            
0x1FFFF(2,097,151)
8ビット幅 2Mbyteですが、命令は16bits幅なので、実際は、1MWord空間。

9.1.1 Memory Access Partition プログラムメモリのセクション分割

以下、3つのブロックを選択的に組みこめる。
• Application block
• Boot block
• Storage Area Flash (SAF) block
Application block
Block Application block is where the user’s firmware resides by default.
Boot Block
Boot block is an area in program memory that is ideal for storing bootloader code. Code placed in this area can be executed by the CPU.
Storage Area Flash
Storage Area Flash (SAF) is the area in program memory that can be used as data storage. 

Program Flush Memoryのパーティション構成

プログラムエリアを、ブート領域、ユーザーアプリケーション領域、データストレージ領域に分割できる。

10. NVM - Nonvolatile Memory Module

The Nonvolatile Memory (NVM) module provides run-time read and write access to the Program Flash Memory (PFM), Data Flash Memory (DFM) and Configuration bits.
ROM領域を、実行時にアクセスするときに使います。

2.オシレータ設定 内蔵オシレータ   Fosc=32Mhz

データシートP195
12. OSC - Oscillator Module (With Fail-Safe Clock Monitor)

18F27/47/57Q43 Clock Source Block Diagram

外付けクロック、内臓オシレータは、クロックソースセレクタNOSCとDividerを通過して、最終的に、CPUに供給されるクロックシステムクロックFoscウォッチドッグタイマーなどのperipheralに供給されます。
3系統の動作クロックに分かれているので、sleep時に、動作クロックを個別に、on/offを選択できるようになっています。
MFINTOSCは、主にウォッチドッグタイマーに使用します。

今回の内蔵オシレータFosc=32Mhz の作り方。

最初に、コンフィグレーションビット RSTOSCを内蔵オシレータ64Mhzにします。パワーオンリセット時、内臓オシレータは64Mhzで起動します。

// CONFIG1
#pragma config FEXTOSC = OFF    // External Oscillator Selection (Oscillator not enabled)
#pragma config RSTOSC = HFINTOSC_64MHZ// Reset Oscillator Selection (HFINTOSC with HFFRQ = 64 MHz and CDIV = 1:1)

次に、コンフィグレーションビットCSWENをONにします。これで、実行中にシステムクロックを変更できます。

// CONFIG2
#pragma config CLKOUTEN = OFF   // Clock out Enable bit (CLKOUT function is disabled)
#pragma config PR1WAY = OFF     // PRLOCKED One-Way Set Enable bit (PRLOCKED bit can be set and cleared repeatedly)
#pragma config CSWEN = ON       // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)

次に、main関数内で、初期動作64Mhzを2で割って32Mhzにします。OSCCON3レジスタのCSWHOLDビットを使用して、変更したクロックが安定したときに新しいクロックで動作させるようにしました。本来は、Sleepモードから復帰した時のクロック変更を安定させるためのビットです。
2で割る指定は、OSCCON1レジスタのNDIVビットを1にセットします。

void oscillatorInit(void)
{
    //オシレータ設定----------------
    OSCCON3bits.CSWHOLD=1;    //Hold
    OSCCON1bits.NDIV=1;       //64Mhz/2=32Mhz;
    while(!OSCCON3bits.NOSCR);//NewOscillatroReady?
    while(!PIR0bits.CSWIF);   //Clock Switch Interrupt Flag
    PIR0bits.CSWIF=0;         //Clear by software
    OSCCON3bits.CSWHOLD=0;    //NewOsc proceed.
    while(!OSCCON3bits.ORDY);
}

ソースコード

// PIC18F27Q43 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1
#pragma config FEXTOSC = OFF    // External Oscillator Selection (Oscillator not enabled)
#pragma config RSTOSC = HFINTOSC_64MHZ// Reset Oscillator Selection (HFINTOSC with HFFRQ = 64 MHz and CDIV = 1:1)

// CONFIG2
#pragma config CLKOUTEN = OFF   // Clock out Enable bit (CLKOUT function is disabled)
#pragma config PR1WAY = OFF     // PRLOCKED One-Way Set Enable bit (PRLOCKED bit can be set and cleared repeatedly)
#pragma config CSWEN = ON       // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)

// CONFIG3
#pragma config MCLRE = EXTMCLR  // MCLR Enable bit (If LVP = 0, MCLR pin is MCLR; If LVP = 1, RE3 pin function is MCLR )
#pragma config PWRTS = PWRT_OFF // Power-up timer selection bits (PWRT is disabled)
#pragma config MVECEN = ON      // Multi-vector enable bit (Multi-vector enabled, Vector table used for interrupts)
#pragma config IVT1WAY = ON     // IVTLOCK bit One-way set enable bit (IVTLOCKED bit can be cleared and set only once)
#pragma config LPBOREN = OFF    // Low Power BOR Enable bit (Low-Power BOR disabled)
#pragma config BOREN = SBORDIS  // Brown-out Reset Enable bits (Brown-out Reset enabled , SBOREN bit is ignored)

// CONFIG4
#pragma config BORV = VBOR_1P9  // Brown-out Reset Voltage Selection bits (Brown-out Reset Voltage (VBOR) set to 1.9V)
#pragma config ZCD = OFF        // ZCD Disable bit (ZCD module is disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCON)
#pragma config PPS1WAY = OFF     // PPSLOCK bit One-Way Set Enable bit (PPSLOCKED bit can be cleared and set only once; PPS registers remain locked after one clear/set cycle)
#pragma config STVREN = ON      // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = ON         // Low Voltage Programming Enable bit (Low voltage programming enabled. MCLR/VPP pin function is MCLR. MCLRE configuration bit is ignored)
#pragma config XINST = OFF      // Extended Instruction Set Enable bit (Extended Instruction Set and Indexed Addressing Mode disabled)

// CONFIG5
#pragma config WDTCPS = WDTCPS_31// WDT Period selection bits (Divider ratio 1:65536; software control of WDTPS)
#pragma config WDTE = OFF       // WDT operating mode (WDT Disabled; SWDTEN is ignored)

// CONFIG6
#pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
#pragma config WDTCCS = SC      // WDT input clock selector (Software Control)

// CONFIG7
#pragma config BBSIZE = BBSIZE_512// Boot Block Size selection bits (Boot Block size is 512 words)
#pragma config BBEN = OFF       // Boot Block enable bit (Boot block disabled)
#pragma config SAFEN = OFF      // Storage Area Flash enable bit (SAF disabled)
#pragma config DEBUG = OFF      // Background Debugger (Background Debugger disabled)

// CONFIG8
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot Block not Write protected)
#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers not Write protected)
#pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM not Write protected)
#pragma config WRTSAF = OFF     // SAF Write protection bit (SAF not Write Protected)
#pragma config WRTAPP = OFF     // Application Block write protection bit (Application Block not write protected)

// CONFIG10
#pragma config CP = OFF         // PFM and Data EEPROM Code Protection bit (PFM and Data EEPROM code protection disabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#include <stdbool.h>
#define _XTAL_FREQ 32000000

void portInit(void);
void oscillatorInit(void);

void main(void)
{
    //CPUハード初期化-----------------------
    portInit();
    oscillatorInit();
    __delay_ms(100);   
    LATAbits.LATA0=0;
    LATBbits.LATB0=1;
    while(1)
    {
        LATAbits.LATA0=~LATAbits.LATA0;
        LATBbits.LATB0=~LATBbits.LATB0;
        __delay_ms(500);
    }
    return;
}

void portInit(void)
{
    PORTA=0x00;
    LATA=0x00;
    ANSELA=0x00;
    TRISA=0x00;
       
    PORTB=0x00;
    LATB=0x00;
    ANSELB=0x00;
    TRISB=0x00;
    
    PORTC=0x00;
    LATC=0x00;
    ANSELC=0x00;
    TRISC=0x00;
}

void oscillatorInit(void)
{
    //オシレータ設定----------------
    OSCCON3bits.CSWHOLD=1;    //Hold
    OSCCON1bits.NDIV=1;       //64Mhz/2=32Mhz;
    while(!OSCCON3bits.NOSCR);//NewOscillatroReady?
    while(!PIR0bits.CSWIF);   //Clock Switch Interrupt Flag
    PIR0bits.CSWIF=0;         //Clear by software
    OSCCON3bits.CSWHOLD=0;    //NewOsc proceed.
    while(!OSCCON3bits.ORDY);
}

上記、コードでLEDを点滅させました。

この記事が気に入ったらサポートをしてみませんか?