The Design Cycle
see at the beginning of Listing 1.
If you continue to follow “Design
Cycle,” you’ll notice that I always like to
establish a baseline serial interface.
Once you get the serial port of that
microcontroller working, other things
just seem to fall into place a little easier.
Recall that I modified my dsPICDEM
28-Pin Demo Board to allow me to use
the alternate dsPIC30F2010 serial port
so I could eliminate the need to move
jumpers to enter and exit debug/
program mode with the MPLAB ICD2.
The U1_ALTIO = 1 statement
tells the dsPIC30F2010 to use the
alternate USART signal pins. The
dsPIC30F2010’s UART is enabled by
setting the most significant bit of the
U1MODE register (U1_UARTEN = 1).
The dsPIC30F2010’s UART baud rate
can then be calculated with the following formula:
remove the comment slashes.
Okay, back to the dsPIC30F2010’s
A/D converter. As you can see in
Listing 1, all of the A/D converter set-up
is performed just before turning on
the A/D converter module. Once a
conversion is complete, the A/D conversion interrupt flag, ADIF, is set. We’ll
poll the ADIF flag to determine when
the current A/D conversion has com-
pleted. We could also poll the A/D conversion DONE bit in a similar manner
to the ADIF flag bit with the same
results. The ASAM bit is cleared to indicate to the A/D converter module that
the setting of the SAMP bit (bit 1 of
ADCON1) will be the signal to begin
sampling the selected analog input pin.
After setting the SAMP bit, some
time must be given to the sampling
Baud Rate = Fcy / ( 16* (BRG+1))
Where: Fcy = Instruction Clock Rate
BRG = Value to load into
U1BRG for baud rate clock
The DIVIDER1 macro statement
in Listing 1 solves for the correct BRG
value. The baud rate generator value
generated by the DIVIDER1 macro is
then loaded into the U1BRG register
using the statement U1BRG =
DIVIDER1. Once the baud rate generator is loaded and the UART is enabled,
the UART transmitter is switched on by
setting the UTXEN bit in the U1STA
register (U1_UTXEN = 1).
The commented-out code between
the while(1) braces in Listing 1 is a simple serial echo mechanism. The
dsPIC30F2010 UART has its own four-byte receive and transmit buffer. When
a character enters the receive buffer,
the URXDA bit is set. If there is no
transmission in progress, the TRMT bit
will be clear. The little echo code snippet waits for a character to enter the
receive buffer and, when all is clear on
the transmit side, it moves the incoming
character from the receive buffer into
the transmit buffer for transmission. If
you wish to run the echo code, simply
Circle #86 on the Reader Service Card.
Circle #99 on the Reader Service Card.