Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Find a serial C program of mini2440.
Find a serial C program of mini2440.
The Mini2440 has three serial ports, USRT 0, UART 1 and UART 3.

There are three serial port operations: serial port initialization, serial port receiving and serial port sending.

1 serial port initialization

1) serial port initialization function

void Uart_Init(int pclk,int baud)

{

int I;

if(pclk == 0)

pclk = PCLK;

RUFCON0 = 0x0//UART Channel 0 FIFO control register, FIFO disabled.

rufcon 1 = 0x 0; //UART channel 1 FIFO control register, FIFO is disabled.

RUFCON2 = 0x0//UART Channel 2 FIFO control register, FIFO disabled.

RUMCON0 = 0x0//UART chaneel 0 Modem Control Register, AFC disabled.

rum con 1 = 0x 0; //UART Chanel1Modem control register, AFC disabled.

//UART0

RULCON0 = 0x3// Line control register: normal, no parity, 1 stop, 8 bits.

// [ 10] [9] [8] [7] [6] [5] [4] [3:2] [ 1:0]

//clock Sel, Tx Int, Rx Int, Rx timeout, Rx err, loopback, transmit interrupt, transmit mode, receive mode.

// 0 1 0 , 0 1 0 0 , 0 1 0 1

// PCLK level pulses prohibit normal interrupt or polling.

RUCON0 = 0x245// control register

rUBRDIV0=( (int)(pclk/ 16。 /Potter+0.5)-1); //Baud Rate Divider Register 0

//UART 1

rulcon 1 = 0x 3;

rucon 1 = 0x 245;

rubr div 1 =((int)(pclk/ 16。 /Potter+0.5)-1);

//UART2

rULCON2 = 0x3

rUCON2 = 0x245

rUBRDIV2=( (int)(pclk/ 16。 /Potter+0.5)-1);

for(I = 0; I< 100; i++);

}

L setting of serial clock

pclk = PCLK; Use PCLK as the clock of serial port.

The l register is set.

There are mainly three kinds of registers.

Rufconn (n = 0, 1, 2)- serial FIFO control register.

Rumconn (n = 0, 1)- serial port modulation control register.

Rulconn (n = 0, 1, 2)- serial port linear control register.

Ruconn (n = 0, 1, 2)- serial port control register.

Rubrdivn (n = 0, 1, 2)- Sets the transmission and reception rates of the serial ports TX and RX.

2) Selection of serial port

void Uart_Select(int ch)

{

whichUart = ch

}

2 serial port sending

Serial port sends data by judging whether the second bit of rUTRSTATn (n=0, 1, 2) is 1 to judge whether the sending buffer is empty.

1) serial port sends bytes.

Void Uart_SendByte(int data)

{

if(whichUart==0)

{

if(data=='\n ')

{

And (! (rUTRSTAT0 & amp0x 2));

//Delay( 1); //Because hyper_terminal is slow.

wrutxh 0(' \ r ');

}

And (! (rUTRSTAT0 & amp0x 2)); //Wait until the THR is empty.

//Delay( 1);

WrUTXH0 (data);

}

else if(whichUart== 1)

{

if(data=='\n ')

{

And (! (rutrstat 1 & amp; 0x 2));

//Delay( 1); //Because hyper_terminal is slow.

rutxh 1 = ' \ r ';

}

And (! (rutrstat 1 & amp; 0x 2)); //Wait until the THR is empty.

//Delay( 1);

RUTXH 1 = data;

}

else if(whichUart==2)

{

if(data=='\n ')

{

And (! (rUTRSTAT2 & amp0x 2));

//Delay( 1); //Because hyper_terminal is slow.

rUTXH2 = ' \ r

}

And (! (rUTRSTAT2 & amp0x 2)); //Wait until the THR is empty.

//Delay( 1);

RUTXH2 = data;

}

}

The first is the choice of serial channel; Then judge whether THR is empty or not, and exit the while loop until the sending buffer is empty; Byte data to be sent is placed in the send buffer. If the data sent is carriage return, send the escape character /r directly. Of course, it can't be sent until THR is empty.

2) sending a string through a serial port

void Uart_SendString(char *pt)

{

while(*pt)

UART _ send byte(* p t++);

}

The character string is regarded as a character array, and the character pointer *pt refers to the first address of the character array, and the character pointed by the pointer is read, so that the sent character string can be sent byte by byte.

2 serial port reception

Serial port reception judges whether the sending buffer is empty by judging whether the 1 bit of rUTRSTATn (n=0, 1, 2) is 1.

1) serial port waiting for receiving status

void Uart_TxEmpty(int ch)

{

If (ch==0)

And (! (rUTRSTAT0 & amp0x 4)); //Wait until the tx shifter is empty.

else if(ch== 1)

And (! (rutrstat 1 & amp; 0x 4)); //Wait until the tx shifter is empty.

else if(ch==2)

And (! (rUTRSTAT2 & amp0x 4)); //Wait until the tx shifter is empty.

}

Register rutrstatn &;; 0x4 (n=0, 1, 2) determines whether the tx shifter is empty.

3) receiving characters

char Uart_Getch(void)

{

if(whichUart==0)

{

And (! (rUTRSTAT0 & amp0x 1)); //Ready to receive data

Returns rdurxh0 ();

}

else if(whichUart== 1)

{

And (! (rutrstat 1 & amp; 0x 1)); //Ready to receive data

Returns rdurxh1();

}

else if(whichUart==2)

{

And (! (rUTRSTAT2 & amp0x 1)); //Ready to receive data

Returns rdurxh2 ();

}

Returns 0;

}

Register rutrstatn &;; 0x 1 (n=0, 1, 2) to judge the amount of data received? And writes the received data into the receiving cache.

4) receiving a character string

void Uart_GetString(char *string)

{

char * string2 = string

char c;

while((c = Uart_Getch())! ='\r ')

{

if(c=='\b ')

{

if((int)string 2 & lt; (int) string)

{

UART _ Printf(" \ b \ b ");

String-;

}

}

other

{

* string++ = c;

UART _ send byte(c);

}

}

* string = ' \ 0

UART _ send byte(' \ n ');

}

char Uart_GetKey(void)

{

if(whichUart==0)

{

if(rutrstat 0 & amp; 0x 1)// Received data is ready.

Returns rdurxh0 ();

other

Returns 0;

}

else if(whichUart== 1)

{

if(rutrstat 1 & amp; 0x 1)// Received data is ready.

Returns rdurxh1();

other

Returns 0;

}

else if(whichUart==2)

{

if(rutrstat 2 & amp; 0x 1)// Received data is ready.

Returns rdurxh2 ();

other

Returns 0;

}

Returns 0;

}

Register rutrstatn &;; 0x 1 (n=0, 1,2)

Judge whether the received data is ready? And writes the received data into the receiving cache. Otherwise, it returns null. Similar to getchar, it is also a return character type.

5) receiving the shaped numbers.

Function return type: plastic

int Uart_GetIntNum(void)

{

String [30];

char * string = str

int base = 10;

Int minus = 0;

int result = 0;

int lastIndex

int I;

Uart_GetString (string);

if(string[0]=='-')

{

Minus =1;

string++;

}

if(string[0]= = ' 0 ' & amp; & amp(string[ 1]= = ' X ' | | string[ 1]= = ' X '))

{

base = 16;

string+= 2;

}

last index = strlen(string)- 1;

if(last index & lt; 0)

return- 1;

if(string[last index]= = ' H ' | | string[last index]= = ' H ')

{

base = 16;

string[last index]= 0;

last index-;

}

if(base== 10)

{

Result = atoi (string);

Result = negative? (-1*result): result;

}

other

{

for(I = 0; I<= lastIndexi++)

{

if(isalpha(string))

{

if(isupper(string))

Result = (result & lt& lt4)+string-'a'+10;

other

Result = (result & lt& lt4)+string-'a'+10;

}

other

Result = (Result & lt& lt4)+String-'0';

}

Result = negative? (-1*result): result;

}

Return the result;

}

Firstly, data Uart_GetString(string) is received in the form of a string;

Then judge whether it is negative by judging whether it starts with'-',and if it is negative, subtract =1; Then judge whether it is 16 base starting with 0x, oX or ending with h, h, otherwise it is 10 base; If it is 10, the string will be converted into numbers by atoi, plus positive and negative characters; If it is 16, it will judge whether it is a letter or not, and then make case conversion respectively. Sign is also supported in 16.

6) Receive the number in 10 radix.

int Uart_GetIntNum_GJ(void)

{

Char string [16];

char * p _ string = string

char c;

int I = 0;

int data = 0;

while( ( c = Uart_Getch())! = '\r ')

{

if(c = = ' \ b ')p _ string-;

else * p _ string++ = c;

UART _ send byte(c);

}

* p _ string = ' \ 0

I = 0;

While (string! = '\0' )

{

Data = data *10;

if(string & lt; 0 ' | | string & gt'9' )

return- 1;

Data = data+(string-'0');

i++;

}

Return data;

}

Only 10 digits are converted. First, the single character received by getchar is stored in a string, and then the string is converted into integer data. And returns the shaped data.

3 serial port printing

//If you don't use vsprintf (), the code amount will be much reduced.

void Uart_Printf(char *fmt,...)

{

va _ list ap

char string[256];

va_start(ap,fmt);

vsprintf(string,fmt,AP);

Uart_SendString (string);

va _ end(AP);

}

Va_list completes the operation of variable parameters.

The concrete implementation is as follows

N Ellipsis is used to specify the parameter list when the types and quantities of all parameters of the transfer function cannot be listed.

void Uart_Printf(char *fmt,...)

Transfer principle of n function parameters

Function parameters are accessed in the form of data structure: stack, from right to left.

N Gets the parameter specified by the ellipsis.

Declare a va_list in the function body, then use the va_start function to get the parameters in the parameter table, and call va_end () after use.

va _ list ap

char string[256];

va_start(ap,fmt);

vsprintf(string,fmt,AP);

Uart_SendString (string);

va _ end(AP);

Va_start points the ap to the first optional parameter. Va_end clears the ap pointer to null. These parameters can be traversed many times in the function body, but they must all start with va_start and end with va_end.

vsprintf(string,fmt,AP); Implements writing the contents of the specified format into a character array.

This article is from CSDN blog, please indicate the source:/garby2004/archive/2009/09/28/4604887.aspx.