/* * Copyright (c) 2022 Tokyo Seiden CO.,LTD. * All Right Reserved. * File: main.c * Created 2022/1/12 * *  Timer2 周期,CCPR1L デューティ:8bit */ #include #include #include #include "time.h" typedef char CHAR; typedef signed int int16; typedef unsigned int Uint16; typedef signed long int32; typedef signed long LONG; typedef unsigned char BYTE; typedef unsigned int WORD; typedef unsigned long DWORD; BYTE const SINDATA[200][2]={ 0 , 1 , 6 , 1 , 13 , 1 , 19 , 1 , 25 , 1 , 31 , 1 , 37 , 1 , 43 , 1 , 50 , 1 , 56 , 1 , 62 , 1 , 68 , 1 , 73 , 1 , 79 , 1 , 85 , 1 , 91 , 1 , 96 , 1 , 102 , 1 , 107 , 1 , 112 , 1 , 117 , 1 , 122 , 1 , 127 , 1 , 132 , 1 , 136 , 1 , 141 , 1 , 145 , 1 , 150 , 1 , 154 , 1 , 158 , 1 , 161 , 1 , 165 , 1 , 168 , 1 , 172 , 1 , 175 , 1 , 178 , 1 , 180 , 1 , 183 , 1 , 185 , 1 , 188 , 1 , 190 , 1 , 191 , 1 , 193 , 1 , 195 , 1 , 196 , 1 , 197 , 1 , 198 , 1 , 199 , 1 , 199 , 1 , 199 , 1 , 199 , 1 , 199 , 1 , 199 , 1 , 199 , 1 , 198 , 1 , 197 , 1 , 196 , 1 , 195 , 1 , 193 , 1 , 191 , 1 , 190 , 1 , 188 , 1 , 185 , 1 , 183 , 1 , 180 , 1 , 178 , 1 , 175 , 1 , 172 , 1 , 168 , 1 , 165 , 1 , 161 , 1 , 158 , 1 , 154 , 1 , 150 , 1 , 145 , 1 , 141 , 1 , 136 , 1 , 132 , 1 , 127 , 1 , 122 , 1 , 117 , 1 , 112 , 1 , 107 , 1 , 102 , 1 , 96 , 1 , 91 , 1 , 85 , 1 , 79 , 1 , 73 , 1 , 68 , 1 , 62 , 1 , 56 , 1 , 50 , 1 , 43 , 1 , 37 , 1 , 31 , 1 , 25 , 1 , 19 , 1 , 13 , 1 , 6 , 1 , 0 , 0 , 6 , 0 , 13 , 0 , 19 , 0 , 25 , 0 , 31 , 0 , 37 , 0 , 43 , 0 , 50 , 0 , 56 , 0 , 62 , 0 , 68 , 0 , 73 , 0 , 79 , 0 , 85 , 0 , 91 , 0 , 96 , 0 , 102 , 0 , 107 , 0 , 112 , 0 , 117 , 0 , 122 , 0 , 127 , 0 , 132 , 0 , 136 , 0 , 141 , 0 , 145 , 0 , 150 , 0 , 154 , 0 , 158 , 0 , 161 , 0 , 165 , 0 , 168 , 0 , 172 , 0 , 175 , 0 , 178 , 0 , 180 , 0 , 183 , 0 , 185 , 0 , 188 , 0 , 190 , 0 , 191 , 0 , 193 , 0 , 195 , 0 , 196 , 0 , 197 , 0 , 198 , 0 , 199 , 0 , 199 , 0 , 199 , 0 , 199 , 0 , 199 , 0 , 199 , 0 , 199 , 0 , 198 , 0 , 197 , 0 , 196 , 0 , 195 , 0 , 193 , 0 , 191 , 0 , 190 , 0 , 188 , 0 , 185 , 0 , 183 , 0 , 180 , 0 , 178 , 0 , 175 , 0 , 172 , 0 , 168 , 0 , 165 , 0 , 161 , 0 , 158 , 0 , 154 , 0 , 150 , 0 , 145 , 0 , 141 , 0 , 136 , 0 , 132 , 0 , 127 , 0 , 122 , 0 , 117 , 0 , 112 , 0 , 107 , 0 , 102 , 0 , 96 , 0 , 91 , 0 , 85 , 0 , 79 , 0 , 73 , 0 , 68 , 0 , 62 , 0 , 56 , 0 , 50 , 0 , 43 , 0 , 37 , 0 , 31 , 0 , 25 , 0 , 19 , 0 , 13 , 0 , 6 , 0 , }; // CONFIG1 #pragma config FOSC = INTOSC // Oscillator Selection (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 CPD = OFF // Data Memory Code Protection (Data 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 = ON // Internal/External Switchover (Internal/External Switchover mode is enabled) #pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled) // CONFIG2 #pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off) #pragma config PLLEN = ON // PLL Enable (4x PLL 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 LVP = ON // Low-Voltage Programming Enable (Low-voltage programming enabled) BYTE counter; BYTE ADV[8]; // 移動平均用Buff int16 Sum; // 加算結果 int16 Vin_LPF; // LPF後の値 BYTE Duty; // 最大199 void TMR2_Initialize(void) { PR2 = 199; // 32Mz/4/4/2MHz 基準CLK // 2MHz÷50Hz = 40k:一波の基準CLK数 // √(40000)=200、パルス分解能200PWMで200回動くと1波になる IRCF0 = 0; //内蔵CLK8MHz IRCF1 = 1; //内蔵CLK8MHz IRCF2 = 1; //内蔵CLK8MHz IRCF3 = 1; //内蔵CLK8MHz T2CON = 0x01; // prescaler 1/4 off postscaler 1/1 TMR2 = 0x0; T2CONbits.TMR2ON = 1; PIR1bits.TMR2IF = 0; // Timer2 PIE1bits.TMR2IE = 1; // Timer2 PEIE = 1 ; // GIE = 1 ; // } void __interrupt() Lo_Isr(void) // Low LEVEL { if(PIR1bits.TMR2IF) // Timer2 { PIR1bits.TMR2IF = 0; // Reset Interrupt if(++counter >= 200) { counter=0; } Duty = (((Uint16)SINDATA[counter][0] * Vin_LPF) >> 8) & 0xFF; CCPR1L = Duty ; // ここでデュティを変える LATA5 = SINDATA[counter][1]; // Pole } } void EPWM_Initialize (void) { // CCP1M P1A: active high; P1B: active high; DC1B 3; P1M single; CCP1CON = 0x0C; CCP1CONbits.DC1B = 0b00; // CCPL1L8bit + bit1 +bit0 // CCP1ASE operating; PSS1BD low; PSS1AC low; CCP1AS disabled; ECCP1AS = 0x00; // P1RSEN automatic_restart; P1DC 0; PWM1CON = 0x80; // STR1B P1B_to_port; STR1A P1A_to_CCP1M; STR1SYNC start_at_begin; PSTR1CON = 0x01; CCPR1L = 0; // ここでデュティを変える CCPR1H = 0x00; } void PIN_MANAGER_Initialize(void) { LATA = 0x00; TRISA = 0x1B; // 0:output RA2,RA5 0b01 1011 ANSELA = 0x10; // 0:digital 1:analog:RA5 0b01 0000 WPUA = 0x2F; // Weak pllup 1:enable OPTION_REGbits.nWPUEN = 0; APFCON = 0x00; // RA2 is CCP1 } void main(void) { static BYTE count; // AD result PIN_MANAGER_Initialize(); EPWM_Initialize(); TMR2_Initialize(); /* 10bitAD 初期化 */ ADCON0 = 0x0F; // AN3 ADON,GO ADCON1 = 0x23; // Left fosc/32 InternalVref FVRCON = 0x83; // VrefEnable,4.096V // ADRESH 0; ADRESL 0; ADRESH = 0x00; ADRESL = 0x00; for(count=0 ; count<8 ; count++) { ADV[count] = 0; // clear buff } count=0; // 初期化 Sum = 0; // 加算 while(1) { if(ADCON0bits.GO_nDONE == 0) //AD完了 { if(++count >= 8) { count=0; } Sum -= ADV[count]; // 引いてから加算する、最初は0なのでOK ADV[count] = ADRESH; // 上位8bitのみでOK ADCON0 = 0x0f; // AD3 ,ADON,GO 再トリガ Sum += ADV[count]; // 加算する Vin_LPF = Sum >> 3; // 移動平均 } } }