kyunghee univ. 1-0 문자열 입 · 출력 프로그램 작성 예. kyunghee univ. 1-1 모니터에...
TRANSCRIPT
KyungHee Univ. 1-1
문자열 입 ·출력 프로그램 작성 예
KyungHee Univ. 1-2
모니터에 문자열 출력 하는 프로그램 작성
표준 출력 장치에 문자를 출력 하는 함수 함수의 기본형 : void SCI_OutChar(letter)
문자를 출력 하는 표준 함수 장치에 따라 실제 문자를 출력 하는 함수를 별도로 작성 하여 출력 장치가 변경 되어도 프로그램 변경은 최소가 되도록 한다 .
시리얼 모니터에 문자열을 출력하는 함수 . 함수의 기본형 : void SCI_OutString(char *pt)
문자열이 저장된 기억장소의 포인터를 인수로 받아 USART 장치에 문자열을 출력하는 한다 .
문자열의 끝은 C 의 문자열과 같이 NULL(0) 가 저장되어 있다 .
영문 소문자 또는 대문자를 전달 받아 대문자로 변환 하여 되돌려 주는 함수 . 함수의 기본형 : void SCI_UpCase(char character)
부호 없는 정수를 출력하는 함수 . 함수의 기본형 : void SCI_OutUDec(unsigned short number) 부호 없는 정수 number 의 값을 전달 받아 문자로 변환 하여 USART 장치에
출력 한다 .
KyungHee Univ. 1-3
모니터에 문자열 출력 하는 프로그램 작성
부호 있는 정수를 출력하는 함수 . 함수의 기본형 : void SCI_OutSDec(unsigned short number 부호 있는 정수 number 의 값을 전달 받아 문자로 변환 하여 USART 장치에
출력 한다 .
Null 문자로 끝나는 문자열을 전달 받아 대 문자로 변환하여 되돌려 주는 함수 . 함수의 기본형 : void SCI_UpCaseString(char *inString)
문자열이 저장된 기억장소의 포인터를 인수로 받아 대문자로 변환 한 후 되돌려 주는 함수
부호 없는 정수를 16 진 수로 변환하여 USART 장치에 출력하는 함수 . 함수의 기본형 : void SCI_OutUHex(unsigned short number) 부호 없는 정수 number 의 값을 전달 받아 16 진 문자열로 변환 하여
USART 장치에 출력 한다 .
KyungHee Univ. 1-4
Key Board 로 부터 문자열을 입력 하는 프로그램
표준 입력 장치로 부터 한 문자를 입력 받는 함수 함수의 기본형 : unsigned short SCI_InChar()
문자열을 Key Board 로부터 입력하는 함수 . 함수의 기본형 : void SCI_InString(char *string, unsigned int max)
부호 없는 10 진 수를 Key Board 로부터 입력하는 함수 . 함수의 기본형 : unsigned short SCI_InUDec(void)
부호 있는 10 진 수를 Key Board 로부터 입력하는 함수 . 함수의 기본형 : short SCI_InSDec(void)
부호 없는 16 진 수를 Key Board 로부터 입력하는 함수 . 함수의 기본형 : unsigned short SCI_InUHex(void)
KyungHee Univ. 1-5
표준 출력 함수 예
// SCI_OutChar to USARTvoid SCI_OutChar(char letter){
tx0_char(letter);
}
// UART0 을 이용한 출력void tx0_char(unsigned char data)
{
// Wait for empty transmit buffer
while ( !(UCSR0A & (1<<UDRE)));
// Put data into buffer, sends the data
UDR0 = data;
}
KyungHee Univ. 1-6
표준 입력 함수 예
// SCI_InChar from USARTunsigned short SCI_InChar(){
return (rx0_char());
}
// UART0 을 이용한 입력unsigned char rx0_char(void)
{
// Wait for data to be received
while ( !(UCSR0A & (1<<RXC)) );
// Get and return received data from buffer
return UDR0;
}
KyungHee Univ. 1-7
문자열을 출력하는 함수
// Output String (NULL termination)
void SCI_OutString(char *pt){
char letter;
while(letter=*pt++){
SCI_OutChar(letter);
}
}
KyungHee Univ. 1-8
영문 소문자 또는 대문자를 전달 받아 대문자로 변환 하여 되돌려 주는 함수
// converts lowercase to uppercase
// char by subtracting $20 from lowercase ASCII to make
// uppercase ASCII
char SCI_UpCase(char character){
return ((character>='a') && (character<='z'))
? character-0x20 : character;
}
KyungHee Univ. 1-9
부호 없는 정수를 출력하는 함수
//-----------------------SCI_OutUDec-----------------------
// Output a 16 bit number in unsigned decimal format
// Variable format 1-5 digits with no space before or after
// This function uses recursion to convert decimal number
// of unspecified length as an ASCII string
void SCI_OutUDec(unsigned short n){
if(n >= 10){
SCI_OutUDec(n/10); Recursive Operation
n=n % 10;
}
SCI_OutChar( n + '0‘ ); /* n is between 0 and 9 */
}
KyungHee Univ. 1-10
부호 있는 정수를 출력하는 함수// Output a 16 bit number in signed decimal format
// Variable format (optional sign)1 to 5 digits with no space before or after
// This function checks if the input parameter is negative,
// If the number is negative, then
// 1) it outputs a "-",
// 2) negates the number and
// 3) outputs it with OutUDec.
// Otherwise, it just calls OutUDec (i.e., no "+" sign)
void SCI_OutSDec(short number){
if(number < 0){
number = -number;
SCI_OutChar('-');
}
SCI_OutUDec(number);
}
KyungHee Univ. 1-11
부호 없는 정수를 16 진 수로 변환하여 출력하는 함수
// Output a 32 bit number in unsigned hexadecimal format
// Variable format 1 to 8 digits with no space before or after
// This function uses recursion to convert the number of
// unspecified length as an ASCII string
void SCI_OutUHex(unsigned short number){
if(number >= 0x10) {
SCI_OutUHex(number / 0x10);
SCI_OutUHex(number % 0x10);
}
else if(number < 0xA){
SCI_OutChar(number + '0');
}
else{
SCI_OutChar((number - 0x0A) + 'A');
}
}
KyungHee Univ. 1-12
Null 문자로 끝나는 문자열을 전달 받아 대 문자로 변환하여 되돌려 주는 함수
// converts a NULL terminated string to uppercase
void SCI_upCaseString(char *inString){
char *pt = inString;
// 'a' = 0x61 and 'A' = 0x41, so their difference is 0x20
while(*pt){ // NULL => done
if((*pt >= 'a') && (*pt <= 'z')) *pt -= 0x20;
pt++;
}
}
KyungHee Univ. 1-13
//----------------------SCI_InUDec-------------------------------
// InUDec accepts ASCII input in unsigned decimal format
// and converts to a 16 bit unsigned number
// with a maximum value of 65535
// If you enter a number above 65535, it will truncate without reporting the error
// Backspace will remove last digit typed
unsigned short SCI_InUDec(void){
unsigned short number=0, length=0;
unsigned char character;
while((character=SCI_InChar())!=CR){ // accepts until carriage return input
// The next line checks that the input is a digit, 0-9.
// If the character is not 0-9, it is ignored and not echoed
if((character>='0') && (character<='9')) {
number = 10*number+(character-'0'); // this line overflows if above 65535
length++;
SCI_OutChar(character);
}
부호 없는 10 진 수를 Key Board 로부터 입력하는 함수
KyungHee Univ. 1-14
// If the input is a backspace, then the return number is
// changed and a backspace is outputted to the screen
else if((character==BS) && length){
number /= 10;
length--;
SCI_OutChar(character);
}
}
return number;
}
KyungHee Univ. 1-15
//----------------------------SCI_InSDec-----------------------------
// InSDec accepts ASCII input in signed decimal format
// and converts to a signed 16 bit number
// with an absolute value up to 32767
// If you enter a number above 32767 or below -32767,
// it will truncate without reporting the error
// Backspace will remove last digit typed
short SCI_InSDec(void){
short number=0, sign=1; // sign flag 1= positive -1 = negative
unsigned int length=0;
unsigned char character;
while ((character=SCI_InChar())!=CR){ // Check for carriage return
if(!length) { // + or - only valid as first char
if(character=='-'){
sign = -1;
length++;
SCI_OutChar('-'); // if - inputted, sign is negative
}
else if(character=='+'){
length++;
SCI_OutChar('+'); //if + inputted, sign is positive
}
}
부호 있는 10 진 수를 Key Board 로부터 입력하는 함수
KyungHee Univ. 1-16
// The next line checks that the input is a digit, 0-9
// If the character is not 0-9, it is ignored and not echoed
if((character>='0') && (character<='9')){
number = number*10+character-'0'; // this line overflows if above 32767
length++;
SCI_OutChar(character);
}
// If the input is a backspace, then the return number is changed and a backspace
// is outputted to the screen. If the backspace erases a minus, then sign is
// reset to positive
else if((character==BS) && length){
number /=10;
length--;
if(!length){
sign = 1;
}
SCI_OutChar(BS);
}
}
return sign*number;
}
KyungHee Univ. 1-17
//---------------------SCI_InUHex----------------------------------------
// InUHex accepts ASCII input in unsigned hexadecimal (base 16) format
// No '$' or '0x' need be entered, just the 1 to 4 hex digits
// It will convert lower case a-f to uppercase A-F
// and converts to a 16 bit unsigned number
// with a maximum value of FFFF
// If you enter a number above FFFF, it will truncate without reporting the error
// Backspace will remove last digit typed
unsigned short SCI_InUHex(void){
unsigned short number=0, digit, length=0;
unsigned char character;
while((character=SCI_UpCase(SCI_InChar()))!=CR){
digit = 0x10; // assume bad
if((character>='0') && (character<='9')){
digit = character-'0';
}
else if((character>='A') && (character<='F')){
digit = (character-'A')+0xA;
}
부호 없는 16 진 수를 Key Board 로부터 입력하는 함수 .
KyungHee Univ. 1-18
// If the character is not 0-9 or A-F, it is ignored and not echoed
if(digit <= 0xF ){
number = number *0x10 + digit;
length++;
SCI_OutChar(character);
}
// Backspace outputted and return value changed if a backspace is inputted
else if(character == BS && length){
number /= 0x10;
length--;
SCI_OutChar(character);
}
}
return number;
}
KyungHee Univ. 1-19
//------------------------SCI_InString------------------------
// This function accepts ASCII characters from the serial port
// and adds them to a string until a carriage return is inputted
// or until max length of the string is reached.
// It echoes each character as it is inputted.
// If a backspace is inputted, the string is modified
// and the backspace is echoed
// InString terminates the string with a null character
// -- Modified by Agustinus Darmawan + Mingjie Qiu --
void SCI_InString(char *string, unsigned int max) {
unsigned int length=0;
unsigned char character;
문자열을 Key Board 로부터 입력하는 함수
KyungHee Univ. 1-20
while((character = SCI_InChar()) != CR){
if(character == BS){
if(length){
string--;
length--;
SCI_OutChar(BS);
}
}
else if(length < max){
*string++ = character;
length++;
SCI_OutChar(character);
}
}
*string = 0; // 문자열의 끝에 Null code 삽입함 .
}