標題:
STM32白光通信發(fā)送與接收端程序
[打印本頁]
作者:
atcmccc
時間:
2017-5-17 22:40
標題:
STM32白光通信發(fā)送與接收端程序
白光通信發(fā)送與接收端程序
◆實驗?zāi)康?
本實驗為新建工程實驗,僅供大家新建工程時參考。
◆參考資料:電子手冊:《STM32F1開發(fā)指南-庫函數(shù)版本》第3.3節(jié)。
視頻教程:《手把手教你學(xué)STM32》系列視頻
參考書本:《原子教你玩STM32-庫函數(shù)版本》
◆硬件資源:
1,LED0,LED1
◆實驗現(xiàn)象:
本實驗下載后,LED0和LED1循環(huán)閃爍。
◆注意事項:
無.
0.png
(45.77 KB, 下載次數(shù): 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]; /*定義兩個任務(wù)的堆棧數(shù)組*/
#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) /*宏定義兩個選擇函數(shù)*/
// ADC1轉(zhuǎn)換的電壓值通過MDA方式傳到SRAM
extern __IO uint16_t ADC_ConvertedValue[2];
// 局部變量,用于保存轉(zhuǎn)換計算后的電壓值
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的外設(shè)時鐘*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE); /*開啟GPIOA的外設(shè)時鐘*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; /*選擇要控制的GPIOA引腳*/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; /*設(shè)置引腳模式為通用推挽輸出*/
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_Init(GPIOE, &GPIO_InitStructure); /*調(diào)用庫函數(shù),初始化GPIOA*/
/* 關(guān)閉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(); //在第一個任務(wù)中開啟系統(tǒng)時鐘
while (1)
{
ADC_ConvertedValueLocal =(float) ADC_ConvertedValue[0]/4096*3.3; // 讀取轉(zhuǎn)換的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黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
白光通信源碼.rar
(761.94 KB, 下載次數(shù): 41)
2017-5-18 00:14 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
作者:
dasusu
時間:
2017-8-9 13:52
真的不錯,我要感謝你
作者:
jasonhuang1982
時間:
2017-8-9 20:57
代碼與白光通信完全無關(guān),僅僅是串口發(fā)送接收
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1