Wednesday, February 10, 2016

Disabling JTAG in avr microcontroller



In this post I am going to explain how to disable JTAG. The JTAG stands for Joint Test Action Group(JTAG), Which is an international standard method for testing the microcontroller performance and delay calculations. Therefore, most of the  microcontrollers are having this facility to test and debug by the programmer. This is done by four signals namely TDI (Test Data Input), TDO (Test Data Output), TMS (Test Mode Select) and TCK (Test Clock).  It is important to know how to disable JTAG because the JTAG is enabled while the microcontroller shipping which makes the port pins occupied by the JTAG signal do not work properly. For  example, in ATmega16 microcontroller, in which the JTAG pins are occupied with PORTC. Which are  PC5, PC6, PC7 and PC8. So you cannot use this pins for any port operation. You can disable JTAG using Fuse pins but it is not a good practice. The best way is to write a “1” to JTD bit of MCUCSR register of you AVR microcontroller as shown below code.



MCUCSR = (1<<JTD); // Disables JTAG pins

MCUCSR = (1<<JTD); // Disables JTAG pins

 Sometimes you have to write this code twice for proper working

Tuesday, February 9, 2016

INTERFACING PIR SENSOR WITH AVR MICROCONTROLLERS



Sometimes we have to interface a variety of sensors with microcontroller for monitoring different environmental conditions such as temperature, humidity, light intensity , motion, color etc. most of the sensors are making its output pin HIGH or LOW when the desired value is met or detected. For example a motion sensor makes its output pin HIGH when a motion is detected in its covering area and otherwise its pin is pulled LOW.  So this kind of sensors is very easy to interface with microcontroller because only we have to check the sensors output pin status. Following examples shows how to interface different sensors with AVR microcontroller. 



1)      Interfacing PIR sensor with AVR


A PIR sensor is used to detect the motion of living things around its covering area.   Normaly it has three pins namely Vcc, GND and OUT. When the sensor detects a motion, it makes its OUT pin HIGH. So we can simply check the OUT pin status to know whether the motion is detected or not. The following points and program illustrates how to interface a PIR sensor with AVR microcontroller

·         At first point we have to decide what  we have to when a motion is detected by the motion sensor
·         Here we are going to detect the motion sensor output and when it detects we glow a LED
·         I am decided to interface the motion sensor OUT pin with PA0 of the PORTA of AVR. So make PA0 as input pin. We use DDRA register to set the direction of PA0 as input. For that any of the following code can be used

DDRA=0x00; //makes all the PORTA pins as input

DDRA=0xFE; //makes PA0 input and all other pins are output

DDRA |=(0<<PA0); // this code helps us to make PA0 of PORTA as input without altering the   
                                    value of other bits in DDRA register

·         Next we have to choose a pin in PORTB to connect the LED which will glow when the senor makes a HIGH output at its OUT pin. I decided PB0 of PORTB to connect with LED. So we have to make this pin as out pin in PORTB. The following are methods to do so

DDRB=0xFF; //makes all the PORTB pins as output

DDRB=0x01; //makes PB0 output and all other pins are input

DDRB |=(1<<PB0); // this code helps us to make PB0 of PORTB as output without altering the   
                                    Values of other bits in DDRB register

·         Next we have to monitor the PA0 pin of PORTA whether it is set or not. For that we have to check the PINA register. For this case we don’t want to check all value of PINA register because we need only PA0 of the PINA register is set on not. For doing that we can use the following code.


If (PIN_is_set (PINA, PA0))
{

}
                          This code checks the PA0 of the PINA register is set (HIGH) or not. When it is high the code in the brackets will run. So we are using IF condition to monitor the PA0 pin of PORTA.

·          Next step we have to glow the LED when the PA0 detects a HIGH in its pin. So LED is connected to the PB0 pin of the PORTB.



If (PIN_is_set (PINA, PA0))
{

PORTB=0x01; // makes PB0 high which glows LED and all other pins LOW
Or
PORTB|=(PB0<<1); // this makes PB0 high without effecting the other pins of PORTB
                }



The complete code and diagram of PIR sensor interfacing  is as given below.



The complete Code



#include <avr/io.h>

int main(void)
{
   
    DDRA|=(0<<PA0); //making PA0 as input pin
    DDRB|=(1<<PB0); //making PB0 as output to connect LED
 
  PORTB|=(0<<PB0); // LED is off by sending zero to PB0
 
 
 
    while(1) //infinite loop
    {
        if(bit_is_set(PINA,PA0)) //checking  whether PA0 is set
        {
            PORTB|=(1<<PB0); //switch ON LED
        }           
    }
}























Introduction

             In this tutorial I am going to give an introduction of how to program an AVR microcontroller input/output pins. AVR is a seris of microcontrollers manufactured by ATMEL corporation. Before learning how to program an AVR, the learner must know the organization of the controller. In this first part I am going to explain the port of the AVR mocrocontroller. We know , every microcontrollers have ports to communicate with outside word. A port may be used to provide a data to the outside devices or may be used to take data in to the controller.The first case we can say that the port if an OUTPUT port and later case it is an INPUT port. So in simple word a port can be OUTPUT or INPUT. If a port is output, the data can only be travel form the controller to the outside device. If a port is input, it can only take a data from outside device in to the controller. Every microcontrollers have to communicate with outside world. So in order to communicate, it need port. The following are the typical ports which are usually find in a microcontroller.

  • The parallel Port
  • The serial port


1)The parallel port 


           In the parallel communication, the data bits are transmitted at a time using number of lines corresponding to the data bits. That is if we are using 8bit data , we need 8 lines or 8 pins in order to transmit 8bits at a time.  

AVR Parallel PORT 


            In AVR, there are parallel port for communication. A port can only be INPUT or OUTPUT at a time. The parallel port in avr is programmable. It means that the user can be configure the port as input or output. A port can be 8bit,16bit or 32bit according to the series of the controller. Here we are taking an 8bit port usually found in most AVR microcontrollers. The following points are to noted first 

* A parallel port in AVR is a collection of two or more registers
* Each of these registers can be used to set the port behavior of the port
*The following are the registers associated with parallel port in AVR

  1.         DDR REGISTER 
  2.         PORT REGISTER 
  3.         PIN REGISTER
            So PORT, DDR and PIN are the registers helping the port pins to be configured. Normally the AVR series controllers have more than one ports. In that case each port is named with alphabets starting from A. That is the name of the port will be PORTA, PORTB, PORTC and so on.  So the corresponding port registers also be named as PORTA, PORTB, PORTC. Also DDRA, DDRB, DDRC and PINA, PINB,PINC so on. Now on wards we use x instead of port name. x can be any alphabet name of port. PORTx,DDRx and PINx.

 What is DDRx Register ?


   As already detailed above, the port pins of avr can  be input or output at a time. So before doing any programming, let the controller know whether the corresponding PORT is inputting or outputting the data. In order to tell the microcontroller , we use DDR registers. Wich is Data Direction Register which sets the direction of the PORT pins.

   For example, we are considering PORTA of AVR microcontroller, as shown in the figure 1, it has 8 pins on IC named PA0 to PA7. These 8 bits are used for 8bit parallel communication. As mentioned earlier, each port in AVR is a combination of three registers. For PORTA, it will be PORTA, DDRA and PINA registers. All of these registers are 8 bit because the port bits are 8 bit in IC.  At first stage we have to mention or program wheather the port pins are inputting or outputting the data. For that we are using DDRA register. If we load a one or set a bit in DDR register, the corresponding port bit will act as OUTPUT port line. The DDRA  register is shown in below figure, it has 8bits named PA0 through PA7. Each of the bit is associated with PA0 through PA7 physical port pins of PORTA. So writing a bit “1” in DDRA register makes the corresponding PORTA bit in the physical output to be set as OUTPUT. And if we write a “0” in DDRA register makes corresponding bits in port as input.






Making all pins of PORTA as OUTPUT






  For making all PORTA pins as OUTPUT, only we have to do to set all bits in DDRA register by writing the code as below.

DDRA=0xFF; //hex value
DDRA=0b’11111111; //binary value

 You can use this code in AVR GCC . First one is the hex value which is indicated by 0x at the staring and second is the binary value which is indicated 0b’ at stating. So the whole PORTA is OUTPUTTING the data.

Splitting PORTA into two halves and make one half INPUT and other as OUTPUT

 








  For making half input and half output we use the following code

DDRA=0xF0; //hex value
DDRA=0b’11110000; //binary value

 So after writing this, the lower half(4 bits) of the PORTA is an INPUT port and upper half(4 bits) is an output is output port. 


Making individual pins of PORTA as OUTPUT or INPUT

 




DDRA=0x56; //hex value
DDRA=0b’10011010; //binary value


 By writing this code you make PA6,PA5,PA2and PA0 as INPUT port bits and all others are OUTPUT port bits. So you can also individually program the pins of a port either INPUT or OUTPUT.




What is PORT register ?





   The data written in to the PORT register will be transmitted though the PORT pins to the outside world. Considering the PORTA register, if we write a value to this register, it will be available at physical PORTA pins of the IC only if you made this port as output 
in DDRA register. The code as follows 

DDRA=0xFF; // making PORTA as OUTPUT

PORTA=0x05; // outputting hex data 05 through PORTA 








What is PIN register?

 






 PIN register is also an 8 bit register, which stores the data incoming through the corresponding PORT of AVR, only if the PORT is configured as INPUT in DDR resister.
Considering PORTA, if you want to read an incoming data from PORTA, the first we have to declare a character or integer. The following is the code

DDRA=0x00; //making PORTA as INPUT

Unsigned char Value; // declaring a char to store the PIN register Value

Value = PINA; //the incoming data through PORTA is in the PINA register is Transferred to Value