Created
October 28, 2020 18:26
-
-
Save circuit4u-medium/638c609838491d3ad3adcc577ddd83fc to your computer and use it in GitHub Desktop.
FT232H with MCP3201
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": [ | |
{ | |
"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": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Ok(())" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"ft.close()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## MCP3201 SPI functions" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 26, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"const SCK:u8 = 0x01u8; //AD0\n", | |
"const CS:u8 = 0x20u8; //AC5\n", | |
"\n", | |
"pub trait MCP3201: 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", | |
" fn sample(&mut self) -> f64 { \n", | |
" self.write(&[0x80,0, SCK, 0x82,0, CS, 0x20,1,0,0x82, CS,CS, 0x87,0x80,0,SCK]); //zero output; cs low; read 2 bytes; cs high; send back; zero output\n", | |
" let mut buf:[u8;2] = [0;2];\n", | |
" self.read(&mut buf); \n", | |
" if buf[0] & (1<<5) == 0 {\n", | |
" (((buf[0] & 0x1f)as f64)*((1<<7) as f64) + ((buf[1]>>1) as f64))/4096.0*3.3} \n", | |
" else {\n", | |
" println!(\"ADC read error\");\n", | |
" 0.0\n", | |
" }\n", | |
" }\n", | |
"}\n", | |
"\n", | |
"impl MCP3201 for Ftdi {}" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Example" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"()" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"ft.init_spi()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 27, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0.0064453125" | |
] | |
}, | |
"execution_count": 27, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"ft.sample()" | |
] | |
}, | |
{ | |
"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