how to write a he value to a register address in c

Hi,

In C, how would you write a hex value to a particular register.

For example if I wanted to write the value 0x12 to the register address of a 0x26 of a particular chip.

Thank you

Hard to answer without particulars. I2C, SPI, serial, parallel data bus, USB, etc.

arbitraryRegisterWrite (particularChip, 0x26, 0x12);

(This code needs a little padding)

I'll take a total guess here on the OP's intent (internal MPU IO registers) and say either,

PINC = 0x12;

or,

TCNT0 = 0x12;

You CANNOT write to hardware in C/C++ as such. The language is hardware independent! It therefore does not and must not allow this kind of thing!

BUT you can write to a location in memory and all the "registers" of the devices (eg I2C SPI Serial ) have been memory map in what is known as DMA.

Therefore you just assign a value to a location eg

= value;

Each of the registers has a name in the datasheet, it also has an address, and someone has already setup a "list" of the reg's and there address and told the compiler how to deal with them so that you can write/read them as if they are just another variable!.

But note this is not part of the language (syntax) but just the way modern computers work

Mark

holmes4:
have been memory map in what is known as DMA.

Sorry, this is not what DMA means or is.

Sorry, this is not what DMA means or is.

The wiki is wrong (yet again)

in DMA we give all the "locations/registers" within the device a location/address in memory. We can therefore access the device as if it where a group of variables. We use the linker to force our variables to have certain locations.

The concept of having the registers of the device mapped to locations within the address space can then be extended to allow the device to write to memory (SRAM in arduino terms).

This is not used on the AVR's even thow all the registers of the devices and the CPU are DMA / memory mapped.

ON the DUE and other more advanced SoC's (System (computer) on a Chip) things get even more compex with devices on the data bus being allowed to take to each other!

Mark

PS dot edu sites are a 1000 times better than the wiki!
Mark

The wiki is wrong (yet again)

No, you are wrong, again.

You're confusing "memory-mapped" with "DMA".

They've never, ever been the same thing.

PS dot edu sites are a 1000 times better than the wiki!

But they seem not to be, somewhat ironically, always peer-reviewed.

I certainly wouldn't trust ps.edu

You CANNOT write to hardware in C/C++ as such. The language is hardware independent

Utter nonsense.

[OT]

There is a lot of confusion on this board.

[/OT]

No confusion here. 8)

And no confusion here I have used the term DMA as it is used in the AVR datasheet I have also used in the same wau as it was used in the 1970's when I earned my degree in computer science.

What common term would you use for the devices on the address/data/control bus of a cpu?

Mark

As already mentioned: memory-mapped.

My CS degree was also mostly studied in the 70s, and DMA did not mean simply memory-mapped even then.

I'd be surprised if the term even appears in an AVR data sheet - maybe for SPI /I2C( the only interfaces I can think of fast enough to require it)
I know the Due has DMA, but that's an ARM.

Look at the Wikipedia page for "Direct Memory Access". It contains what has been the accepted definition of the term for many decades. DMA refers to dedicated hardware for moving data between a device and memory, WITHOUT involving the CPU. I don't believe any AVR processors contain DMA hardware. The ARM-based Atmel processors, like the SAM chip on the Due, DO have DMA hardware. The definition of a term is not changed by the fact that you've used it incorrectly for years.

Regards,
Ray L.

As already mentioned: memory-mapped.

From just me! Again what else are you calling it!

Mark

holmes4:
And no confusion here I have used the term DMA as it is used in the AVR datasheet

I just looked at the 328's datasheet. (Edit: and the 640/1280/1281/2560 - you'd think the big ones with lots of pins would have DMA, right?)
Not surprisingly, the string "DMA" doesn't appear in the datasheet anywhere.
Do you work for the Cheeto-in-Chief by any chance?

What common term would you use for the devices on the address/data/control bus of a cpu?

Memory-mapped (as opposed to I/O or port mapped, as per, say, the Z-80), same as it was in the 70s.

See section 12.5

The I/O space definition of the device is shown in the Register Summary.
All device I/Os and peripherals are placed in the I/O space. All I/O locations may be accessed by the
LD/LDS/LDD and ST/STS/STD instructions

Mark

holmes4:
See section 12.5

Mark

And where does it say "DMA"?

It doesn't.

The Z-80 had memory space and I/O space. You could have memory-mapped peripherals if you wanted, but the Zilog preference was for I/O mapped, but it is nothing at all to do with DMA, which, as has already been stated, relates to direct access (geddit?) by the peripheral to the processor's memory, NOT mere co-habitation in that memory.

Think about it:

Programmed transfer: let's say a UART signals it has a character available. It interrupts the processor, and the processor executes its ISR (which has to be fetched from memory space) reads the character from a register in the peripheral chip into a processor register, and then writes it to memory; rinse and repeat for every character.

DMA: The UART is programmed with a memory address. It receives a character, takes control of the processor's memory bus (cycle-stealing), writes the character directly to the memory address given, then interrupts the processor to say a character (actually, more likely a block of data) is available, without any direct processor intervention or use of processor registers in the transfer.

@holmes4, I'm sorry you've been misusing this term all this time - time now to admit it, and move on.