Genuino(Arduino)101で複数pinに対応したPWM周波数の変更を試してみました。
#include "aux_regs.h" #include "interrupt.h" #include "conf.h" #define pinCnt 2 // number of PIN #define PWMPeriod 300 // Hz typedef struct { unsigned int nbr; unsigned int duty; unsigned int isState; unsigned long highTicks; unsigned long lowTicks; unsigned long nextTicks; } pin_t; unsigned long totalTicks; pin_t PWMPin[pinCnt]; void setup() { PWMPin[0].nbr = 13; //PWM pin assign PWMPin[1].nbr = 6; //PWM pin assign PWMPin[0].duty = 30; //duty % PWMPin[1].duty = 70; //duty % totalTicks = 32000000 / PWMPeriod; for (int i = 0; i < pinCnt; i++) { pinMode(PWMPin[i].nbr, OUTPUT); PWMPin[i].isState = 0; PWMPin[i].highTicks = (totalTicks * PWMPin[i].duty) / 100; PWMPin[i].lowTicks = totalTicks - PWMPin[i].highTicks; PWMPin[i].nextTicks = 0; digitalWrite(PWMPin[i].nbr, PWMPin[i].isState); } // Timer1 Initialization. aux_reg_write(ARC_V2_TMR1_CONTROL, 0); aux_reg_write(ARC_V2_TMR1_LIMIT, 0); aux_reg_write(ARC_V2_TMR1_COUNT, 0); // Timer1 interrupt start. interrupt_connect(ARCV2_IRQ_TIMER1, timer1Isr); aux_reg_write(ARC_V2_TMR1_LIMIT, 10000); aux_reg_write(ARC_V2_TMR1_CONTROL, ARC_V2_TMR_CTRL_NH | ARC_V2_TMR_CTRL_IE); aux_reg_write(ARC_V2_TMR1_COUNT, 0); interrupt_enable(ARCV2_IRQ_TIMER1); } void loop() { } void timer1Isr() { // Timer1 counter RESET. aux_reg_write(ARC_V2_TMR1_COUNT, 0); //PWM for (int i = 0 ; i < pinCnt; i++) { if (PWMPin[i].nextTicks == 0) { PWMPin[i].isState = 1 - PWMPin[i].isState; digitalWrite(PWMPin[i].nbr, PWMPin[i].isState); PWMPin[i].nextTicks = (PWMPin[i].isState ? PWMPin[i].highTicks : PWMPin[i].lowTicks); } } //nextTicks unsigned long minTicks = PWMPin[pinCnt - 1].nextTicks; for (int i = 0; i < (pinCnt - 1); i++) { if (PWMPin[i].nextTicks < minTicks) minTicks = PWMPin[i].nextTicks; } for (int i = 0; i < pinCnt; i++) { PWMPin[i].nextTicks = PWMPin[i].nextTicks - minTicks; } // Timer1 set aux_reg_write(ARC_V2_TMR1_CONTROL, ARC_V2_TMR_CTRL_NH | ARC_V2_TMR_CTRL_IE); aux_reg_write(ARC_V2_TMR1_LIMIT, minTicks); }
このスケッチでは、2つのピンでPWM出力をしてます。周波数を300Hz、Duty比はそれぞれ30、70に設定。totalTicksを各々のピン毎に設定するようにスケッチをすこし修正すれば、ピン毎に違う周波数での出力も可能です。
UNOのように簡単にはできないようです。もう少し簡単にできないものでしょうか。
コメント