Temp++; // Increment data byte
Usart_Write(Temp); // Send the byte byte
}
}
Figure 4.26: Program listing of Example 4.14
In PIC microcontrollers that have more than one USART, the second USART is accessed by appending a “2” to the end of the function (e.g., Usart_Write2, Usart_Read2, etc.).
4.3.5 Sound Library
Functions in the sound library make it possible to generate sounds in our applications. A speaker (e.g., a piezo speaker) should be connected to the required microcontroller port. The following functions are offered by the sound library:
• Sound_Init
• Sound_Play
The Sound_Init function initializes the sound library and requires two parameters: the name and the bit number of the port where the speaker is connected. The address of the port name should be passed to the function. For example, if the speaker is connected to bit 3 of PORTB, then the function should be called as:
Sount_Init(&PORTB, 3);
The Sound_Play function plays a sound at a specified port pin. The function receives two arguments: the period divided by 10 (TDIV) and the number of periods (N). The first parameter is the period in microcontroller cycles divided by 10. The second parameter specifies the duration (number of clock periods) of the sound.
The following formula calculates the value used as the first parameter:
where
TDIV is the number to be used as the first parameter
F is the required sound frequency (Hz)
f is the microcontroller clock frequency (Hz)
Write a program to play a sound at 1KHz, assuming the clock frequency is 4MHz. The required duration of the sound is 250 periods.
The first parameter is calculated as follows:
Since the required duration is 250 periods, the function is called with the parameters:
Sound_Play(100, 250);
4.3.6 ANSI C Library
The ANSI C library consists of the following libraries (further details on these libraries are available in the mikroC user manual):
• Ctype library
• Math library
• Stdlib library
• String library
The functions in the Ctype library are mainly used for testing or data conversion. Table 4.6 lists the commonly used functions in this library.
Table 4.6: Commonly used Ctype library functions
| Function | Description |
|---|---|
| isalnum | Returns 1 if the specified character is alphanumeric (a–z, A–Z, 0–9) |
| isalpha | Returns 1 if the specified character is alphabetic (a–z, A–Z) |
| iscntrl | Returns 1 if the specified character is a control character (decimal 0–31 and 127) |
| isdigit | Returns 1 if the specified character is a digit (0–9) |
| islower | Returns 1 if the specified character is lowercase |
| isprint | Returns 1 if the specified character is printable (decimal 32–126) |
| isupper | Returns 1 if the specified character is uppercase |
| toupper | Convert a character to uppercase |
| tolower | Convert a character to lowercase |
The functions in the Math library are used for floating point mathematical operations. Table 4.7 lists the commonly used functions in this library.
Table 4.7: Commonly used Math library functions
| Function | Description |
|---|---|
| acos | Returns in radians the arc cosine of its parameter |
| asin | Returns in radians the arc sine of its parameter |
| atan | Returns in radians the arc tangent of its parameter |
| atan2 | Returns in radians the arc tangent of its parameter where the signs of both parameters are used to determine the quadrant of the result |
| cos | Returns the cosine of its parameter in radians |
| cosh | Returns the hyperbolic cosine of its parameter |
| exp | Returns the exponential of its parameter |
| fabs | Returns the absolute value of its parameter |
| log | Returns the natural logarithm of its parameter |
| log10 | Returns the logarithm to base 10 of its parameter |
| pow | Returns the power of a number |
| sin | Returns the sine of its parameter in radians |
| sinh | Returns the hyperbolic sine of its parameter |
| sqrt | Returns the square root of its parameter |
| tan | Returns the tangent of its parameter in radians |
| tanh | Returns the hyperbolic sine of its parameter |