■Uart_Print■

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "2450addr.h"
#include <stdarg.h>
#include "libc.h"
#include "option.h"
 
#define rUTXH1 (*(volatile unsigned *)0x50004020)
#define rURXH1 (*(volatile unsigned *)0x50004024)
#define rUTRSTAT1 (*(volatile unsigned *)0x50004010)
#define rUDIVSLOT1    (*(volatile unsigned *)0x5000402C)
 
// Function Declaration
void Uart_Init(int baud);
void Uart_Printf(char *fmt,...);
void Uart_Send_String(char *pt);
void Uart_Send_Byte(int data);
char Uart_Get_Char();
void Uart_get_String(char* str);
 
extern int vsprintf(char *const char *, va_list);
 
 
void Main()
{
 
    char str[]="";
    //Uart_Init(NULL);
    while(1)
    {
    Uart_get_String(str);
    Uart_Send_String(str);
    }
}
 
 
 
void Uart_Init(int baud)
{
    int pclk;
    pclk = PCLK;
    
    // PORT GPIO initial
    rGPHCON &= ~(0xf<<4);
    rGPHCON |= (0xa<<4);    
 
    
    rUFCON1 = 0x0;
    rUMCON1 = 0x0;
    
    /* TODO : Line Control(Normal mode, No parity, One stop bit, 8bit Word length */
    rULCON1 = 0x03;
 
    /* TODO : Transmit & Receive Mode is polling mode  */
    rUCON1 = 0x05;
 
    /* TODO : Baud rate 설정  */        
    rUBRDIV1=0x22;
    rUDIVSLOT1=0xDFDD;
}
#if 0
void Uart_Printf(char *fmt,...)
{
    va_list ap;
    char string[256];
 
    va_start(ap,fmt);
    vsprintf(string,fmt,ap);
    Uart_Send_String(string);
    va_end(ap);        
}
#endif
void Uart_Send_String(char *pt)
{
    while(*pt)
    {
        /* TODO : 문자 하나씩 증가시키면서 문자열 출력  */
        /*YOUR CODE HERE*/
        Uart_Send_Byte(*pt++);
    }
}
 
void Uart_Send_Byte(int data)
{
    while(!(rUTRSTAT1 & 0x2));//tx버퍼가 비어있으면 출력(while문 끝나지 x)
                        //비어있다 1 -> while(0)
                        //비어있지 않다 0 ->while(1)
    rUTXH1 = (char)data;
    if(data=='\r')
    {
        //while(!(rUTRSTAT1 & 0x2));
        rUTXH1 = '\r';
        //while(!(rUTRSTAT1 & 0x2));
        rUTXH1 = '\n';
    }
    /* TODO : UTRSTAT1의 값을 확인하여 TX 버퍼가 비어있으면 문자 출력   */    
    /*YOUR CODE HERE*/
}
 
char Uart_Get_Char()
{
    /* TODO : UTRSTAT1의 값을 확인하여 문자열 입력   */        
    /*YOUR CODE HERE*/
    
    while(!(rUTRSTAT1 & 0x1));
    return rURXH1;
 
}
 
void Uart_get_String(char* str)
{    
    unsigned char data;
    while(1)
    {
        data = Uart_Get_Char();
        *str++ = data;
        Uart_Send_Byte(data);    
        if(data == '\r')
        {
        break;
        }
    }
    *str = '\0';
}
 
cs

#Uart_Get_Char에서 폴링 방식으로 TX버퍼가 0인지 1인지 검사를 계속한다.


#결과(Tera Term)




+ Recent posts