在线时间6059 小时
UID3426478
注册时间2017-9-25
NXP金币5448
TA的每日心情 | 衰 半小时前 |
---|
签到天数: 1481 天 [LV.10]以坛为家III
超级版主
- 积分
- 92875
- 最后登录
- 2024-4-30
|
本帖最后由 stm1024 于 2019-12-12 09:47 编辑
仔细看手册就知道LPC51U68同样支持ISP。
先说一下如何进入ISP。先按住SW2(ISP0)不松手,然后再按住RESET键,两个键都按下的情况下,先送RESET,过个1-2秒再松SW2,这个时候就进入了串口的ISP。
如果打开串口调试助手,配置正确的话,发送“?”就可以看到ISP有回复“Synchronized”,然后再发送“OK”,ISP应答“OK”,表明同步完成。然后就可以按UUCode编码发送数据了。
为了完成串口的ISP,需要先实现UU编码的编码和解码。简单来说这个编码是将ASCII映射到ASCII的可打印区域,这样所有的原始字节流都可以通过此编码方式,用键盘敲出来后再解码,这种编码3个字节会变成4个字节,因此编码后膨胀了33%,解码方式就是反过来了。这个编码的说明见文档UART ISP的UU编码方式
https://www.nxpic.org.cn/document/detail/index/id-14538
有需要的可以自己去下载。
如果需要做自己的ISP上位机程序,就需要实现此编码功能,这里给出的是实现代码。
测试代码:
- using System;
- using System.Runtime.InteropServices;
- namespace UUCoder
- {
- class Program
- {
- [DllImport("Kernel32.dll")]
- public static extern bool Beep(int frequency, int duration);
- static byte[] GeneateRandomBytes(int cnt)
- {
- Random r = new Random(DateTime.Now.Millisecond);
- byte[] ret = new byte[cnt];
- for (int i = 0; i < cnt; i++)
- ret[i] = (byte)r.Next(0x00, 0xff);
- return ret;
- }
- static bool CompareBytes(byte[] b1,byte[] b2)
- {
- if (b1 == null && b2 == null)
- return true;
- if (b1 != null && b2 == null)
- return false;
- if (b2 != null && b1 == null)
- return false;
- if (b1.Length != b2.Length)
- return false;
- int xsum = 0;
- for(int i=0;i<b1.Length;i++)
- {
- xsum += b1[i] ^ b2[i];
- }
- if (xsum == 0)
- return true;
- else
- return false;
- }
- static void Main(string[] args)
- {
-
- for (int t=0;t<255;t++)
- {
- byte[] b= GeneateRandomBytes(t);
- PrintBytes(b);
- string s = UUCoder.Encode(b);
- Console.Write(s);
- byte[] bd = UUCoder.Decode(s);
- PrintBytes(bd);
- if (CompareBytes(b, bd))
- {
- Console.WriteLine("Match!");
- }
- else
- {
- Console.WriteLine("Mismatch!");
- Beep(4000, 80);
- }
- Console.WriteLine("==================================================");
- }
- }
- static void PrintBytes(byte[] b)
- {
- for (int i = 0; i < b.Length; i++)
- Console.Write("0x" + b[i].ToString("X02") + " ");
- Console.WriteLine();
- }
- }
复制代码 运行效果:
|
|