Created
December 17, 2018 14:04
-
-
Save satakagi/2702b81810c807ab3cde57572e135137 to your computer and use it in GitHub Desktop.
PCF8591 driver for CHIRIMEN RPi3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PCF8591 driver for CHIRIMEN raspberry pi3 | |
// 4ch 8bit ADC, 1ch 8bit DAC | |
// CDS, Thermister, VR and a LED | |
// Programmed by Satoru Takagi | |
var PCF8591 = function(i2cPort,slaveAddress){ | |
if (!slaveAddress){ | |
slaveAddress = 0x48; | |
} | |
this.i2cPort = i2cPort; | |
this.i2cSlave = null; | |
this.slaveAddress = slaveAddress; | |
this.refV = 3.3; | |
} | |
PCF8591.prototype = { | |
init: async function(){ | |
this.i2cSlave = await this.i2cPort.open(this.slaveAddress); | |
}, | |
readADC: async function(ch){ | |
if ( ch >=0 && ch < 4 ){ | |
var prev = await this.i2cSlave.read8(ch + 0x40); // prev data.. | |
var ans = await this.i2cSlave.read8(ch + 0x40); | |
return( this.refV * ans / 0xff ); | |
} else { | |
return(-1); | |
} | |
}, | |
setDAC: async function(vl){ | |
var dav = Math.floor( 0xff * vl / this.refV ) & 0xff; | |
console.log("setDAC:",dav); | |
await this.i2cSlave.write8( 0x40, dav ); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
window.addEventListener( | |
"load", | |
function() { | |
mainFunction(); | |
}, | |
false | |
); | |
var pcfPromise; | |
async function mainFunction() { | |
try { | |
var i2cAccess = await navigator.requestI2CAccess(); | |
var port = i2cAccess.ports.get(1); | |
var pcf = new PCF8591(port); | |
await pcf.init(); | |
pcfPromise = pcf; | |
while (1) { | |
var adv0 = await pcf.readADC(0); | |
var adv1 = await pcf.readADC(1); | |
var adv2 = await pcf.readADC(2); | |
var adv3 = await pcf.readADC(3); | |
adc0.innerHTML=adv0+"V"; | |
adc1.innerHTML=adv1+"V"; | |
adc2.innerHTML=adv2+"V"; | |
adc3.innerHTML=adv3+"V"; | |
await sleep(200); | |
} | |
} catch (error) { | |
console.error("error", error); | |
} | |
} | |
async function dac(v){ | |
var pcf = await pcfPromise; | |
await pcf.setDAC(v); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1"> | |
<title>i2c-BMP180</title> | |
<script src="../../../polyfill/polyfill.js"></script> | |
<script src="../../drivers/i2c-PCF8591.js"></script> | |
<script src="./mainPcf.js"></script> | |
<style> | |
table,p { | |
color: blue; | |
text-align: center; | |
font-size: 24px; | |
} | |
img { | |
position: absolute; | |
top: 50px; | |
right: 0px; | |
display: block; | |
margin-left: auto; | |
margin-right: auto; | |
} | |
</style> | |
</head> | |
<body> | |
<p id="head">PCF8591</p> | |
<img src="schematic.png" width="400"/> | |
<table> | |
<tr><td>ADC0</td><td id="adc0"></td></tr> | |
<tr><td>ADC1</td><td id="adc1"></td></tr> | |
<tr><td>ADC2</td><td id="adc2"></td></tr> | |
<tr><td>ADC3</td><td id="adc3"></td></tr> | |
<tr><td>DAC</td><td id="dac"> | |
<input type="button" value="0V" onClick="dac(0)"/> | |
<input type="button" value="1.1V" onClick="dac(1.1)"/> | |
<input type="button" value="2.2V" onClick="dac(2.2)"/> | |
<input type="button" value="3.3V" onClick="dac(3.3)"/> | |
</td></tr> | |
</table> | |
</body> | |
</html> |
Author
satakagi
commented
Dec 17, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment