/*************************************************************/
/* this programme for UART Communication */
/* Company: New Future */
/* Programmer: windy */
/* phone: 13891912727 weixin same name */
/* Email: cjfwindy@163.com */
/* Time: 2021-04-08 13:36 */
/* Copyright(C) 2021 New Future All rights reserved */
/*************************************************************/
#include "global.h"
//=================================================================================
unsigned char REC_Data[200],REC_Count=0,Buffer[200], DataLength;
TYPE_BYTE_IO _RAM8;
char* gpUartTxAddress; /* uart transmit buffer address */
int gUartTxCnt; /* uart transmit data number */
//=================================================================================
/*----函数:向PC传输一行字符串----*/
void print(char * head)
{
BYTE i,j;
j = strlen(head);
for (i=0; i<j; i++)
{
send(head[i]);
}
}
//-----------------------------------------------------------------
/*----函数:向串口发送一个字节的数据----*/
void send(BYTE a)
{
UART_CheckOERR();
while (!(USART1->SR & USART_SR_TXE));
USART1->DR = a;
//NOP();
//while ((USART1->SR & USART_SR_TXE) == 0);
while ((USART1->SR & USART_SR_TC) == 0);
}
//-----------------------------------------------------------------
void REC_SER(void)
{
if (bTimeover)
{
bTimeover = 0;
F_UART_First = 1;
}
if (!F_UART_REC) return;
F_UART_REC = 0;
//WriteScanCode(0x400, REC_Count);
REC_Count = 0;
//LCDBeep(3);
// LED1 COLOUR 5A A5 02 F1 01
if((0x02==REC_Data[2]) && (0xF1==REC_Data[3]))
{
Colour = REC_Data[4];
}
// LED2 COLOUR 5A A5 02 F1 01
if((0x02==REC_Data[2]) && (0xF==REC_Data[3]))
{
Colour2 = REC_Data[4];
}
// LED3 COLOUR 5A A5 02 F1 01
if((0x02==REC_Data[2]) && (0xF3==REC_Data[3]))
{
Colour3 = REC_Data[4];
}
// LED4 COLOUR 5A A5 02 F1 01
if((0x02==REC_Data[2]) && (0xF4==REC_Data[3]))
{
Colour4 = REC_Data[4];
}
// LED5 COLOUR 5A A5 02 F1 01
if((0x02==REC_Data[2]) && (0xF5==REC_Data[3]))
{
Colour5 = REC_Data[4];
}
// LED6 COLOUR 5A A5 02 F1 01
if((0x02==REC_Data[2]) && (0xF6==REC_Data[3]))
{
Colour6 = REC_Data[4];
}
}
//-----------------------------------------------------------------
void UARTTX_SER(void)
{
if (0 == gUartTxCnt) return;
if (!(USART1->SR & USART_SR_TC)) return;
UART_CheckOERR();
if( gUartTxCnt > 0 )
{
USART1->DR = *gpUartTxAddress;
gpUartTxAddress++;
gUartTxCnt--;
if (0 == gUartTxCnt)
{
while ((USART1->SR & USART_SR_TC) != 1);
}
}
}
//=================================================================================
// 初始化IO 串口1
// pclk2:PCLK2时钟频率(Mhz)
// bound:波特率
void uart_init(u32 pclk2,u32 bound)
{
float temp;
u16 mantissa;
u16 fraction;
temp = (float)(pclk2*1000000)/(bound*16); // 得到USARTDIV
mantissa = temp; // 得到整数部分
fraction = (temp-mantissa)*16; // 得到小数部分
mantissa <<= 4;
mantissa += fraction;
RCC->APB2ENR |= 1<<2; // 使能PORTA口时钟
RCC->APB2ENR |= 1<<14; // 使能串口时钟
GPIOA->CRH &= 0XFFFFF00F; // IO状态设置
GPIOA->CRH |= 0X000008B0; // IO状态设置
RCC->APB2RSTR |= 1<<14; // 复位串口1
RCC->APB2RSTR &= ~(1<<14); // 停止复位
//波特率设置
USART1->BRR = mantissa; // 波特率设置
USART1->CR1 |= 0X200C; // 1位停止,无校验位.
//使能接收中断
USART1->CR1 |= 1<<8; // PE中断使能
USART1->CR1 |= 1<<5; // 接收缓冲区非空中断使能
MY_NVIC_Init(3,3,USART1_IRQn,2); // 组2,最低优先级
}
//-----------------------------------------------------------------
BYTE UART_CheckOERR(void)
{
BYTE volatile Temp; // volatile 防止被优化
if(USART1->SR & 0x000F) // 1: Overrun error is detected
{
Temp = USART1->SR;
Temp = USART1->DR;
//Temp++;
return 1;
}
return 0;
}
//-----------------------------------------------------------------
void putch(BYTE a)
{
/* output one byte */
while(!(USART1->SR & USART_SR_TXE)) /* set when register is empty */
continue;
USART1->DR = a;
while(!(USART1->SR & USART_SR_TC)) // 1:发送完成
continue;
}
//-----------------------------------------------------------------
BYTE getch(void)
{
/* retrieve one byte */
while(!(USART1->SR & USART_SR_RXNE)) /* set when register is not empty */
continue;
return USART1->DR;
}
//-----------------------------------------------------------------
void UART_SendData( char* txbuf, int txnum )
{
if( txnum < 1 ){
return;
}
UART_CheckOERR();
while (!(USART1->SR & USART_SR_TXE));
gpUartTxAddress = txbuf;
gUartTxCnt = txnum;
if( txnum == 1)
{
USART1->DR = *gpUartTxAddress;
gUartTxCnt--;
}
else
{
USART1->DR = *gpUartTxAddress;
gpUartTxAddress++;
gUartTxCnt--;
}
}
//=================================================================================