由于魔方机器人魔方复原算法的命令长度是不确定的,但是每个命令都是由字符和数字组合而成规律。这样我们开辟一个较大的字符串数组,大小为100。接收完毕后将多余的部分和空格去掉,然后进行命令的解析(例子程序如下):          main.c代码:

  #include "Includes.h"                   //总头文件  //在此添加全局变量定义uint8 msg[14] = "Hello! World!";void PWM_Init(void){PWME_PWME3=0x00;   // Disable  PWM            禁止(通道3)             PWMPRCLK=0x33;     // 0011 0011 A=B=32M/8=4M  时钟预分频寄存器设置 PWMSCLA=200;       // SA=A/2/200=10k          时钟设置 PWMSCLB=200;       // SB=B/2/200 =10k          时钟设置                     PWMCLK_PCLK3=1;   // PWM3-----SB             时钟源的选择 PWMPOL_PPOL3=1;   // Duty=High Time          极性设置 PWMCAE_CAE3=0;    // Left-aligned            对齐方式设置 PWMCTL=0x00;      // no concatenation        控制寄存器设置  PWMPER3=200;     // Frequency=SB/200=50HZ   周期寄存器设置 PWMDTY3=15;     // 1.5ms对应的占空比       占空比寄存器设置 PWME_PWME3=1;     // Enable  PWM             使能 } void main(){//关总中断-------0.1DisableInterrupt(); //芯片初始化-----0.2     MCUInit(FBUS_32M);        //模块初始化-----0.3SCIInit(0,FBUS_32M,9600);  //串口0初始化//SCISendN(0,13,msg);        //发送"Hello!World!"// 开放中断------0.4EnableSCIReInt0;           //开放SCI0接收中断EnableInterrupt();         //开放总中断     PWM_Init();             //PWM初始化 1.5ms对应的占空比-----90°      while(1);//for_end(主循环结束)  } //main_end

isr.c 代码:

//--------------------------------------------------------------------------*
//文件名: isr.c                                                             *
//说  明: 中断处理函数,本文件包含:                                          *                                                             *
//        isr_default: 中断处理程序                                         *
//--------------------------------------------------------------------------*//头文件包含,及宏定义区//头文件包含#include "Includes.h"       //包含总头文件uint8 Serial[15];  //存放去除空格和0之后的字符串#pragma CODE_SEG __NEAR_SEG NON_BANKED//中断服务程序区//未定义的中断处理函数,本函数不能删除,默认__interrupt void isr_default(void){   DisableInterrupt();EnableInterrupt();}//串行接受中断__interrupt void SCI0_Recv(void){    int i=0;int j=0;int m=0;int k=0;uint8 SerialBuff[15];                       //存放从上位机接收到的字符串 for(i=0;i<15;i++) {SerialBuff[i]=0;}DisableInterrupt();                          //禁止总中断SCIReN(0,15,SerialBuff);                    //等待接收150个数据for(j=0;j<15;j++) { if((SerialBuff[j]!=' ')&&(SerialBuff[j]!=0)) {Serial[k]=SerialBuff[j];k++;}}for(m=0;m<k;) {if((Serial[m]=='F')&&(Serial[m+1]=='1')) {PWMDTY3=5;          //0.5ms对应的占空比------0°Delay(1000000);} else if((Serial[m]=='F')&&(Serial[m+1]=='2')) {PWMDTY3=15;          //1.5ms对应的占空比------90°Delay(1000000);} else if((Serial[m]=='F')&&(Serial[m+1]=='3')){PWMDTY3=25;          //2.5ms对应的占空比------180°Delay(1000000);} m=m+2;  }SCISendN(0,k-1,Serial);EnableInterrupt();                           //开放总中断}//复制上述默认中断处理函数isr_default(),作为模板而得到相应的中断程序#pragma CODE_SEG DEFAULT//中断矢量表对应区//中断处理子程序类型定义typedef void (*near tIsrFunc)(void);//中断矢量表,如果需要定义其它中断函数,请修改下表中的相应项目const tIsrFunc _InterruptVectorTable[] @0xFF10 = { // ISR name                     No.   Address  Pri Name            isr_default,                 // 0x08  0xFF10   -   ivVsi           isr_default,                 // 0x09  0xFF12   -   ivVsyscall      isr_default,                 // 0x0A  0xFF14   1   ivVReserved118  isr_default,                 // 0x0B  0xFF16   1   ivVReserved117  isr_default,                 // 0x0C  0xFF18   1   ivVReserved116  isr_default,                 // 0x0D  0xFF1A   1   ivVReserved115  isr_default,                 // 0x0E  0xFF1C   1   ivVReserved114  isr_default,                 // 0x0F  0xFF1E   1   ivVReserved113  isr_default,                 // 0x10  0xFF20   1   ivVReserved112  isr_default,                 // 0x11  0xFF22   1   ivVReserved111  isr_default,                 // 0x12  0xFF24   1   ivVReserved110  isr_default,                 // 0x13  0xFF26   1   ivVReserved109  isr_default,                 // 0x14  0xFF28   1   ivVReserved108  isr_default,                 // 0x15  0xFF2A   1   ivVReserved107  isr_default,                 // 0x16  0xFF2C   1   ivVReserved106  isr_default,                 // 0x17  0xFF2E   1   ivVReserved105  isr_default,                 // 0x18  0xFF30   1   ivVReserved104  isr_default,                 // 0x19  0xFF32   1   ivVReserved103  isr_default,                 // 0x1A  0xFF34   1   ivVReserved102  isr_default,                 // 0x1B  0xFF36   1   ivVReserved101  isr_default,                 // 0x1C  0xFF38   1   ivVReserved100  isr_default,                 // 0x1D  0xFF3A   1   ivVReserved99   isr_default,                 // 0x1E  0xFF3C   1   ivVReserved98   isr_default,                 // 0x1F  0xFF3E   1   ivVatd0compare  isr_default,                 // 0x20  0xFF40   1   ivVReserved96   isr_default,                 // 0x21  0xFF42   1   ivVReserved95   isr_default,                 // 0x22  0xFF44   1   ivVReserved94   isr_default,                 // 0x23  0xFF46   1   ivVReserved93   isr_default,                 // 0x24  0xFF48   1   ivVReserved92   isr_default,                 // 0x25  0xFF4A   1   ivVReserved91   isr_default,                 // 0x26  0xFF4C   1   ivVReserved90   isr_default,                 // 0x27  0xFF4E   1   ivVReserved89   isr_default,                 // 0x28  0xFF50   1   ivVReserved88   isr_default,                 // 0x29  0xFF52   1   ivVReserved87   isr_default,                 // 0x2A  0xFF54   1   ivVReserved86   isr_default,                 // 0x2B  0xFF56   1   ivVReserved85   isr_default,                 // 0x2C  0xFF58   1   ivVReserved84   isr_default,                 // 0x2D  0xFF5A   1   ivVReserved83   isr_default,                 // 0x2E  0xFF5C   1   ivVReserved82   isr_default,                 // 0x2F  0xFF5E   1   ivVReserved81   isr_default,                 // 0x30  0xFF60   1   ivVReserved79   isr_default,                 // 0x31  0xFF62   1   ivVReserved78   isr_default,                 // 0x32  0xFF64   1   ivVReserved77   isr_default,                 // 0x33  0xFF66   1   ivVReserved76   isr_default,                 // 0x34  0xFF68   1   ivVReserved75   isr_default,                 // 0x35  0xFF6A   1   ivVReserved74   isr_default,                 // 0x36  0xFF6C   1   ivVReserved73   isr_default,                 // 0x37  0xFF6E   1   ivVReserved72   isr_default,                 // 0x38  0xFF70   1   ivVReserved71   isr_default,                 // 0x39  0xFF72   1   ivVReserved70   isr_default,                 // 0x3A  0xFF74   1   ivVpit3         isr_default,                 // 0x3B  0xFF76   1   ivVpit2         isr_default,                 // 0x3C  0xFF78   1   ivVpit1         isr_default,                 // 0x3D  0xFF7A   1   ivVpit0         isr_default,                 // 0x3E  0xFF7C   1   ivVhti          isr_default,                 // 0x3F  0xFF7E   1   ivVapi          isr_default,                 // 0x40  0xFF80   1   ivVlvi          isr_default,                 // 0x41  0xFF82   1   ivVReserved62   isr_default,                 // 0x42  0xFF84   1   ivVReserved61   isr_default,                 // 0x43  0xFF86   1   ivVReserved60   isr_default,                 // 0x44  0xFF88   1   ivVReserved59   isr_default,                 // 0x45  0xFF8A   1   ivVReserved58   isr_default,                 // 0x46  0xFF8C   1   ivVpwmesdn      isr_default,                 // 0x47  0xFF8E   1   ivVportp        isr_default,                 // 0x48  0xFF90   1   ivVReserved55   isr_default,                 // 0x49  0xFF92   1   ivVReserved54   isr_default,                 // 0x4A  0xFF94   1   ivVReserved53   isr_default,                 // 0x4B  0xFF96   1   ivVReserved52   isr_default,                 // 0x4C  0xFF98   1   ivVReserved51   isr_default,                 // 0x4D  0xFF9A   1   ivVReserved50   isr_default,                 // 0x4E  0xFF9C   1   ivVReserved49   isr_default,                 // 0x4F  0xFF9E   1   ivVReserved48   isr_default,                 // 0x50  0xFFA0   1   ivVReserved47   isr_default,                 // 0x51  0xFFA2   1   ivVReserved46   isr_default,                 // 0x52  0xFFA4   1   ivVReserved45   isr_default,                 // 0x53  0xFFA6   1   ivVReserved44   isr_default,                 // 0x54  0xFFA8   1   ivVReserved43   isr_default,                 // 0x55  0xFFAA   1   ivVReserved42   isr_default,                 // 0x56  0xFFAC   1   ivVReserved41   isr_default,                 // 0x57  0xFFAE   1   ivVReserved40   isr_default,                 // 0x58  0xFFB0   1   ivVcan0tx       isr_default,                 // 0x59  0xFFB2   1   ivVcan0rx       isr_default,                 // 0x5A  0xFFB4   1   ivVcan0err      isr_default,                 // 0x5B  0xFFB6   1   ivVcan0wkup     isr_default,                 // 0x5C  0xFFB8   1   ivVflash        isr_default,                 // 0x5D  0xFFBA   1   ivVflashfd      isr_default,                 // 0x5E  0xFFBC   1   ivVReserved33   isr_default,                 // 0x5F  0xFFBE   1   ivVReserved32   isr_default,                 // 0x60  0xFFC0   1   ivVReserved31   isr_default,                 // 0x61  0xFFC2   1   ivVReserved30   isr_default,                 // 0x62  0xFFC4   1   ivVcrgscm       isr_default,                 // 0x63  0xFFC6   1   ivVcrgplllck    isr_default,                 // 0x64  0xFFC8   1   ivVReserved27   isr_default,                 // 0x65  0xFFCA   1   ivVReserved26   isr_default,                 // 0x66  0xFFCC   1   ivVporth        isr_default,                 // 0x67  0xFFCE   1   ivVportj        isr_default,                 // 0x68  0xFFD0   1   ivVReserved23   isr_default,                 // 0x69  0xFFD2   1   ivVatd0         isr_default,                 // 0x6A  0xFFD4   1   ivVsci1         SCI0_Recv,                   // 0x6B  0xFFD6   1   ivVsci0         isr_default,                 // 0x6C  0xFFD8   1   ivVspi0         isr_default,                 // 0x6D  0xFFDA   1   ivVtimpaie      isr_default,                 // 0x6E  0xFFDC   1   ivVtimpaaovf    isr_default,                 // 0x6F  0xFFDE   1   ivVtimovf       isr_default,                 // 0x70  0xFFE0   1   ivVtimch7       isr_default,                 // 0x71  0xFFE2   1   ivVtimch6       isr_default,                 // 0x72  0xFFE4   1   ivVtimch5       isr_default,                 // 0x73  0xFFE6   1   ivVtimch4       isr_default,                 // 0x74  0xFFE8   1   ivVtimch3       isr_default,                 // 0x75  0xFFEA   1   ivVtimch2       isr_default,                 // 0x76  0xFFEC   1   ivVtimch1       isr_default,                 // 0x77  0xFFEE   1   ivVtimch0       isr_default,                 // 0x78  0xFFF0   1   ivVrti          isr_default,                 // 0x79  0xFFF2   1   ivVirq          isr_default,                 // 0x7A  0xFFF4   -   ivVxirq         isr_default,                 // 0x7B  0xFFF6   -   ivVswi          isr_default                  // 0x7C  0xFFF8   -   ivVtrap           };                                      

这里为了调试方便,将字符串的长度定义为了15个,用串口调试助手发送舵机控制命令,测试通过,测试结果如下下                                                                                                       代码

魔方机器人之下位机编程----串口接收数据并解析相关推荐

  1. C#编程简单模拟与下位机的串口通信

    在学校里的团队里,遇到需要与下位机实现串口通信的项目,假期里查了很多资料,在此发一篇加深一下印象 需要: 电脑,vs软件(visual studio 2010) Serial Port Tester( ...

  2. 6.PMAC下位机-下位机编程

    PMAC的下位机编程包括三种程序:命令序列程序.运动(Program)程序和PLC程序. 如下图,PMAC中编写程序,直接在File->new新建文件会打开编辑器,所有文件为PMC后缀. 查看当 ...

  3. 5.PMAC下位机-下位机编程基础

    在开始下位机编程之前,先要了解下位机编程的基础-各种变量.运动参数和命令行操作. 变量 PMAC的下位机实际上是一种类Basic的编程语言,但是又类似汇编语言,所有变量的存储空间都是预先分配好的,操作 ...

  4. STM32如何从串口接收数据,取出数据中的密码段再保存到flash中。

    STM32如何从串口接收数据,取出数据中的密码段再保存到flash中 STM32如何从串口接收数据,取出数据中的密码段再保存到flash中u8 Usart2_buf[256]; unsigned in ...

  5. 【嵌入式】——串口实验——实现芯片串口收发数据,按键中断串口发送数据,串口接收数据中断来控制LED亮/灭

    实验目的: 实现芯片串口收发数据,按键中断串口发送数据:按下按键,向串口发送数据,并通过虚拟终端显示出来: 串口接收数据中断来控制LED亮/灭:通过串口助手向MCU发送数据,"A" ...

  6. 多串口接收数据并显示-自定义串口通信类-调用委托显示数据-保存数据-实例:10串口接收数据并显示加保存

    当时搜了很久,没咋都到合适的思路,就自己整理一份吧 大致实现的功能: 通过串口接收数据,进行解析,然后显示,或者保存数据: 主要:多个串口接收显示都能用: 大致思路: 1.新建一个类SerialPor ...

  7. 单片机串口接收数据的一些经验与教训

    一些好文章 一种单片机数据解析方法:https://mp.weixin.qq.com/s/GVH8MZrlbVKXanD45wOYOw 单片机串口接收数据需要注意以下几点 1.接收数据要有顺序,接收到 ...

  8. STM32MP157实现串口接收数据上云-MP157连接4G模块和电脑

    文章目录 前言 一.打开设备文件 二.串口初始化 三.多线程 四.程序的移植和执行 五.源码(转载请注明出处) 结语 前言 本篇分享: 这次将会用几篇博客分享STM32MP157实现串口接收数据上云的 ...

  9. 串口通信——串口接收数据,发送数据

    十六进制        HEX /hexadecimal   /ˌheksəˈdesɪml/ 十进制            DEC /decimalism     /'desiməlizəm/ 二进制 ...

  10. ROS实现串口GPS数据的解析与通信(这篇文章所用的代码和我买的带有ROS功能包的GPS模块的功能包的代码一样)

    我发现这篇文章所用的代码和我买的带有ROS功能包的GPS模块的功能包的代码一样!!! https://gitee.com/maxibooksiyi/gps_driver 转载自:https://blo ...

最新文章

  1. currenttimemillis 毫秒还是秒_Elasticsearch(ES)如何做到亿级数据查询毫秒级返回
  2. sphinx-release 2.1.4
  3. 趣味SQL——创建指定的数据类型
  4. asp网页设计流量统计代码
  5. STM32 ISP 下载程序, C源码,
  6. fast group合计 report_FastReport报表设计.doc
  7. SCCM 2016 配置管理系列(Part4)
  8. CVPR 2020 分方向论文大盘点合集
  9. python语音属于什么语言_python到底是什么类型的语言
  10. Treeview动态添加用户控件 取值和传值(第二种样式)
  11. c mysql事务回滚_MYSQL事务回滚的2个问题分析
  12. 毕设题目:Matlab优化选址
  13. 01_kubernetes初始化系统和全局变量
  14. RTSP视频流直播实现(海康)
  15. 分享一个基于labview的2048小游戏(附详细教程+代码)
  16. excel中的if函数使用方法
  17. 上海市高新技术企业各区奖励政策汇总2022版
  18. 传Apple开发TD版iPhone,使用Marvell方案
  19. CSS — 表格 和 表单
  20. Day5作业,商城+ATM机+后台管理

热门文章

  1. vins 解读_VINS 中的 IMU 预积分推导和代码解读
  2. PHP什么架构,PHP是什么-PHP的架构及道理概述_后端开发
  3. 变量名可以是python的关键字_python – 如何使用关键字作为变量名?
  4. formdata 嵌套_解决form嵌套
  5. 实现会员到期_微信电子会员卡管理系统
  6. Java编程:KMP算法
  7. Javascript:json数据根据某一个字段进行排序
  8. 实战Python:详解利用Python和Pygame实现飞机大战
  9. lan pci 联想开机_我的联想电脑开机老显示DHCP
  10. 愤怒的小鸟4只编外鸟_幼儿园小班游戏教案小鸟找食