Upload
algo
View
225
Download
0
Embed Size (px)
Citation preview
8/10/2019 Umassamherstm5.Org Tech-tutorials Pic32-Tutorials Pic32m
1/7
Home About M5
About Us
People
Photo Tour Contact Us
Calendar
ECE 297DP Des!n Pro"ects M5 E#permental Colle!e
$pen %ource Pro"ects
Part&duno '()2
M*D*arcA+de
Tech Tutorals
,nu# Tutorals
Arduno Tutorals
P*C-2 Tutorals ,n.s
Ma.erspaces
M5 Tal.
UART to Serial Terminal
%hare/0oo.mar.
Althou!h the P*C-2 s an ele!ant and po1erul mcrocontroller3 t doesn4t stand so tall 1hen compared a!anst a PC n terms o ra1 computn! po1er
%ometmes t ma.es sense to use the easablt& o the mcrocontroller to 1or. as a sensor or an embedded controller o some t&pe but stll be able tocommuncate 1th a more po1erul computer) %ometmes &ou m!ht "ust 1ant a 6uc. 'sual dspla& or aprintftermnal or debu!!n!) hate'er the
reason3 ths tutoral 1ll pro'de a basc !ude to P*C-2 / PC communcaton)
UART
The module used or such communcaton s the P*C4s Universal Asynchronous Receiver/Transmitter (UART)pronounced 8&ouart:) ;or those
amlar 1th computer musc3 the M*D* protocol uses a UAt1o corrupted
bts s treated as no error?3 &ou ma& desre somethn! more po1erul l.e a yclic Re!un!ancy hec" (R)1hch s outsde the scope o ths
tutoral)
Pa!e ( o 7UA
8/10/2019 Umassamherstm5.Org Tech-tutorials Pic32-Tutorials Pic32m
2/7
The stop bit signifies the end of a data transfer. There will almost always be either one or two stop bits. If the stop bits read by the receiving unit dont
match those expected, the receiver will assume its clock drifted out of phase and a the software must deal with this error. It is also implied that a start
bit is being sent as well with each transmission. The start bit allows the clock on the receiving end to do aphase loc"with the data being received.
Otherwise, the receiving end might end up sampling on the data transition edges instead of right in the middle of each bit. The data received would
then look like utter nonsense!
o, in an "# configuration, each byte of data sent is transmitted in the form of a #$%bit frame with the addition of one start bit, one stop bit, and no
parity bits.
The example below configures the &I'()*s +-T# module to an "# format with a baud rate of #)$$ bits/s and enables both transmit and receive
functions 0infull !uplexmode1.
intFPb = 40000000; // peripheral bus clock frequency set at max SYSCLKintdesired_baud = 19200;U1MODE = 0; // disable autobaud, TX and X enabled only, !"#, idle$%&'%U1STA = 0x1400; // enable TX and XU1B! = "FPb # "1$%desired_baud&& ' 1
U1MODESET = 0x(000; // enable ()T#
"ote that in the example above, we are using the standard baud mode set when Ux#$%&' * +. The +-T clock is only able to take on specific
fre2uencies and this bit helps determine how close the desired baud rate will be to the actual baud rate. 3hen 4-56 7 # 0i.e. +x8O9:;(< 7 #1,
high%speed mode is activated and the actual baud rate will probably be closer to the desired baud rate. 6owever, each bit received on the +x-= pin
will only be sampled once. 3hen 4-56 7 $ 0i.e. +x8O9:;(< 7 $1, multiple samples are taken on the +x-= pin to determine whether a 6I56 or
>O3 is present. :ven though the high%speed mode may have a more accurate baud rate clock, in standard%mode there is less chance to make receiving
errors. ee the +-T reference manual section )#.? for more information.
The following table provides a set of desired baud rates, the value you would set 4-56 to for this baud rate and the error.
@inally, the code below will configure the &I'() for basic communication with a computer serial terminal. +sing the configuration given and some of
the functions, it can very easily be adapted for a variety of purposes. 3e decided it would be suitable Aust to mock youB
/* This pro+ram confi+ures the &C-. for communication ith a computer at 01233 baud4* 5hen the communication link is set up, the pro+ram ill simply take input text* from the user and mock him/her4 &t demonstrates the basics of ()T data x/Tx4*/
// &nclude %eader 6iles)in*+ude ,-.2xxxx/ ## in*+ude *i- s-e*ii* eader i+e)in*+ude ,-+ib/ ## in*+ude -eri-era+ +ibrar3 un*tins
// Confi+uration 7its)-ra56a *ni5 F7OS8 = F8P ## :nterna+ Fast 8 s*i++atr "( M = D:>_2 ## Di?ide F8 bere P "n 4 M_1 ## PB8 = SC8
// 8efines)deine SCS8 40000000
// 9acros// :quation to set baud rate from ()T reference manual equation .#;#)deine Baud2B!"desired_baud& " "SCS8 # "1$%desired_baud&&'1&
// 6unction rototypes
intSeria+Trans6it"*nst *ar %buer&;unsi5ned intSeria+e*ei?e"*ar %buer unsi5ned int6ax_si
8/10/2019 Umassamherstm5.Org Tech-tutorials Pic32-Tutorials Pic32m
3/7
char buf[1024]; // declare recei
// eripheral in Select U2RXRbits.U2RXR = 4; //S:T X to 7! RPB9Rbits.RPB9R = 2; //S:T 7? to TX
UART2Confiur!"#$%00&; // Confi+ure ()T. for a baud rate of 01233 U2'()*+*T = 0,-000; // enable ()T.
/* )dd a small delay for the serial terminal* )lthou+h the &C sends out data fine, &@
8/10/2019 Umassamherstm5.Org Tech-tutorials Pic32-Tutorials Pic32m
4/7
buff!r//; nuchar//;
r!turnnuchar; // :"8 Serialecei
8/10/2019 Umassamherstm5.Org Tech-tutorials Pic32-Tutorials Pic32m
5/7
instead of 5V.
Software
Although we no longer need to communicate via asynchronous serial data directly to the PIC, we now must send and receive USB ac!ets to our go"
#etween"device. $ortunately, this is retty simle.
$or Windowsusers, you can use Pu%%& and select 'serial( as the tye of communication. %hen find the C)* ort num#er #y going to%evice
#ana,er - .ortsafter the hardware is connected. %he #aud rate we have chosen in the code e+amle is 5-.
$or Macand Linuxusers, I(ve found /0U Screen to #e an incredi#ly useful rogram. 0ot only can you use it as a serial terminal #ut it also allows
you to multile+ #etween different terminal sessions in a single sace1 /ood#ye screen clutter1
$irst, to find the device name we(ll #e communicating with, oen a terminal and enter the command2
ls /dev | grep usb
$or 3inu+ users, the outut will #e something li!e2
ttyUSB0
where the num#er 4 a#ove will increase #ased on how many USB devices are connected.
$or *ac users, the outut will #e something li!e2
cu.usbserial-A800cBEp
tty.usbserial-A800cBEp
where #oth names reference the same USB device. Crudely, the 'tty( device reference e+ects a hardware assertion. Communication will #e #loc!ed
until some reset line 46C676%8 has #een asserted. %he 'cu( device reference will #e oen for communication even without this initiali9ation. Since
we(re using a 6%8 line through the USB BUB, the 'tty( name is used #elow although the 'cu( name should wor! fine.
If multile devices are listed, disconnect7reconnect and run the list device command as needed.
)nce the PIC is rogrammed, the hardware all set, and the device name is !nown, use the following command to oen a communication channel. 0ote
that the rogram secifies a #aud rate of 5-. $eel free to use the 'ta#( command to finish any tying any commands that only have one useful
outcome. %hat is, tty.usb[tab]is faster than tying tty.usbserial-A800cBEpif there(s only one USB device connected.
screen /dev/tty.usbserial-A800cBEp 5!00
%o e+it Screen, tye ctrl"a # y. %o see more Screen otions, ctrl"a $.
Page 5 of UA8% to Serial %erminal
:;7
8/10/2019 Umassamherstm5.Org Tech-tutorials Pic32-Tutorials Pic32m
6/7
Below is a screenshot of the Saleae logic analyzer. Note that the DTR line is the reset signal sent from the computer to initiate communication with the
PIC. The bau rate was set to !"## for this e$ample.
% & comment' rea it below or a one(
shalamNo)ember &*+ *#&* at #&,!!
hi+
i am trying to sent my unsigne int R-&*/!012"3 to uart.
unsigne int R-4
char str5&164
main7)oi8
%
m9SCSetPBDI:7 9SC;PB;DI:;& 84
8/10/2019 Umassamherstm5.Org Tech-tutorials Pic32-Tutorials Pic32m
7/7
Leave a Comment
Name *
E-mail *
Website
Submit
Get smart with the Thesis WordPress Themefrom DIYthemes.
WordPress dmin
Pa!e " of "#$T to %erial Terminal