/* * File: PWM_2.c * Author: kubalik * * Created on 25. unora 2016, */ #include "p33EV32GM002.h" // je to v C:\Program Files (x86)\Microchip\xc16\v1.24\support\dsPIC33E\h // od radky 8852 je definovani tech TRIS // dale si udelejte CntrlF a hledejet ve file *.h /* nasledujici makra nastavuji oscilator * hledame v header file * * Macros for setting device configuration registers * pin OSC1 jako I/O pin * vypinaji Watchdog - opet se podivejte do header file, co vsecno se da nastavit * */ _FOSCSEL( FNOSC_FRC ) _FOSC( OSCIOFNC_ON & IOL1WAY_OFF) _FWDT( FWDTEN_OFF ) unsigned int j; /* * */ int main() { int i; i = 0; /* sem si dejte breakpoint ! */ j = 0; TRISA = 0x0; // zapisujeme do celeho registru najednou TRISB = 0x00; PORTB = 0; PORTA = 0; ANSELB = 0; ANSELA = 0; /* viz poznamka na konci */ IFS5bits.PWM1IF = 0; /* 1:1 Prescaler */ PTCON2 = 0x0000; /* Set PWM Periods on PHASEx Registers */ PHASE1 = 3685; PHASE2 = 737; PHASE3 = 921; /* Set Duty Cycles */ PDC1 = 1228; PDC2 = 210; PDC3 = 84; /* Set Dead Time Values */ DTR1 = DTR2 = DTR3 = 0; ALTDTR1 = ALTDTR2 = ALTDTR3 = 0; /* Set PWM Mode to Complementary */ IOCON1 = IOCON2 = IOCON3 = 0xC000; /* Set Independent Time Bases, Edge-Aligned Mode and Independent Duty Cycles */ PWMCON1 = PWMCON2 = PWMCON3 = 0x0200; /* Configure Faults */ FCLCON1 = FCLCON2 = FCLCON3 = 0x0003; /* Enable PWM Module */ PTCON = 0x8000; /* a jeste k tomu PWM1 generuje preruseni - strana 12 TRGIEN bit (PWMCON<10>). */ PWMCON1 = 0x0600; /* Configure Faults */ FCLCON1 = FCLCON2 = FCLCON3 = 0x0003; /*To configure interrupts, * the application software should first clear the interrupt flags, * enable the interrupts (see the PTCON (Register 14-1), * STCON (Register 14-4), and PWMCONx (Register 14-12) registers), * and finally, enable the PWM module. */ /* a interrupty - strana 98 PWM.pdf */ PTCON = 0x0800; /* finally Enable PWM Module */ PTCONbits.PTEN = 1; /* povoluji preruseni od PWM1 */ IEC5bits.PWM1IE = 1; IFS5bits.PWM1IF = 0; /* globalni povoleni preruseni, tohle se musi povolit, * jinak nebude zadne preruseni krome TRAP */ INTCON2bits.GIE = 1; while(1) { i++; /* tady proste musi byt nejaka instrukce, jinak to nesimuluje interrupty */ } return 0; } void __attribute__((interrupt, auto_psv)) _PWM1Interrupt (void) { IFS5bits.PWM1IF = 0; /* shazujeme pozadavkovy bit */ j++; /* sem si dejte breakpoint */ } /* zaviraci od podprogramu _PWM1Interrupt retfie prida prekladac sam, je to dano atributem interrupt */