Monday, January 2, 2012

§Embedded ‘C’ Programming Notes – Part2

Read First:

     
9. Current Sinking and Current Sourcing: we can configure MCU as current sinking as well as current sinking, see the figure, we can connect LED to MCU in two ways.
 
Current sourcing: Vcc supplied by MCU, MCU provides current to drive LED.
Current sinking: MCU sinking the current.

Note: The maximum current sourcing rating of AVR MCU is about 250ma, if our circuit needs more then 250ma then we must configure MCU as current sink.

10. Inputs to microcontroller: MCU works on TTL logic if the output of the corresponding circuit is in TTL level then we can directly connect the output of the circuit with an input pin of our controller. If the circuit has output which is not in TTL level then we need to adjust the level of hardware for ex. Using optocouplers, voltage divider, Level converter etc.

At 5 volt : 0 to 1v = LOW  and  3.5v to 5v = High


11. Reading Input from Registers:

input = PINX;  //Copies the status of the input pins from port X

EX: Let’s take input at PB0 and output at PA0.
TASK: LED should glow when input=1.
 
Code :
int main()
 {
   DDRA=(1 << PA0);//PA0 is output pin
   DDRB&=~(1 << PB0);//PB0 is input pin

  PORTA=0; //initially LED is off 
 while(1)
 {
   uint8_t input;
   input=PINB;

  if(input)
      PORTA=(1 << PA0);
  else
       PORTA &=~(1 << PA0);
    }
}
This code is correct and has no error but the output will not be seen as we want, LED will fluctuate continuously. Because we leave input pin open. When we will not give any input  then it will take random value from environment. Means this input pin is floating.

When we configure any pin as input, it does not drive the port pin and the port pin is said to be floating. A floating pin can be taken to any voltage level by even a weak signal or EMI.

In order to take an input or connect other floating components (like - switch) a pull up is required to make the clear logic level on the pin.

 
Active Low configuration (PULL UP): Used to drag the level at open pin on logic ‘1’(High).
Active High configuration (PULL DOWN): Used to drag the level at open pin on logic ‘0’(Low).

The value of pull-up resistance should be between 4k ohm to 10k ohm.10k ohm is common value for this.AVR have software switchable internal pull-Up resistors, whih can be used instead of external components.

To activate internal pull-up we have to write PORTB=(1<<PB0);

The correct way to take Input to Microcontroller:
int main()
 {
 DDRA=(1 << PA0);//PA0  is input
 DDRB &=~(1 << PB0); //PB0 is output

 PORTA=0; //initially LED is off 
 PORTB=(1 << PB0); //Enable Internal Pull-Up
 while(1)
 {
   char input;
   input=PINB;

  if(!input)
      PORTA=(1<<PA0);
  else
       PORTA&=~(1<<PA0);
    }
}

12. Input with Masking the Bits: In the last code we took separate ports for input and output but if our controller pins is limited then we have to adjust input and outputs in same ports or if we take multiple inputs in the same port then if we check the status of one input pin then the other input pin status will interfere with this.

We can query the status of the input bits by reading the entire contents of the register and hide the bits which state is not of interest. Hiding unwanted bits is known as masking.

Let’s take an example:
 
PB1 and PB2 is input pins and PB0 is output pin.
TASK: LED should glow by pressing S1 and goes off by pressing S2

Code :
int main()
 {
  DDRB=(1<<PB0);// PB0 is output rest is input
  PORTB=(1<<PB1)|(1<<PB2); //PULL Ups
  PORTB&=~(1<<PB0);//initially LED is off

 uint8_t input;
 while(1)
 {
  input = (PINB & ((1<<PB1)|(1<<PB2)) ); // masking of input pins  
  //or input=(PINB & 0x06);

 switch(input)
 {
  case 0x04:  PORTB&=~(1<<PB0); //when S1 is pressed LED will glow
  case 0x02 : PORTB|=(1<<PB0);  //when S2 is pressed LED will goes off
    }
  }
}
  
13. Read a bit: The AVR library (avr libc) gives two functions to the inquiry of an individual bit of a register.
This function examines whether a bit is set, if the bit is set, a non-zero value is returned.

 
This function examines whether a bit is reset, if the bit is reset, a non-zero value is returned.

Ex: if(bit_is_set(PINB,0))                           Ex: if(bit_is_clear(PINB,0))
      {                                                                             {
         Do this                                                                Do this
       }                                                                            }


14. Waiting for a specific state:


Loop_until_bit_is_set(PINB,0)
OR
while( ! (PINB & (1 << PB0) ) );


Loop_until_bit_is_clear(PINB,0)
OR
while( PINB & (1 << PB0) );
                    
                                                                                                                 ~Pratyush Gehlot

No comments:

Post a Comment

PID Controlled Self balancing Robot using ESP8266 and MPU6050

This ROBOT is made using ESP8266 Node MCU and MPU6050 accelerometer. Motor used here is normal gear motor of 150 rpm with TB6612FNG motor dr...