FRIENDz

FAILURE IS THE SIGN OF SUCCESS!!

Extend I/O Ports using PIC

The mikroC PRO for PIC provides a library for communication with the Microchip’s Port Expander MCP23S17 via SPI interface. Connections of the PIC compliant MCU and MCP23S17 is given on the schematic at the bottom of this page.
  Important :
  • The library uses the SPI module for communication. User must initialize the appropriate SPI module before using the Port Expander Library.
  • Library does not use Port Expander interrupts.

External dependencies of Port Expander Library

The following variables must be defined in all projects using Port Expander Library: Description : Example :
extern sfr sbit SPExpanderRST; Reset line. sbit SPExpanderRST at RC0_bit;
extern sfr sbit SPExpanderCS; Chip Select line. sbit SPExpanderCS at RC1_bit;
extern sfr sbit SPExpanderRST_Direction; Direction of the Reset pin. sbit SPExpanderRST_Direction at TRISC0_bit;
extern sfr sbit SPExpanderCS_Direction; Direction of the Chip Select pin. sbit SPExpanderCS_Direction at TRISC1_bit;

Connect Memory Card using PIC18F4520 Code

The Multi Media Card (MMC) is a Flash memory card standard. MMC cards are currently available in sizes up to and including 32 GB and are used in cellular phones, digital audio players, digital cameras and PDA’s.
mikroC PRO for PIC provides a library for accessing data on Multi Media Card via SPI communication. This library also supports SD (Secure Digital) and high capacity SDHC (Secure Digital High Capacity) memory cards .

Secure Digital Card

Secure Digital (SD) is a Flash memory card standard, based on the older Multi Media Card (MMC) format.
SD cards are currently available in sizes of up to and including 2 GB, and are used in digital cameras, digital camcorders, handheld computers, media players, mobile phones, GPS receivers, video games and PDAs.

Create Musical Tone using PIC Code

The mikroC PRO for PIC provides a Sound Library to supply users with routines necessary for sound signalization in their applications. Sound generation needs additional hardware, such as piezo-speaker (example of piezo-speaker interface is given on the schematic at the bottom of this page).

Library Routines

  • Sound_Init
  • Sound_Play

Sound_Init

Prototype void Sound_Init(char *snd_port, char snd_pin);
Returns Nothing.
Description Configures the appropriate MCU pin for sound generation.
Parameters :
  • snd_port: sound output port address
  • snd_pin: sound output pin

Connect Ethernet controller with PIC Code

The ENC28J60 is a stand-alone Ethernet controller with an industry standard Serial Peripheral Interface (SPI). It is designed to serve as an Ethernet network interface for any controller equipped with SPI.
The ENC28J60 meets all of the IEEE 802.3 specifications. It incorporates a number of packet filtering schemes to limit incoming packets. It also provides an internal DMA module for fast data throughput and hardware assisted IP checksum calculations. Communication with the host controller is implemented via two interrupt pins and the SPI, with data rates of up to 10 Mb/s. Two dedicated pins are used for LED link and network activity indication.
This library is designed to simplify handling of the underlying hardware (ENC28J60). It works with any PIC with integrated SPI and more than 4 Kb ROM memory. 38 to 40 MHz clock is recommended to get from 8 to 10 Mhz SPI clock, otherwise PIC should be clocked by ENC28J60 clock output due to its silicon bug in SPI hardware. If you try lower PIC clock speed, there might be board hang or miss some requests.
SPI Ethernet ENC28J60 Library supports:

UART Communication using PIC Code

The UART hardware module is available with a number of PIC compliant MCUs. The mikroC PRO for PIC UART Library provides comfortable work with the Asynchronous (full duplex) mode.
You can easily communicate with other devices via RS-232 protocol (for example with PC, see the figure at the end of the topic – RS-232 HW connection). You need a PIC MCU with hardware integrated UART, for example 16F887. Then, simply use the functions listed below.
  Important :
  • UART library routines require you to specify the module you want to use. To select the desired UART module, simply change the letter x in the routine prototype for a number from 1 to 2.
  • Switching between the UART modules in the UART library is done by the UART_Set_Active function (UART modules have to be previously initialized).
  • Number of UART modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.

Connect USB HID with PIC18F4450 Code

Universal Serial Bus (USB) provides a serial bus standard for connecting a wide variety of devices, including computers, cell phones, game consoles, PDA’s, etc.
USB Library contains HID routines that support HID class devices, and also the generic routines that can be used with vendor specified drivers.

USB HID Class

The HID class consists primarily of devices that are used by humans to control the operation of computer systems. Typical examples of HID class devices include :
  • Keyboards and pointing devices, for example: standard mouse devices, trackballs, and joysticks.
  • Front-panel controls, for example: knobs, switches, buttons, and sliders.
  • Controls that might be found on devices such as telephones, VCR remote controls, games or simulation devices, for example: data gloves, throttles, steering wheels, and rudder pedals.
  • Devices that may not require human interaction but provide data in a similar format to HID class devices, for example, bar-code readers, thermometers, or voltmeters.
Many typical HID class devices include indicators, specialized displays, audio feedback, and force or tactile feedback. Therefore, the HID class definition includes support for various types of output directed to the end user.

Connect Matrix Keypad with PIC Controller Code

The mikroC PRO for PIC provides a library for working with 4x4 keypad. The library routines can also be used with 4x1, 4x2, or 4x3 keypad. For connections explanation see schematic at the bottom of this page.

External dependencies of Keypad Library

The following variable must be defined in all projects using Keypad Library: Description : Example :
extern sfr char keypadPort; Keypad Port. char keypadPort at PORTD;

Library Routines

  • Keypad_Init
  • Keypad_Key_Press
  • Keypad_Key_Click

Connect I²C with PIC

The I²C full master MSSP module is available with a number of PIC MCU models. mikroC PRO for PIC provides library which supports the master I²C mode.

  Important :
  • Some MCUs have multiple I²C modules. In order to use the desired I²C library routine, simply change the number 1 in the prototype with the appropriate module number, i.e. I2C2_Init(100000);

Library Routines

  • I2C1_Init
  • I2C1_Start
  • I2C1_Repeated_Start
  • I2C1_Is_Idle
  • I2C1_Rd
  • I2C1_Wr
  • I2C1_Stop

Connect Flash Card with PIC18F4520

The Compact Flash Library provides routines for accessing data on Compact Flash card (abbr. CF further in text). CF cards are widely used memory elements, commonly used with digital cameras. Great capacity and excellent access time of only a few microseconds make them very attractive for microcontroller applications.
In CF card, data is divided into sectors. One sector usually comprises 512 bytes. Routines for file handling, the Cf_Fat routines, are not performed directly but successively through 512B buffer.
  Important :
  • Routines for file handling can be used only with FAT16 file system.
  • Library functions create and read files from the root directory only.
  • Library functions populate both FAT1 and FAT2 tables when writing to files, but the file data is being read from the FAT1 table only; i.e. there is no recovery if the FAT1 table gets corrupted.
  • If MMC/SD card has Master Boot Record (MBR), the library will work with the first available primary (logical) partition that has non-zero size. If MMC/SD card has Volume Boot Record (i.e. there is only one logical partition and no MBRs), the library works with entire card as a single partition. For more information on MBR, physical and logical drives, primary/secondary partitions and partition tables, please consult other resources, e.g. Wikipedia and similar.
  • Before writing operation, make sure not to overwrite boot or FAT sector as it could make your card on PC or digital camera unreadable. Drive mapping tools, such as Winhex, can be of great assistance.


Connect CAN-SPI with PIC Controller

The SPI module is available with a number of the PIC compliant MCUs. The mikroC PRO for PIC provides a library (driver) for working with mikroElektronika's CANSPI Add-on boards (with MCP2515 or MCP2510) via SPI interface.
The CAN is a very robust protocol that has error detection and signalization, self–checking and fault confinement. Faulty CAN data and remote frames are re-transmitted automatically, similar to the Ethernet.
In the mikroC PRO for PIC, each routine of the CAN library has its own CANSPI counterpart with identical syntax. For more information on Controller Area Network, consult the CAN Library. Note that an effective communication speed depends on SPI and certainly is slower than "real" CAN.
Data transfer rates depend on distance. For example, 1 Mbit/s can be achieved at network lengths below 40m while 250 Kbit/s can be achieved at network lengths below 250m. The greater distance the lower maximum bitrate that can be achieved. The lowest bitrate defined by the standard is 200Kbit/s. Cables used are shielded twisted pairs.
CAN supports two message formats:
  • Standard format, with 11 identifier bits and
  • Extended format, with 29 identifier bits

Connect CAN Protocol with PIC

The mikroC PRO for PIC provides a library (driver) for working with the CAN module.
The CAN is a very robust protocol that has error detection and signalization, self–checking and fault confinement. Faulty CAN data and remote frames are re-transmitted automatically, similar to the Ethernet.
Data transfer rates depends on the distance. For example, 1 Mbit/s can be achieved at network lengths below 40m while 250 Kbit/s can be achieved at network lengths below 250m. The greater distance the lower maximum bitrate that can be achieved. The lowest bitrate defined by the standard is 200Kbit/s. Cables used are shielded twisted pairs.
CAN supports two message formats:
  • Standard format, with 11 identifier bits, and
  • Extended format, with 29 identifier bits
  Important :
  • Consult the CAN standard about CAN bus termination resistance.

Connect ADC with PIC16F877

ADC (Analog to Digital Converter) module is available with a number of PIC MCU modules. ADC is an electronic circuit that converts continuous signals to discrete digital numbers. ADC Library provides you a comfortable work with the module.

Library Routines

  • ADC_Init
  • ADC_Get_Sample
  • ADC_Read

Control LED Light brightness using PWM

CMO module is available with a number of AVR MCUs. mikroC PRO for AVR provides library which simplifies using PWM HW Module.
  Important :
  • AVR MCUs require you to specify the module you want to use. To select the desired PWM, simply change the letter x in the prototype for a number from 1 to 2. Number of UART modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library. 
    For the XMEGA family of MCUs change the xn in the routine prototype with C0, C1, D0, D1, E0, E1, F0 or F1 (MCU dependent).
  • For better understanding of PWM module it would be best to start with the example provided in Examples folder of our mikroC PRO for AVR compiler.
  • When you select a MCU, mikroC PRO for AVR automatically loads the correct PWM library (or libraries), which can be verified by looking at the Library Manager.
  • PWM library handles and initializes the PWM module on the given AVR MCU, but it is up to user to set the correct pins as PWM output, this topic will be covered later in this section.
  • mikroC PRO for AVR does not support enhanced PWM modules.

PS/2 Keyboard connect with ATMEGA16

The mikroC PRO for AVR provides a library for communication with the common PS/2 keyboard.
  Important :
  • The library does not utilize interrupts for data retrieval, and requires the oscillator clock to be at least 6MHz.
  • The pins to which a PS/2 keyboard is attached should be connected to the pull-up resistors.
  • Although PS/2 is a two-way communication bus, this library does not provide MCU-to-keyboard communication; e.g. pressing the Caps Lock key will not turn on the Caps Lock LED.

Expand I/O Ports in ATmega16

The mikroC PRO for AVR provides a library for communication with the Microchip’s Port Expander MCP23S17 via SPI interface. Connections of the AVR compliant MCU and MCP23S17 is given on the schematic at the bottom of this page.
  Important :
  • The library uses the SPI module for communication. User must initialize the appropriate SPI module before using the Port Expander Library.
  • Library does not use Port Expander interrupts.

Transmitter and Receiver connect with ATmega16


The mikroC PRO for AVR provides a library for handling Manchester coded signals. The Manchester code is a code in which data and clock signals are combined to form a single self-synchronizing data stream; each encoded bit contains a transition at the midpoint of a bit period, the direction of transition determines whether the bit is 0 or 1;

  Important :
  • The Manchester receive routines are blocking calls (Man_Receive_Init and Man_Synchro). This means that MCU will wait until the task has been performed (e.g. byte is received, synchronization achieved, etc).
  • Manchester code library implements time-based activities, so interrupts need to be disabled when using it.

Connect Memory Card with ATmega128 Code


The Compact Flash Library provides routines for accessing data on Compact Flash card. CF cards are widely used memory elements, commonly used with digital cameras. Great capacity and excellent access time of only a few microseconds make them very attractive for microcontroller applications.
In CF card, data is divided into sectors. One sector usually comprises 512 bytes. Routines for file handling, the Cf_Fat routines, are not performed directly but successively through 512B buffer.

  Important :
  • Routines for file handling can be used only with FAT16 file system.
  • Library functions create and read files from the root directory only.
  • Library functions populate both FAT1 and FAT2 tables when writing to files, but the file data is being read from the FAT1 table only; i.e. there is no recovery if the FAT1 table gets corrupted.
  • If MMC/SD card has Master Boot Record (MBR), the library will work with the first available primary (logical) partition that has non-zero size. If MMC/SD card has Volume Boot Record (i.e. there is only one logical partition and no MBRs), the library works with entire card as a single partition. For more information on MBR, physical and logical drives, primary/secondary partitions and partition tables, please consult other resources, e.g. Wikipedia and similar.
  • Before writing operation, make sure not to overwrite boot or FAT sector as it could make your card on PC or digital camera unreadable. Drive mapping tools, such as Winhex, can be of great assistance.

CAN Protocol connect with ATMEGA16

The SPI module is available with a number of the AVR compliant MCUs. The mikroC PRO for AVR provides a library (driver) for working with mikroElektronika's CANSPI Add-on boards (with MCP2515 or MCP2510) via SPI interface.The CAN is a very robust protocol that has error detection and signalization, self–checking and fault confinement. Faulty CAN data and remote frames are re-transmitted automatically, similar to the Ethernet.

Data transfer rates depend on distance. For example, 1 Mbit/s can be achieved at network lengths below 40m while 250 Kbit/s can be achieved at network lengths below 250m. The greater distance the lower maximum bitrate that can be achieved. The lowest bitrate defined by the standard is 200Kbit/s. Cables used are shielded twisted pairs.
CAN supports two message formats:
  • Standard format, with 11 identifier bits and
  • Extended format, with 29 identifier bits

Connect Sensor with ATMEGA16 Code

ADC (Analog to Digital Converter) module is available with a number of AVR MCUs. Several library routines are included to provide you comfortable work with the module in single-ended mode.

Library Routines

  • ADC_Init
  • ADCx_Init_Advanced
  • ADC_Get_Sample
  • ADC_Read

ADC_Init

Prototype void ADC_Init();
// for XMEGA family of MCUs void ADCx_Init();

Connecting 4*4 Matrix KeyPad with ATMEGA16

The mikroC PRO for AVR provides a library for working with 4x4 keypad. The library routines can also be used with 4x1, 4x2, or 4x3 keypad.

External dependencies of Keypad Library
The following variable must be defined in all projects using Keypad Library: Description : Example :
extern sfr char keypadPort; Keypad Port. char keypadPort at PORTB;
extern sfr char keypadPort_Direction; Direction of the Keypad Port. char keypadPort_Direction at DDRB;

Library Routines