标题:
STM32白光通信发送与接收端程序
[打印本页]
作者:
atcmccc
时间:
2017-5-17 22:40
标题:
STM32白光通信发送与接收端程序
白光通信发送与接收端程序
◆实验目的:
本实验为新建工程实验,仅供大家新建工程时参考。
◆参考资料:电子手册:《STM32F1开发指南-库函数版本》第3.3节。
视频教程:《手把手教你学STM32》系列视频
参考书本:《原子教你玩STM32-库函数版本》
◆硬件资源:
1,LED0,LED1
◆实验现象:
本实验下载后,LED0和LED1循环闪烁。
◆注意事项:
无.
0.png
(45.77 KB, 下载次数: 90)
下载附件
2017-5-18 00:14 上传
单片机源程序如下:
#include "stm32f10x.h"
#include "stm32f10x_conf.h"
#include "ucos_ii.h"
#include "sys.h"
#include "delay.h"
#include "dac.h"
#include "adc.h"
#include "bsp_usart1.h"
#include "uart_api.h"
#define TASK_STK_SIZE 64 //定义堆栈长度
OS_STK LED0_TASK_STK[TASK_STK_SIZE];
OS_STK LED1_TASK_STK[TASK_STK_SIZE]; /*定义两个任务的堆栈数组*/
#define ON 0
#define OFF 1
#define LED0(a) if (a) \
GPIO_SetBits(GPIOB,GPIO_Pin_5);\
else \
GPIO_ResetBits(GPIOB,GPIO_Pin_5)
#define LED1(a) if (a) \
GPIO_SetBits(GPIOE,GPIO_Pin_5);\
else \
GPIO_ResetBits(GPIOE,GPIO_Pin_5) /*宏定义两个选择函数*/
// ADC1转换的电压值通过MDA方式传到SRAM
extern __IO uint16_t ADC_ConvertedValue[2];
// 局部变量,用于保存转换计算后的电压值
float ADC_ConvertedValueLocal;
u16 f;
u16 g=4095;
uint8_t ucaRxBuf[1024];
uint16_t usRxCount;
uint8_t ucTemp;
uint8_t data1,data2,data;
uint16_t ADC_ConvVaule; //采样值
uint16_t ADC_true; //采样的原码
int a=0;
int b=0;
void GPIO_configuration(void) //配置I/O口
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); /*开启GPIOA的外设时钟*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE); /*开启GPIOA的外设时钟*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; /*选择要控制的GPIOA引脚*/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; /*设置引脚模式为通用推挽输出*/
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_Init(GPIOE, &GPIO_InitStructure); /*调用库函数,初始化GPIOA*/
/* 关闭led0灯 */
GPIO_SetBits(GPIOB, GPIO_Pin_5);
GPIO_SetBits(GPIOE, GPIO_Pin_5);
}
void BSP_Init(void) //硬件配置
{
GPIO_configuration(); /* GPIO端口初始化 */
}
void SysTick_init(void) /* SysTick_init 配置SysTick定时器 */
{
SysTick_Config(SystemCoreClock/OS_TICKS_PER_SEC); //初始化并使能SysTick定时器
}
void Task_LED0(void *p_arg)
{
(void)p_arg; // 'p_arg' 并没有用到,防止编译器提示警告
SysTick_init(); //在第一个任务中开启系统时钟
while (1)
{
ADC_ConvertedValueLocal =(float) ADC_ConvertedValue[0]/4096*3.3; // 读取转换的AD值
f =ADC_ConvertedValueLocal*1000.0;
DAC1_Set_Vol(f);
// a= f/256;
// b= f%256;
// comSendChar(COM1,a);
// comSendChar(COM1, b);
// comSendChar(COM1, 254);
// comSendChar(COM1, 255);
//
// comSendChar(COM1, 0);
ADC_ConvVaule = f;
ADC_true = ADC_ConvVaule; //保存原码
data1 = (( ADC_true ) >> 6) | 0x80;
data2 = ((uint8_t)( ADC_true )) | 0xc0; //+ 1060
comSendChar(COM1,data1);
comSendChar(COM1,data2);
}
}
void Task_LED1(void *p_arg)
{
(void)p_arg; // 'p_arg' 并没有用到,防止编译器提示警告
SysTick_init();
while (1)
{
OSTimeDlyHMSM(0, 0,0,0);
}
}
int main(void)
{
BSP_Init();
OSInit();
comInit();
usRxCount = 0;
……………………
…………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
白光通信源码.rar
(761.94 KB, 下载次数: 41)
2017-5-18 00:14 上传
点击文件名下载附件
下载积分: 黑币 -5
作者:
dasusu
时间:
2017-8-9 13:52
真的不错,我要感谢你
作者:
jasonhuang1982
时间:
2017-8-9 20:57
代码与白光通信完全无关,仅仅是串口发送接收
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1