<xml xmlns="https://developers.google.com/blockly/xml">
<description>
<![CDATA[

## Set 6 bit DAC, and read using ADC - 0

The resistors connected to PORTC pins 7,6,5,4,3 and 2 in a zig zag fashion form a ladder DAC(Digital to analog convertor)

We will measure the output of the this dac using the ADC input A0 and plot it for different set values.

+ Set PORTC pins to output type using the DDRC register
+ Set the voltage of the DAC by setting the appropriate pins to HIGH / LOW.

## Working of a ladder DAC

The supply voltage is 5V

Contribution of the final step (PC7) is 5V/2  = 2.5 V


Contribution of the  next (PC6) is 5V/4 = 1.25 Volts


and so on...

Since we have a 6-bit DAC(Pins PC7->PC2), we can set a maximum value of 63 (2^6-1). The bits to be set are as shown below

<img class="ui image fluid" src='data:image/jpeg;base64,[encoded data, 6476 characters]'/>

Since PORTC starts from PC0, if we write 63 to it, the pins set HIGH will be [PC5,4,3,2,1,0] . But we want to set [PC7,6,5,4,3,2] which is where our DAC is wired.

Therefore, we use the left shift operator to shift 63 two places to the left, and then write it to PORTC.

<img class="ui image fluid" src='data:image/jpeg;base64,[encoded data, 36220 characters]'/>


## Reading using the ADC

The ADC has a resolution of 10 bits, so the minimum value is 0, and maximum is (2^10-1 = 1023)  .




]]>
</description>



  <variables>
    <variable id="LbE2%mV/CRs,w^fwYIZy">i</variable>
  </variables>
  <block type="controls_for" id="gt?!R8%BaDn7}#qh,%n~" x="10" y="100">
    <field name="VAR" id="LbE2%mV/CRs,w^fwYIZy">i</field>
    <value name="FROM">
      <shadow type="math_number" id="wA=$)l4vc%P1L:-NvQ^%">
        <field name="NUM">1</field>
      </shadow>
    </value>
    <value name="TO">
      <shadow type="math_number" id="`Z;M=:kBfL@egmUwye3?">
        <field name="NUM">63</field>
      </shadow>
    </value>
    <value name="BY">
      <shadow type="math_number" id="DRD-|NhQpO8%ycET!4]6">
        <field name="NUM">1</field>
      </shadow>
    </value>
    <statement name="DO">
      <block type="set_dac" id="]{v}+q~{uF0ZKVnSFF3?">
        <value name="VALUE">
          <block type="shift_bits" id="js0[s.cVjA+UzAqu5!=5">
            <field name="DIRECTION">&lt;&lt;</field>
            <field name="BITS">2</field>
            <value name="VALUE">
              <block type="variables_get" id="KiT^m;oaUu0ZYeL)=fB=">
                <field name="VAR" id="LbE2%mV/CRs,w^fwYIZy">i</field>
              </block>
            </value>
          </block>
        </value>
        <next>
          <block type="plot_xy" id="*#jk`)`j*deL9MyXa{r.">
            <value name="X">
              <block type="variables_get" id="oe.Zkp%yB9:AHR)~Y!-@">
                <field name="VAR" id="LbE2%mV/CRs,w^fwYIZy">i</field>
              </block>
            </value>
            <value name="Y">
              <block type="get_voltage" id="JqJdT@xp|z9b!|4BB/O:">
                <field name="CHANNEL">0</field>
              </block>
            </value>
            <next>
              <block type="wait_seconds" id="!fjOg8~uC84oHBMsad+M">
                <field name="SECONDS">0.01</field>
              </block>
            </next>
          </block>
        </next>
      </block>
    </statement>
  </block>
</xml>
