FRIENDz

FAILURE IS THE SIGN OF SUCCESS!!
Showing posts with label AVR. Show all posts
Showing posts with label AVR. Show all posts

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

Driving a Unipolar Stepper Motor

     If your stepper requires a higher current than the L293 can provide (600 mA for the L293D), you can use the SN754410 chip that handles up to 1 amp. For current up to 2 amps, you can use the L298 chip.A simple way to connect an L298 to Arduino is to use the SparkFun Ardumoto shield (DEV-09213). This plugs on top of an Arduino board and only requires external connection to the motor windings; the motor power comes from the Arduino Vin pin. In1/2 is controlled by pin 12, and ENA is pin 10. In3/4 is connected to pin 13, and ENB is on pin 11. Make the following changes to the code to use the preceding sketch with Ardumoto:

Controlling the Direction of a Brushed Motor with an H-Bridge

            An H-Bridge can control two brushed motors. Figure 8-10 shows the connections for the L293D H-Bridge IC, you can also use the SN754410 which has the same pin layout.In the sketch in this recipe’s Solution, a single motor is controlled using the IN1 and IN2 pins; the EN pin is permanently HIGH because it is connected to +5V.

Controlling Solenoids and Relays

         The choice of transistor is dependent on the amount of current required to activate the solenoid or relay. The data sheet may specify this in milliamperes (mA) or as the resistance of the coil. To find the current needed by your solenoid or relay, divide the voltage of the coil by its resistance in ohms. For example, a 12V relay with a coil of 185 ohms draws 65 mA: 12 (volts) / 185 (ohms) = 0.065 amps, which is 65 mA. Small transistors such as the 2N2222 are sufficient for solenoids requiring up to a few hundred milliamps. Larger solenoids will require a higher power transistor, like the TIP102/TIP120 or similar. There are many suitable transistor alternatives.The purpose of the diode is to prevent reverse EMF from the coil from damaging the transistor (reverse EMF is a voltage produced when current through a coil is switched off). The polarity of the diode is important; there is a colored band indicating the cathode—this should be connected to the

Controlling Servos from the Serial Port

 Each servo line wire gets connected to a digital pin. All servo grounds are connected to Arduino ground. The servo power lines are connected together, and you may need an external 5V or 6V power source if your servos require more current than the Arduino power supply can provide. An array named myservo is used to hold references for the four servos. A for loop in setup attaches each servo in the array to consecutive pins defined in the servoPins array. If the character received from serial is a digit (the character will be greater than or equal to zero and less than or equal to 9), its value is accumulated in the variable pos. If the character is the letter a, the position is written to the first servo in the array (the servo connected to pin 7). The letters b, c, and d control the subsequent servos. 

Controlling the Speed of Rotation Servos

You can use similar code for continuous rotation and normal servos, but be aware that continuous rotation servos may not stop rotating when writing exactly 90 degrees. Some servos have a small potentiometer you can trim to adjust for this, or you can add or subtract a few degrees to stop the servo. For example, if the left servo stops rotating at 92 degrees, you can change the lines that write to the servos as follows:
myservoLeft.write(angle+TRIM); // declare int TRIM=2; at beginning of sketch.


Continuous rotation servos are a form of gear reduced motor with forward and backward speed adjustment. Control of continuous rotation servos is similar to normal servos. The servo rotates in one direction as the angle is increased from 90 degrees; it rotates in the other direction when the angle is decreased from 90

7-Segment LED Displays: Multiplexing

A matrix is created by passing pin numbers for the data, load, and clock pins. loop uses the write method to turn pixels on; the clar method turns the pixels off. write has three parameters: the first two identify the column and row (x and y) of an LED and the third  parameter (HIGH or LOW) turns the LED on or off.This Solution uses the popular MAX7221 LED driver chip to control four-digit commoncathode displays The MAX7221 provides a simpler solution  because it handles multiplexing and digit decoding in hardware.

Driving a 7-Segment LED Display

The segments to be lit for each numeral are held in the array called numeral. There isone byte per numeral where each bit in the byte represents one of seven segments .The array called segmentPins holds the pins associated with each segment. TheshowDigit function checks that the number ranges from zero to 9, and if valid, looks ateach segment bit and turns on the segment if the bit is set. Apin is set HIGH when turning on a segment on a commoncathode display, and it’s set LOW when turning on a segment on a common anode
display. The code here is for a common anode display, so it inverts the value as follows:

Controlling a Matrix of LEDs: Charlieplexing

The term Charlieplexing comes from Charlie Allen (of Microchip Technology, Inc.), who published the method. The technique is based on the fact that LEDs only turn on when connected the “right way” around (with the anode more positive than the cathode). Here is the table showing the LED number that is lit for the valid combinations of the three pins. L is LOW, H is HIGH, and i is INPUT mode. Setting a pin in INPUT mode effectively disconnects it from the circuit:

Controlling an LED Matrix Using Multiplexing

      The resistor’s value must be chosen to ensure that the maximum current through a pin does not exceed 40 mA. Because the current for up to eight LEDs can flow through each column pin, the maximum current for each LED must be one-eighth of 40 mA, or 5 mA. Each LED in a typical small red matrix has a forward voltage of around 1.8 volts. Calculating the resistor that results in 5 mA with a forward voltage of 1.8 volts
gives a value of 680 ohms. Check your data sheet to find the forward voltage of the matrix you want to use. Each column of the matrix is connected through the series resistor to a digital pin. When the column pin goes low and a row pin goes high, the corresponding LED will light. For all LEDs where the column pin is high or its row pin is low, no current will flow through the LED and it will not light. The for loop scans through each row and column and turns on sequential LEDs until all LEDs are lit. The loop starts with the first column and row and increments the row counter until all LEDs in that row are lit; it then moves to the next column, and so on, lighting another LED with each pass through the loop until all the LEDs are lit. You can control the

Detecting Rotation Using a Gyroscope

Arduino-Detecting Rotation Using a Gyroscope:
         Most low-cost gyroscopes use an analog voltage proportional to rotation rate, although some also  output using I2C. This recipe works with a gyro with an analog output proportional to rotation rate. Figure 6-16 shows an LISY300AL breakout board from SparkFun. Many low-cost gyros, such as the one used here, are 3.3V devices and must not be plugged into the 5V power pin.The Gyro OUT connection is the analog output and is connected to Arduino analog input 0. The PD connection enables the gyro to be switched into low power mode and is connected to analog pin 1 (in this sketch, it is used as a digital output pin). You can connect PD to any digital pin; the pin used here was chosen to keep the wiring neater. If you don’t need to switch the gyro into low-power mode, you can connect the PD line to Gnd.

Reading RFID Tags using Arduino

RFID Connected with Arduino
A tag consists of a start character followed by a 10-digit tag and is terminated by an end character. The sketch waits for a complete tag message to be available and displays the tag if it is valid. The tag is received as ASCII digits (see Recipe 4.4 for more on receiving ASCII digits). You may want to convert this into a number if you want to store or compare the values received. To do this, change the last few lines as follows:
if( Serial.read() == endByte) // check for the correct end character
{
tag[bytesread] = 0; // terminate the string
long tagValue = atol(tag); // convert the ASCII tag to a long integer

Measuring Temperature LM35

Arduino-Measuring Temperature using LM35:
The LM35 temperature sensor produces an analog voltage directly proportional to temperature with an output of 1 millivolt per 0.1°C (10 mV per degree). The sketch converts the analogRead values into millivolts and divides this by 10 to get degrees. The sensor accuracy is around 0.5°C, and in many cases you can use integer math instead of floating point. The following sketch turns on pin 2 when the temperature is above a threshold:
       This recipe displays the temperature in Fahrenheit and Celsius (Centigrade) using the popular LM35