找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 279|回复: 5
打印 上一主题 下一主题
收起左侧

程序写入51单片机后,单片机没有按照预期运行

[复制链接]
回帖奖励 10 黑币 回复本帖可获得 1 黑币奖励! 每人限 1 次
跳转到指定楼层
楼主
如图所示,本人使用的江协科技的51单片机,烧录成功后单片机没有按照程序运行。蜂鸣器持续响起,LED全亮。


本人排查了好久都没有找到问题,望各位专家解答。
部分代码如下:
main.c:
  1. #include <REGX52.H>
  2. #include "Delay.h"
  3. #include "ADC0832.h"
  4. #include<stdio.h>
  5. #include "uart.h"
  6. #define uchar unsigned char
  7. #define uint unsigned int
  8. #define false 0
  9. #define true 1
  10. unsigned int AD_X;
  11. unsigned int AD_Y;

  12. //Unicode
  13. char Unicode_latitude[10*latitude_Length]='0';
  14. char Unicode_N_S[10*N_S_Length];
  15. char Unicode_longitude[10*longitude_Length];
  16. char Unicode_E_W[10*E_W_Length];


  17. void errorLog(int num)//解析错误
  18. {
  19.         
  20.         while (1)
  21.         {
  22.                   Uart1Sends("ERROR");
  23.                 Uart1Send(num+0x30);
  24.                 Uart1Sends("\r\n");
  25.         }
  26. }

  27. void parseGpsBuffer()//获得GPS数据
  28. {
  29.         char *subString;
  30.         char *subStringNext;
  31.         char i = 0;
  32.         if (Save_Data.isGetData)
  33.         {
  34.                 Save_Data.isGetData = false;
  35.                 //Uart1Sends("**************\r\n");
  36.                 //Uart1Sends(Save_Data.GPS_Buffer);

  37.                
  38.                 for (i = 0 ; i <= 6 ; i++)
  39.                 {
  40.                         if (i == 0)
  41.                         {
  42.                                 if ((subString = strstr(Save_Data.GPS_Buffer, ",")) == NULL)
  43.                                         errorLog(1);        //解析错误
  44.                         }
  45.                         else
  46.                         {
  47.                                 subString++;
  48.                                 if ((subStringNext = strstr(subString, ",")) != NULL)
  49.                                 {
  50.                                         char usefullBuffer[2];
  51.                                         switch(i)
  52.                                         {
  53.                                                 case 1:memcpy(Save_Data.UTCTime, subString, subStringNext - subString);break;        //获取UTC时间
  54.                                                 case 2:memcpy(usefullBuffer, subString, subStringNext - subString);break;        //获取UTC时间
  55.                                                 case 3:memcpy(Save_Data.latitude, subString, subStringNext - subString);break;        //获取纬度信息
  56.                                                 case 4:memcpy(Save_Data.N_S, subString, subStringNext - subString);break;        //获取N/S
  57.                                                 case 5:memcpy(Save_Data.longitude, subString, subStringNext - subString);break;        //获取经度信息
  58.                                                 case 6:memcpy(Save_Data.E_W, subString, subStringNext - subString);break;        //获取E/W

  59.                                                 default:break;
  60.                                         }

  61.                                         subString = subStringNext;
  62.                                         Save_Data.isParseData = true;
  63.                                         if(usefullBuffer[0] == 'A')
  64.                                                 Save_Data.isUsefull = true;
  65.                                         else if(usefullBuffer[0] == 'V')
  66.                                                 Save_Data.isUsefull = false;

  67.                                 }
  68.                                 else
  69.                                 {
  70.                                         errorLog(2);        //解析错误
  71.                                 }
  72.                         }


  73.                 }
  74.         }
  75. }
  76. /***
  77. void printGpsBuffer()//输出GPS数据
  78. {
  79.         if (Save_Data.isParseData)
  80.         {
  81.                 Save_Data.isParseData = false;
  82.                
  83.                 Uart1Sends("Save_Data.UTCTime = ");
  84.                 Uart1Sends(Save_Data.UTCTime);
  85.                 Uart1Sends("\r\n");

  86.                 if(Save_Data.isUsefull)
  87.                 {
  88.                         Save_Data.isUsefull = false;
  89.                         Uart1Sends("Save_Data.latitude = ");
  90.                         Uart1Sends(Save_Data.latitude);
  91.                         Uart1Sends("\r\n");


  92.                         Uart1Sends("Save_Data.N_S = ");
  93.                         Uart1Sends(Save_Data.N_S);
  94.                         Uart1Sends("\r\n");

  95.                         Uart1Sends("Save_Data.longitude = ");
  96.                         Uart1Sends(Save_Data.longitude);
  97.                         Uart1Sends("\r\n");

  98.                         Uart1Sends("Save_Data.E_W = ");
  99.                         Uart1Sends(Save_Data.E_W);
  100.                         Uart1Sends("\r\n");
  101.                 }
  102.                 else
  103.                 {
  104.                         Uart1Sends("GPS DATA is not usefull!\r\n");
  105.                 }
  106.                
  107.         }
  108. }
  109. ***/
  110. //ASCLL-Unicode
  111. void ASCII_TO_Unicode(char *ASCII,char *Unicode)
  112. {
  113.     int length;
  114.     int i = 0;
  115.     int j = 0;
  116.         memset(Unicode,'\0',sizeof(Unicode));
  117.     length = strlen(ASCII);

  118.     for(i=0;i<length;i++)
  119.     {
  120.         Unicode[j++] = '0';
  121.         Unicode[j++] = '0';

  122.         Unicode[j++] = (ASCII[i] / 16) + 0x30;
  123.         Unicode[j++] = (ASCII[i] % 16) + 0x30;
  124.     }

  125. }
  126. void main()
  127. {
  128.         Uart_Init();
  129.         Delay(10000);
  130.         //clrStruct();
  131.         while(1)
  132.         {
  133.                 AD_X=ADC0832(1);
  134.     AD_Y=ADC0832(0);
  135.                 parseGpsBuffer();
  136.                 //ASCII_TO_Unicode(Save_Data.E_W,Unicode_E_W);
  137.                 //printGpsBuffer();
  138.                 if((AD_X<=47  ||  AD_X>=126) || (AD_Y>=141 ||  AD_Y<=57))
  139.                 {
  140.                                 //----------------为什么是下面这些AT指令呢,请看群共享文件SIM900A重要的信息指令文件------------
  141.                         ASCII_TO_Unicode(Save_Data.latitude,Unicode_latitude);
  142.                         ASCII_TO_Unicode(Save_Data.longitude,Unicode_longitude);
  143.                                 Uart1Sends("AT\r\n");
  144.                                 Delay(2000);//延时大约2秒
  145.                                 //Uart1Sends("AT+CSCS=\"UCS2\"\r\n");
  146.                                 //Delay(2000);//延时大约2秒
  147.                                 Uart1Sends("AT+CMGF=1\r\n");
  148.                                 Delay(2000);//延时2秒
  149.                         Uart1Sends("AT+CSCA?\n");
  150.                                 Delay(2000);//延时2秒
  151.                         Uart1Sends("AT+CSMP=17,167,0,25\r\n");
  152.                                 Delay(2000);//延时2秒
  153.                                 //Uart1Sends("AT+CMGS=\"00310039003100360037003500340036003700320034\"\r\n");//此处修改为对方的电话号,电话话的UNICON编码,可使用我们配套的
  154.                                                                                                                                                                                 //资料包中的 中文转UNICON编码工具来获取。特别注意:那个软件
  155.                                                                                                                                                                                 //获得的unicon 编码待有空格,实际中要去掉,否则模块会报错
  156.                                 Uart1Sends("AT+CMGS=\"19167546724\"\r\n");
  157.                                 Delay(2000);//延时2秒
  158.                                 Uart1Sends("4F7F7528800553EF80FD64545012FF0C8BF7524D5F8063075B9A573070B9655152A9FF01");//修改信息内容,内容可为中文、英文和数字,但都是对应的unicon 编码。        
  159.                         Uart1Sends(Unicode_latitude);
  160.                         Uart1Sends("004E0053");
  161.                         Uart1Sends(Unicode_longitude);
  162.                         Uart1Sends("00450057");
  163.                         /***
  164.                         Uart1Sends("使用者可能摔倒,请前往");
  165.                         Uart1Sends(Save_Data.N_S);
  166.                         Uart1Sends(Save_Data.latitude);
  167.                         Uart1Sends(Save_Data.E_W);
  168.                         Uart1Sends(Save_Data.longitude);
  169.                         Uart1Sends("进行救助!");
  170.                         ***/
  171.                         Uart1Send(0x1a);
  172.                                 Delay(8000);//延时10秒
  173.                 }
  174.                 Delay(100);
  175.         }
  176. }
复制代码
uart.c:
  1. #include "uart.h"
  2. //以下是开机后发送到手机的内容,发送的号码在程序中修改。        
  3. unsigned int  rec_data_len_uart=0;    //标记Buffer_Uart0接收数组
  4. unsigned char idata Buffer_Uart0_Rec[25]={0};                 //Uart0中断接收数组
  5. //注意,无论接收到信号还是发送完信号,都会进中断服务程序的
  6. char idata gpsRxBuffer[gpsRxBufferLength];
  7. unsigned char RX_Count = 0;
  8. _SaveData Save_Data;

  9. void Uart_Init()                                                                     
  10. {
  11.         SCON = 0X50;  //UART方式1;8位UART
  12.         REN  = 1;     //允许串行口接收数据
  13.         PCON = 0x00;  //SMOD=0;波特率不加倍
  14.         TMOD = 0x20;  //T1方式2,用于产生波特率
  15.         TH1  = 0xFD;  //装初值
  16.         TL1  = 0xFD;
  17.         TR1  = 1;     //启动定时器1
  18.         EA   = 1;     //打开全局中断控制
  19.         ES   = 1;     //打开串行口中断        
  20. }

  21. void Uart1Sends(unsigned char *p)                                //·¢?í×?·?′?
  22. {        
  23.          while(*p)
  24.          {
  25.                  SBUF=*(p++);
  26.                 while(TI==0)
  27.                 {

  28.                 };
  29.                 TI=0;
  30.         }   
  31. }

  32. void Uart1Send(unsigned char c)                                //·¢?íò???×?·?
  33. {
  34.     TI=0;   
  35.     SBUF=c;   
  36.     while(TI==0);   
  37.     TI=0;   
  38. }
  39. /***
  40. void Serial_interrupt() interrupt 4
  41. {
  42.         unsigned char temp_rec_data_uart0;        
  43.         temp_rec_data_uart0 = SBUF;//读取接收数据               
  44.         RI=0;//接收中断信号清零,表示将继续接收                        
  45.         Buffer_Uart0_Rec[rec_data_len_uart]=temp_rec_data_uart0;        //接收数据
  46.         rec_data_len_uart++;
  47.         if(rec_data_len_uart>24)
  48.         {
  49.                 rec_data_len_uart=0;         //从头开始接收数据
  50.         }        
  51. }
  52. ***/
  53. /*串行通讯中断,收发完成将进入该中断*/
  54. void RECEIVE_DATA(void) interrupt 4 using 3                          
  55. {
  56.         unsigned char temp = 0;
  57.     char i = 0;
  58.    
  59.     ES=0;
  60.     temp = SBUF;
  61.     RI = 0;
  62.    
  63.     if(temp == ')
  64.     {
  65.         RX_Count = 0;   
  66.     }
  67.             
  68.     if(RX_Count <= 5)
  69.     {
  70.              gpsRxBuffer[RX_Count++] = temp;
  71.         if(temp == '\n')                                       
  72.         {
  73.             memset(Save_Data.GPS_Buffer, 0, GPS_Buffer_Length);      //=?||
  74.             memcpy(Save_Data.GPS_Buffer, gpsRxBuffer, RX_Count);     // mph||
  75.             Save_Data.isGetData = true;
  76.             RX_Count = 0;
  77.             memset(gpsRxBuffer, 0, gpsRxBufferLength);      //??Σ
  78.         }
  79.             
  80.         if(RX_Count >= 75)
  81.         {
  82.             RX_Count = 75;
  83.             gpsRxBuffer[RX_Count] = '\0';//ìí?ó?áê?·?
  84.         }            
  85.     }
  86.     ES=1;
  87. }

  88. void clrStruct()
  89. {
  90.         Save_Data.isGetData = false;
  91.         Save_Data.isParseData = false;
  92.         Save_Data.isUsefull = false;
  93.         memset(Save_Data.GPS_Buffer, 0, GPS_Buffer_Length);      //清空
  94.         memset(Save_Data.UTCTime, 0, UTCTime_Length);
  95.         memset(Save_Data.latitude, 0, latitude_Length);
  96.         memset(Save_Data.N_S, 0, N_S_Length);
  97.         memset(Save_Data.longitude, 0, longitude_Length);
  98.         memset(Save_Data.E_W, 0, E_W_Length);
  99.         
  100. }

复制代码

uart.h:
  1. /******************************************************************
  2.         作者:神秘藏宝室
  3.         最终解释权归原作者所有,鄙视其他店铺不劳而获的抄行为!
  4. ******************************************************************/
  5. #ifndef __UART_H__
  6. #define __UART_H__

  7. #include &lt;REGX52.H&gt;
  8. #include &lt;stdio.h&gt;
  9. #include &lt;stdlib.h&gt;
  10. #include &lt;string.h&gt;

  11. #define false 0
  12. #define true 1

  13. //定义数组长度
  14. #define GPS_Buffer_Length 80
  15. #define UTCTime_Length 11
  16. #define latitude_Length 11
  17. #define N_S_Length 2
  18. #define longitude_Length 12
  19. #define E_W_Length 2

  20. typedef struct SaveData
  21. {
  22.         char GPS_Buffer[GPS_Buffer_Length];
  23.         char isGetData;                //是否获取到GPS数据
  24.         char isParseData;        //是否解析完成
  25.         char UTCTime[UTCTime_Length];                //UTC时间
  26.         char latitude[latitude_Length];                //纬度
  27.         char N_S[N_S_Length];                //N/S
  28.         char longitude[longitude_Length];                //经度
  29.         char E_W[E_W_Length];                //E/W
  30.         char isUsefull;                //定位信息是否有效
  31. } xdata _SaveData;



  32. //函数或者变量声明
  33. extern void Uart_Init();
  34. extern void Uart1Sends(unsigned char *p);
  35. extern void Uart1Send(unsigned char c);
  36. extern void clrStruct();


  37. #define gpsRxBufferLength  76
  38. extern char idata gpsRxBuffer[gpsRxBufferLength];
  39. extern unsigned char RX_Count;
  40. extern _SaveData Save_Data;
  41. extern unsigned int  rec_data_len_uart;    //标记Buffer_Uart0接收数组
  42. extern unsigned char idata Buffer_Uart0_Rec[25];        

  43. #endif
复制代码
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:79094 发表于 2024-4-4 10:55 | 只看该作者
生成的少录文件 完整不  
回复

使用道具 举报

板凳
ID:1113476 发表于 2024-4-4 20:49 来自手机 | 只看该作者
估计是烧录文件不完整吧
回复

使用道具 举报

地板
ID:402383 发表于 2024-4-6 14:16 | 只看该作者
1-可能是缺少了I/O口配置初始化
2-可能延时函数时间太长,需要等待一段时间才正常
3-单片机芯片可能损坏,建议更换试一下
回复

使用道具 举报

5#
ID:281828 发表于 2024-4-6 19:48 | 只看该作者
使用STC单片机时建议在STC-ISP软件中下载、使用相关的STC***.h头文件,可以避免很多奇怪的问题。
回复

使用道具 举报

6#
ID:1115609 发表于 2024-4-7 10:40 | 只看该作者

1-可能是缺少了I/O口配置初始化
2-可能延时函数时间太长,需要等待一段时间才正常
3-单片机芯片可能损坏,建议更换试一下
另外使用STC单片机时建议在STC-ISP软件中下载、使用相关的STC***.h头文件,可以避免很多奇怪的问题
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表