JavaScript开发单片机:I/O篇 -- 驱动LED键盘控制IC-FD620K1.

2019-04-15 17:55发布

手头上有一台坏掉的卫星接收机,拆下面板上的显示键盘小板,使用单片机I/O驱动成功。 var usbio = uopen(0x0908, 0xa, 0x100); /** * CLK = P1.5 * DIO = P1.6 * STB = P1.7 * * fn uwrite * param 1 Device handle. * param 2 Port selector. * param 2 Write mode. * param 4 P0 port. * param 5 P1 port. * param 6 P2 port. * param 7 P3 port. * uwrite(device, Port selector, P0, P1, P2, P3); */ /** 0; j--) seq.push(parseInt(str.substr(j - 1, 1))); } led_display_seq(seq); } } function led_display_seq(s) { set_led_display(s[0], s[1], s[2]); s.shift() } function led_set_number(n) { var a1 = n % 10, a2 = (n - a1) / 10, a3 = (a2 - a2 % 10) / 10; set_led_display(a3, a2, a1); } function led_num_to_7seg(n) { /**< * 0 0 0 0 0 0 0 0 * g f e d c b a */ var seg = [0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f]; return seg[n % 10]; } function set_led_display() { set_display_mode(0); set_io(0); set_addr(0); write_byte(led_num_to_7seg(arguments[0])); if (typeof arguments[1] == 'number') { write_byte(0); write_byte(led_num_to_7seg(arguments[1])); } if (typeof arguments[2] == 'number') { write_byte(0); write_byte(led_num_to_7seg(arguments[2])); } set_display_ctrl(1, 0); // Sets STB to high. ioset('P1:|x80'); } function ioset(data) { ///< Reads the values of port. if (typeof data != 'string') { if (typeof data == 'number') ///< Reads a specified port value. return uread(usbio, data); ///< Reads all ports, return a DWORD value. return uread(usbio); } var ports = 0, modes = 0, values = [0, 0, 0, 0]; data.replace(/P([0123]):([=&^|])?(0?x)?([da-f]+)/gi, function(r1, r2, r3, r4, r5) { ports |= 1 << r2; modes |= '=&|^'.indexOf(r3) << (r2 * 2); values[r2] = parseInt(r5, r4.search(/x$/gi) != -1 ? 0x10 : 0x0a); }); uwrite(usbio, ports, modes, values[0], values[1], values[2], values[3]); } function write_byte(c) { for (var i = 0; i < 8; i++) { ioset(c & 1 ? 'P1:x40' : 'P1:x00'); ioset('P1:|x20'); c >>= 1; } } function read_byte() { var temp = 0; for (var i = 0; i < 8; i++) { ioset('P1:|x20'); ioset('P1:&x4f'); temp >>= 1; if (ioset(0x01) & 0x40) temp |= 0x80; } return temp; } /**< display mode * 00 = 4位,9段 * 01 = 5位,8段 * 10 = 6位,7段 * 11 = 7位,6段 */ function set_display_mode(m) { // Sets STB to high. ioset('P1:|x80'); // Sets STB to low. ioset('P1:&x7f'); write_byte(m & 0x03); } /**< * s = display switch * 0 = close * 1 = open * g = display gray * 000: 设置脉冲宽度为1/16 * 001: 设置脉冲宽度为2/16 * 010: 设置脉冲宽度为4/16 * 011: 设置脉冲宽度为10/16 * 100: 设置脉冲宽度为11/16 * 101: 设置脉冲宽度为12/16 * 110: 设置脉冲宽度为13/16 * 111: 设置脉冲宽度为14/16 */ function set_display_ctrl(s, g) { // Sets STB to high. ioset('P1:|x80'); // Sets STB to low. ioset('P1:&x7f'); write_byte(0x80 | (s ? 0x08:0) | (g & 0x07)); } /**< * 数据读写模式设定: * 00:写数据到显示寄存器 * 10:读键扫数据 */ function set_io(m) { // Sets STB to high. ioset('P1:|x80'); // Sets STB to low. ioset('P1:&x7f'); write_byte(0x40 | (m & 0x03)); } function set_addr(a) { // Sets STB to high. ioset('P1:|x80'); // Sets STB to low. ioset('P1:&x7f'); write_byte(0xc0 | (a & 0x08)); }