[菜农助学交流] [2012助学第一期]南信大瑜的学习笔记之USB温度采集

[复制链接]
4027|12
 楼主| 南信大瑜 发表于 2012-4-25 17:20 | 显示全部楼层 |阅读模式
这个程序是在上一个USB控制LED的程序的基础上修改而来的,实现了通过DS18B20采集温度,用USB传输到上PC机,PC机接收数据,显示采集的温度并绘图。
我偷了个赖,DS18B20的驱动直接用的lixiaoxu2meng的,
原帖为:https://bbs.21ic.com/icview-272599-1-1.html
1.单片机USB处理函数。

  1. void HID_SetOutReport(uint8_t *pu8EpBuf)
  2. {
  3.     static uint8_t pu8Buffer[62] = {0};  //定义一个数组,用于处理数据
  4.     memcpy(pu8Buffer, pu8EpBuf, 62);
  5.     if((pu8Buffer[2]==0x55)&&(pu8Buffer[3]==0xaa)) //如果上位机发送0X55,0XAA给单片机,则进行一次温度采集
  6.     {
  7.         temperature=read_temp();//读取温度
  8.         temperature_zheng=temperature/10;//温度整数
  9.         temperature_xiao=temperature%10;//温度小数(小数点后一位)
  10.         //pu8Buffer[0]=1;
  11.         pu8Buffer[1]=2;//发送两个字节
  12.         pu8Buffer[2]=temperature_zheng;//第一个字节为整数
  13.         pu8Buffer[3]=temperature_xiao;//第二个字节为小数

  14.         memcpy(g_au8DeviceReport,pu8Buffer,62);//把数据复制到USB缓冲区
  15.     }

  16. }




2.上位机是MFC的,只简单的实现了打开,关闭,退出,显示温度几个功能。

  1. BOOL CTemperatureDlg::OnInitDialog() //DIALOG初始化函数
  2. {
  3.     CDialog::OnInitDialog();

  4.     // Add "About..." menu item to system menu.

  5.     // IDM_ABOUTBOX must be in the system command range.
  6.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  7.     ASSERT(IDM_ABOUTBOX < 0xF000);

  8.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  9.     if (pSysMenu != NULL)
  10.     {
  11.         CString strAboutMenu;
  12.         strAboutMenu.LoadString(IDS_ABOUTBOX);
  13.         if (!strAboutMenu.IsEmpty())
  14.         {
  15.             pSysMenu->AppendMenu(MF_SEPARATOR);
  16.             pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  17.         }
  18.     }

  19.     // Set the icon for this dialog.  The framework does this automatically
  20.     //  when the application's main window is not a dialog
  21.     SetIcon(m_hIcon, TRUE);            // Set big icon
  22.     SetIcon(m_hIcon, FALSE);        // Set small icon
  23.    
  24.     // TODO: Add extra initialization here
  25.     CRect rect;
  26.     GetDlgItem(IDC_STATIC_HISTOGRAM)->GetWindowRect(rect);
  27.     ScreenToClient(rect);
  28.     m_ctrlHistogram.Create(WS_VISIBLE | WS_CHILD | WS_TABSTOP, rect, this, 1000);
  29.     m_ctrlHistogram.SetRange(1, 500); //设置绘图范围
  30.     m_ctrlHistogram.SetSpeed(CHistogramCtrl::HIGH_SPEED);//设置波形速度
  31.     m_ctrlHistogram.SetGridsColor(RGB(0,120, 120)); //设置栅格的颜色
  32.     m_ctrlHistogram.SetBkColor(RGB(0, 0, 0));//设置背景颜色
  33.     m_ctrlHistogram.SetPen(3, RGB(255, 255, 0));//设置波形颜色
  34.    

  35.     open_close=0;
  36.     SetTimer(1,1000,NULL); //创建一个定时器,用于采集温度
  37.     return TRUE;  // return TRUE  unless you set the focus to a control
  38. }



  39. void CTemperatureDlg::OnOpenUsb()   //打开按钮处理函数
  40. {
  41.     // TODO: Add your control notification handler code here
  42.     if(open_close==0)
  43.     {
  44.         if(!io.OpenDevice(0x051A,0x511B))  //打开设备
  45.         {
  46.             MessageBox("打开失败!","Error",MB_OK);
  47.             m_status_label.SetWindowText("USB设备状态:关闭");   
  48.         }
  49.         else
  50.         {
  51.             MessageBox("打开成功","Ok",MB_OK);
  52.             m_status_label.SetWindowText("USB设备状态:打开");
  53.             open_close=1;
  54.         }
  55.     }
  56.     else
  57.     {
  58.         MessageBox("设备已经打开","WARNING",MB_OK);
  59.     }

  60.    
  61. }

  62. void CTemperatureDlg::OnCloseUsb() //关闭按钮处理函数
  63. {
  64.     // TODO: Add your control notification handler code here
  65.     if(open_close==1)
  66.     {
  67.         io.CloseDevice();
  68.         m_status_label.SetWindowText("USB设备状态:关闭");
  69.         MessageBox("设备已经关闭","CLOSE",MB_OK);
  70.         open_close=0;
  71.         sprintf(temp_string,"00.0");
  72.         m_temp.SetWindowText(temp_string);
  73.         m_ctrlHistogram.SetPos(1);
  74.     }
  75.     else
  76.     {
  77.         MessageBox("设备未打开,无需关闭","WARNING",MB_OK);
  78.     }
  79.    

  80. }

  81. void CTemperatureDlg::OnTimer(UINT nIDEvent)   //定时器处理函数
  82. {
  83.     // TODO: Add your message handler code here and/or call default
  84.     //m_status_label.SetWindowText("Timer...");
  85.     if(open_close==1)  如果设备打开了
  86.     {
  87.         io.WriteFile((const char *)&cmd,sizeof(cmd),&Length,2000);  //向USB发送采集温度命令
  88.         io.ReadFile(usb_buf,2, &length,2000);//读取温度
  89.         if(usb_buf[0]>49)  //这里温度的显示值为1-49,如果大于49,要做相应处理
  90.             usb_buf[0]=49;
  91.         sprintf(temp_string,"%d.%d",usb_buf[0],usb_buf[1]);
  92.         m_temp.SetWindowText(temp_string);//设置显示温度值
  93.         m_ctrlHistogram.SetPos(usb_buf[0]*10+usb_buf[1]); //显示坐标值
  94.     }
  95.     CDialog::OnTimer(nIDEvent);
  96. }






本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
 楼主| 南信大瑜 发表于 2012-4-25 17:25 | 显示全部楼层
本帖最后由 南信大瑜 于 2012-4-25 17:26 编辑

上位机程序:

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
hotpower 发表于 2012-4-25 18:20 | 显示全部楼层
再上一篇订金250立即返还。
lixiaoxu2meng 发表于 2012-4-26 09:44 | 显示全部楼层
顶 助学板子的USB 一直没玩 有时间学学 还得像高手们请教啊
zhaor 发表于 2012-4-26 20:50 | 显示全部楼层
编译的时候有提示:
Source\main.c(31): warning:  #223-D: function "HID_MainProcess" declared implicitly
plc_avr 发表于 2012-4-27 06:15 | 显示全部楼层
这个贴子非常有价值,顶!
Swallow_0322 发表于 2012-4-27 07:43 | 显示全部楼层
顶!上位机+下位机,强悍!
天涯乐草 发表于 2012-4-27 10:14 | 显示全部楼层
abin0415 发表于 2012-4-27 19:48 | 显示全部楼层
乡村男孩 发表于 2012-5-4 09:13 | 显示全部楼层
学习了
xyz549040622 发表于 2012-5-5 09:22 | 显示全部楼层
牛叉,正在学习中。顶
xyz549040622 发表于 2012-5-5 09:23 | 显示全部楼层
牛叉,正在学习中。顶
你好陌生人 发表于 2013-4-28 17:33 | 显示全部楼层
学习中   
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

27

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部