刚接触单片机,我也有这个疑问,网上搜索的码表和自己的情况不同,因为大家IO角映射是不一样的,把别人的拿来用不一定适合自己,实际上只要搞懂了原理,针脚是可以乱插的,我自己写了html工具,就可以乱插针脚(自己配置段码和IO针脚映射),然后自动计算码表。但是我还没实际实验,有空的时候实验下。
- <html>
- <head>
- <meta charset="utf-8">
- <style type="text/css">
- table.gridtable {
- font-family: verdana,arial,sans-serif;
- font-size:11px;
- color:#333333;
- border-width: 1px;
- border-color: #666666;
- border-collapse: collapse;
- }
- table.gridtable th {
- border-width: 1px;
- padding: 8px;
- border-style: solid;
- border-color: #666666;
- background-color: #dedede;
- }
- table.gridtable td {
- border-width: 1px;
- padding: 8px;
- border-style: solid;
- border-color: #666666;
- background-color: #ffffff;
- }
- table.gridtable caption {
- border-width: 1px 1px 0px 1px;
- padding: 8px;
- border-style: solid;
- border-color: #666666;
- font-weight: bold;
- background-color: #dedede;
- }
- pre {
- background:#f5f5f5;
- color:#333;
- padding:10px;
- border: 1px solid #ccc;
- font-size: 13px;
- line-height: 1.4;
- font-family: Menlo,Monaco,Consolas,"Courier New",monospace;
- border-radius: 4px;
- overflow: auto;
- }
- </style>
- <script src="https://cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
- </head>
- <body>
- <table class="gridtable" name="pin">
- <caption>数码管配置</caption>
- <tr>
- <th>段码</th><th>段脚</th><th>IO脚</th>
- </tr>
- <tr>
- <td>a</td> <td>1</td>
- <td><select value="0" name="a"></select></td>
- </tr>
- <tr>
- <td>b</td> <td>2</td> <td><select value="1" name="b"></select></td>
- </tr>
- <tr>
- <td>c</td> <td>4</td> <td><select value="2" name="c"></select></td>
- </tr>
- <tr>
- <td>d</td> <td>5</td> <td><select value="3" name="d"></select></td>
- </tr>
- <tr>
- <td>e</td> <td>6</td> <td><select value="4" name="e"></select></td>
- </tr>
- <tr>
- <td>f</td> <td>7</td> <td><select value="5" name="f"></select></td>
- </tr>
- <tr>
- <td>g</td> <td>9</td> <td><select value="6" name="g"></select></td>
- </tr>
- <tr>
- <td>p</td><td>10</td><td><select value="7" name="p"></select></td>
- </tr>
- </table>
- <br/>
- 字符与段码映射表:<input type="button" value="计算" id="calc"></input><br/><br/>
- <textarea style="height:100px;" id="code_map">
- 0:abcdef
- 1:bc
- 2:abged
- 3:abgcd
- 4:fgcb
- 5:afgcd
- 6:afeddcg
- 7:abc
- 8:abcdefg
- 9:gfabcd
- A:efabcg
- b:fedcg
- C:afed
- D:gcde
- E:afedg
- F:afeg
- </textarea>
- <pre id="code">
- </pre>
- <script>
- var template = "{7}{6}{5}{4}{3}{2}{1}{0}";
- var option = "";
- for(var i=0;i<8;i++){
- option += '<option value="'+i+'">'+i+'</option>';
- }
- $("table[name=pin] select").each(function(idx, ele){
- $(ele).html(option);
- $(ele).val(idx);
- });
- function parse(){
- var code_map_list = $("#code_map").val().split("\n");
- var pin_map = {};
- $("table[name=pin] select[name]").each(function(){
- var _this = $(this);
- pin_map[_this.attr('name')] = _this.val();
- });
- var table_positive = [];
- var table_negative = [];
- var char_positive = [];
- var char_negative = [];
- $.each(code_map_list,function(idx,code_map){
- code_map = $.trim(code_map);
- var split_idx = code_map.indexOf(":");
- if(code_map == "" || split_idx == -1){return;}
- var _c = code_map.split(":");
- var _char = _c[0];
- var _map_list = _c[1].split("");
- var bin_positive = template;//共阳极,低电平有效
- var bin_negative = template;//共阴极,高电平有效
- $.each(_map_list,function(_idx, _map){
- var pin = pin_map[_map];
- if(pin == undefined){alert("错误的段码:"+_map);throw "错误的段码:"+_map;}
- bin_positive = bin_positive.replace("{"+pin+"}",0);
- bin_negative = bin_negative.replace("{"+pin+"}",1);
- });
- bin_positive = bin_positive.replace(/\{\d\}/g,1);
- bin_negative = bin_negative.replace(/\{\d\}/g,0);
- var _p = parseInt(bin_positive,2).toString(16);
- var _n = parseInt(bin_negative,2).toString(16);
- table_positive.push(_p);
- table_negative.push(_n);
- char_positive.push({c:_char,hex:_p});
- char_negative.push({c:_char,hex:_n});
- });
-
- var text = "//共阳码表\nchar code table_positive[] = {";
- $.each(table_positive,function(idx,hex){
- text += idx == 0 ? "0x" + hex : ", 0x" + hex;
- });
- text += "};\n";
-
- text += "//共阴码表\nchar code table_negative[] = {";
- $.each(table_negative,function(idx,hex){
- text += idx == 0 ? "0x" + hex : ", 0x" + hex;
- });
- text += "};\n\n";
-
- $.each(char_positive,function(idx,char_map){
- text += "char code positive_"+char_map['c'] + " = 0x" + char_map['hex'] + ";\n";
- });
- text += "\n";
-
- $.each(char_negative,function(idx,char_map){
- text += "char code negative_"+char_map['c'] + " = 0x" + char_map['hex'] + ";\n";
- });
-
- console.info(text);
- $("pre#code").text(text);
- }
- parse();
- $("#calc").click(parse);
- </script>
- </body>
- </html>
复制代码 |