ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 13.06.2025
Просмотров: 2232
Скачиваний: 0
CodeVisionAVR
4. Library Functions Reference
You must #include the appropriate header files for the library functions that you use in your program. Example:
/* Header files are included before using the functions */ #include <math.h> // for abs
#include <stdio.h> // for putsf
void main(void) { int a,b;
a=-99;
/* Here you actually use the functions */ b=abs(a);
putsf(“Hello world”);
}
© 1998-2007 HP InfoTech S.R.L. |
Page 111 |
CodeVisionAVR
4.1 Character Type Functions
The prototypes for these functions are placed in the file ctype.h, located in the ..\INC subdirectory. This file must be #include -ed before using the functions.
unsigned char isalnum(char c)
returns 1 if c is alphanumeric.
unsigned char isalpha(char c)
returns 1 if c is alphabetic.
unsigned char isascii(char c)
returns 1 if c is an ASCII character (0..127).
unsigned char iscntrl(char c)
returns 1 if c is a control character (0..31 or 127).
unsigned char isdigit(char c)
returns 1 if c is a decimal digit.
unsigned char islower(char c)
returns 1 if c is a lower case alphabetic character.
unsigned char isprint(char c)
returns 1 if c is a printable character (32..127).
unsigned char ispunct(char c)
returns 1 if c is a punctuation character (all but control and alphanumeric).
unsigned char isspace(char c)
returns 1 c is a white-space character (space, CR, HT).
unsigned char isupper(char c)
returns 1 if c is an upper-case alphabetic character.
unsigned char isxdigit(char c)
returns 1 if c is a hexadecimal digit.
char toascii(char c)
returns the ASCII equivalent of character c.
unsigned char toint(char c)
interprets c as a hexadecimal digit and returns an usigned char from 0 to 15.
© 1998-2007 HP InfoTech S.R.L. |
Page 112 |
CodeVisionAVR
char tolower(char c)
returns the lower case of c if c is an upper case character, else c.
char toupper(char c)
returns the upper case of c if c is a lower case character, else c.
4.2 Standard C Input/Output Functions
The prototypes for these functions are placed in the file stdio.h, located in the ..\INC subdirectory. This file must be #include -ed before using the functions.
The standard C language I/O functions were adapted to work on embedded microcontrollers with limited resources.
The lowest level Input/Output functions are:
char getchar(void)
returns a character received by the UART, using polling.
void putchar(char c)
transmits the character c using the UART, using polling.
Prior to using these functions you must:
•initialize the UART's Baud rate
•enable the UART transmitter
•enable the UART receiver.
Example:
#include <90s8515.h> #include <stdio.h>
/* quartz crystal frequency [Hz] */ #define xtal 4000000L
/* Baud rate */ #define baud 9600
void main(void) { char k;
/* initialize the UART's baud rate */ UBRR=xtal/16/baud-1;
/* initialize the UART control register
RX & TX enabled, no interrupts, 8 data bits */ UCR=0x18;
© 1998-2007 HP InfoTech S.R.L. |
Page 113 |
CodeVisionAVR
while (1) {
/* receive the character */ k=getchar();
/* and echo it back */ putchar(k);
};
}
If you intend to use other peripherals for Input/Output, you must modify accordingly the getchar and putchar functions like in the example below:
#include <stdio.h>
/* inform the compiler that an alternate version of the getchar function will be used */
#define _ALTERNATE_GETCHAR_
/* now define the new getchar function */ char getchar(void) {
/* write your code here */
}
/* inform the compiler that an alternate version of the putchar function will be used */
#define _ALTERNATE_PUTCHAR_
/* now define the new putchar function */ void putchar(char c) {
/* write your code here */
}
All the high level Input/Output functions use getchar and putchar.
void puts(char *str)
outputs, using putchar, the null terminated character string str, located in SRAM, followed by a new line character.
void putsf(char flash *str)
outputs, using putchar, the null terminated character string str, located in FLASH, followed by a new line character.
void printf(char flash *fmtstr [ , arg1, arg2, ...])
outputs formatted text, using putchar, according to the format specifiers in the fmtstr string. The format specifier string fmtstr is constant and must be located in FLASH memory.
The implementation of printf is a reduced version of the standard C function.
This was necessary due to the specific needs of an embedded system and because the full implementation would require a large amount of FLASH memory space.
© 1998-2007 HP InfoTech S.R.L. |
Page 114 |
CodeVisionAVR
The format specifier string has the following structure:
%[flags][width][.precision][l]type_char
The optional flags characters are:
'-' left-justifies the result, padding on the right with spaces. If it's not present, the result will be rightjustified, padded on the left with zeros or spaces;
'+' signed conversion results will always begin with a '+' or '-' sign;
' ' if the value isn't negative, the conversion result will begin with a space. If the value is negative then it will begin with a '-' sign.
The optional width specifier sets the minimal width of an output value. If the result of the conversion is wider than the field width, the field will be expanded to accommodate the result, so not to cause field truncation.
The following width specifiers are supported:
n - at least n characters are outputted. If the result has less than n characters, then it's field will be padded with spaces. If the '-' flag is used, the result field will be padded on the right, otherwise it will be padded on the left;
0n - at least n characters are outputted. If the result has less than n characters, it is padded on the left with zeros.
The optional precision specifier sets the maximal number of characters or minimal number of integer digits that may be outputted.
For the 'e', 'E' and 'f' conversion type characters the precision specifier sets the number of digits that will be outputted to the right of the decimal point.
The precision specifier always begins with a '.' character in order to separate it from the width specifier.
The following precision specifiers are supported:
none - the precision is set to 1 for the 'i', 'd', 'u', 'x', 'X' conversion type characters. For the 's' and 'p' conversion type characters, the char string will be outputted up to the first null character;
.0 - the precision is set to 1 for the 'i', 'd', 'u', 'x', 'X' type characters;
.n - n characters or n decimal places are outputted.
For the 'i', 'd', 'u', 'x', 'X' conversion type characters, if the value has less than n digits, then it will be padded on the left with zeros. If it has more than n digits then it will not be truncated.
For the 's' and 'p' conversion type characters, no more than n characters from the char string will be outputted.
For the 'e', 'E' and 'f' conversion type characters, n digits will be outputted to the right of the decimal point.
The precision specifier has no effect on the 'c' conversion type character.
The optional 'l' input size modifier specifies that the function argument must be treated as a long int for the 'i', 'd', 'u', 'x', 'X' conversion type characters.
The type_char conversion type character is used to specify the way the function argument will be treated.
The following conversion type characters are supported: 'i' - the function argument is a signed decimal integer; 'd' - the function argument is a signed decimal integer;
'u' - the function argument is an unsigned decimal integer;
'e' - the function argument is a float, that will be outputted using the [-]d.dddddd e[-]dd format 'E' - the function argument is a float, that will be outputted using the [-]d.dddddd E[-]dd format 'f' - the function argument is a float, that will be outputted using the [-]ddd.dddddd format
'x' - the function argument is an unsigned hexadecimal integer, that will be outputted with lowercase characters;
'X' - the function argument is an unsigned hexadecimal integer, that will be outputted with with uppercase characters;
'c' - the function argument is a single character;
's' - the function argument is a pointer to a null terminated char string located in SRAM;
© 1998-2007 HP InfoTech S.R.L. |
Page 115 |
CodeVisionAVR
'p' - the function argument is a pointer to a null terminated char string located in FLASH; '%' - the '%' character will be outputted.
void sprintf(char *str, char flash *fmtstr [ , arg1, arg2, ...])
this function is identical to printf except that the formatted text is placed in the null terminated character string str.
void snprintf(char *str, unsigned char size, char flash *fmtstr [ , arg1, arg2, ...]) for the TINY memory model.
void snprintf(char *str, unsigned int size, char flash *fmtstr [ , arg1, arg2, ...]) for the other memory models.
this function is identical to sprintf except that at most size (including the null terminator) characters are placed in the character string str.
In order to reduce program code size, there is the Project|Configure|C Compiler|Code Generation|(s)printf features option.
It allows linking different versions of the printf and sprintf functions, with only the features that are really required by the program.
The following (s)printf features are available:
•int - the following conversion type characters are supported: 'c', 's', 'p', 'i', 'd', 'u', 'x', 'X', '%', no width or precision specifiers are supported, only the '+' and ' ' flags are supported, no input size modifiers are supported
•int, width - the following conversion type characters are supported: 'c', 's', 'p', 'i', 'd', 'u', 'x', 'X', '%', the width specifier is supported, the precision specifier is not supported, only the '+', '-', '0' and ' ' flags are supported, no input size modifiers are supported
•long, width - the following conversion type characters are supported: 'c', 's', 'p', 'i', 'd', 'u', 'x', 'X', '%' the width specifier is supported, the precision specifier is not supported, only the '+', '-', '0' and ' ' flags are supported, only the 'l' input size modifier is supported
•long, width, precision - the following conversion type characters are supported: 'c', 's', 'p', 'i', 'd', 'u', 'x', 'X', '%', the width and precision specifiers are supported, only the '+', '-', '0' and ' ' flags are supported, only the 'l' input size modifier is supported
•float, width, precision - the following conversion type characters are supported: 'c', 's', 'p', 'i', 'd', 'u', 'e', 'E', 'f', 'x', 'X', '%', the width and precision specifiers are supported, only the '+', '-', '0' and ' ' flags are supported, only the 'l' input size modifier is supported.
The more features are selected, the larger is the code size generated for the printf and sprintf functions.
void vprintf(char flash *fmtstr, va_list argptr)
this function is identical to variable list of arguments. The
printf except that the argptr pointer, of va_list type, points to the va_list type is defined in the stdarg.h header file.
void vsprintf(char *str, char flash *fmtstr, va_list argptr)
this function is identical to variable list of arguments. The
sprintf except that the argptr pointer, of va_list type, points to the va_list type is defined in the stdarg.h header file.
© 1998-2007 HP InfoTech S.R.L. |
Page 116 |
CodeVisionAVR
void vsnprintf(char *str, unsigned char size, char flash *fmtstr, va_list argptr) for the TINY memory model.
void vsnprintf(char *str, unsigned int size, char flash *fmtstr, va_list argptr) for the other memory models.
this function is identical to vsprintf except that at most size (including the null terminator) characters are placed in the character string str.
char *gets(char *str, unsigned char len)
inputs, using getchar, the character string str terminated by the new line character. The new line character will be replaced with 0.
The maximum length of the string is len. If len characters were read without encountering the new line character, then the string is terminated with 0 and the function ends.
The function returns a pointer to str.
signed char scanf(char flash *fmtstr [ , arg1 address, arg2 address, ...])
formatted text input by scanning, using getchar, a series of input fields according to the format specifiers in the fmtstr string.
The format specifier string fmtstr is constant and must be located in FLASH memory. The implementation of scanf is a reduced version of the standard C function.
This was necessary due to the specific needs of an embedded system and because the full implementation would require a large amount of FLASH memory space.
The format specifier string has the following structure:
%[width][l]type_char
The optional width specifier sets the maximal number of characters to read. If the function encounters a whitespace character or one that cannot be converted, then it will continue with the next input field, if present.
The optional 'l' input size modifier specifies that the function argument must be treated as a long int for the 'i', 'd', 'u', 'x' conversion type characters.
The type_char conversion type character is used to specify the way the input field will be processed.
The following conversion type characters are supported:
'd' - inputs a signed decimal integer in a pointer to int argument; 'i' - inputs a signed decimal integer in a pointer to int argument;
'u' - inputs an unsigned decimal integer in a pointer to unsigned int argument;
'x' - inputs an unsigned hexadecimal integer in a pointer to unsigned int argument; 'c' - inputs an ASCII character in a pointer to char argument;
's' - inputs an ASCII character string in a pointer to char argument; '%' - no input is done, a '%' is stored.
The function returns the number of successful entries, or -1 on error.
signed char sscanf(char *str, char flash *fmtstr [ , arg1 address, arg2 address, ...])
this function is identical to scanf except that the formatted text is inputted from the null terminated character string str, located in SRAM.
In order to reduce program code size, there is the Project|Configure|C Compiler|Code Generation|(s)scanf features option.
It allows linking different versions of the scanf and sscanf functions, with only the features that are really required by the program.
© 1998-2007 HP InfoTech S.R.L. |
Page 117 |
CodeVisionAVR
The following (s)scanf features are available:
•int, width - the following conversion type characters are supported: 'c', 's', 'i', 'd', 'u', 'x', '%', the width specifier is supported, no input size modifiers are supported
•long, width - the following conversion type characters are supported: 'c', 's', 'i', 'd', 'u', 'x', '%' the width specifier is supported, only the 'l' input size modifier is supported.
The more features are selected, the larger is the code size generated for the scanf and sscanf functions.
4.3 Standard Library Functions
The prototypes for these functions are placed in the file stdlib.h, located in the ..\INC subdirectory. This file must be #include -ed before using the functions.
int atoi(char *str)
converts the string str to integer.
long int atol(char *str)
converts the string str to long integer.
void itoa(int n, char *str)
converts the integer n to characters in string str.
void ltoa(long int n, char *str)
converts the long integer n to characters in string str.
void ftoa(float n, unsigned char decimals, char *str)
converts the floating point number n to characters in string str. The number is represented with a specified number of decimals.
void ftoe(float n, unsigned char decimals, char *str)
converts the floating point number n to characters in string str.
The number is represented as a mantissa with a specified number of decimals and an integer power of 10 exponent (e.g. 12.35e-5).
float atof(char *str)
converts the characters from string str to floating point.
int rand (void)
generates a pseudo-random number between 0 and 32767.
void srand(int seed)
sets the starting value seed used by the pseudo-random number generator in the rand function.
© 1998-2007 HP InfoTech S.R.L. |
Page 118 |
CodeVisionAVR
void *malloc(unsigned int size)
allocates a memory block in the heap, with the length of size bytes.
On success the function returns a pointer to the start of the memory block, the block being filled with zeroes.
The allocated memory block occupies size+4 bytes in the heap.
This must be taken into account when specifying the Heap size in the Project|Configure|C Compiler|Code Generation menu.
If there wasn’t enough contiguous free memory in the heap to allocate, the function returns a null pointer.
void *calloc(unsigned int num, unsigned int size)
allocates a memory block in the heap for an array of num elements, each element having the size length.
On success the function returns a pointer to the start of the memory block, the block being filled with zeroes.
If there wasn’t enough contiguous free memory in the heap to allocate, the function returns a null pointer.
void *realloc(void *ptr, unsigned int size)
changes the size of a memory block allocated in the heap.
The ptr pointer must point to a block of memory previously allocated in the heap. The size argument specifies the new size of the memory block.
On success the function returns a pointer to the start of the newly allocated memory block, the contents of the previously allocated block being copied to the newly allocated one.
If the newly allocated memory block is larger in size than the old one, the size difference is not filled with zeroes.
If there wasn’t enough contiguous free memory in the heap to allocate, the function returns a null pointer.
void free(void *ptr)
frees a memory block allocated in the heap by the malloc, calloc or realloc functions and pointed by the ptr pointer.
After being freed, the memory block is available for new allocation. If ptr is null then it is ignored.
© 1998-2007 HP InfoTech S.R.L. |
Page 119 |