| View previous topic :: View next topic |
| Author |
Message |
brutto
Joined: 24 Jun 2015 Posts: 2
|
| about #byte #bit and xxxx.bits |
Posted: Wed Jun 24, 2015 11:38 am |
|
|
hello,
i have a question, i can do:
| Code: |
#BYTE IFS0=0X0084
#BIT IEC0_AD1IE=0X0094.13 |
but could i do any word to write 3 bits for one register? for example, the register "IPC3bits.AD1IP" have 3 bits, would be the solution this?
| Code: |
#BIT IPC3_AD1IP0=0x00A4.4
#BIT IPC3_AD1IP1=0x00A4.5
#BIT IPC3_AD1IP2=0x00A4.6
ipc3_ad1ip0=0; //IPC3bits.AD1IP=6
ipc3_ad1ip1=1;
ipc3_ad1ip2=1;
|
or is there any other solution to write directly "6" in ipc3.ad1ip register??
thanks in advance... |
|
 |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jun 24, 2015 11:50 am |
|
|
Yes, declare a structure with bitfields. Then write to the bitfields in your
code. The compiler will handle inserting the correct data into the bits.
Look at the .LST file to see this.
| Code: |
#include <16F690.h>
#fuses INTRC_IO,NOWDT
#use delay(clock=4M)
// This structure divides PortB into two bitfields.
struct {
int8 Columns:4; // B0-3: Output pins
int8 Rows:4; // B4-7: Input pins
}PortB;
#byte PortB = getenv("SFR:PORTB")
//=========================
void main()
{
set_tris_b(0);
PortB.Columns = 6;
PortB.Rows = 3;
while(TRUE);
} |
|
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19961
|
|
Posted: Wed Jun 24, 2015 12:06 pm |
|
|
There is another thing you can do, and a comment.
#byte creates a byte variable at a location if asked to. #word creates a 16bit word. To use bit 13, you should be using #word.
However #byte can also do something else. If given the name of an already declared variable, it can _locate_ this variable at the location. So you can declare a struct, using bit fields, like:
| Code: |
#struct
{
int8 dummy:4; //bits 0 to 3
int8 ip:3;
int8 dummy2:1;
} ipc3bits ;
#byte ipc3bits=0xA4
|
You can then write to the variable ipc3bits.ip as a 3bit integer sitting at the required location in the register!.... |
|
 |
brutto
Joined: 24 Jun 2015 Posts: 2
|
|
Posted: Sat Jun 27, 2015 2:53 am |
|
|
| thanks guys, i will try with that |
|
 |
|