Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在单片机上调试C程序_C_Crash_Serial Port_Microcontroller - Fatal编程技术网

如何在单片机上调试C程序

如何在单片机上调试C程序,c,crash,serial-port,microcontroller,C,Crash,Serial Port,Microcontroller,这是我的第一个C代码。所以,我写了一个代码,通过串行端口从微控制器(pololu maestro)的5个电位器获取值,然后从5个电位器值中移动10个伺服器。当我在代码块中编译和运行代码时,没有错误或警告,但是当我运行它时,它崩溃了。它说我的.exe文件已停止运行 // Uses POSIX functions to send and receive data from a Maestro. // NOTE: The Maestro's serial mode must be set to "US

这是我的第一个C代码。所以,我写了一个代码,通过串行端口从微控制器(pololu maestro)的5个电位器获取值,然后从5个电位器值中移动10个伺服器。当我在代码块中编译和运行代码时,没有错误或警告,但是当我运行它时,它崩溃了。它说我的.exe文件已停止运行

// Uses POSIX functions to send and receive data from a Maestro.
// NOTE: The Maestro's serial mode must be set to "USB Dual Port".
// NOTE: You must change the 'const char * device' line below.

#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>

#ifdef _WIN32
#define O_NOCTTY 0
#else
#include <termios.h>
#endif

// Gets the position of a Maestro channel.
// See the "Serial Servo Commands" section of the user's guide.
int maestroGetPosition(int fd, unsigned char channel)
{
  unsigned char command[] = {0x90, channel};
  if(write(fd, command, sizeof(command)) == -1)
  {
    perror("error writing");
    return -1;
  }

  unsigned char response[2];
  if(read(fd,response,2) != 2)
  {
    perror("error reading");
    return -1;
  }

  return response[0] + 256*response[1];
}

// Sets the target of a Maestro channel.
// See the "Serial Servo Commands" section of the user's guide.
// The units of 'target' are quarter-microseconds.
int maestroSetTarget(int fd, unsigned char channel, unsigned short target1, unsigned short target2, unsigned short target3, unsigned short target4, unsigned short target5, unsigned short target6, unsigned short target7, unsigned short target8, unsigned short target9, unsigned short target10)
{
  unsigned char command[] = {170, 12, 31, 10, channel, target1 & 0x7F, target1 >> 7 & 0x7F, target2 & 0x7F, target2 >> 7 & 0x7F, target3 & 0x7F, target3 >> 7 & 0x7F, target4 & 0x7F, target4 >> 7 & 0x7F, target5 & 0x7F, target5 >> 7 & 0x7F, target6 & 0x7F, target6 >> 7 & 0x7F, target7 & 0x7F, target7 >> 7 & 0x7F, target8 & 0x7F, target8 >> 7 & 0x7F, target9 & 0x7F, target9 >> 7 & 0x7F, target10 & 0x7F, target10 >> 7 & 0x7F};
  if (write(fd, command, sizeof(command)) == -1)
  {
    perror("error writing");
    return -1;
  }
  return 0;
}

/////////////////////////////////////////////////////////////////////////////////

int main()
{
  // Open the Maestro's virtual COM port.
  const char * device = "\\\\.\\COM8";  // Windows, "\\\\.\\COM6" also works
  //const char * device = "/dev/ttyACM0";  // Linux
  //const char * device = "/dev/cu.usbmodem00034567"; // Mac OS X
  int fd = open(device, O_RDWR | O_NOCTTY);
  if (fd == -1)
  {
    perror(device);
    return 1;
  }

#ifndef _WIN32
  struct termios options;
  tcgetattr(fd, &options);
  options.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  options.c_oflag &= ~(ONLCR | OCRNL);
  tcsetattr(fd, TCSANOW, &options);
#endif

int count1;
int b_pos;
int b_l;
int x[10];
int rel;
int count2;
int count3;
int count4;
int x0[10];
int x1;
int x2;
int x3;
int x4;
int x5;

for (count1=1; count1<=1000; count1++){

    x1 = maestroGetPosition(fd, 0);
    x2 = maestroGetPosition(fd, 1);
    x3 = maestroGetPosition(fd, 2);
    x4 = maestroGetPosition(fd, 3);
    x5 = maestroGetPosition(fd, 4);

/////////////////////////////////////////////////////////////////////////////////

    if(x1 == 0){
        b_pos = 0;
    }else if( (x1 <= 102) && (x1 > 0) ){
        b_pos = 1;
    }else if( (x1 <= 102) && (x1 > 0) ){
        b_pos = 2;
    }else if( (x1 <= 102) && (x1 > 0) ){
        b_pos = 3;
    }else if( (x1 <= 102) && (x1 > 0) ){
        b_pos = 4;
    }else if( (x1 <= 102) && (x1 > 0) ){
        b_pos = 5;
    }else if( (x1 <= 102) && (x1 > 0) ){
        b_pos = 6;
    }else if( (x1 <= 102) && (x1 > 0) ){
        b_pos = 7;
    }else if( (x1 <= 102) && (x1 > 0) ){
        b_pos = 8;
    }else if( (x1 <= 102) && (x1 > 0) ){
        b_pos = 9;
    }else if( (x1 <= 102) && (x1 > 0) ){
        b_pos = 10;
    }

/////////////////////////////////////////////////////////////////////////////////

    if(b_pos != 0){
        if (x2 <= 255){
            b_l = 1;
        }else if( (x2 <= 510) && (x2 >= 256) ){
            b_l = 2;
        }else if( (x2 <= 765) && (x2 >= 511) ){
            b_l = 3;
        }else{
            b_l = 4;
        }
    }

/////////////////////////////////////////////////////////////////////////////////

    x3 = (x3*0.988)+992;
    for (count4 = 1; count4 <= 10; count4++){
        x[count4] = x3;
    }

/////////////////////////////////////////////////////////////////////////////////

    if(b_pos != 0){
        x4 = (x4*0.39)-200;
        x5 = (x5*0.098);
        x[b_pos] = x[b_pos] + x4;
        rel = 1;

        if (x4 >= 0){
            for (count2 = b_pos; count2<=( (b_pos) + (b_l) ); count2++){
                        if ((count2 > 0) && (count2 < 11)){
                            x[count2] = x[count2] + x4  + ( (x4/abs(x4))*(x5*rel) );
                            rel += 1;
                        }
            }
            for (count2 = b_pos; count2<=( (b_pos) - (b_l) ); count2--){
                        if ((count2 > 0) && (count2 < 11)){
                            x[count2] = x[count2] + x4  + ( (x4/abs(x4))*(x5*rel) );
                            rel += 1;
                        }
            }
        }
    }

/////////////////////////////////////////////////////////////////////////////////


    for (count3 = 1; count3 <= 10; count3++){
        if (x[count3] < 992){
            x[count3] = 992;
        }
        if (x[count3] > 2000){
            x[count3] = 2000;
        }
        x0[count3] = (x[count3] * 4);
    }


  //int target = (position < 6000) ? 7000 : 5000;
  //printf("Setting target to %d (%d us).\n", target, target/4);
  maestroSetTarget(fd, 7, x0[1], x0[2], x0[3], x0[4], x0[5], x0[6], x0[7], x0[8], x0[9], x0[10]);

}

return 0;

close(fd);

}
//使用POSIX函数从Maestro发送和接收数据。
//注意:Maestro的串行模式必须设置为“USB双端口”。
//注意:您必须更改下面的“const char*device”行。
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#ifdef_WIN32
#定义O_NOCTTY 0
#否则
#包括
#恩迪夫
//获取Maestro通道的位置。
//请参阅《用户指南》的“串行伺服命令”部分。
int maestroGetPosition(int fd,无符号字符通道)
{
无符号字符命令[]={0x90,通道};
if(写入(fd,command,sizeof(command))=-1)
{
perror(“错误写入”);
返回-1;
}
无符号字符响应[2];
如果(读取(fd,响应,2)!=2)
{
perror(“错误读取”);
返回-1;
}
返回响应[0]+256*响应[1];
}
//设置大师频道的目标。
//请参阅《用户指南》的“串行伺服命令”部分。
//“目标”的单位是四分之一微秒。
int maestroSetTarget(int fd、无符号字符通道、无符号短target1、无符号短target2、无符号短target3、无符号短target4、无符号短target5、无符号短target6、无符号短target7、无符号短target8、无符号短target9、无符号短target10)
{
无符号字符命令[]={170,12,31,10,频道,target1和0x7F,target1>>7和0x7F,target2和0x7F,target2>>7和0x7F,target3和0x7F,target3>>7和0x7F,target4和0x7F,target4>>7和0x7F,target5和0x7F,target5>>7和0x7F,target6>>7和0x7F,target7和0x7F,target8和0x7F,TARGET7F>>7&0x7F,target10&0x7F,target10>>7&0x7F};
if(写入(fd,command,sizeof(command))=-1)
{
perror(“错误写入”);
返回-1;
}
返回0;
}
/////////////////////////////////////////////////////////////////////////////////
int main()
{
//打开Maestro的虚拟COM端口。
const char*device=“\\\\.\\COM8”//Windows“\\\\\\.\\COM6”也可以工作
//const char*device=“/dev/ttyACM0”;//Linux
//const char*device=“/dev/cu.usbmodem00034567”;//Mac OS X
int fd=打开(设备,O|RDWR | O|NOCTTY);
如果(fd==-1)
{
perror(装置);
返回1;
}
#ifndef\u WIN32
结构termios选项;
tcgetattr(fd和选项);
options.c|lflag&=~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
选项c(标签的U值)&=~(ONLCR | ocrl);
tcsetattr(fd、TCSANOW和选项);
#恩迪夫
int count1;
国际邮政局;
国际图书馆;
int x[10];
国际关系;
int count2;
int count3;
int count4;
int x0[10];
int-x1;
int-x2;
int x3;
int x4;
int x5;
对于(count1=1;count1您有

x[b_pos] = x[b_pos] + x4;

如果
b_pos
的值可能为
10
,但
x
只能容纳
10
元素,那么访问
11
第个元素会导致未定义的行为,而这反过来又会使程序崩溃。

script
?哇……-)你在你的电脑上以代码块的形式运行代码?你有微控制器的模拟器吗?如果没有,我怀疑你是否有足够的时间来测试。在你的电脑上运行你的代码。你必须在微控制器上运行。你可能还想了解如何在微控制器上调试你的代码。好吧,所以我将大小改为11,它仍然崩溃。伙计们,g还有什么建议吗?我并不惊讶,我建议使用调试器,如果你不知道怎么做,网上有很多资料。