Created
October 31, 2020 19:19
-
-
Save circuit4u-medium/315461e862cdea5d6949fedf6b2644df to your computer and use it in GitHub Desktop.
FT232H and TM1638
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": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
":dep libftd2xx" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"use libftd2xx::{Ftdi, FtdiCommon, BitMode};\n", | |
"use std::time::Duration;\n", | |
"use std:thread" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## open FTDI FT232H device (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 mut ft = Ftdi::with_serial_number(\"MLTS91A\")?; //use serial #\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": [ | |
"## I2C functions" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"const SCK:u8 = 0x01u8; //AD0\n", | |
"const MOSI:u8 = 0x02u8; //AD1 MOSI and AD2 MISO\n", | |
"const CS :u8 = 0x08u8; //AD3\n", | |
"\n", | |
"pub trait TM1638: FtdiCommon {\n", | |
" fn init_spi(&mut self){\n", | |
" self.set_bit_mode(0, BitMode::Mpsse);\n", | |
" self.write(&[0x8A, 0x86,59,0]); //1MHz\n", | |
" self.write(&[0x80,CS, CS+MOSI+SCK]);\n", | |
" }\n", | |
" \n", | |
" fn start_spi(&mut self){\n", | |
" self.write(&[0x80,0, CS+MOSI+SCK]);\n", | |
" }\n", | |
" fn end_spi(&mut self){\n", | |
" self.write(&[0x80,CS, CS+MOSI+SCK]); \n", | |
" }\n", | |
" fn send_byte(&mut self, byte_to_send: u8){ \n", | |
" self.write(&[0x19u8,0,0,byte_to_send]); //-ve single byte out; LSB first \n", | |
" \n", | |
" }\n", | |
" fn read_keypress(&mut self) -> [u8;4]{ \n", | |
" self.start_spi();\n", | |
" self.send_byte(0x42);\n", | |
" self.write(&[0x80, 0, SCK+CS, 0x28,3,0,0x87]);//release MOSI; ready 4-byte LSB first; send back \n", | |
" self.end_spi();\n", | |
" let mut buf = [0u8;4];\n", | |
" self.read(&mut buf);\n", | |
" \n", | |
" buf\n", | |
" }\n", | |
" \n", | |
"}\n", | |
"\n", | |
"impl TM1638 for Ftdi {}" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Initial SPI" | |
] | |
}, | |
{ | |
"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": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Turn TM1637 display ON/OFF" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"ft.start_spi(); ft.send_byte(0x8f); ft.end_spi();" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"ft.start_spi(); ft.send_byte(0x80); ft.end_spi();" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Display digits" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"const digits:[u8;10] = [0x3f,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F];" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"enter data mode" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"ft.start_spi(); ft.send_byte(0x40); ft.end_spi();" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"display digits" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"()" | |
] | |
}, | |
"execution_count": 11, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"ft.start_spi();\n", | |
"ft.send_byte(0xc0);\n", | |
"for _ in 0..8 {\n", | |
" ft.send_byte(digits[9]);\n", | |
" ft.send_byte(0);\n", | |
"}\n", | |
"ft.end_spi()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"read keypress" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"ft.read_keypress()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Example" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"()" | |
] | |
}, | |
"execution_count": 23, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"// init\n", | |
"ft.start_spi();\n", | |
"ft.send_byte(0xc0);\n", | |
"let mut d = [0;8];\n", | |
"for i in 0..8 {\n", | |
" ft.send_byte(digits[d[i]]);\n", | |
" ft.send_byte(0);\n", | |
"}\n", | |
"ft.end_spi();\n", | |
"\n", | |
"// keyscan\n", | |
"for _ in 0..100 {\n", | |
" let k = ft.read_keypress();\n", | |
"// for i in 0..4{\n", | |
"// println!(\"{}\", k[i]);\n", | |
"// } \n", | |
" for i in 0..4{ \n", | |
" if k[i]&0x01==0x01{\n", | |
" d[i] = d[i]+1;\n", | |
" } \n", | |
" }\n", | |
" for i in 4..8{\n", | |
" if k[i-4]&0x10==0x10{\n", | |
" d[i] = d[i]+1;\n", | |
" } \n", | |
" }\n", | |
" ft.start_spi();\n", | |
" ft.send_byte(0xc0);\n", | |
" for i in 0..8 {\n", | |
" ft.send_byte(digits[d[i]%10]);\n", | |
" ft.send_byte(0);\n", | |
" }\n", | |
" ft.end_spi();\n", | |
" thread::sleep_ms(180);\n", | |
"\n", | |
"}" | |
] | |
} | |
], | |
"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