找回密码
 立即注册

QQ登录

只需一步,快速开始

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

STM32白光通信发送与接收端程序

[复制链接]
跳转到指定楼层
楼主
白光通信发送与接收端程序
◆实验目的:
    本实验为新建工程实验,仅供大家新建工程时参考。
   
◆参考资料:电子手册:《STM32F1开发指南-库函数版本》第3.3节。
           视频教程:《手把手教你学STM32》系列视频
           参考书本:《原子教你玩STM32-库函数版本》
   
◆硬件资源:
    1,LED0,LED1
   
◆实验现象:
    本实验下载后,LED0和LED1循环闪烁。
   
◆注意事项:
    无.



单片机源程序如下:
  1. #include "stm32f10x.h"
  2. #include "stm32f10x_conf.h"
  3. #include "ucos_ii.h"
  4. #include "sys.h"
  5. #include "delay.h"
  6. #include "dac.h"
  7. #include "adc.h"
  8. #include "bsp_usart1.h"
  9. #include "uart_api.h"

  10. #define        TASK_STK_SIZE         64              //定义堆栈长度


  11. OS_STK LED0_TASK_STK[TASK_STK_SIZE];
  12. OS_STK LED1_TASK_STK[TASK_STK_SIZE];  /*定义两个任务的堆栈数组*/

  13. #define ON 0
  14. #define OFF 1

  15. #define LED0(a)        if (a)        \
  16.                                         GPIO_SetBits(GPIOB,GPIO_Pin_5);\
  17.                                         else                \
  18.                                         GPIO_ResetBits(GPIOB,GPIO_Pin_5)
  19. #define LED1(a)        if (a)        \
  20.                                         GPIO_SetBits(GPIOE,GPIO_Pin_5);\
  21.                                         else                \
  22.                                         GPIO_ResetBits(GPIOE,GPIO_Pin_5)  /*宏定义两个选择函数*/

  23.                                        
  24.                                         // ADC1转换的电压值通过MDA方式传到SRAM
  25. extern __IO uint16_t ADC_ConvertedValue[2];

  26. // 局部变量,用于保存转换计算后的电压值                         
  27. float ADC_ConvertedValueLocal;
  28. u16 f;
  29.                                         u16 g=4095;
  30.         uint8_t ucaRxBuf[1024];
  31.         uint16_t usRxCount;
  32.         uint8_t ucTemp;       
  33.                                        
  34.                                        
  35.                                        
  36. uint8_t data1,data2,data;
  37. uint16_t ADC_ConvVaule;                //采样值
  38. uint16_t ADC_true;                //采样的原码
  39.                                        
  40.                                        
  41.                                             int a=0;
  42.               int b=0;       
  43. void GPIO_configuration(void)                                                      //配置I/O口
  44. {               

  45.         GPIO_InitTypeDef GPIO_InitStructure;
  46.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); /*开启GPIOA的外设时钟*/
  47.             RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE); /*开启GPIOA的外设时钟*/
  48.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;  /*选择要控制的GPIOA引脚*/       
  49.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;      /*设置引脚模式为通用推挽输出*/                 
  50.                                                                                                                   
  51.            GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  52.     GPIO_Init(GPIOB, &GPIO_InitStructure);        
  53.     GPIO_Init(GPIOE, &GPIO_InitStructure);             /*调用库函数,初始化GPIOA*/         

  54.         /* 关闭led0灯        */
  55.         GPIO_SetBits(GPIOB, GPIO_Pin_5);
  56.                 GPIO_SetBits(GPIOE, GPIO_Pin_5);

  57. }

  58. void BSP_Init(void)               //硬件配置
  59. {  
  60.     GPIO_configuration();  /* GPIO端口初始化 */
  61. }



  62. void SysTick_init(void)               /*  SysTick_init   配置SysTick定时器  */
  63. {
  64.     SysTick_Config(SystemCoreClock/OS_TICKS_PER_SEC);   //初始化并使能SysTick定时器
  65. }


  66. void Task_LED0(void *p_arg)
  67. {
  68.     (void)p_arg;                                                 // 'p_arg' 并没有用到,防止编译器提示警告

  69.     SysTick_init();                            //在第一个任务中开启系统时钟

  70.                   
  71.     while (1)
  72.     {


  73.                            ADC_ConvertedValueLocal =(float) ADC_ConvertedValue[0]/4096*3.3; // 读取转换的AD值
  74.         f =ADC_ConvertedValueLocal*1000.0;   
  75.                           DAC1_Set_Vol(f);
  76. //         a=  f/256;
  77. //                           b=  f%256;

  78. //                        comSendChar(COM1,a);
  79. //                comSendChar(COM1, b);
  80. //                  comSendChar(COM1, 254);
  81. //                         comSendChar(COM1, 255);
  82.                
  83. //                       
  84. //                        comSendChar(COM1, 0);

  85.                        
  86.              ADC_ConvVaule = f;
  87.                        

  88.        
  89.                
  90.                                 ADC_true = ADC_ConvVaule;                //保存原码
  91.                                 data1 = (( ADC_true ) >> 6) | 0x80;
  92.                                 data2 = ((uint8_t)( ADC_true )) | 0xc0;  //+ 1060
  93.                         comSendChar(COM1,data1);
  94.                 comSendChar(COM1,data2);

  95.                        
  96.     }
  97. }
  98. void Task_LED1(void *p_arg)
  99. {
  100.     (void)p_arg;                                                 // 'p_arg' 并没有用到,防止编译器提示警告
  101.     SysTick_init();   
  102.              while (1)
  103.     {
  104.    OSTimeDlyHMSM(0, 0,0,0);

  105.     }
  106. }



  107. int main(void)
  108. {
  109.         BSP_Init();
  110.         OSInit();
  111. comInit();
  112.                 usRxCount = 0;
  113. ……………………

  114. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
白光通信源码.rar (761.94 KB, 下载次数: 40)



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

使用道具 举报

沙发
ID:78510 发表于 2017-8-9 13:52 | 只看该作者
真的不错,我要感谢你
回复

使用道具 举报

板凳
ID:226253 发表于 2017-8-9 20:57 | 只看该作者
代码与白光通信完全无关,仅仅是串口发送接收
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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