Last active
October 28, 2020 18:04
-
-
Save circuit4u-medium/2107a89f49578b3ab6ce268675379ea0 to your computer and use it in GitHub Desktop.
FT232H with MCP3204
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
{ | |
"cells": [ | |
{{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Import libftd2xx" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
":dep libftd2xx" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"use libftd2xx::{Ftdi, FtdiCommon, BitMode};" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"\n", | |
"## open FTDI FT232H with serial number" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Device information: DeviceInfo { port_open: true, speed: None, device_type: FT232H, vendor_id: 0x0403, product_id: 0x6014, serial_number: FTU85S8E, description: UM232H }\n" | |
] | |
} | |
], | |
"source": [ | |
"let mut ft = Ftdi::new()?;\n", | |
"let info = ft.device_info()?;\n", | |
"println!(\"Device information: {:?}\", info);" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### **defered**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"ft.close()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## MCP3204 SPI functions" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"const MOSI:u8 = 0x02u8; //AD1\n", | |
"const SCK:u8 = 0x01u8; //AD0\n", | |
"const CS:u8 = 0x20u8; //AC5\n", | |
"\n", | |
"pub trait MCP3204: FtdiCommon {\n", | |
" fn init_spi(&mut self) {\n", | |
" self.set_bit_mode(0, BitMode::Mpsse);\n", | |
" self.write(&[0x8A, 0x86,59,0]); //1MHz SPI clk rate\n", | |
" self.write(&[0x82,CS,CS]); //CS high\n", | |
" }\n", | |
"\n", | |
"\n", | |
" fn sample(&mut self, ch:u8) -> f64 { \n", | |
" self.write(&[0x80,0, SCK+MOSI, 0x82,0, CS, 0x31,2,0,6,(ch<<6),0,0x82, CS,CS, 0x87,0x80,0,SCK+MOSI]); //zero output; cs low; read 3 bytes; cs high; send back; zero output\n", | |
" let mut buf:[u8;3] = [0;3];\n", | |
" self.read(&mut buf);\n", | |
" if buf[1] & (1<<4) == 0 {\n", | |
" (((buf[1] & 0x0f)as f64)*((1<<8) as f64) + (buf[2] as f64))/4096.0*3.3} \n", | |
" else {\n", | |
" println!(\"ADC read error\");\n", | |
" 0.0\n", | |
" }\n", | |
" }\n", | |
"}\n", | |
"\n", | |
"impl MCP3204 for Ftdi {}" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Example" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"ft.init_spi()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"for i in 0..4 {\n", | |
" println!(\"{}\",ft.sample(i))\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"@webio": { | |
"lastCommId": null, | |
"lastKernelId": null | |
}, | |
"kernelspec": { | |
"display_name": "Rust", | |
"language": "rust", | |
"name": "rust" | |
}, | |
"language_info": { | |
"codemirror_mode": "rust", | |
"file_extension": ".rs", | |
"mimetype": "text/rust", | |
"name": "Rust", | |
"pygment_lexer": "rust", | |
"version": "" | |
}, | |
"toc": { | |
"base_numbering": 1, | |
"nav_menu": {}, | |
"number_sections": true, | |
"sideBar": true, | |
"skip_h1_title": false, | |
"title_cell": "Table of Contents", | |
"title_sidebar": "Contents", | |
"toc_cell": false, | |
"toc_position": {}, | |
"toc_section_display": true, | |
"toc_window_display": false | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Import libftd2xx" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
":dep libftd2xx" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"use libftd2xx::{Ftdi, FtdiCommon, BitMode};" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"\n", | |
"## open FTDI FT232H with serial number" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 105, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Device information: DeviceInfo { port_open: true, speed: None, device_type: FT232H, vendor_id: 0x0403, product_id: 0x6014, serial_number: FTY4S2TQ, description: UM232H }\n" | |
] | |
} | |
], | |
"source": [ | |
"let mut ft = Ftdi::new()?;\n", | |
"let info = ft.device_info()?;\n", | |
"println!(\"Device information: {:?}\", info);" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### **defered**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 82, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Ok(())" | |
] | |
}, | |
"execution_count": 82, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"ft.close()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## MCP3204/MCP3201 SPI functions" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 111, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"pub trait MCP3204: FtdiCommon {\n", | |
" const MOSI:u8 = 0x02u8; //AD1\n", | |
" const SCK:u8 = 0x01u8; //AD0\n", | |
" const CS:u8 = 0x20u8; //AC5\n", | |
"\n", | |
"\n", | |
" fn init_spi(&mut self) {\n", | |
" self.set_bit_mode(0, BitMode::Mpsse);\n", | |
" self.write(&[0x8A, 0x86,59,0]); //1MHz SPI clk rate\n", | |
" self.write(&[0x82,CS,CS]); //CS high\n", | |
" }\n", | |
"\n", | |
"\n", | |
"\n", | |
" fn sample(&mut self, ch:u8) -> f64 {\n", | |
" \n", | |
" self.write(&[0x80,0, SCK+MOSI, 0x82,0, CS, 0x31,2,0,6,(ch<<6),0,0x82, CS,CS, 0x87,0x80,0,SCK+MOSI]); //zero output; cs low; read 3 bytes; cs high; send back; zero output\n", | |
" let mut buf:[u8;3] = [0;3];\n", | |
" self.read(&mut buf);\n", | |
" if buf[1] & (1<<4) == 0 {\n", | |
" (((buf[1] & 0x0f)as f64)*((1<<8) as f64) + (buf[2] as f64))/4096.0*3.3} \n", | |
" else {\n", | |
" println!(\"ADC read error\");\n", | |
" 0.0\n", | |
" }\n", | |
" }\n", | |
"}\n", | |
"\n", | |
"impl MCP3204 for Ftdi {}" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Example" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 107, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"()" | |
] | |
}, | |
"execution_count": 107, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"ft.init_spi()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 118, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1.9263427734374998\n", | |
"1.9086181640625\n", | |
"1.8425537109374999\n", | |
"0.001611328125\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"()" | |
] | |
}, | |
"execution_count": 118, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"for i in 0..4 {\n", | |
" println!(\"{}\",ft.sample(i))\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"@webio": { | |
"lastCommId": null, | |
"lastKernelId": null | |
}, | |
"kernelspec": { | |
"display_name": "Rust", | |
"language": "rust", | |
"name": "rust" | |
}, | |
"language_info": { | |
"codemirror_mode": "rust", | |
"file_extension": ".rs", | |
"mimetype": "text/rust", | |
"name": "Rust", | |
"pygment_lexer": "rust", | |
"version": "" | |
}, | |
"toc": { | |
"base_numbering": 1, | |
"nav_menu": {}, | |
"number_sections": true, | |
"sideBar": true, | |
"skip_h1_title": false, | |
"title_cell": "Table of Contents", | |
"title_sidebar": "Contents", | |
"toc_cell": false, | |
"toc_position": {}, | |
"toc_section_display": true, | |
"toc_window_display": false | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment