27,383
社区成员
发帖
与我相关
我的任务
分享
#ifndef __UART_H
#define __UART_H
#include "LPC17xx.h"
#define FOSC 12000000 /* 振荡器频率 */
#define FCCLK (FOSC * 8) /* 主时钟频率<=100Mhz */
/* FOSC的整数倍 */
#define FCCO (FCCLK * 3) /* PLL频率(275Mhz~550Mhz) */
/* 与FCCLK相同,或是其的偶数倍 */
#define FPCLK (FCCLK / 4) /* 外设时钟频率,FCCLK的1/2、1/4*/
/* 或与FCCLK相同 */
#define UART0_BPS (38400) /* 串口0通信波特率 */
#define UART3_BPS (19200) /* 串口3通信波特率 */
//#define UART_SBUF (LPC_UART0->RBR) /*串口0发送*/
#endif
/*********************************************************************************************************
** Function name: UART0_Init
** Descriptions: 按默认值初始化串口0的引脚和通讯参数。设置为8位数据位,1位停止位,无奇偶校验
** input parameters: 无
** output parameters: 无
** Returned value: 无
*********************************************************************************************************/
void UART0_Init (void)
{
uint16_t usFdiv;
/* UART0 */
LPC_PINCON->PINSEL0 |= (1 << 4); /* Pin P0.2 used as TXD0 (Com0) */
LPC_PINCON->PINSEL0 |= (1 << 6); /* Pin P0.3 used as RXD0 (Com0) */
LPC_UART0->LCR = 0x83; /* 允许设置波特率 */
usFdiv = (FPCLK / 16) / UART0_BPS; /* 设置波特率 38400 */
LPC_UART0->DLM = usFdiv / 256;
LPC_UART0->DLL = usFdiv % 256;
LPC_UART0->LCR = 0x03; /* 锁定波特率 */
LPC_UART0->FCR = 0x06;
LPC_UART0->IER = 0x01; /* 使能接收中断 */
NVIC_EnableIRQ(UART0_IRQn); /* enable irq in nvic */
NVIC_SetPriority(UART0_IRQn, 0x02); /*中断优先级最高*/
}
/*********************************************************************************************************
** Function name: UART0_SendByte
** Descriptions: 从串口0发送数据
** input parameters: data: 发送的数据
** output parameters: 无
** Returned value: 无
*********************************************************************************************************/
int UART0_SendByte (int ucData)
{
while (!(LPC_UART0->LSR & 0x20));
return (LPC_UART0->THR = ucData);
}
//*******************************************************************
#define RS485_1 ((uint32_t)1<<22)//P1.22 485控制脚
#define RS485_1_H (LPC_GPIO1->FIOSET =RS485_1) //RS485_1 置高 发送
#define RS485_1_L (LPC_GPIO1->FIOCLR =RS485_1) //RS485_1 置低 接收
if(call==1)//接收到正常数据后回复
{
TempBuf[0]=0xbb; TempBuf[1]=0x10; TempBuf[2]=0x06; TempBuf[3]=0x21;
TempBuf[4]=0x01; TempBuf[5]=0x38; TempBuf[6]=0x00; TempBuf[7]=0x55;
Delay(50);//延时
RS485_1_H;//485置高 发送
Delay(8);//大概2ms
for(i=0; i<8; i++)
{
UART0_SendByte(TempBuf[i]);
}
Delay(8);//大概2ms
RS485_1_L;//485置低 接收
call=0;
}