Вы находитесь на странице: 1из 28

Introduction to MicrochipTM, Digital Signal Controllers (dsPIC30F)

Microchip Web Site: http://www.microchip.com

Dermot Breen , http://eleceng.dit.ie/dermot


Information for guidance only no liability for inaccuracies or omissions

Dublin Institute of Technology http://www.dit.ie Last updated February 2010

Review of main MicrochipTM Digital Signal Controller (dsPIC30F) modules


Digital Signal Controller The digital control of power electronic devices can be achieved with microcontrollers, digital signal processors (DSP) or digital signal controllers (DSC). Digital signal controllers have the advantage of combining the input/output functionality of microcontrollers with the mathematical processing power of digital signal processors. There is a vast array of such devices however we will look at the dsPIC30F from Microchip. Programming this device is achieved using Microchips MPLAB IDE and the C30 compiler. Programming is achieved using the c programming language. Programming digital signal controllers (DSC) for controlling power electronic devices requires initializing the device modules and then programming the control routine. Device initialization can be achieved via the Visual Initializer in the Tools menu of MPLAB.

Oscillator clock

The oscillator clock can be sourced from an external 4 -10 MHz. crystal or internal RC 7.37 MHz. clock. The oscillator frequency can be scaled up by a factor of 4, 8 or 16 or reduced by 4, 16 or 64. The instruction cycle frequency fcy is of the oscillator frequency fosc. If we use a 10 MHz crystal and increase this by a factor of 4 using the internal phased lock loop (PLL) module we will obtain an oscillator fosc frequency of 40 MHz. The internal instruction cycle is of the oscillator frequency therefore the DSC instruction cycle frequency will be 10 MHz. The following are the oscillator registers in the DSC OSCCON and OSCTUN

Watchdog timer The watchdog timer is used to monitor the system. A reset is activated if a program does not run properly. For testing the watchdog timer is usually disabled.

Digital Input/Output Ports (I/0) The dsPIC30F is fitted with a number of digital I/O ports which are designated with letters B, C, D etc. Setting port direction is achieved with the TRISx register where x indicates port name. However the Visual Initializer in the Tools menu of MPLAB can be used to set I/O port directions. Setting and unsetting ports is achieved with the PORTx and LATx registers. To set bit 0 of port B which may for example turn on a power MOSFET we can use _RB0 = 1; // set bit 0 on port B decimal number

To transfer value at bit 2 on input port C to bit 3 of output port B which may transfer a closed switch condition to a power transistor. _RB3 = _RC2 ; // transfer value on bit 2 port C to bit 3 //on Port B

To set a full port using binary PORTF = 0b01001010; // sets bit 1, 3 and 6 Timers The dsPIC30F has up to 5, 16 bit timers. Two timers can be combined to create 32 bit timers. Timers are driven by the instruction cycle clock but this clock can be prescaled (reduced) by a factor of 8, 64 or 256. Accurate timers are ideal for the control of power electronic devices as Pulse width modulation (PWM), frequency and pulse width measurement can be achieved quire easily. There are three registers in the timer modules and they are(x indicates timer number 1,2,3,4 or 5.): TMRx 16 bit counter PRx period register TxCON control register

fcy instruction cycle clock

Prescaler 8/64/256

Timer clock

16 bit TMRx counter 0 - 65536

Creating a delay with a timer can be achieved in code as follows once the timer is set up and an appropriate period set. TMR2 = 0; while(TMR2 < 100); // reset timer counter // wait for 100 timer clock cycles make sure // period set satisfies clock cycles delay
3

Analogue to digital converter (ADC) The DSC has either 10 bit (210 , 1024) or 12 bit (212 4096) ADCs. These convert an analogue input value to its digital representation. Analogue signals are normally sampled at a predetermined rate and the maximum frequency that can be converted without aliasing is half the sampling frequency. Aliasing is the effect of higher frequency signals being seen as low frequency signals by the ADC conversion process. When an ADC read is requested a switch closes and samples the analogue signal. After this sample time the ADC converts the analogue value to its digital representation. The ADC uses a reference voltage to control the full-scale input value. If a reference voltage of 5 V is used on a 10 bit ADC the resolution is 5 / 102 = 5 / 1024 = 0.0049 V/bit. Model of analogue to digital conversion circuit.
Analogue source Rs
ADC input

Digital Signal Controller Ri


Sampling switch

Sampling capacitor

It is very important that the analogue signal is sampled long enough to fully charge the sampling capacitor to the analogue input value. This is a function of the RC ( ) time constant in the ADC circuit. The sample time should be at least 5 times the circuits RC time constant.

Communications The DSC has a number of communication mechanisms, which allow the DSC communicate with external devices such as computers, PLCs and sensors. The communication buses are UART, SPI, I 2C and CAN. Interrupts A powerful control technique is the use of interrupts. A program can be interrupted based on some particular event such as a switch closing, the end of a timer period etc. The interrupt code is executed when the interrupt occurs and the system returns to the main program after the interrupt event.

Digital Signal Controller specifications Refer to the Microchip dsPIC30F data sheet provided extract and answer the following questions What is the maximum MIPS for the device. What is the frequency range for an external clock source and the oscillator input. By what factors can the oscillator frequency be increased using the systems Phased Locked loop module (PLL). How many interrupt sources are there? How many cycles does it take to conduct DSP instructions. What current levels can the I/O/ pins source/sink. What bit depth are the timers/counters and what is the maximum number of steps. Give your answer in decimal form. List three types of communications protocols/interfaces on the device How many ADCs are there? What is their resolution and what is the maximum sampling rate. If the ADC uses a 3 V reference what is the voltage resolution achieved. How many pins are there on the device? List the pin numbers for Port F. List the pin numbers associated with the ADC inputs. List the positive voltage and ground pins.

dsPIC project setup


Software required Download the following programs from Microchip:- http://www.microchip.com and install in the following order PICkit 2, MPLAB, C30 compiler student edition. Project setup Create a directory for your project files e.g. c:\microchipProjects\projectOne Load MPLAB and create a new project using the project - project wizard menu item. Chose device:Activate toolsuite:Toolsuite contents:Project name:Project directory:Add linker file for specific chip e.g C:\Program files\microchip\ MPLABC30\support\gld\p30f4011.gld e.g. dsPIC30F4011 Microchip C30 Toolsuite MPLAB C30 Compiler e.g. projectOne browse to project directory created

Finish

Create a new c program file and name it main.c, save it in your project directory and type the following example template program code into the window. Add the file to your project via Project add files to project menu option. // // // Author: Date: Project:

#include <p30fxxxx.h> #include "Generated Files\VDIInit\init_dsPIC30F4011.sinit_vi.h"

void main(void) { VisualInitialization(); while(1); }// end main

Note: the .h include file and the function VisualInitializaion(). These are the reference files to a powerful graphical environment for setting up initialization code automatically for all the modules on the dsPIC30F chips such as Clock, I/O Ports, timers, ADC etc.

Microchip MPLAB Integrated Development Environment Project Files Visual Initializer

Program

Visual Initializer The Visual Initializer is a graphical drag and drop feature in Microchips MPLAB IDE for setting initial parameters of on chip modules such as the Oscillator, Timers, ADC I/O, UART, I2C PWM etc. The following provides example setups for various modules. Individual setups may be different depending on the chip used and initial parameters required. Change systems Parameters Change system parameters so that dsPIC30F 40-Pin PDIP package type is chosen (Note changing this item after modules are set-up will reset modules)

Oscillator Oscillator Configuration:- 29.48 MHz internal fast RC instruction cycle clock.

10

Watchdog Timer Disable watchdog timer module located in Reset

11

Input/Output Ports There are a number of Ports on the dsPIC30F, B,C,D,F and CN (Change notification). Set port directions

12

Analogue to Digital (ADC) There are a number of Analogue to Digital Converters ADCs on port B of the dsPIC30F. Set ADC parameters

13

16 bit Timer There are a number of 16 bit timers and 32 bit timers on dsPIC30F. Set up timer clock from instruction cycle clock and timer period. An interrupt can be generated from the period end.

14

Code Generation Once all the modules are set up run from Visual Initializer - code generation options and click all three options. Then run from Visual Initializer code generation and three new files will appear in the project window, which provide all the necessary module initialization code.

15

Digital Signal Controller Pulse Width Modulation Set-up


Visual Initializer Settings for Pulse Width modulation (PWM) Actual time period represents PWM period. Number of clock pulses for actual time period represents resolution of PWM signal. The settings below are based on an internal clock frequency of 29.48 MHz. producing a timer (PWM) frequency of 28.789 kHz. This provides a 10 bit (1024) PWM resolution. The PWM output appears on the designated pin (OC1) based on the output compare module settings. The initial PWM duty cycle setting in HEX is 200. Setting it to 0 will provide the off condition.

Program code To change the PWM duty cycle with a 10 bit ADC value add the following line of code to the project program OC1RS = newVALUE; where newValue may be an ADC read value. To set up a variable frequency square wave override the visualizer period register with a new period e.g. PR2 = newValue; and set OC1RS to half this value e.g. OC1RS = PR2/2;

16

Digital Signal Controller - Analogue to Digital (ADC) Set-up

Additional initialisation code ( not necessary in V8. Samp period can be set) Add the following lines of code to set the sample time and to turn on the ADC ADCON3bits.SAMC = 3; // Sampling time 3 TAD ADCON1bits.ADON = 1; // Turn on ADC Read ADC channel function unsigned int readADC(short int ch) { ADCHS = ch; ADCON1bits.SAMP = 1; while(!ADCON1bits.SAMP); ADCON1bits.SAMP = 0; while (!ADCON1bits.DONE); return ADCBUF0; } // end readADC Use of ADC function: pot = readADC(0);

// start sampling // wait to complete sampling // start converting // conversion done? // return ADC value

to view this value on 4 output LEDS shift right by 6 e.g. pot = pot >> 6 and send to port e.g. PORTE = pot;

17

Microchip MCP4921 - 12 bit SPI - DAC

Code example: _RD0 = 1; // Set CS high normal state unsigned int dacValue = 0x3000 | dataVal; // mask control & data _RD0 = 0 ; // Select DAC (CS) SPI1BUF = dacValue; // write value to SPI buffer while(SPI1STATbits.SPITBF); // wait for SPI to finish _RD0 =1; // Deselect DAC (CS) Note: dataVal is the 12 bit data. If 10 bit ADC value used shift 2 places to the right.

18

Interrupts
Timer interrupts If interrupts are turned on for a timer 1, the following function is run on interrupt void _ISR _T1Interrupt(void) { // Interrupt code here _RF0 = ~_RF0; // this code toggles bit on on port F

_T1IF = 0; // Reset interrupt flag }// end interrupt

Digital input interrupt If interrupts are turned on for a digital input with interrupts (INTO INT4) the following function is run on interrupt
void _ISR _INT0Interrupt(void) {

// Interrupt code here _RF0 = ~_RF0; _INTOIF = 0;


} // end interrupt

// this code toggles bit on on port F // Reset interrupt flag

A similar process is used for all other modules interrupts.

19

PICkit 2 programmer/debugger connections to the dsPIC30F4011


dsPIC30F4011 1 MCLR AN0/RB0 AN1/RB1 AN2/RB2 AVDD AVSS RE0 RE1 RE2 RE3 RE4 RE5 VDD VSS RF0 RF1 U2RX/RF4 U2TX/RF5 PGC/U1RX/SDI1/SDA/RF2 PGD/U1TX/SD01/SCL/RF3 RF6 OC1/IC1/INT1/RD0 OC3/RD2 VDD 21 40

0.1F

10k

AN3/RB3 AN4/RB4 AN5/RB5 AN6/RB6 AN7/RB7 AN8/RB8 VDD VSS OSC1 OSC2/RC15 U1ATX//RC13 U1ARX/RC14

USB PICkit 2

MCLR/Vpp VDD VSS PGD PGC AUX

INT0/RE8 OC2/IC2/INT2/RD1 OC4/RD3 VSS 20

Complete list of pin assignments can be found in the Microchip dsPIC30F4011/4012 manual

20

Digital I/O test circuits Digital Input


5V Digital Input R Digital output R

Digital output

21

Signal conditioning op-amp circuits


Model of op-amp Powering op-amps from a single battery source
+ V/2 V+
ROUT Rin

10 k

V V
V = A0(V+ - V-) A0 = open-loop voltage gain For an ideal op amp Rin = ,ROUT = 0

V-

GND 10 k - V/2

Single voltage power supply op-amps (e.g. LM358) Unity gain buffer amplifier rail voltage d.c bias
VDD VDD Vin High input impedance/low output impedance. vi = vo VOUT vin R VDD vOUT = vin + 1/2VDD

a.c. coupling capacitor C cut off frequency = 1/2RC

Low pass Sallen and Key anti aliasing filter (dual power supply required)
VDD vin R R -VDD C C vOUT

This circuit can be modified so as to operate off a single power supply similar to the technique above and modified to include gain. Cut off frequency fc = 1/2 RC ( this occurs at -6dbs) if gain is include the cut of frequency can moved to the -3db point by adjusting the gain.

22

Review of Hex, binary and decimal numbers

Decimal - Hexadecimal -Binary numbers Decimal: 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7

Hexadecimal: 0 Binary:

0000 0001 0010 0011 0100 0101 0110 0111

Decimal:

9 9

10 A

11 B

12 C

13 D

14 E

15 F

Hexadecimal: 8 Binary:

1000 1001 1010 1011 1100 1101 1110 1111

8 bit number binary decimal equivalent

Binary: Decimal:

1 27

1 26

1 25

1 24

1 23

1 22

1 21

1 20

Example:

1 = 107

23

C programming review
Creating Variables char letter = A; int num1, num2 ; float decimal = 7.432453 ; double bigDecimal = 1032.2342123456 ; short int smallNum1 ; long int bigNum1 ; Converting data types num1 = (int)decimal ; /* cast float to integer */ num2 = (int)letter ; Arrays int arr[3] = { 1, 2, 3 } ; arr[1] = 6 ; /* change second element in array to 6 */ float A[2][2] = ({ 2.3, 4.2, }, { 6.5, 2.3 } } ; Arithmetical operations + * / % ++ -Addition Subtraction Multiplication Division Modulus Increment Decrement /* cast character to integer */

24

Logical operators && || ! AND OR Not

Assignment operators Operator = Example a = b Equivalent a = b

+=

a += b

a = a + b

-=

a -= b

a = a - b

*=

a *= b

a = a * b

/=

a /= b

a = a / b

%= Comparison operators == |= > < >= <= Equality Inequality

a %= b

a = a % b

Greater than Less than Greater than or equal to Less than or equal to

25

Conditional operator test) ? if_true_do_this : if_false_do_this; letter = (num2 %2 != 0) ? y : N ;

Conditional if-else statement if (test_expression) { code to be executed ;} else {do this if false ; }

Switch statement switch(num) { case 0 : code to be executed for 0 ; break ; case 1 : code to be executed for 1 ; break ; case 2 : code to be executed for 2 ; break ; default : code to be executed for default ; break ; }

26

The for loop for ( initializer ; test_expression ; increment ) { code statements ;}

int i

int a[3] ; for ( i = 0 ; i < 3 i++) { a[i] += i ; }

Writing functions float functionName(float num) ; /* function prototype * / float function Name(float num) */ { function code here } /* define custom function

Bitwise operators & | ^ ~ AND OR XOR NOT 1001 & 0101 1001 | 0101 1001 ^ 0101 ~1001 -> -> -> -> 0001 1101 1100 0110

27

Extracting bit values

color: Mask: Result:

AAAA AAAA RRRR RRRR GGGG GGGG BBBB BBBB &0000 0000 1111 1111 0000 0000 0000 0000 0000 0000 RRRR RRRR 0000 0000 0000 0000

Mask in hex form 0x00FF0000

result = result >> 16 /* shift red component 16 places to the left */

28

Вам также может понравиться