input-output student version
DESCRIPTION
dsPICTRANSCRIPT
-
Microchip dsPIC microcontrollers
-
16-bit PIC microcontroller-based Embedded Systems
1. PIC24 microcontrollers
2. dsPIC Digital Signal Conditioners (DSC)
PIC24 family of microcontrollers
1. PIC24F Low Power
2. PIC24H/E High Performance
dsPIC family of DSC
1. dsPIC30F 5V operation
2. dsPIC33F/E High Performance
-
We will be using dsPIC30F4013.
40 Pin
48k Program Memory
2k Data Memory
1k EEPROM
5 16-bit Timers
4 Input Capture and 4 Output Compare/PWM
13 Channel 12-bit ADC
2 UART
1 SPI and 1 I2C
1 CAN
-
Applications
In addition to the Mid-range PIC application
Advanced Motor Control
Speech and Audio Embedded Applications
Graphics Display
Advanced Communication Peripherals
Deterministic Interrupt Response
On-Chip Oscillator
Hardware Real-Time Clock
DSP Applications; FFT, Filters etc.
-
dsPIC Resources
dsPIC microcontrollers are relatively new devices. There
are few source of information:
1- dsPIC30F3014-4013 datasheet
2- dsPIC Master Reference Manual
3- Programming dsPIC (Digital Signal Controllers) in C
3- mikroC PRO for dsPIC Help option of the Compiler
-
Pinouts
-
dsPIC30F4013 Program space memory
RESET Vector at 0x00
Interrupt Vector Table
0x000004 to 0000x7FE
-
Data Space memory
is split into two
blocks of X and Y
Data Space is
separate for some
DSP instructions
and linear for MCU
instructions
-
Digital Inputs/Outputs
PORTA- 1 bit, RA11
PORTB- 13 bits, RB0-12
PORTC- 3 bits, RC13-15
PORTD- 6 bits, RD0-3 and RD8-9
PORTF- 7 bits, RF0-6
dsPICF4013 has 5 ports.
Not all the pin are available on each port.
-
PORTA
PORTD
PORTBH
PORTBL
PORTF
-
RF6RF5RF4RF3RF2RF1RF0PORTF
RD9RD8RD3RD2RD1RD0PORTD
RB12RB11RB10RB9RB8PORTBH
RB7RB6RB5RB4RB3RB2RB1RB0PORTBL
RA11RC14RC13PORTA
Bit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0
To allow maximum flexibility, the I/O pins are grouped onto
the ports.
-
Programming digital Inputs and outputs
3 registers are associated with each I/O pin.
1- TRISx
2- LATx
3- PORTx
TRISx determines whether the pin is an input or an
output.
1 makes the pin input
0 makes the pin output
The relevant registers are:
TRISA TRISB TRISC TRISD TRISF
-
A read from latch instruction reads the latch (LATx)
A write to latch instruction writes to the latch (LATx)
A read from the port (PORTx) reads the pin
A write to the port (PORTx) writes to the pin
-
Configuring TRISx and outputs
TRISF=0x00; // Make all the bits of PORTF output
To change a bit to output without affecting status of other
bits use masking technique using AND & operator.
For example to make bit 0 of PORTF output without
changing other bits:
TRISF=(LATF&0xfe);
X X X X X X X X
1 1 1 1 1 1 1 0
X X X X X X X 0
Original TRISF
Mask
New TRISF
-
Configuring TRISx and inputs
TRISF=0x00; // Make all the bits of PORTF output
To change a bit to input without affecting status of other
bits use masking technique and OR | operator.
For example to make bit 1 of PORTF input without
changing other bits:
TRISF=(LATF|0x02);
Original TRISF
Mask
New TRISF
X X X X X X X X
0 0 0 0 0 0 1 0
X X X X X X 1 x
-
Changing status of output without affecting others
TRISF=0x00; // Make PORTF output
PORTF=0x0f; // Turn bits 0-3 on & 4-7 off
To turn bit 0 on without affecting other bits:
PORTF=(LATF&0xff)|0x01;
Alternatively:
PORTF.B0=1;
-
Tutorial
1- Make PORTB output.
2- Make bits 0 and 1 of PORTB input.
3- Turn bit 3 on.
-
Note: In order to use PORTB pins for digital I/O, the
corresponding bits in the ADPCFG register must be set
to 1, even if the A/D module is turned off.
ADPCFG register
PORTB can be used as a digital I/O port or as analogue
input port for the ADC converter. ADPCFG is used to
assign each bit of PORTB as digital or analogue port.
Bit15 Bit14 Bit13 Bit12 Bit11 Bit10 Bit 9 Bit 8 Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0
PCF
G15
PCF
G14
PCF
G13
PCF
G12
PCF
G11
PCF
G10
PCF
G9
PCF
G8
PCF
G7
PCF
G6
PCF
G5
PCF
G4
PCF
G3
PCF
G2
PCF
G1
PCF
G0
For example to make PORTBL digital port and PORTBH
analogue add the following line to your programs.
ADPCFG=0x00ff;
-
Digital I/O example program 1
Turn all the LEDs connected to PORTBL on when RB8 is
pressed.
void main( ) {
ADPCFG=0x01ff; // Configure PORTBL port as digital
TRISB=(TRISB&0xff00)|0x0100; // Make PORTBL
output & RD8 input
while(1)
{
//if(PORTD.B0) PORTB=0x00ff; // Turn PORTBL on
// A better way is
if(PORTD.B0) PORTB=(LATB&0xff00)|0x00ff;
else PORTB=0x0000;
}
}
-
Digital I/O example program 2
Turn all the LEDs connected to PORTBL on when RD0
is pressed. Turn off the LEDs when RD1 is pressed.
void main( ) {
ADPCFG=0x00ff; // Configure PORTBL port as digital
//TRISB=0xff00; // Make PORTBL output
TRISB=(LATB&0x1f00); // Alternative better method
TRISD=0xff; // Make PORTF input
while(1)
{
if (PORTD.B0==1)
{PORTB=0x00ff; }
if(PORTD.B1==1)
{PORTB=0x0000;}
}}
-
Digital I/O Tutorial
Write a program to flash (turn on & off) bit 0 of PORTB
at regular intervals.
Reduce the flashing time intervals every time bit 1 of
PORTD is pressed.
Write your own delay function. For example:
void My_delay(int T)
{
int x,y;
for(y=0;y