/*************************************************************/
/* this programme for UART3 Communication */
/* Company: New Future */
/* Programmer: windy */
/* phone: 13891912727 weixin same name */
/* Email: cjfwindy@163.com */
/* if you have any question, please tell me */
/* Time: 2021-04-08 13:36 */
/* Copyright(C) 2021 New Future All rights reserved */
/*************************************************************/
#include "global.h"
//=================================================================================
unsigned char REC5_Data[50],REC5_Count=0,Buffer5[50], Data5Length;
TYPE_BYTE_IO _RAMC;
char* gpUart5TxAddress; /* uart transmit buffer address */
int gUart5TxCnt; /* uart transmit data number */
//=================================================================================
/*----函数:向PC传输一行字符串----*/
void UART5_print(char * head)
{
BYTE i,j;
j = strlen(head);
for (i=0; i<j; i++)
{
UART5_Send(head[i]);
}
}
//-----------------------------------------------------------------
void Uart5_BSend(char * head, int nub)
{
int i;
for (i=0; i<nub; i++)
{
UART5_Send(head[i]);
}
//UART5_Send(0x0D);
//UART5_Send(0x0A);
}
//-----------------------------------------------------------------
/*----函数:向串口发送一个字节的数据----*/
void UART5_Send(BYTE a)
{
UART5_CheckOERR();
while (!(UART5->SR & USART_SR_TXE));
UART5->DR = a;
//NOP();
//while ((UART5->SR & USART_SR_TXE) != 1);
//while ((UART5->SR & USART_SR_TC) != 1);
while ((UART5->SR & USART_SR_TC) == 0);
}
//-----------------------------------------------------------------
void REC5_SER(void)
{
if (b5Timeover)
{
b5Timeover = 0;
F_UART5_First = 1;
}
if (!F_UART5_REC) return;
F_UART5_REC = 0;
REC5_Count = 0;
// HFORWARD 5A A5 06 83 00 00 01 00 F1
if((0x06==REC5_Data[2]) && ( 0x83==REC5_Data[3]) && (0x00==REC5_Data[4]) && (0x00==REC5_Data[5]) && (0x00==REC5_Data[7]) && (0xF1==REC5_Data[8]))
{
Key_Use = 1;
Key_Value = KEY_HFORWARD;
}
// HBCAKWORD 5A A5 06 83 00 00 01 00 F2
if((0x06==REC5_Data[2]) && ( 0x83==REC5_Data[3]) && (0x00==REC5_Data[4]) && (0x00==REC5_Data[5]) && (0x00==REC5_Data[7]) && (0xF2==REC5_Data[8]))
{
Key_Use = 1;
Key_Value = KEY_HBACKWARD;
}
// HSTOP 5A A5 06 83 00 00 01 00 F3
if((0x06==REC5_Data[2]) && ( 0x83==REC5_Data[3]) && (0x00==REC5_Data[4]) && (0x00==REC5_Data[5]) && (0x00==REC5_Data[7]) && (0xF3==REC5_Data[8]))
{
Key_Use = 1;
Key_Value = KEY_HSTOP;
}
// VFORWARD 5A A5 06 83 00 00 01 00 F4
if((0x06==REC5_Data[2]) && ( 0x83==REC5_Data[3]) && (0x00==REC5_Data[4]) && (0x00==REC5_Data[5]) && (0x00==REC5_Data[7]) && (0xF4==REC5_Data[8]))
{
Key_Use = 1;
Key_Value = KEY_VFORWARD;
}
// VBCAKWORD 5A A5 06 83 00 00 01 00 F5
if((0x06==REC5_Data[2]) && ( 0x83==REC5_Data[3]) && (0x00==REC5_Data[4]) && (0x00==REC5_Data[5]) && (0x00==REC5_Data[7]) && (0xF5==REC5_Data[8]))
{
Key_Use = 1;
Key_Value = KEY_VBACKWARD;
}
// VSTOP 5A A5 06 83 00 00 01 00 F6
if((0x06==REC5_Data[2]) && ( 0x83==REC5_Data[3]) && (0x00==REC5_Data[4]) && (0x00==REC5_Data[5]) && (0x00==REC5_Data[7]) && (0xF6==REC5_Data[8]))
{
Key_Use = 1;
Key_Value = KEY_VSTOP;
}
// SAMPLE1 5A A5 06 83 00 00 01 00 F7
if((0x06==REC5_Data[2]) && ( 0x83==REC5_Data[3]) && (0x00==REC5_Data[4]) && (0x00==REC5_Data[5]) && (0x00==REC5_Data[7]) && (0xF7==REC5_Data[8]))
{
Key_Use = 1;
Key_Value = KEY_SAMPLE1;
}
// SAMPLE2 5A A5 06 83 00 00 01 00 F8
if((0x06==REC5_Data[2]) && ( 0x83==REC5_Data[3]) && (0x00==REC5_Data[4]) && (0x00==REC5_Data[5]) && (0x00==REC5_Data[7]) && (0xF8==REC5_Data[8]))
{
Key_Use = 1;
Key_Value = KEY_SAMPLE2;
}
// SAMPLE3 5A A5 06 83 00 00 01 00 F9
if((0x06==REC5_Data[2]) && ( 0x83==REC5_Data[3]) && (0x00==REC5_Data[4]) && (0x00==REC5_Data[5]) && (0x00==REC5_Data[7]) && (0xF9==REC5_Data[8]))
{
Key_Use = 1;
Key_Value = KEY_SAMPLE3;
}
// SAMPLE4 5A A5 06 83 00 00 01 00 FA
if((0x06==REC5_Data[2]) && ( 0x83==REC5_Data[3]) && (0x00==REC5_Data[4]) && (0x00==REC5_Data[5]) && (0x00==REC5_Data[7]) && (0xFA==REC5_Data[8]))
{
Key_Use = 1;
Key_Value = KEY_SAMPLE4;
}
// RETURN 5A A5 06 83 00 00 01 00 FB
if((0x06==REC5_Data[2]) && ( 0x83==REC5_Data[3]) && (0x00==REC5_Data[4]) && (0x00==REC5_Data[5]) && (0x00==REC5_Data[7]) && (0xFB==REC5_Data[8]))
{
Key_Use = 1;
Key_Value = KEY_RETURN;
}
// RETURN 5A A5 06 83 00 00 01 00 FC
if((0x06==REC5_Data[2]) && ( 0x83==REC5_Data[3]) && (0x00==REC5_Data[4]) && (0x00==REC5_Data[5]) && (0x00==REC5_Data[7]) && (0xFC==REC5_Data[8]))
{
Key_Use = 1;
Key_Value = KEY_REMOTE;
}
// HVELOCITY 5A A5 06 83 03 40 01 00 0C
if((0x06==REC5_Data[2]) && ( 0x83==REC5_Data[3]) && (0x03==REC5_Data[4]) && (0x40==REC5_Data[5]))
{
Key_Use = 1;
Key_Value = KEY_HVELOCITY;
HVELOCITYDATA = REC5_Data[7]<<8;
HVELOCITYDATA = HVELOCITYDATA + REC5_Data[8];
}
// VVELOCITY 5A A5 06 83 03 50 01 00 0C
if((0x06==REC5_Data[2]) && ( 0x83==REC5_Data[3]) && (0x03==REC5_Data[4]) && (0x50==REC5_Data[5]))
{
Key_Use = 1;
Key_Value = KEY_VVELOCITY;
VVELOCITYDATA = REC5_Data[7]<<8;
VVELOCITYDATA = VVELOCITYDATA + REC5_Data[8];
}
// HSTARTER 5A A5 06 83 03 60 01 00 0C
if((0x06==REC5_Data[2]) && ( 0x83==REC5_Data[3]) && (0x03==REC5_Data[4]) && (0x60==REC5_Data[5]))
{
Key_Use = 1;
Key_Value = KEY_HSTARTER;
HSARTERDATA = REC5_Data[7]<<8;
HSARTERDATA = HSARTERDATA + REC5_Data[8];
}
// VSTARTER 5A A5 06 83 03 70 01 00 0C
if((0x06==REC5_Data[2]) && ( 0x83==REC5_Data[3]) && (0x03==REC5_Data[4]) && (0x70==REC5_Data[5]))
{
Key_Use = 1;
Key_Value = KEY_VSTARTER;
VSARTERDATA = REC5_Data[7]<<8;
VSARTERDATA = VSARTERDATA + REC5_Data[8];
}
}
//-----------------------------------------------------------------
void UART5TX_SER(void)
{
if (0 == gUart5TxCnt) return;
if (!(UART5->SR & USART_SR_TC)) return;
UART5_CheckOERR();
if( gUart5TxCnt > 0 )
{
UART5->DR = *gpUart5TxAddress;
gpUart5TxAddress++;
gUart5TxCnt--;
if (0 == gUart5TxCnt)
{
while ((UART5->SR & USART_SR_TC) != 1);
//PIN_4852RE = 0; // 接收状态
}
}
}
//=================================================================================
// 初始化IO 串口1
// pclk2:PCLK2时钟频率(Mhz)
// bound:波特率
void uart5_init(u32 pclk1,u32 bound)
{
float temp;
u16 mantissa;
u16 fraction;
temp = (float)(pclk1*1000000)/(bound*16); // 得到USARTDIV
mantissa = temp; // 得到整数部分
fraction = (temp-mantissa)*16; // 得到小数部分
mantissa <<= 4;
mantissa += fraction;
RCC->AHB1ENR |= 1<<2; // 使能PORTC时钟
RCC->AHB1ENR |= 1<<3; // 使能PORTD时钟
RCC->APB1ENR |= 1<<20; // 使能串口时钟
GPIO_Set(GPIOC,PIN12,GPIO_MODE_AF,GPIO_OTYPE_PP,GPIO_SPEED_50M,GPIO_PUPD_PU);//PC12,复用功能,上拉输出
GPIO_Set(GPIOD,PIN2,GPIO_MODE_AF,GPIO_OTYPE_PP,GPIO_SPEED_50M,GPIO_PUPD_PU);//PD2,复用功能,上拉输出
GPIO_AF_Set(GPIOC,12,8); //PC12,AF8
GPIO_AF_Set(GPIOD,2,8);
RCC->APB1RSTR |= 1<<20; // 复位串口1
RCC->APB1RSTR &= ~(1<<20); // 停止复位
//波特率设置
UART5->BRR = mantissa; // 波特率设置
UART5->CR1 |= 0X200C; // 1位停止,无校验位.
//使�