• AVR Freaks

Hot!Using SFR register with XC8

Page: < 12 Showing page 2 of 2
Author
ric
Super Member
  • Total Posts : 23846
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Using SFR register with XC8 2019/08/25 22:46:16 (permalink)
+1 (1)
Where are you seeing this?
Don't ask such obscure questions without a bit of context.

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#21
NKurzman
A Guy on the Net
  • Total Posts : 17840
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: offline
Re: Using SFR register with XC8 2019/08/25 23:04:40 (permalink)
+1 (1)
ric
NKurzman
... You can write to the SFR directly. But since the PIC is paged. You would need to insure do are in the correct bank. You would be responsible to control the banking if you want that control.
...
They are writing in C, so paging is not a concern... I am equally bemused by the reference to "macros" and feel they are making this all much more complicated than it really is. 

If he is trying to just write to the numerical memory address of an SFR does the compiler keep the bank straight?

This appears to be an elaborate version of the XY problem. I don’t believe anyone has ever asked this question before
#22
ric
Super Member
  • Total Posts : 23846
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Using SFR register with XC8 2019/08/25 23:07:45 (permalink)
+1 (1)
I'm sure they are just trying to find a way to indirectly control a pin.
I agree this is an "XY Problem", they keep concentrating on how to implement the solution they already have in mind, rather than simply explaining what they need to achieve.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#23
newbie_pic
New Member
  • Total Posts : 25
  • Reward points : 0
  • Joined: 2019/07/05 19:39:30
  • Location: India
  • Status: offline
Re: Using SFR register with XC8 2019/08/25 23:32:14 (permalink)
-1 (1)
Where are you seeing this?
Don't ask such obscure questions without a bit of context.

Microchip > xc8 > v2.05 > pic > include > __at.h
#define __at(x) __attribute__((address(x)))

 
pic16f688.h
extern volatile unsigned char PORTA __at(0x005);

 
What is the use of __at(x) ?
#24
ric
Super Member
  • Total Posts : 23846
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Using SFR register with XC8 2019/08/25 23:34:16 (permalink)
+2 (2)
You really shouldn't be delving into files you don't understand.
It's simply forcing the absolute address of that register.
No you can NOT put a variable name in there!
I'm just too exhausted from fighting against your determination to do this the wrong way. Bye.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#25
JPortici
Super Member
  • Total Posts : 771
  • Reward points : 0
  • Joined: 2012/11/17 06:27:45
  • Location: Grappaland
  • Status: offline
Re: Using SFR register with XC8 2019/08/26 00:17:43 (permalink)
+1 (1)
I bet he's trying to implement digital write or such arduino style bullshit.
It would be way easier for everybody if he just said that.
 
@newbie do everybody a favour and try to explain with clarity what you want to do and what your problem is, because any question you asked can be answered by the compiler manual
#26
du00000001
Just Some Member
  • Total Posts : 3061
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: Using SFR register with XC8 2019/08/26 01:15:14 (permalink)
+1 (1)
@ newbie_pic
I suggest to take 2 courses prior coming back:
"English Level 2"
"C 1o1"
Then maybe reading XC8's compiler manual.

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#27
1and0
Access is Denied
  • Total Posts : 9736
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Using SFR register with XC8 2019/08/26 01:31:20 (permalink)
+1 (1)
A few seconds with google yielded this:

#define __SFR_OFFSET 0x20
#define _MMIO_BYTE(mem_addr) (*(volatile uint8_t *)(mem_addr))
#define _SFR_IO8(io_addr) _MMIO_BYTE((io_addr) + __SFR_OFFSET)

 
#28
du00000001
Just Some Member
  • Total Posts : 3061
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: Using SFR register with XC8 2019/08/26 01:36:00 (permalink)
+1 (1)
WTF would one want to use addresses with an offset?
OTOH, the SFR offset for the PICs os just zero.

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#29
1and0
Access is Denied
  • Total Posts : 9736
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Using SFR register with XC8 2019/08/26 01:56:32 (permalink)
+1 (1)
du00000001
WTF would one want to use addresses with an offset?

Yeah, WTF. Look like in the Arduino world:
  1. The _MMIO_BYTE() is a macro that dereferences a byte of data at the specified address.
  2. The _SFR_IO8() converts the I/O address to the memory address. It is a macro that returns a byte of data at an address of io_addr + __SFR_OFFSET.
 

OTOH, the SFR offset for the PICs os just zero.

Yeah, for Bank 0 in a PIC16 anyway. ;)
 
 
#30
1and0
Access is Denied
  • Total Posts : 9736
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Using SFR register with XC8 2019/08/26 02:16:48 (permalink)
+1 (1)
The use of io_addr + __SFR_OFFSET makes more sense when you see the AVR memory architecture:
 
https://protostack.com.au/2010/12/avr-memory-architecture/
#31
du00000001
Just Some Member
  • Total Posts : 3061
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: Using SFR register with XC8 2019/08/26 02:23:49 (permalink)
+1 (1)
@ 1and0
This offset thing is just "philosophy": whether the first SFR address is assigned 0x00 or 0x20 doesn't change anything substantial.

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#32
1and0
Access is Denied
  • Total Posts : 9736
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Using SFR register with XC8 2019/08/26 02:33:02 (permalink)
+1 (1)
... and this one offset won't work for PIC16 devices where there are more than one bank.
#33
du00000001
Just Some Member
  • Total Posts : 3061
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: Using SFR register with XC8 2019/08/26 02:42:10 (permalink)
+1 (1)
Would work for PIC devices as well (although the offset is 0):
just that the access wouldn't be a single instruction (banksel required). But this is the job of the compiler...

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#34
1and0
Access is Denied
  • Total Posts : 9736
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Using SFR register with XC8 2019/08/26 02:45:54 (permalink)
+1 (1)
Look like in the Arduino world, there's a distinction between I/O address and memory address. For example, PORTB has a memory address of 0x25, so
#define PORTB _SFR_IO8(0x05)

 
#35
1and0
Access is Denied
  • Total Posts : 9736
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Using SFR register with XC8 2019/08/26 02:55:33 (permalink)
+1 (1)
newbie_pic
Hi, In AVR we can access any registers using _SFR_IO8(Address). I want to know how to do this with PIC?

So, how about this
#define _PIC_BYTE(mem_addr) (*(volatile uint8_t *)(mem_addr))

 
However, I strongly suggest you to use the SFR symbol names instead, provided in the header file.
post edited by 1and0 - 2019/08/26 03:00:31
#36
Kevil
New Member
  • Total Posts : 12
  • Reward points : 0
  • Joined: 2019/09/09 13:29:33
  • Location: 0
  • Status: offline
Re: Using SFR register with XC8 2019/09/15 16:42:34 (permalink)
-2 (2)
Any advice how to access PORTA registers (e.g. LED flashing at pin 4) in ATtiny202 (AVR) by using XC8? Which include files should i use except avr/io.h? I am a beginner.
#37
ric
Super Member
  • Total Posts : 23846
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Using SFR register with XC8 2019/09/16 13:17:54 (permalink)
+1 (1)
Kevil
Any advice how to access PORTA registers (e.g. LED flashing at pin 4) in ATtiny202 (AVR) by using XC8? Which include files should i use except avr/io.h? I am a beginner.

Have you read the User Guide that comes with the compiler?
For v2.10, that is at:
C:\Program Files (x86)\Microchip\xc8\v2.10\docs\MPLAB_XC8_C_Compiler_User_Guide_for_AVR.pdf
on your hard drive
Specifically look at chapter "3.3.7 Using SFRs From C Code"
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#38
Page: < 12 Showing page 2 of 2
Jump to:
© 2019 APG vNext Commercial Version 4.5