|
@@ -66,7 +66,7 @@ void InitUart3(void) {
|
|
|
U3MODEbits.LPBACK = 0;
|
|
U3MODEbits.LPBACK = 0;
|
|
|
|
|
|
|
|
// U1BRG = 31;
|
|
// U1BRG = 31;
|
|
|
- U3BRG = 389;
|
|
|
|
|
|
|
+ U3BRG = 31;
|
|
|
|
|
|
|
|
U3STAbits.UTXISEL0 = 1;
|
|
U3STAbits.UTXISEL0 = 1;
|
|
|
U3STAbits.UTXISEL1 = 0;
|
|
U3STAbits.UTXISEL1 = 0;
|
|
@@ -1313,7 +1313,7 @@ void Uart1_Ack(void)
|
|
|
|
|
|
|
|
if(g_ucU1RX_Data[4] == 253){
|
|
if(g_ucU1RX_Data[4] == 253){
|
|
|
_RP97R = 0x01;
|
|
_RP97R = 0x01;
|
|
|
- _U1RXR = 0x70;
|
|
|
|
|
|
|
+ _U1RXR = 0x6D;
|
|
|
STU2 = 0;
|
|
STU2 = 0;
|
|
|
}
|
|
}
|
|
|
else if(g_ucU1RX_Data[4] == 252){
|
|
else if(g_ucU1RX_Data[4] == 252){
|
|
@@ -1638,4 +1638,293 @@ void Uart1_Ack(void)
|
|
|
g_ucHeaderFlag1 = 0;
|
|
g_ucHeaderFlag1 = 0;
|
|
|
memset(g_ucU1RX_Data,0,g_uiLth1*sizeof(unsigned char));
|
|
memset(g_ucU1RX_Data,0,g_uiLth1*sizeof(unsigned char));
|
|
|
g_BUSY1 = 0;
|
|
g_BUSY1 = 0;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+uint32_t m_pow_n(uint32_t m, uint32_t n)
|
|
|
|
|
+{
|
|
|
|
|
+ uint32_t i = 0, ret = 1;
|
|
|
|
|
+ if (n < 0) return 0;
|
|
|
|
|
+ for (i = 0; i < n; i++)
|
|
|
|
|
+ {
|
|
|
|
|
+ ret *= m;
|
|
|
|
|
+ }
|
|
|
|
|
+ return ret;
|
|
|
|
|
+}
|
|
|
|
|
+void my_send_char(char ch){
|
|
|
|
|
+ g_ucU3TX_buf[g_ucU3TX_Last++] = ch;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// 返回值为打印字符的个数
|
|
|
|
|
+// 支持%d,%o, %x,%s,%c,%f(只打印6位数字)
|
|
|
|
|
+int my_printf(const char* str, ...)
|
|
|
|
|
+{
|
|
|
|
|
+ if (str == NULL) return -1;
|
|
|
|
|
+
|
|
|
|
|
+ uint32_t ret_num = 0;// 返回打印字符的个数
|
|
|
|
|
+ char* pStr = (char*)str;// 指向str
|
|
|
|
|
+ int ArgIntVal = 0; // 接收整型
|
|
|
|
|
+ uint32_t ArgHexVal = 0;// 接十六进制
|
|
|
|
|
+ char* ArgStrVal = NULL; // 接收字符型
|
|
|
|
|
+ double ArgFloVal = 0.0; // 接受浮点型
|
|
|
|
|
+ uint32_t val_seg = 0; // 数据切分
|
|
|
|
|
+ uint32_t val_temp = 0; // 临时保存数据
|
|
|
|
|
+ int cnt = 0; // 数据长度计数
|
|
|
|
|
+ int cnt_float = 0; // 数据长度计数
|
|
|
|
|
+ int i = 0;
|
|
|
|
|
+
|
|
|
|
|
+ va_list pArgs; // 定义va_list类型指针,用于存储参数的地址
|
|
|
|
|
+ va_start(pArgs, str); // 初始化pArgs
|
|
|
|
|
+ if(g_ucU3TX_Last > 0){
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ while (*pStr != '\0')
|
|
|
|
|
+ {
|
|
|
|
|
+ switch (*pStr)
|
|
|
|
|
+ {
|
|
|
|
|
+ case ' ':
|
|
|
|
|
+ case '\r':
|
|
|
|
|
+ case '\n':
|
|
|
|
|
+ my_send_char(*pStr); ret_num++; break;
|
|
|
|
|
+ case '\t':
|
|
|
|
|
+ my_send_char(*pStr); ret_num += 4; break;
|
|
|
|
|
+ case '%':
|
|
|
|
|
+ pStr++;
|
|
|
|
|
+ // % 格式解析
|
|
|
|
|
+
|
|
|
|
|
+ if('.' == *pStr)
|
|
|
|
|
+ {
|
|
|
|
|
+ pStr++;
|
|
|
|
|
+ cnt_float = *pStr - '0';// %.Nf,输出指定位数小数
|
|
|
|
|
+ pStr++;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ switch (*pStr)
|
|
|
|
|
+ {
|
|
|
|
|
+ case '%':
|
|
|
|
|
+ my_send_char('%');// %%,输出%
|
|
|
|
|
+ ret_num++;
|
|
|
|
|
+ pStr++;
|
|
|
|
|
+ continue;
|
|
|
|
|
+ case 'c':
|
|
|
|
|
+ ArgIntVal = va_arg(pArgs, int);// %c,输出char
|
|
|
|
|
+ my_send_char((char)ArgIntVal);
|
|
|
|
|
+ ret_num++;
|
|
|
|
|
+ pStr++;
|
|
|
|
|
+ continue;
|
|
|
|
|
+ case 'd':
|
|
|
|
|
+ // 接收整型
|
|
|
|
|
+ ArgIntVal = va_arg(pArgs, int);
|
|
|
|
|
+ if (ArgIntVal < 0)// 如果为负数打印,负号
|
|
|
|
|
+ {
|
|
|
|
|
+ ArgIntVal = -ArgIntVal;// 取相反数
|
|
|
|
|
+
|
|
|
|
|
+ my_send_char('-');
|
|
|
|
|
+ ret_num++;
|
|
|
|
|
+ }
|
|
|
|
|
+ val_seg = ArgIntVal;// 赋值给 val_seg处理数据
|
|
|
|
|
+ // 计算ArgIntVal长度
|
|
|
|
|
+ if (ArgIntVal)
|
|
|
|
|
+ {
|
|
|
|
|
+ while (val_seg) {
|
|
|
|
|
+ cnt++;
|
|
|
|
|
+ val_seg /= 10;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ else cnt = 1;// 数字0的长度为1
|
|
|
|
|
+
|
|
|
|
|
+ ret_num += cnt;// 字符个数加上整数的长度
|
|
|
|
|
+
|
|
|
|
|
+ // 将整数转为单个字符打印
|
|
|
|
|
+ while (cnt)
|
|
|
|
|
+ {
|
|
|
|
|
+ val_seg = ArgIntVal / m_pow_n(10, cnt - 1);
|
|
|
|
|
+ ArgIntVal %= m_pow_n(10, cnt - 1);
|
|
|
|
|
+ my_send_char((char)val_seg + '0');
|
|
|
|
|
+ cnt--;
|
|
|
|
|
+ }
|
|
|
|
|
+ pStr++;
|
|
|
|
|
+ continue;
|
|
|
|
|
+ case 'o':
|
|
|
|
|
+ // 接收整型
|
|
|
|
|
+ ArgIntVal = va_arg(pArgs, int);
|
|
|
|
|
+ if (ArgIntVal < 0)// 如果为负数打印,负号
|
|
|
|
|
+ {
|
|
|
|
|
+ ArgIntVal = -ArgIntVal;// 取相反数
|
|
|
|
|
+
|
|
|
|
|
+ my_send_char('-');
|
|
|
|
|
+ ret_num++;
|
|
|
|
|
+ }
|
|
|
|
|
+ val_seg = ArgIntVal;// 赋值给 val_seg处理数据
|
|
|
|
|
+ // 计算ArgIntVal长度
|
|
|
|
|
+ if (ArgIntVal)
|
|
|
|
|
+ {
|
|
|
|
|
+ while (val_seg) {
|
|
|
|
|
+ cnt++;
|
|
|
|
|
+ val_seg /= 8;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ else cnt = 1;// 数字0的长度为1
|
|
|
|
|
+
|
|
|
|
|
+ ret_num += cnt;// 字符个数加上整数的长度
|
|
|
|
|
+
|
|
|
|
|
+ // 将整数转为单个字符打印
|
|
|
|
|
+ while (cnt)
|
|
|
|
|
+ {
|
|
|
|
|
+ val_seg = ArgIntVal / m_pow_n(8, cnt - 1);
|
|
|
|
|
+ ArgIntVal %= m_pow_n(8, cnt - 1);
|
|
|
|
|
+ my_send_char((char)val_seg + '0');
|
|
|
|
|
+ cnt--;
|
|
|
|
|
+ }
|
|
|
|
|
+ pStr++;
|
|
|
|
|
+ continue;
|
|
|
|
|
+ case 'x':
|
|
|
|
|
+ // 接收16进制
|
|
|
|
|
+ ArgHexVal = va_arg(pArgs, uint32_t);
|
|
|
|
|
+ val_seg = ArgHexVal;
|
|
|
|
|
+ // 计算ArgIntVal长度
|
|
|
|
|
+ if (ArgHexVal)
|
|
|
|
|
+ {
|
|
|
|
|
+ while (val_seg) {
|
|
|
|
|
+ cnt++;
|
|
|
|
|
+ val_seg /= 16;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ else cnt = 1;// 数字0的长度为1
|
|
|
|
|
+
|
|
|
|
|
+ ret_num += cnt;// 字符个数加上整数的长度
|
|
|
|
|
+ // 将整数转为单个字符打印
|
|
|
|
|
+ while (cnt)
|
|
|
|
|
+ {
|
|
|
|
|
+ val_seg = ArgHexVal / m_pow_n(16, cnt - 1);
|
|
|
|
|
+ ArgHexVal %= m_pow_n(16, cnt - 1);
|
|
|
|
|
+ if (val_seg <= 9)
|
|
|
|
|
+ my_send_char((char)val_seg + '0');
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ //my_send_char((char)val_seg - 10 + 'a'); //小写字母
|
|
|
|
|
+ my_send_char((char)val_seg - 10 + 'A');
|
|
|
|
|
+ }
|
|
|
|
|
+ cnt--;
|
|
|
|
|
+ }
|
|
|
|
|
+ pStr++;
|
|
|
|
|
+ continue;
|
|
|
|
|
+ case 'b':
|
|
|
|
|
+ // 接收整型
|
|
|
|
|
+ ArgIntVal = va_arg(pArgs, int);
|
|
|
|
|
+ val_seg = ArgIntVal;
|
|
|
|
|
+ // 计算ArgIntVal长度
|
|
|
|
|
+ if (ArgIntVal)
|
|
|
|
|
+ {
|
|
|
|
|
+ while (val_seg) {
|
|
|
|
|
+ cnt++;
|
|
|
|
|
+ val_seg /= 2;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ else cnt = 1;// 数字0的长度为1
|
|
|
|
|
+
|
|
|
|
|
+ ret_num += cnt;// 字符个数加上整数的长度
|
|
|
|
|
+ // 将整数转为单个字符打印
|
|
|
|
|
+ while (cnt)
|
|
|
|
|
+ {
|
|
|
|
|
+ val_seg = ArgIntVal / m_pow_n(2, cnt - 1);
|
|
|
|
|
+ ArgIntVal %= m_pow_n(2, cnt - 1);
|
|
|
|
|
+ my_send_char((char)val_seg + '0');
|
|
|
|
|
+ cnt--;
|
|
|
|
|
+ }
|
|
|
|
|
+ pStr++;
|
|
|
|
|
+ continue;
|
|
|
|
|
+ case 's':
|
|
|
|
|
+ // 接收字符
|
|
|
|
|
+ ArgStrVal = va_arg(pArgs, char*);
|
|
|
|
|
+ ret_num += (uint32_t)strlen(ArgStrVal);
|
|
|
|
|
+ while (*ArgStrVal)
|
|
|
|
|
+ {
|
|
|
|
|
+ my_send_char(*ArgStrVal);
|
|
|
|
|
+ ArgStrVal++;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ pStr++;
|
|
|
|
|
+ continue;
|
|
|
|
|
+
|
|
|
|
|
+ case 'f':
|
|
|
|
|
+ // 接收浮点型 保留6为小数,不采取四舍五入
|
|
|
|
|
+ ArgFloVal = va_arg(pArgs, double);
|
|
|
|
|
+
|
|
|
|
|
+ if (ArgFloVal < 0)// 如果为负数打印,负号
|
|
|
|
|
+ {
|
|
|
|
|
+ ArgFloVal = -ArgFloVal;// 取相反数
|
|
|
|
|
+
|
|
|
|
|
+ my_send_char('-');
|
|
|
|
|
+ ret_num++;
|
|
|
|
|
+ }
|
|
|
|
|
+ val_seg = (uint32_t)ArgFloVal;// 取整数部分
|
|
|
|
|
+ val_temp = val_seg; // 临时保存整数部分数据
|
|
|
|
|
+ ArgFloVal = ArgFloVal - val_seg;// 得出余下的小数部分
|
|
|
|
|
+ // 计算整数部分长度
|
|
|
|
|
+ if (val_seg)
|
|
|
|
|
+ {
|
|
|
|
|
+ while (val_seg) {
|
|
|
|
|
+ cnt++;
|
|
|
|
|
+ val_seg /= 10;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ else cnt = 1;// 数字0的长度为1
|
|
|
|
|
+ ret_num += cnt;// 字符个数加上整数的长度
|
|
|
|
|
+ // 将整数转为单个字符打印
|
|
|
|
|
+ while (cnt)
|
|
|
|
|
+ {
|
|
|
|
|
+ val_seg = val_temp / m_pow_n(10, cnt - 1);
|
|
|
|
|
+ val_temp %= m_pow_n(10, cnt - 1);
|
|
|
|
|
+ my_send_char((char)val_seg + '0');
|
|
|
|
|
+ cnt--;
|
|
|
|
|
+ }
|
|
|
|
|
+ // 打印小数点
|
|
|
|
|
+ my_send_char('.');
|
|
|
|
|
+ ret_num++;
|
|
|
|
|
+ // printf("\r\n %f\r\n", ArgFloVal);
|
|
|
|
|
+ if(cnt_float == 0)
|
|
|
|
|
+ cnt_float = 6;
|
|
|
|
|
+
|
|
|
|
|
+ // 开始输出小数部分
|
|
|
|
|
+ //ArgFloVal *= 1000000;
|
|
|
|
|
+ ArgFloVal *= m_pow_n(10, cnt_float);
|
|
|
|
|
+
|
|
|
|
|
+ val_temp = (int)ArgFloVal;// 取整数部分
|
|
|
|
|
+ while (cnt_float)
|
|
|
|
|
+ {
|
|
|
|
|
+ val_seg = val_temp / m_pow_n(10, cnt_float - 1);
|
|
|
|
|
+ val_temp %= m_pow_n(10, cnt_float - 1);
|
|
|
|
|
+ my_send_char((char)val_seg + '0');
|
|
|
|
|
+ cnt_float--;
|
|
|
|
|
+ }
|
|
|
|
|
+ ret_num += 6;
|
|
|
|
|
+ pStr++;
|
|
|
|
|
+ continue;
|
|
|
|
|
+ default:// % 匹配错误,暂输出空格
|
|
|
|
|
+ my_send_char(' '); ret_num++;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ default:
|
|
|
|
|
+ my_send_char(*pStr); ret_num++;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ pStr++;
|
|
|
|
|
+ }
|
|
|
|
|
+ va_end(pArgs);// 结束取参数
|
|
|
|
|
+
|
|
|
|
|
+ for(int i;i<ret_num;i++){
|
|
|
|
|
+ g_ucU3TX_Data[i] = g_ucU3TX_buf[i];
|
|
|
|
|
+ }
|
|
|
|
|
+ g_ucU3TX_Last = 0;
|
|
|
|
|
+ while(DMA2CONbits.CHEN);
|
|
|
|
|
+ DMA2CNT=(ret_num-1);
|
|
|
|
|
+ DMA2CONbits.CHEN = 1;
|
|
|
|
|
+ DMA2REQbits.FORCE = 1;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ return ret_num;
|
|
|
}
|
|
}
|