Pages: [1]   Go Down
Author Topic: MCP4922 DAC - How this code sample works  (Read 1040 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#define LDACPIN_LOW  ( PORTB &= B11111011 ) // pin 10
#define LDACPIN_HIGH ( PORTB |= B00000100 )
#define SDIPIN_LOW   ( PORTB &= B11110111 ) // pin 11
#define SDIPIN_HIGH  ( PORTB |= B00001000 )
#define SCKPIN_LOW   ( PORTB &= B11101111 ) // pin 12
#define SCKPIN_HIGH  ( PORTB |= B00010000 )
#define CSPIN_LOW    ( PORTB &= B11011111 ) // pin 13
#define CSPIN_HIGH   ( PORTB |= B00100000 )

#define CLOCK        SCKPIN_HIGH; SCKPIN_LOW;
#define SET_HIGH     SDIPIN_HIGH; CLOCK;
#define SET_LOW      SDIPIN_LOW;  CLOCK;

void setup(){
 
  DDRB = DDRB | B11111111;
 
  CSPIN_HIGH;
  SCKPIN_LOW;
  SDIPIN_LOW;
  LDACPIN_HIGH;
}

void set_dac_2 ( int value_a, int value_b ){

  // --- output A ---
  CSPIN_LOW;

  SDIPIN_HIGH; CLOCK; // 15
  SDIPIN_LOW;  CLOCK; // 14
  SDIPIN_HIGH; CLOCK; // 13
  CLOCK;              // 12
 
  SDIPIN_LOW;  if ( 2048 & value_a ){ SDIPIN_HIGH; } CLOCK; // 11
  SDIPIN_LOW;  if ( 1024 & value_a ){ SDIPIN_HIGH; } CLOCK; // 10
  SDIPIN_LOW;  if (  512 & value_a ){ SDIPIN_HIGH; } CLOCK; // 09
  SDIPIN_LOW;  if (  256 & value_a ){ SDIPIN_HIGH; } CLOCK; // 08
  SDIPIN_LOW;  if (  128 & value_a ){ SDIPIN_HIGH; } CLOCK; // 07
  SDIPIN_LOW;  if (   64 & value_a ){ SDIPIN_HIGH; } CLOCK; // 06
  SDIPIN_LOW;  if (   32 & value_a ){ SDIPIN_HIGH; } CLOCK; // 05
  SDIPIN_LOW;  if (   16 & value_a ){ SDIPIN_HIGH; } CLOCK; // 04
  SDIPIN_LOW;  if (    8 & value_a ){ SDIPIN_HIGH; } CLOCK; // 03
  SDIPIN_LOW;  if (    4 & value_a ){ SDIPIN_HIGH; } CLOCK; // 02
  SDIPIN_LOW;  if (    2 & value_a ){ SDIPIN_HIGH; } CLOCK; // 01
  SDIPIN_LOW;  if (    1 & value_a ){ SDIPIN_HIGH; } CLOCK; // 00

  CSPIN_HIGH;

  // --- output B ---
  CSPIN_LOW;

  SDIPIN_LOW; CLOCK;  // 15
  CLOCK;              // 14
  SDIPIN_HIGH; CLOCK; // 13
  CLOCK;              // 12

  SDIPIN_LOW;  if ( 2048 & value_b ){ SDIPIN_HIGH; } CLOCK; // 11
  SDIPIN_LOW;  if ( 1024 & value_b ){ SDIPIN_HIGH; } CLOCK; // 10
  SDIPIN_LOW;  if (  512 & value_b ){ SDIPIN_HIGH; } CLOCK; // 09
  SDIPIN_LOW;  if (  256 & value_b ){ SDIPIN_HIGH; } CLOCK; // 08
  SDIPIN_LOW;  if (  128 & value_b ){ SDIPIN_HIGH; } CLOCK; // 07
  SDIPIN_LOW;  if (   64 & value_b ){ SDIPIN_HIGH; } CLOCK; // 06
  SDIPIN_LOW;  if (   32 & value_b ){ SDIPIN_HIGH; } CLOCK; // 05
  SDIPIN_LOW;  if (   16 & value_b ){ SDIPIN_HIGH; } CLOCK; // 04
  SDIPIN_LOW;  if (    8 & value_b ){ SDIPIN_HIGH; } CLOCK; // 03
  SDIPIN_LOW;  if (    4 & value_b ){ SDIPIN_HIGH; } CLOCK; // 02
  SDIPIN_LOW;  if (    2 & value_b ){ SDIPIN_HIGH; } CLOCK; // 01
  SDIPIN_LOW;  if (    1 & value_b ){ SDIPIN_HIGH; } CLOCK; // 00

  CSPIN_HIGH;

  LDACPIN_LOW;
  SCKPIN_HIGH;
  LDACPIN_HIGH;
  SCKPIN_LOW;

}

void loop() {
  set_dac_2 (4095,4095);
  set_dac_2 (0,0);
}

What I don't get is how do the bits to set the value get sent to the serial data in pin? Where is the value passed?

Also what does this do 
Code:
if ( 2048 & value_b )

Does it just say if value_b = 2048 then do whats in the curly braces?
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 179
Posts: 8064
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Also what does this do 
Code:
if ( 2048 & value_b )

Does it just say if value_b = 2048 then do whats in the curly braces?

No.  It means that is the 11th bit of value_b is a 1, do the rest of the statement.

1 = Bit 0 = 0b0000000000000001
2 = Bit 1 = 0b0000000000000010
4 = Bit 2 = 0b0000000000000100
8 = Bit 3 = 0b0000000000001000
16 = Bit 4 = 0b0000000000010000
32 = Bit 5 = 0b0000000000100000
64 = Bit 6 = 0b0000000001000000
128 = Bit 7 = 0b0000000010000000
256 = Bit 8 = 0b0000000100000000
512 = Bit 9 = 0b0000001000000000
1024 = Bit 10 = 0b0000010000000000
2018 = Bit 11 = 0b0000100000000000
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Germany
Offline Offline
Edison Member
*
Karma: 1
Posts: 1353
Here we go!!!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

No.  It means that is the 11th bit of value_b is a 1, do the rest of the statement.

1 = Bit 0 = 0b0000000000000001
2 = Bit 1 = 0b0000000000000010
4 = Bit 2 = 0b0000000000000100
8 = Bit 3 = 0b0000000000001000
16 = Bit 4 = 0b0000000000010000
32 = Bit 5 = 0b0000000000100000
64 = Bit 6 = 0b0000000001000000
128 = Bit 7 = 0b0000000010000000
256 = Bit 8 = 0b0000000100000000
512 = Bit 9 = 0b0000001000000000
1024 = Bit 10 = 0b0000010000000000
2018 = Bit 11 = 0b0000100000000000

He wanted to write:
2048 = Bit 11 = 0b0000100000000000

smiley

Just found this topic and I wanted to correct it.
Logged

L'impossibile...richiede solo piu' tempo!

Pages: [1]   Go Up
Jump to: