C Memory Addressing

Status
Not open for further replies.

Suraj143

Active Member
I want to point many variables to particular memory locations in a PIC.

In ASM I do as this

Code:
	cblock	40h
	data1,Data2,data3
	endc

So I know the address of each data1, data2, data3...... etc......

In C language how I point memory address?

Code:
	unsigned short data1,data2,data3

In the above I don't know the exact memory address

I use mikroC
 
Absolute addressing is done with the @ symbol. Look it up in your documentation.... But!!! The whole point of C is you don't need to know where it is..

If you get your head around pointers...

Code:
unsigned char variable;   // create variable for data..
unsigned char* location;    // create a pointer ..

location = &variable;  // point at variable..

*location = 0x22;   // assign variable..

variable now contains 0x22..
 
Hi thanks for the reply. Actualy I know a bit about pointers.

Actually this is what I'm going to do.Let say I get numbers from 1 to 30.This number I must store in general purpose registers.

GP registers starts (declared) 40h onwards.

If it is 1 then it must store in 41h--now it contains 1
If it is 4 then it must store in 44h--now it contains 4
If it is 12 then it must store in 4Ch--now it contains 12

In assembly I do like this.

Code:
		movf	Zone_Number,W
		addlw	40h		; add offset (because GP starts from 40h onwards)
		movwf	FSR
Place_Alarms	movf	Zone_Number,W
		movwf	INDF

I want to do this in C.
 
That's how I would do it... Remember that a pointer in C IS the same as the address of an array

So

Code:
unsigned  int alarm[30];

unsigned int* digit = &alarm[];

digit[zone_number] = zone_number;

Not that you'll need it in this case.... Its just easier to pass a pointer and receive a pointer when using functions...
 

If you want to write to memory location 0x40 you can cast that memory adress to a pointer and then write to that location:

(*((uint8_t *)0x40)) = value;

You can use that pointer as an array also:

((uint8_t *)0x40)[index] = value; // write to memory location 0x40+index.
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…