找回密码
 立即注册

QQ登录

只需一步,快速开始

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

MPU6050在STC51的代码

[复制链接]
跳转到指定楼层
楼主
ID:259587 发表于 2017-12-9 21:21 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. function varargout = Serial_Port(varargin)
  2. % SERIAL_PORT M-file for Serial_Port.fig
  3. %      SERIAL_PORT, by itself, creates a new SERIAL_PORT or raises the existing
  4. %      singleton*.
  5. %
  6. %      H = SERIAL_PORT returns the handle to a new SERIAL_PORT or the handle to
  7. %      the existing singleton*.
  8. %
  9. %      SERIAL_PORT('CALLBACK',hObject,eventData,handles,...) calls the local
  10. %      function named CALLBACK in SERIAL_PORT.M with the given input arguments.
  11. %
  12. %      SERIAL_PORT('Property','Value',...) creates a new SERIAL_PORT or raises the
  13. %      existing singleton*.  Starting from the left, property value pairs are
  14. %      applied to the GUI before Serial_Port_OpeningFcn gets called.  An
  15. %      unrecognized property name or invalid value makes property application
  16. %      stop.  All inputs are passed to Serial_Port_OpeningFcn via varargin.
  17. %
  18. %      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
  19. %      instance to run (singleton)".
  20. %
  21. % See also: GUIDE, GUIDATA, GUIHANDLES

  22. % Edit the above text to modify the response to help Serial_Port

  23. % Last Modified by GUIDE v2.5 17-Jun-2015 10:13:45

  24. % Begin initialization code - DO NOT EDIT
  25. gui_Singleton = 1;
  26. gui_State = struct('gui_Name',       mfilename, ...
  27.                    'gui_Singleton',  gui_Singleton, ...
  28.                    'gui_OpeningFcn', @Serial_Port_OpeningFcn, ...
  29.                    'gui_OutputFcn',  @Serial_Port_OutputFcn, ...
  30.                    'gui_LayoutFcn',  [] , ...
  31.                    'gui_Callback',   []);
  32. if nargin && ischar(varargin{1})
  33.     gui_State.gui_Callback = str2func(varargin{1});
  34. end

  35. if nargout
  36.     [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
  37. else
  38.     gui_mainfcn(gui_State, varargin{:});
  39. end
  40. % End initialization code - DO NOT EDIT


  41. % --- Executes just before Serial_Port is made visible.
  42. function Serial_Port_OpeningFcn(hObject, eventdata, handles, varargin)
  43. % This function has no output args, see OutputFcn.
  44. % hObject    handle to figure
  45. % eventdata  reserved - to be defined in a future version of MATLAB
  46. % handles    structure with handles and user data (see GUIDATA)
  47. % varargin   command line arguments to Serial_Port (see VARARGIN)


  48. % Choose default command line output for Serial_Port
  49. handles.output = hObject;

  50. % Update handles structure
  51. guidata(hObject, handles);
  52. %{
  53. 关闭、删除已打开的串口对象
  54. scoms=instrfind;
  55. fclose(scoms);
  56. delete(scoms);
  57. clear scoms;

  58. %}
  59. %获取可用串口
  60. global getcom
  61. getcom=instrhwinfo('serial');
  62. set(handles.popupmenu1,'string',char(getcom.AvailableSerialPorts));
  63. global a
  64. a=clock;

  65. % UIWAIT makes Serial_Port wait for user response (see UIRESUME)
  66. % uiwait(handles.figure1);


  67. % --- Outputs from this function are returned to the command line.
  68. function varargout = Serial_Port_OutputFcn(hObject, eventdata, handles)
  69. % varargout  cell array for returning output args (see VARARGOUT);
  70. % hObject    handle to figure
  71. % eventdata  reserved - to be defined in a future version of MATLAB
  72. % handles    structure with handles and user data (see GUIDATA)




  73. function edit1_Callback(hObject, eventdata, handles)
  74. % hObject    handle to edit1 (see GCBO)
  75. % eventdata  reserved - to be defined in a future version of MATLAB
  76. % handles    structure with handles and user data (see GUIDATA)

  77. % Hints: get(hObject,'String') returns contents of edit1 as text
  78. %        str2double(get(hObject,'String')) returns contents of edit1 as a double


  79. % --- Executes during object creation, after setting all properties.
  80. function edit1_CreateFcn(hObject, eventdata, handles)
  81. % hObject    handle to edit1 (see GCBO)
  82. % eventdata  reserved - to be defined in a future version of MATLAB
  83. % handles    empty - handles not created until after all CreateFcns called

  84. % Hint: edit controls usually have a white background on Windows.
  85. %       See ISPC and COMPUTER.
  86. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  87.     set(hObject,'BackgroundColor','white');
  88. end



  89. function edit2_Callback(hObject, eventdata, handles)
  90. % hObject    handle to edit2 (see GCBO)
  91. % eventdata  reserved - to be defined in a future version of MATLAB
  92. % handles    structure with handles and user data (see GUIDATA)

  93. % Hints: get(hObject,'String') returns contents of edit2 as text
  94. %        str2double(get(hObject,'String')) returns contents of edit2 as a double


  95. % --- Executes during object creation, after setting all properties.
  96. function edit2_CreateFcn(hObject, eventdata, handles)
  97. % hObject    handle to edit2 (see GCBO)
  98. % eventdata  reserved - to be defined in a future version of MATLAB
  99. % handles    empty - handles not created until after all CreateFcns called

  100. % Hint: edit controls usually have a white background on Windows.
  101. %       See ISPC and COMPUTER.
  102. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  103.     set(hObject,'BackgroundColor','white');
  104. end


  105. % --- Executes on button press in pushbutton1.
  106. function pushbutton1_Callback(hObject, eventdata, handles)
  107. % hObject    handle to pushbutton1 (see GCBO)
  108. % eventdata  reserved - to be defined in a future version of MATLAB
  109. % handles    structure with handles and user data (see GUIDATA)

  110. %获取当前按钮的名字
  111. string=get(handles.pushbutton1,'string');
  112. if(strcmp(string,'打开串口')==1)
  113.     set(handles.pushbutton1,'string','关闭串口');
  114.     %禁止修改串口号和波特率
  115.     set(handles.popupmenu1,'Enable','off');
  116.     set(handles.popupmenu2,'Enable','off');
  117.     %getcom=instrhwinfo('serial');
  118.     global getcom
  119.     global scom
  120.     %选择串口号
  121.     if size(getcom.AvailableSerialPorts)==0
  122.         errordlg('无有效COM口','提示','replace');
  123.     else if size(getcom.AvailableSerialPorts)==1
  124.            scom=serial(char(getcom.AvailableSerialPorts));
  125.         else
  126.             for i=1:size(getcom.AvailableSerialPorts)
  127.                 if i==get(handles.popupmenu1,'Value')
  128.                     scom=serial(char(getcom.AvailableSerialPorts(i)));
  129.                     break;
  130.                 end;
  131.             end;
  132.         end;
  133.     end;
  134.     %设置串口波特率
  135.     switch get(handles.popupmenu2,'Value')
  136.         case 1
  137.             scom.BaudRate=115200;
  138.         case 2
  139.             scom.BaudRate=57600;
  140.         case 3
  141.             scom.BaudRate=56000;
  142.         case 4
  143.             scom.BaudRate=43000;
  144.         case 5
  145.             scom.BaudRate=38400;
  146.         case 6
  147.             scom.BaudRate=19200;
  148.         case 7
  149.             scom.BaudRate=9600;
  150.         case 8
  151.             scom.BaudRate=4800;
  152.         case 9
  153.             scom.BaudRate=2400;
  154.         case 10
  155.             scom.BaudRate=1200;
  156.         case 11
  157.             scom.BaudRate=600;
  158.         case 12
  159.             scom.BaudRate=300;
  160.     end;
  161.     %scom.terminator='CR';
  162.     scom.BytesAvailableFcnMode='terminator';
  163.     scom.BytesAvailableFcn={@mycallback,handles};
  164.     fopen(scom);
  165. else
  166.     %按键的名字设为‘打开串口’
  167.     set(handles.pushbutton1,'string','打开串口');
  168.     %禁止修改串口号和波特率
  169.     set(handles.popupmenu1,'Enable','on');
  170.     set(handles.popupmenu2,'Enable','on');
  171.     %查找串口对象
  172.     scoms = instrfind;
  173.     %尝试停止、关闭删除串口对象
  174.     stopasync(scoms);
  175.     fclose(scoms);
  176.     delete(scoms);
  177. end;
  178. % --- Executes on button press in pushbutton2.
  179. function pushbutton2_Callback(hObject, eventdata, handles)
  180. % hObject    handle to pushbutton2 (see GCBO)
  181. % eventdata  reserved - to be defined in a future version of MATLAB
  182. % handles    structure with handles and user data (see GUIDATA)
  183. global scom;
  184. %得到发送区的数据
  185. send_data=get(handles.edit2,'string');
  186. global a;
  187. a=GetSecs;
  188. fprintf(scom,send_data);


  189. % --- Executes on selection change in popupmenu1.
  190. function popupmenu1_Callback(hObject, eventdata, handles)
  191. % hObject    handle to popupmenu1 (see GCBO)
  192. % eventdata  reserved - to be defined in a future version of MATLAB
  193. % handles    structure with handles and user data (see GUIDATA)

  194. % Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
  195. %        contents{get(hObject,'Value')} returns selected item from popupmenu1


  196. % --- Executes during object creation, after setting all properties.
  197. function popupmenu1_CreateFcn(hObject, eventdata, handles)
  198. % hObject    handle to popupmenu1 (see GCBO)
  199. % eventdata  reserved - to be defined in a future version of MATLAB
  200. % handles    empty - handles not created until after all CreateFcns called

  201. % Hint: popupmenu controls usually have a white background on Windows.
  202. %       See ISPC and COMPUTER.
  203. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  204.     set(hObject,'BackgroundColor','white');
  205. end


  206. % --- Executes on selection change in popupmenu2.
  207. function popupmenu2_Callback(hObject, eventdata, handles)
  208. % hObject    handle to popupmenu2 (see GCBO)
  209. % eventdata  reserved - to be defined in a future version of MATLAB
  210. % handles    structure with handles and user data (see GUIDATA)

  211. % Hints: contents = cellstr(get(hObject,'String')) returns popupmenu2 contents as cell array
  212. %        contents{get(hObject,'Value')} returns selected item from popupmenu2



  213. % --- Executes during object creation, after setting all properties.
  214. function popupmenu2_CreateFcn(hObject, eventdata, handles)
  215. % hObject    handle to popupmenu2 (see GCBO)
  216. % eventdata  reserved - to be defined in a future version of MATLAB
  217. % handles    empty - handles not created until after all CreateFcns called

  218. % Hint: popupmenu controls usually have a white background on Windows.
  219. %       See ISPC and COMPUTER.
  220. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  221.     set(hObject,'BackgroundColor','white');
  222. end

  223. %串口接收显示程序
  224. function mycallback(scom,BytesAvailable,handles)
  225. n_bytes=get(scom,'BytesAvailable');
  226. if n_bytes>0
  227.     char=fscanf(scom);
  228. end;
  229. original=get(handles.edit1,'string');
  230. char=strcat(original,char);
  231. set(handles.edit1,'string',char);
  232. global a;
  233. set(handles.edit1,'string',num2char(GetSecs-a));



  234. function edit3_Callback(hObject, eventdata, handles)
  235. % hObject    handle to edit3 (see GCBO)
  236. % eventdata  reserved - to be defined in a future version of MATLAB
  237. % handles    structure with handles and user data (see GUIDATA)

  238. % Hints: get(hObject,'String') returns contents of edit3 as text
  239. %        str2double(get(hObject,'String')) returns contents of edit3 as a double


  240. % --- Executes during object creation, after setting all properties.
  241. function edit3_CreateFcn(hObject, eventdata, handles)
  242. % hObject    handle to edit3 (see GCBO)
  243. % eventdata  reserved - to be defined in a future version of MATLAB
  244. % handles    empty - handles not created until after all CreateFcns called

  245. % Hint: edit controls usually have a white background on Windows.
  246. %       See ISPC and COMPUTER.
  247. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  248.     set(hObject,'BackgroundColor','white');
  249. end
复制代码


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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