Tuesday, November 26, 2013

Communicating two microcontrollers

Components Required




  • ATmega32 microcontroller x 2
  • AVR Programmer Board
  • Crystal
  • Capacitors
  • LEDs
  • +5V supply
  • Breadboard
  • Connecting Wires

Circuit Diagram

Description



The first microcontroller(at the top) takes the analog value of the voltage of the battery provided in the figure and shows its digital value on the given LEDs connected to the PORT B of the first microcontroller. For more information on the ANALOG TO DIGITAL conversion go through the blog analog-to-digital-conversion. Now the digital data is send to the second microcontroller and the LEDs corresponding to the digital value of the data in PORT B glows. If the data transfer is proper then the bulbs glowing through both the microcontrollers will be the same.

Connections

  • LEDs are connected to the Pins 0 to 7 of PORT B for both the microcontrollers.
  • The analog input is given to PIN A0 (ADC 0) of first microcontroller.
  • The Txd of the first microcontroller is connected to the Rxd of the second microcontroller.
  • A crystal is connected to the XTAL1 and XTAL2 pins to provide the clock pulse for both the microcontrollers.
  • Reset is connected to +5V.
NOTE:
  •  The crystal oscillator connection is not shown in the figure due to limited space. Kindly go through the previous blogs to check out the connection.
  • The ground of both the microcontrollers must be at the same potential.


Source Code 1/2

/*The first microcontroller converts the analog data to digital data, glows the corresponding LEDs and sends the same to the second microcontroller*/
#ifndef F_CPU
#define F_CPU 1000000UL               //frequency of the external crystal
#endif

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#define USART_BAUDRATE 9600     
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
void send(uint8_t data);                          //function to send the data
int main()
 { 
DDRB = 0xFF;                             //All pins of PORT B as output
PORTB = 0xFF;                           //Initially all pins of PORTB as output low
UCSRB = (1 << RXEN ) | (1 << TXEN );            //Enabling the RXD and TXD 
UCSRC = (1 << URSEL ) | (1 << UCSZ0 ) | (1 << UCSZ1 );
UBRRH = ( BAUD_PRESCALE >> 8);
  UBRRL = BAUD_PRESCALE ; 
ADCSRA |= 1<<ADPS2;
ADMUX |=1<<REFS0 | 1<<REFS1;
ADMUX |=1<<ADLAR; 
ADCSRA |=1<<ADIE;
ADCSRA |=1<<ADEN;
sei();
ADCSRA |=1<<ADSC;
   while (1)                                                                      //Infinite while loop
   ;
   return 0;
 }
 ISR(ADC_vect)                                                            //Interrupt service routine 
 {
         //theLow stores the converted digital value
uint8_t theLow = ADCH;                                    
          //PORT B is configured according to theLow
PORTB = theLow;       
         //send theLow to the send function                                       
send(theLow);                                                       
 ADCSRA |=1<<ADSC;                                      
  } 
  void send(uint8_t data)
  {
 while (( UCSRA & (1 << UDRE )) == 0);
 _delay_ms(1);
 UDR = data;
  }

Source Code 2/2


/*The second microcontroller receives the data and configures the output of the PORT B according to the input received from the first microcontroller*/

#ifndef F_CPU
#define F_CPU 1000000UL               //frequency of the external crystal
#endif

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#define USART_BAUDRATE 9600 
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
uint8_t receive(void);
uint8_t msg;
int main()
 { 
DDRB = 0xFF;
PORTB = 0xFF;
UCSRB = (1 << RXEN ) | (1 << TXEN );
UCSRC = (1 << URSEL ) | (1 << UCSZ0 ) | (1 << UCSZ1 );
UBRRH = ( BAUD_PRESCALE >> 8);
  UBRRL = BAUD_PRESCALE ; 
sei();                                                      //to enable the interrupt
   while (1)                                                      //Infinite while loop
   {
  msg = receive();                                   //to receive the data from function
           //configure the PORT B according to the received data
  PORTB = msg;                                   
   }
   return 0;
 }

uint8_t receive(void)
  {
 uint8_t data;
 while (!( UCSRA & (1 << RXC )));
      return UDR;                                          //return the received data to the calling function 
  }  


Thank you for reading.

For more tutorial and information, like the Facebook Page. 


Wednesday, October 30, 2013

Analog to Digital Conversion

Components Required



  • ATmega32 microcontroller
  • AVR Programmer Board
  • Crystal
  • Capacitors
  • LEDs
  • +5V supply
  • Breadboard
  • Connecting Wires

Circuit Diagram


Description

The digital value of the analog input at PA0 will be shown in the 8 leds PC0 to PC7 from LSB to MSB.

Connections

  • LEDs are connected to the Pins 0 to 7 of PORT C.
  • The analog input is given to PIN A0 (ADC 0).
  • A crystal is connected to the XTAL1 and XTAL2 pins to provide the clock pulse.
  • Reset is connected to +5V.

    Source Code

    /*The following code will keep on converting the analog value applied at the pin PA0, convert it into digital value and store the most significant 8 bits in ADCH register and the lesser two bits in the ADCL register.
    Since the lesser two values can be ignored the 8 bits present in the ADCH are only taken care of and the LEDs are glown according to the ADCH register*/

    /* Go through the blog http://rickruling.blogspot.in/2013/10/adc-control-and-status-registers.html for more details on adc control and status registers*/

    #include <avr/io.h>
    #include <avr/interrupt.h>                     //For interrupt handling

    #ifndef F_CPU
    #define F_CPU 1000000UL               //frequency of the external crystal
    #endif

    int main(void)
    {
    DDRC = 0xFF;                     //All ports of port C as output
    PORTB = 0x00;                            //Initially all pins as output low
    //configure the ADC
    ADCSRA |=1<<ADPS2;       //ADC Prescaler Select Bits (Division Factor is 16 for 100)
    ADMUX |=1<<ADLAR;      //MSB in ADCH
    ADMUX |=1<<REFS0;        // AREF pin
    ADCSRA |=1<<ADIE;         //ADC Interrupt Enable
    ADCSRA |=1<<ADEN;        //ADC Enable
    sei();                                      //set global interrupt enable
    ADCSRA |=1<<ADSC;        //ADC Start Conversion
        while(1)                                      //Infinite loop
              ;
    }

    ISR(ADC_vect)
    {
    PORTB = ADCH;                     //Result of conversion to PORTB
    ADCSRA |=1<<ADSC;            //Restart the conversion
    }


    Thank you for reading.

    For more tutorial and information, like the Facebook Page 

    ADC Control and Status Registers

    Features

    • 10-bit Resolution
    • 0.5 LSB Integral Non-linearity
    • ±2 LSB Absolute Accuracy
    • 13 μs - 260 μs Conversion Time
    • Up to 15 kSPS at Maximum Resolution
    • 8 Multiplexed Single Ended Input Channels
    • 7 Differential Input Channels
    • 2 Differential Input Channels with Optional Gain of 10x and 200x
    • Optional Left adjustment for ADC Result Readout
    • 0 - VCC ADC Input Voltage Range
    • Selectable 2.56V ADC Reference Voltage
    • Free Running or Single Conversion Mode
    • ADC Start Conversion by Auto Triggering on Interrupt Sources
    • Interrupt on ADC Conversion Complete
    • Sleep Mode Noise Canceler

    The ATmega32 features a 10-bit successive approximation ADC. The ADC is connected to an 8-channel Analog Multiplexer which allows 8 single-ended voltage inputs constructed from the pins of Port A.The device also supports 16 differential voltage input combinations. Two of the differential inputs(ADC1, ADC0 and ADC3, ADC2) are equipped with a programmable gain stage, providing amplification steps of 0 dB (1x), 20 dB (10x), or 46 dB (200x) on the differential input voltage before the A/D conversion. Seven differential analog input channels share a common negative terminal (ADC1), while any other ADC input can be selected as the positive input terminal. If 1x or 10x gain is used, 8-bit resolution can be expected. If 200x gain is used, 7-bit resolution can be expected.The ADC contains a Sample and Hold circuit which ensures that the input voltage to the ADC is held at a constant level during conversion.The ADC has a separate analog supply voltage pin, AVCC. AVCC must not differ more than ±0.3V from VCC. Internal reference voltages of nominally 2.56V or AVCC are provided On-chip. The voltage reference may be externally decoupled at the AREF pin by a capacitor for better noise performance.


    ADC Multiplexer Selection Register - ADMUX



    Bit 7:6 – REFS1:0: Reference Selection Bits
    These bits select the voltage reference for the ADC, as shown in Table 83. If these bits are
    changed during a conversion, the change will not go in effect until this conversion is complete
    (ADIF in ADCSRA is set). The internal voltage reference options may not be used if an external
    reference voltage is being applied to the AREF pin.

    Bit 5 – ADLAR: ADC Left Adjust Result
    The ADLAR bit affects the presentation of the ADC conversion result in the ADC Data Register.
    Write one to ADLAR to left adjust the result. Otherwise, the result is right adjusted. Changing the
    ADLAR bit will affect the ADC Data Register immediately, regardless of any ongoing conversions.

    Bits 4:0 – MUX4:0: Analog Channel and Gain Selection Bits
    The value of these bits selects which combination of analog inputs are connected to the ADC.
    These bits also select the gain for the differential channels.  If these bits
    are changed during a conversion, t
    he change will not go in effect until this conversion is
    complete (ADIF in ADCSRA is set).


    ADC Control and  Status Regiter A - ADCSRA


    Bit 7 – ADEN: ADC Enable
    Writing this bit to one enables the ADC. By writing it to zero, the ADC is turned off. Turning the
    ADC off while a conversion is in progress, will terminate this conversion.

    Bit 6 – ADSC: ADC Start Conversion
    In Single Conversion mode, write this bit to one to start each conversion. In Free Running Mode,
    write this bit to one to start the first conversion. The first conversion after ADSC has been written
    after the ADC has been enabled, or if ADSC is written at the same time as the ADC is enabled,
    will take 25 ADC clock cycles instead of the normal 13. This first conversion performs initialization
    of the ADC.
    ADSC will read as one as long as a conversion is in progress. When the conversion is complete,
    it returns to zero. Writing zero to this bit has no effect.

    Bit 5 – ADATE: ADC Auto Trigger Enable
    When this bit is written to one, Auto Triggering of the ADC is enabled. The ADC will start a 
    conversion on 
    a positive edge of the selected trigger signal. The trigger source is selected by setting
    the ADC Trigger Select bits, ADTS in SFIOR.


    Bit 4 – ADIF: ADC Interrupt Flag
    This bit is set when an ADC conversion completes and the Data Registers are updated. The
    ADC Conversion Complete Interrupt is executed if the ADIE bit and the I-bit in SREG are set.
    ADIF is cleared by hardware when executing the corresponding interrupt handling vector. 
    Alternatively, ADIF
    is cleared by writing a logical one to the flag. Beware that if doing a Read-Modify-
    Write on ADCSRA, a pending interrupt can be disabled. This also applies if the SBI and CBI
    instructions are used.

    Bit 3 – ADIE: ADC Interrupt Enable
    When this bit is written to one and the I-bit in SREG is set, the ADC Conversion Complete Interrupt
    is activated.

    Bits 2:0 – ADPS2:0: ADC Prescaler Select Bits
    These bits determine the division factor between the XTAL frequency and the input clock to the
    ADC.

    The ADC Data Registers - ADCL and ADCH


    ADLAR = 0





    ADLAR = 1





    When an ADC conversion is complete, the result is found in these two registers. If differential
    channels are used, the result is presented in two’s complement form.
    When ADCL is read, the ADC Data Register is not updated until ADCH is read. Consequently, if
    the result is left adjusted and no more than 8-bit precision is required, it is sufficient to read
    ADCH. Otherwise, ADCL must be read first, then ADCH.
    The ADLAR bit in ADMUX, and the MUXn bits in ADMUX affect the way the result is read from
    the registers. If ADLAR is set, the result is left adjusted. If ADLAR is cleared (default), the result
    is right adjusted.

    For more tutorial and information, like the Facebook Page 

    https://www.facebook.com/Robowares

    Sunday, October 27, 2013

    Interfacing LCD with the ATmega32

    Components Required


    • ATmega32 microcontroller
    • AVR Programmer Board
    • Crystal
    • Capacitors
    • 16x2 LCD
    • +5V supply
    • Breadboard
    • Connecting Wires

    Circuit Diagram






    Description


      DB0-DB7: Data Pins( use DB4-DB7 in 4 bit mode) 

      RS(Register Select) : 1 - Data is sent , 0 - Command is sent . 

      R/W(Read/Write) :1 - Read the LCD , 0 - Write to LCD. 

      E(Enable): to enable an operation . first make low(0) to send data and then set the other two control lines and when they are configured, bring E high (1) and wait for the minimum amount of time required by the LCD and bring it low (0) again. 

      VEE: Contrast Adjust Pin.

      Connections 

    • PC0 to PC7 are connected to the data pins D0 to D7 of the LCD.
    • VDD and VEE are connected to the +5 volts directly.
    • VEE can be connected to a potentiometer to adjust the contrast.
    • VSS is connected to ground.
    • RS, RW and E are connected to PD5, PD6 and PD7 respectively.
    • A crystal is connected to XTAL1 and XTAL2 pins to provide the clock pulse.
    • Reset is connected to +5V.

    Source Code

    /*
    The following code will display the string defined in the main function.
     */ 

    #include <avr/io.h>
    #include <util/delay.h>

    #ifndef F_CPU
    #define F_CPU 1000000UL //Frequency of the external crystal
    #endif

    /*CWR - Command Word Register*/
    #define ENABLE_LCD PORTD |= 0x80 //CWR state  to enable LCD
    #define DISABLE_LCD PORTD &= ~0x80//CWR state to disable LCD
    #define SET_LCD_DATA PORTD |= 0x20 //CWR state to send data
    #define SET_LCD_CMD PORTD &= ~0x20 //CWR state to send command

    void LCD_WriteCommand (unsigned char CMD);
    void LCD_WriteData (unsigned char Data);
    void LCD_DisplayString_F(char row, char column,char *string);
    void LCD_Cursor(char row, char column);

    void init_devices(void)
    {
    DDRC = 0xFF; //All pins of PORTC declared as Output
    PORTC = 0x00;//Initially all pins declared as output low
    DDRD = 0xF0;//Pins D4 to D7 declared as input,remaining as o/p
    PORTD = 0x00;//All output pins as low
    _delay_ms(100); // wait for 100ms
    LCD_WriteCommand (0x38); // 8 data lines
    LCD_WriteCommand (0x06); // cursor setting
    LCD_WriteCommand (0x0f); // display ON
    LCD_WriteCommand (0x01); // clear LCD memory
    _delay_ms (10); // 10ms delay after clearing LCD
    }
    int main(void)
    {
    init_devices();
            //Text to be displayed in first row
    LCD_DisplayString_F(1,1,"    ROBOWARES ");
            //Text to be displayed in second row
    LCD_DisplayString_F(2,1,"rickruling.blogspot.in");
    return 0;
    void LCD_WriteCommand (unsigned char Command)
    {
    SET_LCD_CMD; // Set LCD in command mode
    PORTC = Command; // Load data to port
    ENABLE_LCD; // Write data to LCD
    _delay_ms(1);
    DISABLE_LCD; // Disable LCD
    _delay_ms(1); // wait for 1ms
    }
    void LCD_WriteData (unsigned char Data)
    {
    SET_LCD_DATA; // Set LCD in data mode
    PORTC = Data; // Load data to port
    ENABLE_LCD; // Write data to LCD
    _delay_ms(1);
    DISABLE_LCD; // Disable LCD
    _delay_ms(1); // wait for 1ms
    }

    void LCD_DisplayString_F (char row, char column ,char *string)
    {
             /*Break the string into characters and send them for the display*/
    LCD_Cursor (row, column);
    while (*string)//Loop till the string is not null
    LCD_WriteData(*string++);
    }
    void LCD_Cursor (char row, char column)
    {
    switch (row)
    {
    case 1:     //Set the position in the first row
                     LCD_WriteCommand (0x80 + column - 1);
    break;
    case 2:     //Set the position in the second row
                     LCD_WriteCommand (0xC0 + column - 1);
    break;
    default: break;
    }
    }


    For more tutorial and information, like the Facebook Page 

    https://www.facebook.com/Robowares


    Interfacing Unipolar Stepper motor using ULN2803

    Unipolar Stepper Motor

    A unipolar stepper motor has one winding with center tap per phase. Each section of windings is switched on for each direction of magnetic field. Since in this arrangement a magnetic pole can be reversed without switching the direction of current, the commutation circuit can be made very simple (e.g., a single transistor) for each winding. Typically, given a phase, the center tap of each winding is made common: giving three leads per phase and six leads for a typical two phase motor. Often, these two phase commons are internally joined, so the motor has only five leads.

    Components Required

    • ATmega32 microcontroller
    • AVR programmer board
    • Crystal
    • Capacitors
    • Unipolar Stepper Motor
    • ULN2803
    • +5 V Supply
    • Battery Equivalent to the voltage rating of the motor
    • Breadboard
    • Connecting wires 

    Circuit Diagram



    Description

    • PC0 to PC3 are connected to the four inputs of the ULN2803 IC.
    • Both the enable pins are separately connected to Vcc.
    • A crystal is connected to the XTAL1 and XTAL2 pins to provide the clock pulse.
    • Reset is connected to +5 volt.

    Control Logic

    To control a unipolar stepper, you use a Darlington Transistor Array. The stepping sequence is as shown below. Wires 5 and 6 are wired to the supply voltage.


    Source Code

    /*The following code will make the stepper motor to run continously*/
    #include <avr/io.h>
    #include<util/delay.h>

    #ifndef F_CPU
    #define F_CPU 1000000UL
    #endif

    int main()
     { 
             DDRC = 0xFF;                        //All pins of PORT C as output
    PORTC = 0x00;                      //Initially all pins as output low
       while (1)                                         //infinite loop
       {
    PORTC = 0x03;                       //0011
    _delay_ms(100);
    PORTC = 0x06;                      //0110
    _delay_ms(100);
    PORTC = 0x0C;                      //1100
    _delay_ms(100);
    PORTC = 0x09;                       //1001
    _delay_ms(100);
       }
       return 0;
     }

    Source Code for controlled Rotation


    /* This code will make the stepper motor to run for a specific angle */ 

    #include <avr/io.h>
    #include <util/delay.h>

    #ifndef F_CPU
    #define F_CPU 1000000UL         // frequency of external crystal
    #endif

    int main()
     {
             /*Calculate x by the minimum step angle of your motor and required rotation angle*/
              int x=20;
              DDRC = 0xFF;                         //All pins of PORT C as output
     PORTC = 0x00;                        //Initially all pins as output low
       while (x)                                           //loop till x is not equal to zero
       {
    PORTC = 0x03;                         //0011
    _delay_ms(100);
    PORTC = 0x06;                         //0110
    _delay_ms(100);
    PORTC = 0x0C;                        //1100
    _delay_ms(100);
    PORTC = 0x09;                         //1001
    _delay_ms(100);
      x--;                                          //decrement x by 1
       }
       return 0;
     }


    For more tutorial and information, like the Facebook Page 

    Saturday, October 26, 2013

    Interfacing Bipolar Stepper motor using L293D


    Bipolar Stepper motor

    Bipolar motors have a single winding per phase. The current in a winding needs to be reversed in order to reverse a magnetic pole, so the driving circuit must be more complicated, typically with an H - bridge arrangement (however there are several off-the-shelf driver chips available to make this a simple affair). There are two leads per phase, none are common.Dithering the stepper signal at a higher frequency than the motor can respond to will reduce this "static friction" effect.
    Because windings are better utilized, they are more powerful than a unipolar motor of the same weight. This is due to the physical space occupied by the windings. A unipolar motor has twice the amount of wire in the same space, but only half used at any point in time, hence is 50% efficient (or approximately 70% of the torque output available). Though a bipolar stepper motor is more complicated to drive, the abundance of driver chips means this is much less difficult to achieve.


    Components Required

    • ATmega32 microcontroller
    • AVR programmer board
    • Crystal
    • Capacitors
    • Bipolar Stepper Motor
    • L293D
    • +5 V Supply
    • Battery Equivalent to the voltage rating of the motor
    • Breadboard
    • Connecting wires 

    Circuit Diagram


    For details on L293D IC go to 

    Description

    • PB0 to PB3 are connected to the four inputs of the L293D IC.
    • Both the enable pins are separately connected to Vcc.
    • A crystal is connected to the XTAL1 and XTAL2 pins to provide the clock pulse.
    • Reset is connected to +5 volt

    Control Logic

    To control the bipolar motor,apply voltage to each of the coils in a specific sequence. The sequence would go like this:


    Source Code

    /* This code will make the stepper motor to run continously */ 

    #include <avr/io.h>
    #include <util/delay.h>

    #ifndef F_CPU
    #define F_CPU 1000000UL         // frequency of external crystal
    #endif

    int main()
     { 
    DDRB = 0xFF;                   //All pins of PORTB as output
    PORTB = 0x00;                 //Initially all pins as output high
    while(1)
    {
    PORTB = 0x01;          //0001
    _delay_ms(10);
    PORTB = 0x04;          //0100
    _delay_ms(10);
    PORTB = 0x02;          //0010
    _delay_ms(10);
    PORTB = 0x08;          //1000
    _delay_ms(10);
    }
    return 0;
    }


    Source Code for controlled Rotation

    /* This code will make the stepper motor to run for a specific angle */ 

    #include <avr/io.h>
    #include <util/delay.h>

    #ifndef F_CPU
    #define F_CPU 1000000UL         // frequency of external crystal
    #endif

    int main()
     {
             /*Calculate x by the minimum step angle of your motor and required rotation angle*/
             int x = 200;
             DDRB = 0xFF;                   //All pins of PORTB as output
     PORTB = 0x00;                 //Initially all pins as output high
    while(x)                              //will loop till x is not equal to zero
    {
    PORTB = 0x01;          //0001
    _delay_ms(10);
    PORTB = 0x04;          //0100
    _delay_ms(10);
    PORTB = 0x02;          //0010
    _delay_ms(10);
    PORTB = 0x08;          //1000
    _delay_ms(10);
                    x--;                             //decrement x by 1
    }
    return 0;
    }

    For more tutorial and information, like the Facebook Page 


    Interfacing D.C. Motor with ATmega32 using Mosfets


    The main Drawback of interfacing D.C. motors using IC's like L293D is that the maximum current which it can give to the motor is limited to less than 1 Ampere. So, it is not suitable for running high torque motors which require higher amount of current. For that purpose we can create our own H - bridge circuit using mosfets.

    Components Required

    • ATmega32 microcontroller
    • AVR programmer board
    • Crystal
    • Capacitors
    • Motors
    • Mosfets (2 N-channel and 2 P-channel)
    • +5 V Supply
    • Battery Equivalent to the voltage rating of the motor
    • Breadboard
    • Connecting wires 

    Instead of  2 N-channel Mosfets and 2 - P channel Mosfets all the 4 Mosfets can be of same type i.e. all 4 can be N - type or all four can be of P-type, but you have to make corresponding changes in the source code.

    Control Logic

    • To make the motor move in the forward direction, +12 V and 0 V must be applied to the ends of the D.C. motor which will be controlled by controlling the Gates of the mosfet through the Source Code in the ATmega32.
    • The dark lines show the flow of current in the circuit.

    For forward rotation Gate of Q1 and Q3 are high and Q2 and Q4 are low.So Q1 will conduct because of positive current being a N - channel Mosfet and Q4 will conduct without a positive current being a P - channel Mosfet making the terminals +12 V and 0 V.


    For forward rotation Gate of Q1 and Q3 are low and Q2 and Q4 are high.So Q2 will conduct because of positive current being a N - channel Mosfet and Q3 will conduct without a positive current being a P - channel Mosfet making the terminals +12 V and 0 V.



    Circuit Diagram


    Description

    • PC0 to PC3 are connected to the four gates of the mosfet.
    • A crystal is connected to XTAL1 and XTAL2 pins to provide clock pulse.
    • Reset is connected to +5V.

    Source Code

    /* The following Source code will make the motor to run forward two seconds and then backward two seconds for infinite time*/

    #include <avr/io.h>
    #include <util/delay.h>

    #define F_CPU 1000000UL        //Frequency of the external crystal

    int main()
     { 
             DDRC = 0xFF;                   // PORTC as output port
    PORTC = 0x00;                  // Initially all pins of PORTC as output low
     
       while (1)                                     // Infinte Loop
       {
      PORTC = 0b00000101;     //forward rotation 
      _delay_ms(2000);
      PORTC = 0b00001010;     //backward rotation
      _delay_ms(2000);
       }
       return 0;
     }

    Circuit Diagram (if reverse rotation is not required)


    Description

    • PC0 to PC1 are connected to the two gates of the mosfet.
    • A crystal is connected to XTAL1 and XTAL2 pins to provide clock pulse.
    • Reset is connected to +5V.

    Source Code for Normal Run of D.C. Motor

    #include <avr/io.h>

    int main()
     { 
             DDRC = 0xFF;                              // PORTC as output port
       PORTC = 0b00000001;              //forward rotation 
                while (1);                                     // Infinte Loop
       return 0;
     }


    Source code for Speed Control Using PWM

    /***
    *In order to provide PWM the output of the gates must be fluctuating
    *In the on time of the duty cycle the required mosfets must be on
    * In the off time of the duty cycle all the mostets must be of which is done by applying logic high to P - mosfets and logic low to N - mosfets
    ***/
    #include <avr/io.h>
    #include <util/delay.h>

    #define F_CPU 1000000UL        //Frequency of the external crystal

    int main()
     { 
             DDRC = 0xFF;                   // PORTC as output port
    PORTC = 0x00;                  // Initially all pins of PORTC as output low
     
       while (1)
       {
      PORTC = 0b00000001;    //on time
      _delay_ms(1);
      PORTC = 0b00000010;   //off time
      _delay_ms(1);

       }
       return 0;
     }


    For more tutorial and information like the Facebook Page 

    Thank You for Reading


    Friday, October 25, 2013

    Speed Control of DC Motor using PWM

    Pulse-width modulation is an effective method for adjusting the speed and the amount of power delivered to an electrical load.

    Components Required




  • ATmega32 microcontroller
  • AVR Programmer Board
  • Crystal
  • Capacitors
  • Motors
  • L293D IC
  • +5V supply
  • Battery equivalent to voltage rating of motor 
  • Breadboard
  • Connecting Wires

  • To know about the L293D motor controller go through the previous blog on Interfacing D.C. Motor with ATmega32 using L293D.
    http://rickruling.blogspot.in/2013/10/interfacing-dc-motor-with-atmega32.html

    Circuit Diagram


    Description

    • PC0 and PC3 are connected to Enable 1 and Enable 2 respectively.
    • PC1,PC2,PC4 and PC5 are connected to Input 1-4.
    • OUT1 and OUT2 are connected to the terminals of first motor and OUT3 and OUT4 are connected to the terminals of the second motor.
    • A crystal is connected to XTAL1 and XTAL2 pins to provide the clock pulse.
    • Reset is connected to +5V.

    Source Code

    /*The following code will make the motor to run at 50% duty cycle*/
    #include <avr/io.h>
    #include <util/delay.h>

    #define F_CPU 1000000UL

    int main()
     { 
    DDRC = 0xFF;                                       //All pins of PORT C as output 
    PORTC = 0x00;                                     //Initially all pins at low output
    PORTC|=(1<<PC1)|(1<<PC4);            //PORT C 1 and 4 as high
    PORTC&=~((1<<PC2)|(1<<PC5));     //PORT C 2 and 5 as low
      /*In order to make the motor to rotate in opposite direction interchange the terminals of the motors (PINC1 and PINC2 for motor 1 and PINC4 and PINC5 for motor 2)*/
       while (1)                                                 //Infinite Loop
       {
          /*ON TIME*/
      PORTC|=(1<<PC0)|(1<<PC3);
          _delay_us(5);
          /*OFF TIME*/
          PORTC&=~((1<<PC0)|(1<<PC3));
          _delay_us(5);
               
       }
       return 0;
    }


    duty cycle = (on_time)/(on_time+off_time)
    duty cycle = 5/10 = 0.5
    To increase the duty cycle increase the delay of on_time and to decrease the duty cycle increase the delay of off_time.

    Practical Limitations of Pulse Width Modulation Motor Control

    Pulse-width modulation is difficult below 25% for motors because they don’t gain the same rotational inertia in comparison to the static resistances of the grease, gearing, and gaps between commutators. A weird aspect of PWM on motors is that it can create audible whining. Basically, if you select a PWM frequency in a human-audible range, the mechanical device will likely oscillate audibly. Increasing the frequency above 20 kHz may silence the motor whining. But, some motors, transistors, or motor driver chips are unable to switch on and off that quickly.


    For more tutorial and information like the Facebook Page 

    Thank You for Reading

    Thursday, October 24, 2013

    Interfacing D.C. Motor with ATmega32 using L293D

    Components Required

    • ATmega32 microcontroller
    • AVR Programmer Board
    • Crystal
    • Capacitors
    • Motors
    • L293D IC
    • +5V supply
    • Battery equivalent to voltage rating of motor 
    • Breadboard
    • Connecting Wires

    Motor Controller


    L293D - Dual D.C. Motor Controller
    • Enable-1 controls the output of output1 and output2.
    • Enable-2 controls the output of output3 and output4.
    • Vss is connected to +5V.
    • Vs is connected to a voltage equal to the voltage rating of the motor.
    • Enable 1-2 and Input 1-4 comes from the microcontroller.
    • If speed control using PWM is not needed, the Enable Pins can be directly connected to +5V.
    • All 4 grounds are shorted to a common ground.
    • Output 1-4 goes to the motor.

    Circuit Diagram


    Description

    • PC0 and PC3 are connected to Enable 1 and Enable 2 respectively.
    • PC1,PC2,PC4 and PC5 are connected to Input 1-4.
    • OUT1 and OUT2 are connected to the terminals of first motor and OUT3 and OUT4 are connected to the terminals of the second motor.
    • A crystal is connected to XTAL1 and XTAL2 pins to provide the clock pulse.
    • Reset is connected to +5V.

    Source Code

    /*The following code will make the motor to run forward for 2 seconds 
    and then backward for 2 seconds for infinite time*/
    #include <avr/io.h>
    #include <util/delay.h>

    #define F_CPU 1000000UL

    int main()
     { 
    DDRC = 0xFF;                                  // PORTC as output port
    PORTC = 0x00;                                //Initially all pins as output low
    PORTC|=(1<<PC0)|(1<<PC3);        //C0 and C3 are connected to enable
             while (1)                                            // Infinite Loop
             {
                       /*Forward Motion*/
      PORTC|=(1<<PC1)|(1<<PC4);              //#1
               PORTC&=~((1<<PC2)|(1<<PC5));       //#2
                       _delay_ms(2000);                                    //#3
                      /*Backward Motion*/
          PORTC&=~((1<<PC1)|(1<<PC4));       //#4
               PORTC|=(1<<PC2)|(1<<PC5);              //#5
                _delay_ms(2000);                                   //#6
             }
             return 0;
      }

       # 1 .  PORTC|=(1<<PC1)|(1<<PC4);  
                1<<PC1 will generate a character 00000010 and 1<<PC4 will genarate a character 00010000.
                Taking OR of this values will generate 00010010. Now OR of this value and the initial condition of               PORTC will make the Bits 1 and 4 as high independent of all other pins of  PORTC.

        # 2 .  PORTC&=~((1<<PC2)|(1<<PC5));  
                1<<PC2 will generate a character 00000100 and 1<<PC5 will genarate a character 00100000.
                Taking OR of this values will generate 00100100 and NOT of this value will generate 11011011.                 Now AND of this value and the initial condition of PORTC will make the Bits 2 and 5 as low                       independent of all other pins of  PORTC.

        # 3. _delay_ms(2000);  
                A delay of 2 seconds after which the motor will change its direction.

        # 4 . PORTC&=~((1<<PC1)|(1<<PC4));  
                1<<PC1 will generate a character 00000010 and 1<<PC4 will genarate a character 00010000.
                Taking OR of this values will generate 00010010 and NOT of this value will generate 11101101.                 Now AND of this value and the initial condition of PORTC will make the Bits 1 and 4 as low                       independent of all other pins of  PORTC.

        # 5 .  PORTC|=(1<<PC2)|(1<<PC5);  
                1<<PC2 will generate a character 00000100 and 1<<PC5 will genarate a character 00100000.
                Taking OR of this values will generate 00100100. Now OR of this value and the initial condition of               PORTC will make the Bits 2 and 5 as high independent of all other pins of  PORTC.

        # 6. _delay_ms(2000);  
                A delay of 2 seconds after which the motor will change its direction.

    Source Code for Normal Run of DC motor

    #include <avr/io.h>

    #define F_CPU 1000000UL

    int main()
     { 
    DDRC = 0xFF;
    PORTC = 0x00;
    PORTC|=(1<<PC0)|(1<<PC3);               //C0 and C3 are connected to enable
             PORTC|=(1<<PC1)|(1<<PC4);               //C1 and C4 as output high
    PORTC&=~((1<<PC2)|(1<<PC5));        //C2 and C5 as output low
             while(1)                                                    // Infinite Loop
               ;
       return 0;
    }

    ThankYou for Reading.