{"id":584,"date":"2016-03-02T13:32:55","date_gmt":"2016-03-02T05:32:55","guid":{"rendered":"http:\/\/www.datarelab.com\/blog\/?p=584"},"modified":"2016-03-02T13:35:49","modified_gmt":"2016-03-02T05:35:49","slug":"%e6%a0%91%e8%8e%93%e6%b4%be-pcf8591-adda","status":"publish","type":"post","link":"https:\/\/www.datarelab.com\/blog\/Technical_literature\/584.html","title":{"rendered":"\u6811\u8393\u6d3e PCF8591 AD\/DA"},"content":{"rendered":"<p>\u6811\u8393\u6d3e\u672c\u8eab\u6ca1\u6709AD\/DA\u529f\u80fd\uff0c\u5982\u679c\u6811\u8393\u6d3e\u5916\u63a5\u6a21\u62df\u4f20\u611f\u5668\uff0c\u5219\u5fc5\u987b\u5916\u63a5AD\/DA\u529f\u80fd\u6269\u5c55\u677f\u624d\u80fd\u7528\u3002Pioneer 600\u6269\u5c55\u5e26\u6709AD\/DA\u82af\u7247PCF8591,pcf8591\u5e261\u901a\u90538\u4f4dDA,4\u901a\u90538\u4f4dAD,\u901a\u8fc7I2C\u63a7\u5236\u3002<\/p>\n<p>\u4e00\u3001DAC<br \/>\n1\u3001bcm2835\u7a0b\u5e8f<\/p>\n<pre class=\"lang:c decode:true\">#include &lt;bcm2835.h&gt;  \r\n#include &lt;stdio.h&gt;\r\n#include &lt;unistd.h&gt;\r\n\r\nint main(int argc, char **argv)\r\n{  \r\n\tchar Buf[]={0,0};\r\n\tunsigned char value=0;\r\n\r\n\tif (!bcm2835_init())return 1;  \r\n\tbcm2835_i2c_begin(); \r\n\tbcm2835_i2c_setSlaveAddress(0x48);\r\n    bcm2835_i2c_set_baudrate(10000);\r\n    printf(\"start..........\\n\");\r\n\r\n    while(1)  \r\n    {\r\n\t\tBuf[0] = 0x40;\r\n\t\tBuf[1] = value++;\r\n\t\tbcm2835_i2c_write(Buf,2);\r\n\t\tprintf(\"AOUT: %d\\n\",value);\r\n\r\n        bcm2835_delay(20);  \r\n\t}  \r\n\r\n    bcm2835_i2c_end();  \r\n    bcm2835_close();  \r\n    return 0;  \r\n}<\/pre>\n<p>\u7f16\u8bd1\u5e76\u6267\u884c<br \/>\ngcc \u2013Wall pcf8591.c \u2013o pcf8591 \u2013lbcm2835<br \/>\nsudo .\/ pcf8591<\/p>\n<p>2\u3001Python\u7a0b\u5e8f<\/p>\n<pre class=\"lang:python decode:true \">#!\/usr\/bin\/python\r\n# -*- coding:utf-8 -*-\r\nimport smbus\r\nimport time\r\n\r\naddress = 0x48\r\ncmd = 0x40\r\nvalue = 0 \r\n\r\nbus = smbus.SMBus(1)\r\nwhile True:\r\n    bus.write_byte_data(address,cmd,value)\r\n    value += 1\r\n    if value == 256:\r\n        value =0\r\n    print(\"AOUT:%3d\" %value)\r\n    time.sleep(0.01)<\/pre>\n<p>\u6267\u884c\u7a0b\u5e8f<br \/>\nsudo python pcf8591<br \/>\n3\u3001wiringPi\u7a0b\u5e8f<\/p>\n<pre class=\"lang:c decode:true \">#include &lt;wiringpi.h&gt;\r\n#include &lt;pcf8591.h&gt;\r\n#include &lt;stdio.h&gt;\r\n\r\n#define Address 0x48\r\n#define BASE 64\r\n#define A0 BASE+0\r\n#define A1 BASE+1\r\n#define A2 BASE+2\r\n#define A3 BASE+3\r\n\r\nint main(void)\r\n{\r\n    unsigned char value;\r\n    wiringPiSetup();\r\n    pcf8591Setup(BASE,Address);\r\n\r\n    while(1)\r\n    {   \r\n        analogWrite(A0,value);\r\n        printf(\"AOUT: %d\\n\",value++);\r\n        delay(20);\r\n    }   \r\n}<\/pre>\n<p>\u7f16\u8bd1\u5e76\u6267\u884c\u7a0b\u5e8f<br \/>\ngcc \u2013Wall pcf8591.c \u2013o pcf8591 \u2013lbcm2835 -lwiringPi<br \/>\nsudo .\/ pcf8591<br \/>\n\u4e8c\u3001ADC<br \/>\n1\u3001bcm2835\u7a0b\u5e8f<\/p>\n<pre class=\"lang:c decode:true \">#include &lt;bcm2835.h&gt;  \r\n#include &lt;stdio.h&gt;\r\n#include &lt;unistd.h&gt;\r\n\r\nint main(int argc, char **argv)  \r\n{  \r\n    char Buf[]={0};\r\n    unsigned char i;\r\n\r\n    if (!bcm2835_init())return 1;  \r\n    bcm2835_i2c_begin(); \r\n    bcm2835_i2c_setSlaveAddress(0x48);  \r\n    bcm2835_i2c_set_baudrate(10000);  \r\n    printf(\"start..........\\n\"); \r\n\r\n    while(1)  \r\n    {   \r\n        for(i = 0;i &lt; 4;i++)\r\n        {   \r\n            Buf[0] = i;  \r\n            bcm2835_i2c_write_read_rs(Buf,1,Buf,1);  \r\n            bcm2835_i2c_read (Buf,1);\r\n            printf(\"AIN%d:%5.2f  \",i,(double)Buf[0]*3.3\/255);\r\n        }   \r\n        printf(\"\\n\");\r\n        bcm2835_delay(100);  \r\n    }   \r\n    \r\n    bcm2835_i2c_end();  \r\n    bcm2835_close();  \r\n    return 0;  \r\n}<\/pre>\n<p>\u7f16\u8bd1\u5e76\u6267\u884c<br \/>\ngcc \u2013Wall pcf8591.c \u2013o pcf8591 \u2013lbcm2835<br \/>\nsudo .\/ pcf8591<br \/>\n2\u3001Python\u7a0b\u5e8f<\/p>\n<pre class=\"lang:python decode:true \">#!\/usr\/bin\/python\r\n# -*- coding:utf-8 -*-\r\nimport smbus\r\nimport time\r\n\r\naddress = 0x48\r\nA0 = 0x40\r\nA1 = 0x41\r\nA2 = 0x42\r\nA3 = 0x43\r\nbus = smbus.SMBus(1)\r\nwhile True:\r\n    bus.write_byte(address,A0)  \r\n    value = bus.read_byte(address)\r\n    print(\"AOUT:%1.3f  \" %(value*3.3\/255))\r\n    time.sleep(0.1)<\/pre>\n<p>\u6267\u884c\u7a0b\u5e8f<br \/>\nsudo python pcf8591<br \/>\n3\u3001wiringPi\u7a0b\u5e8f<\/p>\n<pre class=\"lang:c decode:true \">#include &lt;wiringpi.h&gt;\r\n#include &lt;pcf8591.h&gt;\r\n#include &lt;stdio.h&gt;\r\n\r\n#define Address 0x48\r\n#define BASE 64\r\n#define A0 BASE+0\r\n#define A1 BASE+1\r\n#define A2 BASE+2\r\n#define A3 BASE+3\r\n\r\nint main(void)\r\n{\r\n    int value;\r\n    wiringPiSetup();\r\n    pcf8591Setup(BASE,Address);\r\n\r\n    while(1)\r\n    {   \r\n        value = analogRead(A0);\r\n        printf(\"Analoge: %dmv\\n\",value*3300\/255);\r\n        delay(1000);\r\n    }   \r\n}<\/pre>\n<p>\u7f16\u8bd1\u5e76\u6267\u884c\u7a0b\u5e8f<br \/>\ngcc \u2013Wall pcf8591.c \u2013o pcf8591 -lwiringPi<br \/>\nsudo .\/ pcf8591<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6811\u8393\u6d3e\u672c\u8eab\u6ca1\u6709AD\/DA\u529f\u80fd\uff0c\u5982\u679c\u6811\u8393\u6d3e\u5916\u63a5\u6a21\u62df\u4f20\u611f\u5668\uff0c\u5219\u5fc5\u987b\u5916\u63a5AD\/DA\u529f\u80fd\u6269\u5c55\u677f\u624d\u80fd\u7528\u3002Pioneer  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-584","post","type-post","status-publish","format-standard","hentry","category-Technical_literature"],"views":2551,"_links":{"self":[{"href":"https:\/\/www.datarelab.com\/blog\/wp-json\/wp\/v2\/posts\/584","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.datarelab.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.datarelab.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.datarelab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.datarelab.com\/blog\/wp-json\/wp\/v2\/comments?post=584"}],"version-history":[{"count":0,"href":"https:\/\/www.datarelab.com\/blog\/wp-json\/wp\/v2\/posts\/584\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.datarelab.com\/blog\/wp-json\/wp\/v2\/media?parent=584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.datarelab.com\/blog\/wp-json\/wp\/v2\/categories?post=584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.datarelab.com\/blog\/wp-json\/wp\/v2\/tags?post=584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}