create a 1 second delay between the flashes.
Programmer: Dogan Ibrahim
File: FLASH2.C
Date: May, 2007
********************************************************************/
#define LED PORTB.0
#define ON 1
#define OFF 0
#define One_Second_Delay Delay_ms(1000)
void main() {
TRISB = 0; // Configure PORTB as output
for(;;) // Endless loop
{
LED = ON; // Turn ON LED
One_Second_Delay; // 1 second delay
LED = OFF; // Turn OFF LED
One_Second_Delay; // 1 second delay
}
}
Figure 4.17: Another program to flash an LED
4.3 mikroC Library Functions
A large set of library functions is available with the mikroC compiler. These library functions can be called from anywhere in a program, and they do not require that header files are included in the program. The mikroC user manual gives a detailed description of each library function, with examples. In this section, the available library functions are identified, and the important and commonly used library functions are described in detail, with examples.
Table 4.2 gives a list of the mikroC library functions, organized in functional order.
Table 4.2: mikroC library functions
Library |
Description |
ADC |
Analog-to-digital conversion functions |
CAN |
CAN bus functions |
CANSPI |
SPI-based CAN bus functions |
Compact Flash |
Compact flash memory functions |
EEPROM |
EEPROM memory read/write functions |
Ethernet |
Ethernet functions |
SPI Ethernet |
SPI-based Ethernet functions |
Flash Memory |
Flash memory functions |
Graphics LCD |
Standard graphics LCD functions |
T6963C Graphics LCD |
T6963-based graphics LCD functions |
I²C |
I²C bus functions |
Keypad |
Keypad functions |
LCD |
Standard LCD functions |
Manchester Code |
Manchester code functions |
Multi Media |
Multimedia functions |
One Wire |
One wire functions |
PS/2 |
PS/2 functions |
PWM |
PWM functions |
RS-485 |
RS-485 communication functions |
Sound |
Sound functions |
SPI |
SPI bus functions |
USART |
USART serial communication functions |
Util |
Utilities functions |
SPI Graphics LCD |
SPI-based graphics LCD functions |
Port Expander |
Port expander functions |
SPI LCD |
SPI-based LCD functions |
ANSI C Ctype |
C Ctype functions |
ANSI C Math |
C Math functions |
ANSI C Stdlib |
C Stdlib functions |
ANSI C String |
C String functions |
Conversion |
Conversion functions |
Trigonometry |
Trigonometry functions |
Time |
Time functions |
Some of the frequently used library functions are:
• EEPROM library
• LCD library
• Software UART library
• Hardware USART library
• Sound library
• ANSI C library
• Miscellaneous library
The EEPROM library includes functions to read data from the on-chip PIC microcontroller nonvolatile EEPROM memory, or to write data to this memory. Two functions are provided:
• Eeprom_Read
• Eeprom_Write
The Eeprom_Read function reads a byte from a specified address of the EEPROM. The address is of type integer, and thus the function supports PIC microcontrollers with more than 256 bytes. A 20ms delay should be used between successive reads from the EEPROM to guarantee the return of correct data. In the following example, the byte at address 0x1F of the EEPROM is read and stored in variable Temp :
Temp = Eeprom_Read(0x1F);
The Eeprom_Write function writes a byte to a specified address of the EEPROM. The address is of type integer and thus the function supports PIC microcontrollers with more than 256 bytes. A 20ms delay should be used between successive reads or writes to the EEPROM to guarantee the correct transfer of data to the EEPROM. In the following example, number 0x05 is written to address 0x2F of the EEPROM:
Eeprom_Write(0x2F, 0x05);
Example 4.11
Write a program to read the contents of EEPROM from address 0 to 0x2F and then send this data to PORTB of a PIC microcontroller.
Solution 4.11
The required program is given in Figure 4.18. A for loop is used to read data from the EEPROM and then send it to PORT B of the microcontroller. Notice that a 20ms delay is used between each successive read.
/***********************************************************************
READING FROM THE EEPROM
=========================
This program reads data from addresses 0 to 0x2F of the EEPROM and then
sends this data to PORTB of the microcontroller.
Programmer: Dogan Ibrahim
File: EEPROM.C
Date: May, 2007
************************************************************************/
void main() {
unsigned int j;
unsigned char Temp;
TRISB = 0; // Configure PORTB as output
for (j=0; j <= 0x2F; j++) {
Temp = Eeprom_Read(j);
PORTB = Temp;
Delay_ms(20);
}
}
Figure 4.18: Program to read from the EEPROM
One thing all microcontrollers lack is some kind of video display. A video display would make a microcontroller much more user-friendly, enabling text messages, graphics, and numeric values to be output in a more versatile manner than with 7-segment displays, LEDs, or alphanumeric displays. Standard video displays require complex interfaces and their cost is relatively high. LCDs are alphanumeric (or graphic) displays which are frequently used in microcontroller-based applications. These display devices come in different shapes and sizes. Some LCDs have forty or more character lengths with the capability to display several lines. Others can be programmed to display graphic images. Some modules offer color displays, while others incorporate backlighting so they can be viewed in dimly lit conditions.
There are basically two types of LCDs as far as the interfacing technique is concerned: parallel and serial. Parallel LCDs (e.g., the Hitachi HD44780 series) are connected to the microcontroller circuitry such that data is transferred to the LCD using more than one line, usually four or eight data lines. Serial LCDs are connected to a microcontroller using one data line only, and data is transferred using the RS232 asynchronous data communications protocol. Serial LCDs are generally much easier to work with but more costly than parallel ones. In this book only parallel LCDs are discussed, as they are used more often in microcontroller-based projects.
Читать дальше