Searching \ for '[PIC] C programming the DSPIC' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/devprogs.htm?key=programming
Search entire site for: 'C programming the DSPIC'.

Exact match. Not showing close matches.
PICList Thread
'[PIC] C programming the DSPIC'
2011\09\18@094907 by xavierpereira

picon face

#include <libpic30.h>
#include <p30f4011.h>

// Configuration settings
_FOSC(CSW_FSCM_OFF & FRC_PLL16);  // Fosc=16x7.5MHz, Fcy=30MHz
_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 = read_analog_channel(0);
               PDC1 = (int)((voltage / 1023.0) * 2 * PTPER);

       }

       return 0;
}

void configure_pins()
{
       // Configure RD0 as a digital output
       LATD = 0;
       TRISD = 0b11111110;

       // Configure analog inputs
       TRISB = 0x01FF;      // Port B all inputs
       ADPCFG = 0xFF00;     // Lowest 8 PORTB pins are analog inputs
       ADCON1 = 0;          // Manually clear SAMP to end sampling, start
conversion
       ADCON2 = 0;          // Voltage reference from AVDD and AVSS
       ADCON3 = 0x0005;     // Manual Sample, ADCS=5 -> Tad = 3*Tcy = 0.1us
       ADCON1bits.ADON = 1; // Turn ADC ON

       // Configure PWM for free running mode
       // PWM period = Tcy * prescale * PTPER = 0.33ns * 64 * 9470 = 20ms
       PWMCON1 = 0x00FF; // Enable all PWM pairs in complementary mode
       PTCON = 0;
       _PTCKPS = 3;      // prescale=1:64 (0=1:1, 1=1:4, 2=1:16, 3=1:64)
       PTPER = 9470;     // 20ms PWM period (15-bit period value)
       PDC1 = 0;         // 0% duty cycle on channel 1 (max is 65536)
       PTMR = 0;         // Clear 15-bit PWM timer counter
       _PTEN = 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 = channel;          // Select the requested channel
       ADCON1bits.SAMP = 1;      // start sampling
       __delay32(30);            // 1us delay @ 30 MIPS
       ADCON1bits.SAMP = 0;      // start Converting
       while (!ADCON1bits.DONE); // Should take 12 * Tad = 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 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=0.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 = 0;         // 0% duty cycle on channel 1 (max is 65536)


Please help!!
-- View this message in context: old.nabble.com/C-programming-the-DSPIC-tp32489501p32489501.html
Sent from the PIC - [PIC] mailing list archive at Nabble.com.

2011\09\20@053812 by cdb

flavicon
face
::I want get freq of 80K so I can drive my Mosfet

Is your DSPIC running with the same crystal frequency - you might need to alter the divisor in the Macro.

As far as the duty cycle is concerned, if 0 = 0 duty cycle and 65536 = 100%, then it would seem logical that 50% would be somewhere around half of 65536, but I don't know if the PDC1 takes 16 bit numbers, if it is a 16 bit register then that would be a good place to start, if it isn't you need to look at the formula in the datasheet for the chip.

Colin
--
cdb, spam_OUTcolinTakeThisOuTspambtech-online.co.uk on 20/09/2011
Web presence: http://www.btech-online.co.uk   Hosted by:  http://www.justhost.com.au
 This email is to be considered private if addressed to a named  individual or Personnel Department, and public if addressed to a blog,  forum or news article.

More... (looser matching)
- Last day of these posts
- In 2011 , 2012 only
- Today
- New search...