<< Chapter < Page | Chapter >> Page > |
There are several functions for transmitting and receiving serial data within the C environment:
SerialRX()
takes no arguments and returns an integer, which is the next byte waiting in the serial input buffer. If there is no byte waiting, the function returns -1.SerialTX()
takes one argument, an integer to place in the serial output buffer. It returns nothing.SerialRXBufCheck()
takes no arguments and returns the number of bytes waiting in the serial input buffer.SerialRXm()
takes two arguments: the first is the number of bytes to read from the serial input buffer, and the second is a pointer, which is usually the name of an array into which the bytes will be copied. If you attempt to read more bytes than are waiting, the function will only copy those bytes that are waiting and then return. It always returns the number of characters read.SerialTXm()
takes two arguments: the first is the number of characters to write into the serial output buffer, and the second is a pointer, which is usually the name of an array containing the bytes to copy. It returns nothing.The following example shows a simple C program that will echo received serial data back through the serial port, much like the assembly example from the previous section.
1 #include "v:/ece320/54x/dspclib/core.h" /* Declarations for core file */
23 main()
4 {5 int *Rcvptr, *Xmitptr; /* pointers to Xmit and Rcv Bufs */
6 int i;7
8 while(1)9 {
10 WaitAudio(&Rcvptr,&Xmitptr);
1112 for(i=0; i<BlockLen; i++)
13 {14 Xmitptr[6*i] = Rcvptr[4*i];
15 Xmitptr[6*i+1]= Rcvptr[4*i];16 Xmitptr[6*i+2] = Rcvptr[4*i];
17 Xmitptr[6*i+3]= Rcvptr[4*i+2];18 Xmitptr[6*i+4] = Rcvptr[4*i+2];
19 Xmitptr[6*i+5]= Rcvptr[4*i+2];20 }
2122 i = SerialRX(); /* Check serial port */
23 if (i>0)
24 SerialTX(i); /* Echo received byte */25
26 }27 }
As you can see, working with the serial port is easier in C than in assembly.
The next example demonstrates how to receive and transmit multiple bytes using
SerialRXm()
and
SerialTXm
.
1 #include "v:/ece320/54x/dspclib/core.h" /* Declarations for core file */2
3 main()4 {
5 int *Rcvptr, *Xmitptr; /* pointers to Xmit and Rcv Bufs */6 int i;
7 int array[10];
89 while(1)
10 {11 WaitAudio(&Rcvptr,&Xmitptr);
1213 for(i=0; i<BlockLen; i++)
14 {15 Xmitptr[6*i] = Rcvptr[4*i];
16 Xmitptr[6*i+1]= Rcvptr[4*i];17 Xmitptr[6*i+2] = Rcvptr[4*i];
18 Xmitptr[6*i+3]= Rcvptr[4*i+2];19 Xmitptr[6*i+4] = Rcvptr[4*i+2];
20 Xmitptr[6*i+5]= Rcvptr[4*i+2];21 }
2223 if ( SerialRXBufCheck()>= 10 )
24 SerialRXm(10,array); /* copy serial receive data into array1 */25 SerialTXm(10,array); /* echo all ten bytes */
2627 }
28 }
Notification Switch
Would you like to follow the 'Digital signal processing laboratory (ece 420)' conversation and receive update notifications?