DOC PREVIEW
SJSU ME 106 - ATmega Programming Logic/Port Control Example Guide

This preview shows page 1 out of 2 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

ME106 Fundamentals of MechatronicsATmega Programming Logic/Port Control Example GuidebyM. RatnerUpdated 09FEB2012Hex Values:0xFF = all bits set (turned on/high/1/5V) __________________________binary equivalent is 0b11111111 0x00 = all bits cleared (turned off/low/0/0V) ________________________binary equivalent is 0b000000000xF0 = bits 0-3 are cleared (0) and bits 4-7 are set (1)_________________binary equivalent is 0b111100000x88 = bits 3 and 7 set (1) the rest are cleared (0) ____________________binary equivalent is 0b10001000Look up Wikipedia: hexadecimal for an entire list of possible hex values.ATmega pin number equivalents example: Number 7 6 5 4 3 2 1 0 – Pin numbers on the port (and bit numbers)0x0F = = 0b00001111 0 0 0 0 1 1 1 1 – set to high and low with hex/binary valuesSetting 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 maskwith a 1 in bit number 2 location. The corresponding bit mask is easily built by the command: _BV(PA2)Setting the port’s 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.)DDRX = 0xFF; // 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, LED’s, speakers, etc., pretty much anything you would want the microcontroller to control (keep in mind that the ATmega cannot source much current, so you will probably need to use a transistorbetween 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 1ME106 Fundamentals of Mechatronicson/high/1/5V, which would be the same as writing: PORTA = 0b11111111; Keep inmind that the right-most bit corresponds to pin 0 on PORTA in this example.PORTX = 0x00; // 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;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 bitvalues 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 bitvalues 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;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 disturbingbits 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 1 ADCSRA |= 0x80; Equivalently, you could have written: ADCSRA |= _BV(ADEN); // (ADEN is macro-defined to be


View Full Document

SJSU ME 106 - ATmega Programming Logic/Port Control Example Guide

Download ATmega Programming Logic/Port Control Example Guide
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view ATmega Programming Logic/Port Control Example Guide and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view ATmega Programming Logic/Port Control Example Guide 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?