#include #include // Configuration settings _FOSC(CSW_FSCM_OFF & FRC_PLL16); // Fosc=3D16x7.5MHz, Fcy=3D30MHz _FWDT(WDT_OFF); // Watchdog timer off _FBORPOR(MCLR_DIS); // Disable reset pin // Function prototypes void configure_pins(); unsigned int read_analog_channel(int n); int main() { int voltage; // Set up which pins are which configure_pins(); while(1) { // Analog input 0 controls PWM 1 duty cycle. voltage =3D read_analog_channel(0); PDC1 =3D (int)((voltage / 1023.0) * 2 * PTPER); } return 0; } void configure_pins() { // Configure RD0 as a digital output LATD =3D 0; TRISD =3D 0b11111110; // Configure analog inputs TRISB =3D 0x01FF; // Port B all inputs ADPCFG =3D 0xFF00; // Lowest 8 PORTB pins are analog inputs ADCON1 =3D 0; // Manually clear SAMP to end sampling, start conversion ADCON2 =3D 0; // Voltage reference from AVDD and AVSS ADCON3 =3D 0x0005; // Manual Sample, ADCS=3D5 -> Tad =3D 3*Tcy =3D 0.1= us ADCON1bits.ADON =3D 1; // Turn ADC ON // Configure PWM for free running mode // PWM period =3D Tcy * prescale * PTPER =3D 0.33ns * 64 * 9470 =3D 20ms PWMCON1 =3D 0x00FF; // Enable all PWM pairs in complementary mode PTCON =3D 0; _PTCKPS =3D 3; // prescale=3D1:64 (0=3D1:1, 1=3D1:4, 2=3D1:16, 3=3D1:= 64) PTPER =3D 9470; // 20ms PWM period (15-bit period value) PDC1 =3D 0; // 0% duty cycle on channel 1 (max is 65536) PTMR =3D 0; // Clear 15-bit PWM timer counter _PTEN =3D 1; // Enable PWM time base } // This function reads a single sample from the specified // analog input. It should take less than 2.5us if the chip // is running at about 30 MIPS. unsigned int read_analog_channel(int channel) { ADCHS =3D channel; // Select the requested channel ADCON1bits.SAMP =3D 1; // start sampling __delay32(30); // 1us delay @ 30 MIPS ADCON1bits.SAMP =3D 0; // start Converting while (!ADCON1bits.DONE); // Should take 12 * Tad =3D 1.2us return ADCBUF0; } I have got this code from one of my friend... I have made some changes and it actually works fine but the Problem is=20 I m confused with some macros' as I m going to drive my mosfet with this code. I want get freq of 80K so I can drive my Mosfet PWM time should be 1/f=3D0.0125 ms I tried changing my PTPER but its still doesnt give me 80K frequency what else should I update inorder to get a freq of 80K and if I wanted to set Duty cycle on PDC1 to 50% what should the number to put in the line below PDC1 =3D 0; // 0% duty cycle on channel 1 (max is 65536) Please help!! --=20 View this message in context: http://old.nabble.com/C-programming-the-DSPIC= -tp32489501p32489501.html Sent from the PIC - [PIC] mailing list archive at Nabble.com. --=20 http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .