📄 uartlib.c
字号:
/***********************************************/
/* */
/* DSP BF532/3串口设置程序 */
/* */
/***********************************************/
#include <cdefBF532.h>
#include <ccblkfn.h>
#include <stdio.h>
#include <sys\exception.h>
#include <stdlib.h>
#include <math.h>
#include "uartlib.h"
//extern volatile BYTE uart_recv_buf[];
//extern volatile WORD uart_recv_cnt;
//DWORD MEGA = 1000000; // MHz
#define BAUD 19200
#define SCLK 133000000
////////////////////////////////////////////////
//串口初始化
////////////////////////////////////////////////
void UART_Init(void)
{
//enable UART clock
int iDivisor = 0;
*pUART_GCTL = UCEN;
//enable access to UART_DL registers
*pUART_LCR = DLAB;
//set the the two 8-bit DL registers (DLH:DLL)
//*pUART_DLL = 0x41;
//*pUART_DLH = 0x0; //
//iDivisor = 0x57e;//UART_getSCLK()/BAUD*16;
//iDivisor = UART_getSCLK()/(BAUD*16);
iDivisor = SCLK/(BAUD*16);
*pUART_DLL = iDivisor;
*pUART_DLH = iDivisor>>8;
//set UART frame to 8 bits, no parity, 1 stop bit.
*pUART_LCR = WLS(8);
//start the uart interrupt
*pUART_IER = ELSI | ERBFI;
}
void UART_putc(BYTE DATA)
{
while (!(*pUART_LSR & THRE)) {}; //wait
*pUART_THR = DATA;
}
//////////////////////////////////////////////////////////////
void UART_puts(BYTE* src,WORD len)
{
WORD i;
for(i=0;i<len;i++)
{
UART_putc((BYTE)(*(src+i)));
//src++;
}
}
// 计算CRC校验码
unsigned short CalCRC(BYTE* pchData, DWORD iLen)
{
BYTE pchCRCH = 0xFF ;
BYTE pchCRCL = 0xFF ;
BYTE uIndex;
while(iLen--)
{
uIndex = pchCRCH ^ *pchData++;
pchCRCH = pchCRCL ^ auchCRCHi[uIndex];
pchCRCL = auchCRCLo[uIndex];
}
return (pchCRCH << 8 | pchCRCL) ;
}
//***********************************************************************/
//
// Routine : Delay
// Input : None
// Output : None
// Function : 延时1ms
//
//***********************************************************************/
void DelayMili(int uiCount)
{
int uiTemp;
int uiCountTemp;
for(uiTemp=0;uiTemp<uiCount;uiTemp++)
{
for(uiCountTemp=0;uiCountTemp<14500;uiCountTemp++);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -