Академический Документы
Профессиональный Документы
Культура Документы
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
Prescaler 8/64/256
Timer clock
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
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.
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:
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.
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
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
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
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
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
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) {
19
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
Complete list of pin assignments can be found in the Microchip dsPIC30F4011/4012 manual
20
Digital output
21
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
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
Hexadecimal: 0 Binary:
Decimal:
9 9
10 A
11 B
12 C
13 D
14 E
15 F
Hexadecimal: 8 Binary:
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
+=
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
Greater than Less than Greater than or equal to Less than or equal to
25
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
int 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
AAAA AAAA RRRR RRRR GGGG GGGG BBBB BBBB &0000 0000 1111 1111 0000 0000 0000 0000 0000 0000 RRRR RRRR 0000 0000 0000 0000
28