Digital Signal Controller

28
1 Introduction to Microchip TM , 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

Transcript of Digital Signal Controller

Page 1: Digital Signal Controller

1

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

Page 2: Digital Signal Controller

2

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.

Page 3: Digital Signal Controller

3

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

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; // reset timer counter

while(TMR2 < 100); // wait for 100 timer clock cycles make sure

// period set satisfies clock cycles delay

16 bit TMRx counter

0 - 65536

Prescaler

8/64/256

fcy

instruction

cycle clock

Timer

clock

Page 4: Digital Signal Controller

4

Analogue to digital converter (ADC)

The DSC has either 10 bit (210

, 1024) or 12 bit (212

4096) ADC’s. 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.

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 circuit’s RC time constant.

Communications

The DSC has a number of communication mechanisms, which allow the DSC communicate with

external devices such as computers, PLC’s and sensors.

The communication buses are UART, SPI, I2C 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.

V

Rs Ri ADC input

Digital Signal Controller Analogue source

Sampling

switch Sampling

capacitor

Page 5: Digital Signal Controller

5

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 ADC’s 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.

Page 6: Digital Signal Controller

6

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:- e.g. dsPIC30F4011

Activate toolsuite:- Microchip C30 Toolsuite

Toolsuite contents:- MPLAB C30 Compiler

Project name:- e.g. projectOne

Project directory:- browse to project directory created

Add linker file for

specific chip e.g C:\Program files\microchip\

MPLABC30\support\gld\p30f4011.gld

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"

Page 7: Digital Signal Controller

7

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.

Page 8: Digital Signal Controller

8

Microchip MPLAB Integrated Development Environment

Program

Project Files Visual Initializer

Page 9: Digital Signal Controller

9

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)

Page 10: Digital Signal Controller

10

Oscillator

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

Page 11: Digital Signal Controller

11

Watchdog Timer

Disable watchdog timer module located in Reset

Page 12: Digital Signal Controller

12

Input/Output Ports

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

directions

Page 13: Digital Signal Controller

13

Analogue to Digital (ADC)

There are a number of Analogue to Digital Converters ADC’s on port B of the dsPIC30F. Set

ADC parameters

Page 14: Digital Signal Controller

14

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.

Page 15: Digital Signal Controller

15

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.

Page 16: Digital Signal Controller

16

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;

Page 17: Digital Signal Controller

17

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; // start sampling

while(!ADCON1bits.SAMP); // wait to complete sampling

ADCON1bits.SAMP = 0; // start converting

while (!ADCON1bits.DONE); // conversion done?

return ADCBUF0; // return ADC value

// end readADC

Use of ADC function: pot = readADC(0);

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;

Page 18: Digital Signal Controller

18

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.

Page 19: Digital Signal Controller

19

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; // this code toggles bit on on port F

_INTOIF = 0; // Reset interrupt flag

// end interrupt

A similar process is used for all other modules interrupts.

Page 20: Digital Signal Controller

20

PICkit 2 programmer/debugger connections to the dsPIC30F4011

MCLR

AN0/RB0

AVDD 1 40

20 21

AVSS

dsPIC30F4011

AN1/RB1

AN2/RB2

AN3/RB3

AN4/RB4

AN5/RB5

AN6/RB6

AN7/RB7

VDD

VSS

OSC1

OSC2/RC15

U1ATX//RC13

U1ARX/RC14

INT0/RE8 OC2/IC2/INT2/RD1

OC4/RD3

VSS

OC1/IC1/INT1/RD0

OC3/RD2

VDD

RF6

PGD/U1TX/SD01/SCL/RF3

PGC/U1RX/SDI1/SDA/RF2

U2TX/RF5

U2RX/RF4

RF1

RF0

VSS

VDD

RE5

RE4

RE3

RE2

AN8/RB8

RE1

RE0

Complete list of pin assignments can be found in the

Microchip dsPIC30F4011/4012 manual

MCLR/Vpp

VDD VSS

PGD PGC

AUX

PICkit 2 USB

10kΩ 0.1µF

Page 21: Digital Signal Controller

21

Digital I/O test circuits

Digital Input Digital output

5 V

Digital

Input

R

Digital

output

R

Page 22: Digital Signal Controller

22

Signal conditioning op-amp circuits

Model of op-amp Powering op-amps from a single battery source

Single voltage power supply op-amps (e.g. LM358)

Unity gain buffer amplifier ½ rail voltage d.c bias

Low pass Sallen and Key anti aliasing filter (dual power supply required)

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 -6db’s) if gain is include the cut

of frequency can moved to the -3db point by adjusting the gain.

V ROUT

Rin

V+

V-

V = A0(V+ - V-)

A0 = open-loop voltage gain

For an ideal op amp Rin = ∞ ,ROUT = 0

V GND

+ V/2

- V/2

10 kΩ

10 kΩ

VDD

vOUT = vin + 1/2VDD

vin

VDD

a.c. coupling capacitor C

cut off frequency = 1/2πRC

C R

R

VDD

VOUT Vin

High input impedance/low

output impedance. vi = vo

VDD

vOUT vin

C

R

-VDD

R

C

Page 23: Digital Signal Controller

23

Review of Hex, binary and decimal numbers

Decimal - Hexadecimal -Binary numbers

Decimal: 0 1 2 3 4 5 6 7

Hexadecimal: 0 1 2 3 4 5 6 7

Binary: 0000 0001 0010 0011 0100 0101 0110 0111

Decimal: 8 9 10 11 12 13 14 15

Hexadecimal: 8 9 A B C D E F

Binary: 1000 1001 1010 1011 1100 1101 1110 1111

8 bit number binary – decimal equivalent

Binary: 1 1 1 1 1 1 1 1

Decimal: 27 26 25 24 23 22 21 20

Example: 0 1 1 0 1 1 0 1 = 107

Page 24: Digital Signal Controller

24

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 ; /* cast character to integer */

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

Page 25: Digital Signal Controller

25

Logical operators

&& AND

|| OR

! Not

Assignment operators

Operator Example Equivalent

= a = b a = b

+= a += b a = 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

> Greater than

< Less than

>= Greater than or equal to

<= Less than or equal to

Page 26: Digital Signal Controller

26

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 ;

Page 27: Digital Signal Controller

27

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) /* define custom function

*/

function code here

Bitwise operators

& AND 1001 & 0101 -> 0001

| OR 1001 | 0101 -> 1101

^ XOR 1001 ^ 0101 -> 1100

~ NOT ~1001 -> 0110

Page 28: Digital Signal Controller

28

Extracting bit values

color: AAAA AAAA RRRR RRRR GGGG GGGG BBBB BBBB

Mask: &0000 0000 1111 1111 0000 0000 0000 0000

Result: 0000 0000 RRRR RRRR 0000 0000 0000 0000

Mask in hex form 0x00FF0000

result = result >> 16

/* shift red component 16 places to the left */