0% found this document useful (0 votes)
64 views3 pages

Atmega Programming Logic/Port Control Example Guide

This document provides an overview of programming logic and port control for the ATmega microcontroller. It explains hexadecimal and binary number representations for setting and clearing bit values. It describes how to set the data direction register to configure ports as inputs or outputs and how to initialize port values. The document also demonstrates different bitwise logic operations like OR, AND, and XOR that can be used to set, clear, or toggle individual pin values without affecting others. An example is given to turn on the analog-to-digital converter by setting a specific bit in the ADCSRA register.

Uploaded by

9971553633
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views3 pages

Atmega Programming Logic/Port Control Example Guide

This document provides an overview of programming logic and port control for the ATmega microcontroller. It explains hexadecimal and binary number representations for setting and clearing bit values. It describes how to set the data direction register to configure ports as inputs or outputs and how to initialize port values. The document also demonstrates different bitwise logic operations like OR, AND, and XOR that can be used to set, clear, or toggle individual pin values without affecting others. An example is given to turn on the analog-to-digital converter by setting a specific bit in the ADCSRA register.

Uploaded by

9971553633
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

ME106 Fundamentals of Mechatronics ATmega Programming Logic/Port Control Example Guide by M.

Ratner Updated 09FEB2012 Hex Values: 0xFF = all bits set (turned on/high/1/5V) __________________binary equivalent 0b11111111 0x00 = all bits cleared (turned off/low/0/0V)_______________binary equivalent 0b00000000 0xF0 = bits 0-3 are cleared (0) and bits 4-7 are set (1)_____binary equivalent 0b11110000 0x88 = bits 3 and 7 set (1) the rest are cleared (0)_________binary equivalent 0b10001000 Look up Wikipedia: hexadecimal for an entire list of possible hex values. ATmega pin number equivalents example: Number 0x0F = = 0b00001111 Setting Bit Values: Many times you would like to set the voltage high for only a single pin of a port, and to do that you create what is called a bit mask, which is a binary number that has single 1-bit in the location corresponding to the pin you want to set, and all the rest of the bits are zero. You then use this number with a bitwise logical operator to finish the task. To create such a number, you would use the command: _BV(PXn) // Where X is the port letter, and n is the pin number. For example, suppose you would like to make pin 2 on PORTA to be an output. You therefore need a bit mask with a 1 in bit number 2 location. The corresponding bit mask is easily built by the command: _BV(PA2) Setting the ports data direction (input or output) on an ATmega: DDRX = 0x00; // Clears all the bits in the PORTx register, which makes all the associated pins to be inputs (keep in mind there are only ports A, B, C, and D on the ATmega328), so you can read various signals from things such as switches, sensors, or for analog to digital conversion, etc. (by default all pins are set to input, but you should always set them to make sure anyway.) // Sets all the bits in the PORTx register, which makes all the associated pins to be outputs (keep in mind there are only ports A,B,C, and D on the ATmega328), so you can control devices, such as motors, LEDs, speakers, etc., pretty much anything you would want the microcontroller to control (keep in mind that 7 6 5 4 3 2 1 0 Pin numbers on the port (and bit numbers) 0 0 0 0 1 1 1 1 set to high and low with hex/binary values is is is is

DDRX = 0xFF;

ME106 Fundamentals of Mechatronics the ATmega cannot source much current, so you will probably need to use a transistor between the microcontroller and whatever you are trying to control if it needs more than 10 mA of current). Initializing Port Values: PORTX = 0xFF; // Clears all the bits in the PORTx register, and assuming that the pins are outputs, will make all the pins in PORTx go high (as mentioned in the section on Hex values above). So for example, PORTA = 0xFF; would set all pins in port A to on/high/1/5V, which would be the same as writing: PORTA = 0b11111111; Keep in mind that the right-most bit corresponds to pin 0 on PORTA in this example. // Clears all bits in the PORTx register and makes all pins in PORTx go low (as mentioned in the section on Hex values above). So for example, PORTB = 0x00; would clear all bits in the PORTB register and make all pins in PORTB go off/low/0/0V. This would be the same as writing: PORTB = 0b00000000;

PORTX = 0x00;

Bitwise Logic: PORTX |= 0xF0; // Method for setting bits. Performs a bitwise OR operation with the current bit values of PORTX and the bit mask represented by the binary number, 0xF0. The result is that only pins 4-7 in PORTX are set high, and the other pins are not affected. Equivalent in long hand would be: PORTX = PORTX | 0xF0; PORTX &= ~0x01; // Method for clearing bits. Performs a bitwise AND operation with the current bit values of PORTX and the bit mask represented by the binary number, ~(0b00000001) or 0b11111110. The result is that pin 0 is set low (referred to as cleared), regardless of what was there before. In this example, only pin 0 is cleared, and the state of the other pins are not affected. Equivalent in long hand would be PORTX = PORTX & ~0x01; PORTX ^= 0x02; // Method for toggling bits. Performs a bitwise XOR operation with the current bit values of PORTX and the bit mask represented by the binary number, 0x02. The result is that pin 1 is toggled between the off and on states. Think of this operation like a light switch. Each time the statement is executed, it changes the state to be the opposite of what it was previously. Similar to the prior statements, this method of toggling only affects the bits in the locations where there are ones in the bit mask hex value. Long hand would be PORTX = PORTX ^ 0x02;

ME106 Fundamentals of Mechatronics The methods shown above are what you need to do to set up and control all the functions of the microcontroller. For example, suppose you wanted to turn on the ADC (analog to digital converter). To do so, you would set bit 7 in the ADCSRA register. See if you can remember how to do this (without disturbing bits 6 0) from the description above1. For the entire list of port names, pin numbers, and their functions, look at the ATmega datasheet for the microcontroller you are interested in. Visit the Atmel website to obtain the data sheets: http://www.atmel.com/dyn/products/datasheets.asp?family_id=607

ADCSRA |= 0x80; // (ADEN is macro-

Equivalently, you could have written: ADCSRA |= _BV(ADEN); defined to be 7)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy