0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

PIC16F1765でPWMをする

Last updated at Posted at 2022-08-02

PIC16F1765について

 PIC16F1765は、秋月電子通商さんでお求めやすい価格で販売されているマイコンです。
https://akizukidenshi.com/catalog/g/gI-15238/

発売日が2020年4月21日と比較的新しい製品であるため、2022年8月現在でもインターネット上の情報がとても少ないです。
このマイコンを使ったPWMの動作に苦労しましたので、情報共有のために記事を残します。

苦労したところ - 16-bit PWM(PWM5)

 多くのPICマイコンは、CCP1CONレジスタやPWM5CONレジスタなどを設定して、TIMER2を動かしてやればPWMが動きます。
しかしPIC16F1765には新しく(?)、PWM5LDCON(PWMxLDCON)レジスタが設けられています。

PWM5のレジスタの変更を有効にする
    PWM5LDCONbits.LDA = 1;

 このLDAビットを設定してからでないと次のレジスタ値が反映されないようです。
このレジスタ達も古いマイコンには無かったものです。

PWM カウンタの設定
    PWM5PHH = 0x00;    // 位相カウント
    PWM5PHL = 0x00;
    PWM5PRH = 0xFF;    // 周期カウント
    PWM5PRL = 0xFF;
    PWM5OFH = 0x00;    // オフセットカウント
    PWM5OFL = 0x00;

 これら以外にもデューティーサイクルのレジスタにも影響します。

デューティーサイクル
    PWM5DCH = 0x80;    // デューティーサイクル
    PWM5DCL = 0x80;

調べ残り(要検証)

 Standard PWM(CCP1)や10-bit PWM(PWM3)については、LDAビットは関係が無いので、タイマー設定など旧来の方法で動くはずですが、私が試していたときは動きませんでした。
 最終的にはPWM5が動くようになったら、CCP1/PWM3ともに動いていました。どこかのレジスタ設定が間違っていたのかもしれません。

コード全体

 次はPWMの動作を確認するためのコードです。
出力ピンにオシロスコープやLEDなどをつけてPWMを確認してください。

コードサンプル

// CONFIG1
#pragma config FOSC = INTOSC    // Oscillator Selection Bits (INTOSC oscillator: I/O function on CLKIN pin)
#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 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 = OFF       // Internal/External Switchover Mode (Internal/External Switchover Mode is disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled)

// CONFIG2
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
#pragma config PPS1WAY = OFF    // Peripheral Pin Select one-way control (The PPSLOCK bit cannot be cleared once it is set by software)
#pragma config ZCD = OFF        // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR)
#pragma config PLLEN = ON       // Phase Lock Loop enable (4x PLL is always 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 LPBOR = OFF      // Low-Power Brown Out Reset (Low-Power BOR is disabled)
#pragma config LVP = ON         // Low-Voltage Programming Enable (Low-voltage programming enabled)

#define _XTAL_FREQ 32000000

#include <xc.h>

void main(void) {
    
    OSCCON = 0b01110000;        // 8MHz up to 32MHz(PLLx4)
    //
    ANSELA = 0b00000000;
    ANSELC = 0b00000000;
    TRISA  = 0b00001000;
    TRISC  = 0b00000000;
    //
    LATA = 0x00;
    LATC = 0x00;
    
    //
    CCPTMRS = 0x00;
    //
    // Timer2
    T2PR     = 0xFF;
    T2CLKCON = 0b0001;
    T2CON    = 0b10000000;
    
    // 16bit PWM
    PWM5LDCONbits.LDA = 1;
    PWM5CLKCON = 0x00;
    
    PWM5PHH = 0x00;
    PWM5PHL = 0x00;
    PWM5PRH = 0xFF;
    PWM5PRL = 0xFF;

    PWM5CON = 0b10000000;
    
    RC0PPS  = 0b10000;

    PWM5DCH = 0x80;
    PWM5DCL = 0x80;
    
    //
    // 10bit PWM
    PWM3CON = 0b10000000;
    
    RC4PPS  = 0b01110;
    
    PWM3DCH = 0x33;
    PWM3DCL = 0x33;
    //
    
    // CCP Standard PWM
    CCP1CON = 0b10011100;       // EN[7],FMT[4],MODE[3-0]
    
    RC3PPS  = 0b01100;
    
    CCPR1H  = 0x80;
    CCPR1L  = 0x80;

    while(1) { }
}

参考

 詳しくはデータシートを参照してください。
https://akizukidenshi.com/download/ds/microchip/PIC16F1764_PIC16LF1769.pdf
「16-BIT PULSE-WIDTH MODULATION (PWM) MODULE」の章を読み込めば何かわかるかもです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?