{"id":2674,"date":"2015-08-24T13:53:24","date_gmt":"2015-08-24T13:53:24","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=2674"},"modified":"2015-08-24T13:53:24","modified_gmt":"2015-08-24T13:53:24","slug":"ad5791-board-patches-revisited","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2015\/08\/ad5791-board-patches-revisited\/","title":{"rendered":"AD5791 Board Patches Revisited"},"content":{"rendered":"<p><a href=\"http:\/\/41j.com\/blog\/wp-content\/uploads\/2015\/08\/photo3.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter  wp-image-2675\" src=\"http:\/\/41j.com\/blog\/wp-content\/uploads\/2015\/08\/photo3.jpg\" alt=\"photo(3)\" width=\"585\" height=\"439\" \/><\/a><\/p>\n<p>Tried to tidy up the patches a bit and fix the <a href=\"http:\/\/41j.com\/blog\/2015\/08\/ad5791-shield-first-rev\/\">gain issue previously noted<\/a> appears to have been a config error (wasn&#8217;t configured correctly for the external unity gain opamp). Test code follows:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &lt;SPI.h&gt;\r\n\r\nconst int reset = A5;\r\nconst int clr   = A4;\r\nconst int ldac  = A3;\r\nconst int sync  = A2;\r\n\r\n#define AD5791_NOP 0 \/\/ No operation (NOP).\r\n#define AD5791_REG_DAC 1 \/\/ DAC register.\r\n#define AD5791_REG_CTRL 2 \/\/ Control register.\r\n#define AD5791_REG_CLR_CODE 3 \/\/ Clearcode register.\r\n#define AD5791_CMD_WR_SOFT_CTRL 4 \/\/ Software control register(Write only).\r\n\r\ntypedef enum {\r\nID_AD5760,\r\nID_AD5780,\r\nID_AD5781,\r\nID_AD5790,\r\nID_AD5791,\r\n} AD5791_type;\r\n\r\nstruct ad5791_chip_info {\r\nunsigned int resolution;\r\n};\r\nstatic const struct ad5791_chip_info ad5791_chip_info&#x5B;] = {\r\n&#x5B;ID_AD5760] = {\r\n.resolution = 16,\r\n},\r\n&#x5B;ID_AD5780] = {\r\n.resolution = 18,\r\n},\r\n&#x5B;ID_AD5781] = {\r\n.resolution = 18,\r\n},\r\n&#x5B;ID_AD5790] = {\r\n.resolution = 20,\r\n},\r\n&#x5B;ID_AD5791] = {\r\n.resolution = 20,\r\n}\r\n};\r\n\r\nAD5791_type act_device;\r\n\r\n\/* Maximum resolution *\/\r\n#define MAX_RESOLUTION 20\r\n\/* Register Map *\/\r\n#define AD5791_NOP 0 \/\/ No operation (NOP).\r\n#define AD5791_REG_DAC 1 \/\/ DAC register.\r\n#define AD5791_REG_CTRL 2 \/\/ Control register.\r\n#define AD5791_REG_CLR_CODE 3 \/\/ Clearcode register.\r\n#define AD5791_CMD_WR_SOFT_CTRL 4 \/\/ Software control register(Write only).\r\n\/* Input Shift Register bit definition. *\/\r\n#define AD5791_READ (1ul &lt;&lt; 23)\r\n#define AD5791_WRITE (0ul &lt;&lt; 23)\r\n#define AD5791_ADDR_REG(x) (((unsigned long)(x) &amp; 0x7) &lt;&lt; 20)\r\n\/* Control Register bit Definition *\/\r\n#define AD5791_CTRL_LINCOMP(x) (((x) &amp; 0xF) &lt;&lt; 6) \/\/ Linearity error compensation.\r\n#define AD5791_CTRL_SDODIS (1 &lt;&lt; 5) \/\/ SDO pin enable\/disable control.\r\n#define AD5791_CTRL_BIN2SC (1 &lt;&lt; 4) \/\/ DAC register coding selection.\r\n#define AD5791_CTRL_DACTRI (1 &lt;&lt; 3) \/\/ DAC tristate control.\r\n#define AD5791_CTRL_OPGND (1 &lt;&lt; 2) \/\/ Output ground clamp control.\r\n#define AD5791_CTRL_RBUF (1 &lt;&lt; 1) \/\/ Output amplifier configuration control.\r\n\/* Software Control Register bit definition *\/\r\n#define AD5791_SOFT_CTRL_RESET (1 &lt;&lt; 2) \/\/ RESET function.\r\n#define AD5791_SOFT_CTRL_CLR (1 &lt;&lt; 1) \/\/ CLR function.\r\n#define AD5791_SOFT_CTRL_LDAC (1 &lt;&lt; 0) \/\/ LDAC function.\r\n\/* DAC OUTPUT STATES *\/\r\n#define AD5791_OUT_NORMAL 0x0\r\n#define AD5791_OUT_CLAMPED_6K 0x1\r\n#define AD5791_OUT_TRISTATE 0x2\r\n\r\nvoid setup() {\r\n  Serial.begin(115200);\r\n\r\n  SPI.begin();\r\n \r\n  pinMode(11,OUTPUT);\r\n  digitalWrite(11,LOW);\r\n\r\n  pinMode(reset, OUTPUT);\r\n  pinMode(clr  , OUTPUT);\r\n  pinMode(ldac , OUTPUT);\r\n  pinMode(sync , OUTPUT);  \r\n\r\n  \/\/ setup\r\n  digitalWrite(ldac,HIGH);\r\n  digitalWrite(reset,HIGH);\r\n  digitalWrite(clr,HIGH);\r\n  digitalWrite(sync,HIGH);\r\n  SPI.beginTransaction(SPISettings(20000000, MSBFIRST, SPI_MODE1));\r\n    \r\n  long status = AD5791_GetRegisterValue(AD5791_REG_CTRL);\r\n  \r\n  status = AD5791_GetRegisterValue(AD5791_REG_CTRL);\r\n  \r\n  Serial.print(status,BIN);\r\n  Serial.println();\r\n\r\n  digitalWrite(ldac,LOW);\r\n  status = AD5791_SetRegisterValue(AD5791_REG_DAC, 1);\r\n  digitalWrite(ldac,HIGH);\r\n\r\n\r\n  unsigned long oldCtrl = status;\r\n  oldCtrl = oldCtrl &amp; ~(AD5791_CTRL_LINCOMP(-1) | AD5791_CTRL_SDODIS | AD5791_CTRL_BIN2SC | AD5791_CTRL_OPGND);\r\n\r\n  oldCtrl = oldCtrl | AD5791_CTRL_RBUF;\r\n  status = AD5791_SetRegisterValue(AD5791_REG_CTRL, oldCtrl);\r\n  \r\n  status = AD5791_GetRegisterValue(AD5791_REG_CTRL);\r\n  \r\n\r\n\r\n  Serial.print(status,BIN);\r\n  Serial.println();\r\n  Serial.println(&quot;DAC&quot;);\r\n\r\n  digitalWrite(ldac,LOW);\r\n  status = AD5791_SetRegisterValue(AD5791_REG_DAC, 1);\r\n  digitalWrite(ldac,HIGH);\r\n\r\n}\r\n\r\n\r\n\r\nlong AD5791_SetRegisterValue(unsigned char registerAddress, unsigned long registerValue) {\r\n  unsigned char writeCommand&#x5B;3] = {0, 0, 0};\r\n  unsigned long spiWord = 0;\r\n  char status = 0;\r\n  spiWord = AD5791_WRITE | AD5791_ADDR_REG(registerAddress) | (registerValue &amp; 0xFFFFF);\r\n  writeCommand&#x5B;0] = (spiWord &gt;&gt; 16) &amp; 0x0000FF;\r\n  writeCommand&#x5B;1] = (spiWord &gt;&gt; 8 ) &amp; 0x0000FF;\r\n  writeCommand&#x5B;2] = (spiWord &gt;&gt; 0 ) &amp; 0x0000FF;\r\n  \r\n  digitalWrite(sync,LOW);\r\n  status = SPI.transfer(writeCommand&#x5B;0]);\r\n  status = SPI.transfer(writeCommand&#x5B;1]);\r\n  status = SPI.transfer(writeCommand&#x5B;2]);\r\n  digitalWrite(sync,HIGH);\r\n\r\n  return 0;\r\n}\r\n\r\nlong AD5791_GetRegisterValue(unsigned char registerAddress) {\r\n  unsigned char registerWord&#x5B;3] = {0, 0, 0};\r\n  unsigned long dataRead = 0x0;\r\n  char status = 0;\r\n  registerWord&#x5B;0] = (AD5791_READ | AD5791_ADDR_REG(registerAddress)) &gt;&gt; 16;\r\n\r\n  digitalWrite(sync,LOW);\r\n\r\n  status = SPI.transfer(registerWord&#x5B;0]);\r\n  status = SPI.transfer(registerWord&#x5B;1]);\r\n  status = SPI.transfer(registerWord&#x5B;2]);\r\n  digitalWrite(sync,HIGH);\r\n\r\n\r\n  registerWord&#x5B;0] = 0x00;\r\n  registerWord&#x5B;1] = 0x00;\r\n  registerWord&#x5B;2] = 0x00;\r\n    digitalWrite(sync,LOW);\r\n  registerWord&#x5B;0] = SPI.transfer(0x00);\r\n  registerWord&#x5B;1] = SPI.transfer(0x00);\r\n  registerWord&#x5B;2] = SPI.transfer(0x00);\r\n    digitalWrite(sync,HIGH);\r\n  dataRead = ((long)registerWord&#x5B;0] &lt;&lt; 16) |\r\n             ((long)registerWord&#x5B;1] &lt;&lt; 8) |\r\n             ((long)registerWord&#x5B;2] &lt;&lt; 0);\r\n  return dataRead;\r\n}\r\n\r\nlong current_value=0;\r\nlong d=0;\r\nvoid loop() {\r\n   \r\n    digitalWrite(ldac,LOW);\r\n    int status = AD5791_SetRegisterValue(AD5791_REG_DAC, current_value);\r\n    digitalWrite(ldac,HIGH);\r\n   \/\/ Serial.print(current_value);\r\n   \/\/ Serial.print(&quot;\\r\\n&quot;);\r\n \r\n  current_value+=10000; \r\nif(current_value&gt;524000) current_value=-524000;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Tried to tidy up the patches a bit and fix the gain issue previously noted appears to have been a config error (wasn&#8217;t configured correctly for the external unity gain opamp). Test code follows: #include &lt;SPI.h&gt; const int reset = A5; const int clr = A4; const int ldac = A3; const int sync = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-2674","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-H8","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/2674","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/comments?post=2674"}],"version-history":[{"count":1,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/2674\/revisions"}],"predecessor-version":[{"id":2676,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/2674\/revisions\/2676"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=2674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=2674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=2674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}