Tuesday 16 December 2014

Disabling JTAG for Atmega16

JTAG stands for Joint Test Action Group. Atmega16 has one JTAG port which shares four pins with PORTC. To use these pins one must disable JTAG port.
There are two methods to disable the JTAG port:

  1. To do it temporarily.If you use this method you need to write this in your code every time.
    Include the following lines in your code
    MCUCSR = (1<<JTD); MCUCSR = (1<<JTD);  Add these lines just after main() fuction.
  2. To do it permanently, you need to change the hfuse and lfuse of your atmega16. Open avrdude-gui and select the target device as atmega16, programmer as usbasp and  go to fuse bits first select lfuse and write 11111111 and program

 Next select hfuse and write 10001001 and program

Tuesday 25 November 2014

Polling

While working with any microcontroller, we generally interface multiple devices. Also, the microcontroller need to communicate with these devices regularly for input/output operations.

Now the question arises as to which device to communicate to? The solution to this lies in Polling.

Polling refers to checking the status of a device(s) continuously in a repeated manner. In other words, a the microcontroller performs certain operations repeatedly to check for any change in the status of the device.

Simple use of polling can be seen in this program:

Disadvantages of Polling:
  • Polling causes a wastage of energy and resource. It causes the microcontroller to perform several operations continuously, even when the system is idle.
However, polling can be useful while writing small programs or when a large number of devices need to be interfaced.

Masking

On the atmega16, to read the input from a port x, we use the PINx command. Now this command reads the input from the entire port.

Now consider a situation where you have connected 8 switches - one to each pin of PORT C.
However, now you want to only read the switches connected to PC3 and PC0. This is where masking comes into picture.

Masking, in simple words is nothing but hiding the information that is not required. In the situation we considered above, we do not require the input from all pins except PC3 and PC0. Therefore, we need to mask these pins.

A simple way to achieve this is to use the AND Mask.

AND Mask:

While using the AND masking technique, we perform a bitwise AND operation on the input we recieve from PINx command. We AND the input bit with 0 if the bit needs to be masked, else we AND it with 1.
Considering the above example, since we require the input only from PC3 and PC0, we mask all other bits by ANDing them with 0. We AND PC3 and PC0 with 1. Hence, the output that we get will contain all bits as 0 except PC3 and PC0, where these two bits will have the values read from the PINC command.


Masking techniques can not only be used while taking inputs, but also while reading bits from certain registers and providing output as well.

Use of masking technique can be seen here:
http://avrlogic.blogspot.in/2014/11/switch-interfacing.html