NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Registers?????

February 24, 2011
by dgikuljot
dgikuljot's Avatar

Hey guys, I have been reading through the nerdkits guide to . I am totally lost on how registers work, and their use. I looked through the data sheet and the register names are scattered throughout the data sheet. Is there any place where all the register names are listed in one place of the avr 168. Also can someone also please explain or point out a link, to Registers. Thanks, Kuljot

February 24, 2011
by Rick_S
Rick_S's Avatar

Look at the end of the datasheet. You'll find a full register summary there.

Rick

February 24, 2011
by dgikuljot
dgikuljot's Avatar

Ok, thanks Rick for the summar info. Can some still explain about the registers please.

February 24, 2011
by Ralphxyz
Ralphxyz's Avatar

Knowing very little and hoping a more knowledgeable sole than I would contribute here is the little I know.

You can use the registers to store values, most of the registers are dedicated to mcu functions Timers for example.

I know there are three GPIOR (General Purpose I/O Registers), page 25 in the specsheet has a table but just search for GPIO.

One advantage to using registers to hold your values is that you do not use up program memory space. I am currently trying to understand registers also.

I understand the concept that a timer woud need to have a place to hold it's count, but I am not sure of the whole picture.

Ralph

February 24, 2011
by bretm
bretm's Avatar

That's a pretty big question. The reason that registers are scattered all throughout the datasheet is that registers control pretty much everything that the microcontroller does.

The microcontroller has memory. Each byte of memory has an address. Most of the read/write memory is just for data storage, but certain memory locations at low-numbered addresses have their bits hard-wired to perform specific functions. These special hard-wired addresses are called registers and they have names associated with them.

For example, the byte of memory at address 0x240 is just a boring regular old byte that you can write data to or read data from. The microcontroller doesn't care what you do with those bits. But the byte of memory at address 0x7A is hard-wired to control parts of the analog-to-digital converter (or, more likely, the address is hard-wired to the ADC instead of the regular memory bank). It has the name "ADCSRA" associated with it, which means "Analog to Digital Controller Status Register A". When you write data to that memory location it causes the microcontroller to do something with the analog-to-digital converter. For example if you set bit #6 (called ADSC) to "1", it starts an analog-to-digital conversion.

Each of the hardware subsystems of the microcontroller typically has several registers associated with it.

February 24, 2011
by dgikuljot
dgikuljot's Avatar

Ok Bretm and Ralph XYZ, Thanks for your guys's replies. So basically what are the registers for turning a microcontroller pin on and off. Also is it possible to set what voltage the pin should output. I think these to lines have something to do with 2 different registers, but can someone please explain in detail what exactly these lines do.

 DDRC |= (1<<PC4)
 PORTC |= (1<<PC4)
I know these turn on and off a pin on the microcontroller, but how?

Thanks, kuljot Dhami

February 25, 2011
by Rick_S
Rick_S's Avatar

These set the 4th bits in the Data Direction Register C (DDRC) and PORTC without affecting the other bits.

For example if the value stored in DDRC was 0b11000011 the statement DDRC |= (1<<PC4) would make it 0b11010011. Remember there are 8 bits in a byte zero based. So they are numbered 0 thru 7. PC4 is defined in one of the includes as 4 so we are shiting (<<) the number 1 four times.

Start with 1

0b00000001

Shift once

0b00000010

Twice

0b00000100

Three times

0b00001000

Four times

0b00010000

By using or (|) we leave the existing bits alone and only deal with the bit we choose to.

Rick

February 25, 2011
by bretm
bretm's Avatar

The output voltage is always zero or the supply voltage. The atmega168 doesn't have any analog output pins. Set the data direction register bits to 1 for output, and the PORT register bits to 0 or 1 to set a pin to GND or VCC.

February 25, 2011
by dgikuljot
dgikuljot's Avatar

Ok thanks Rick, So basically if i understand it correctly, setting ab it on the register switches that pin on and off. IF i wanted to go to a different pin what would i change. Thanks, Kuljot Dhami

February 25, 2011
by dgikuljot
dgikuljot's Avatar

Oops big typo, Ok thanks Rick, So basically if i understand it correctly, setting a bit on a register switches that pin on and off. IF i wanted to go to a different pin what would i change. Thanks, Kuljot Dhami

February 25, 2011
by dgikuljot
dgikuljot's Avatar

So basically the way we choose which pin to use and wheather to turn it on or off is with registers? Thanks, Kuljot Dhami

February 27, 2011
by bretm
bretm's Avatar

Page 62 of the NerdKits Guide.

March 01, 2011
by BStory
BStory's Avatar

To start learning pin functions I printed page 88 and 89 from the ATmega168 datasheet and pinned it to the wall. I still look at it to figure out what registers I need to work with. I also have the pinout diagram (p. 17 of the NK guide/p. 2 of the datasheet)pinned on the wall that I use to figure out what pins I'm using. I just about have it memorized and I haven't been trying to do that. I pretty much pin everything I need to reference with to the wall.

So as someone just starting out, I so far have found that to be very helpful and I would recommend getting some push pins and a cork board. Cork boards tend to get pretty spendy so instead of a corkboard I just bought some painters calk. That way I can patch the pin holes if I decide to sell my house :D

March 01, 2011
by dgikuljot
dgikuljot's Avatar

Hey Bstory, Thanks for pointing out the helpful tip and giving me the page references. Everyone thanks for your help. If anyone has more info then post it here, so it can help all the other beginners, as i am sure that most necomers get stuck on registers.

Post a Reply

Please log in to post a reply.

Did you know that Pulse Width Modulation (PWM) can be used to control the speed of a motor digitally? Learn more...