Problems with bitwise operators

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
Stefke33
Posts: 37
Joined: 09 Apr 2009 14:10

Problems with bitwise operators

#1 Post by Stefke33 » 12 Apr 2009 14:06

Dear friends

I don't know if that is right what I have written for setup following the datasheets AD9833

I would like setup the control register as follows:

B28 = 0
HLB = 1
FSELECT = 1
RESET = 0
OPBITEN = 0
MODE = 0

This is the 16 bit word:

0 0 0 1 1 X 0 0 X X 0 0 0 X 0 0 0

Code: Select all


#define   FREQ0        0x4000           // Address Freq0 = 0100 0000 0000 0000

   // AD9833 Control Register bits
   #define   B28          (1 << 13)         // bit 13 B28
   #define   HLB          (1 << 12)         // bit 12 HLB
   #define   FSELECT   (1 << 11)         // bit 11 FSELECT
   #define   RESET       (1 << 8)          // bit 8 RESET
   #define   OPBITEN   (1 << 5)          // bit 5 OPBITEN
   #define   MODE        (1)
   #define 	 SINE           1

I try this:

SPI_Write(FREQ0 & B28 | HLB | FSELECT & RESET & OPBITEN & MODE);

Is that corrected if I the same results as this:
0 0 0 1 1 X 0 0 X X 0 0 0 X 0 0 0

I have learn that & = stands for reset an bit  and | set an bit

Can someone help me if I have right to the end?

The_RB
Posts: 172
Joined: 18 Jan 2008 05:32
Location: Australia
Contact:

#2 Post by The_RB » 15 Apr 2009 04:09

Hi, it looks like you are using & wrong.

Use | (or +) to set ONLY the bits you want set.

Code: Select all


// so this is wrong
SPI_Write(FREQ0 & B28 | HLB | FSELECT & RESET & OPBITEN & MODE);


// try this instead, to just set the FREQ and the 2 bits;
SPI_Write(FREQ0 | HLB | FSELECT );
[/code]
Using; EasyPic4, EasyPIC6, BigPic4, MikroC. MPLab, MPASM.
C since 1991, PIC asm since 1998. Author of many freeware and open-source goodies; www.RomanBlack.com Designer of the www.TalkBotBrain.com talking PIC project.

Post Reply

Return to “mikroC PRO for PIC General”