串口SBUF0寄存器写不进去数据
void UART0_Init (void)
{
U8 SFRPAGE_save = SFRPAGE;
SFRPAGE = CONFIG_PAGE;
value=0x60;
baud(value); //波特率选择
//BAUDRATE =9600;
SCON0 = 0x10; // SCON0: 8-bit variable bit rate
// clear RI0 and
ti0 bits
// Baud Rate = [BRG Clock / (65536 - (SBRLH0:SBRLL0))] x 1/2 x 1/Prescaler
if ((SYSCLK / BAUDRATE / 2 / 0xFFFF) < 1)
{
SBRL0 = -(SYSCLK / BAUDRATE / 2);
SBCON0 |= 0x03; // Set prescaler to 1
}
else if ((SYSCLK / BAUDRATE / 2 / 0xFFFF) < 4)
{
SBRL0 = -(SYSCLK / BAUDRATE / 2/ 4);
SBCON0 &= ~0x03;
SBCON0 |= 0x01; // Set prescaler to 4
}
else if ((SYSCLK / BAUDRATE / 2 / 0xFFFF) < 12)
{
SBRL0 = -(SYSCLK / BAUDRATE / 2 / 12);
SBCON0 &= ~0x03; // Set prescaler to 12
}
else
{
SBRL0 = -(SYSCLK / BAUDRATE / 2 / 48);
SBCON0 &= ~0x03;
SBCON0 |= 0x02; // Set prescaler to 48
}
SBCON0 |= 0x40; // Enable baud rate generator
//SCON0 = 0x10;
ES0 = 1; // Enable UART0 interrupts
// TX_Ready = 1; // Flag showing that UART can transmit
SFRPAGE = SFRPAGE_save;
}
//-----------------------------------------------------------------------------
// 串口单字节发送函数
//----------------------------------------------------------------------------
void UART0_Send_Byte(U8 U_Data)
{
U8 Txdata;
U8 SFRPAGE_save= SFRPAGE ;
ES0=0; //关串口0中断
SFRPAGE = CONFIG_PAGE;
DE=0; //串口发送
Txdata=U_Data;
SBUF0 =Txdata;
//SBUF0数据一直不变
while(TI0==0);
//等待发送完成 程序停在此处
TI0=1; //清0发送完成中断标志位
//SFRPAGE = SFRPAGE_save;
//SFRPAGE = CONFIG_PAGE;
DE=1; //接收
SFRPAGE = SFRPAGE_save;
}
//-----------------------------------------------------------------------------
// 串口发送函数
//-----------------------------------------------------------------------------
void UART0_Send(int count)
{
U8 Txdata0;
U8 SFRPAGE_save = SFRPAGE;
int txdatal;
for(txdatal=0;txdatal
{
Txdata0=UART_T_Buffer[txdatal];
UART0_Send_Byte(Txdata0);
}
SFRPAGE = SFRPAGE_save;
}
//-----------------------------------------------------------------------------
// 485采集
//-----------------------------------------------------------------------------
void Equi_Acquisition()
{
Byte_order=0x02; //字节顺序
Date_type=0x02; //数据类型
UART_T_Buffer[0]=0xD3;
UART_T_Buffer[1]=0x03;
UART_T_Buffer[2]=0x91;
UART_T_Buffer[3]=0x00;
UART_T_Buffer[4]=0x00;
UART_T_Buffer[5]=0x02;
UART_T_Buffer[6]=0xFA;
UART_T_Buffer[7]=0x85;
UART0_Send(8);
ES0=1;
RS485_Work();
}
//-----------------------------------------------------------------------------
// 波特率选择函数
//-----------------------------------------------------------------------------
void baud(U8 value)
{
switch(value) //波特率选择
{case 0x20:{BAUDRATE =115200;break; }
case 0x40:{BAUDRATE =57600; break; }
case 0x80:{BAUDRATE =38400; break; }
case 0xCD:{BAUDRATE =19200; break; }
case 0x60:{BAUDRATE =9600; break; }
case 0x30:{BAUDRATE =4800; break; }
case 0x18:{BAUDRATE =2400; break; }
case 0x0C:{BAUDRATE =1200; break; }
}
}
//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// UART0_Interrupt
//-----------------------------------------------------------------------------
//
// This routine is invoked whenever a character is entered or displayed on the
// Hyperterminal.
//
//-----------------------------------------------------------------------------
INTERRUPT(UART0_ISR, INTERRUPT_UART0)
{
U8 SFRPAGE_save = SFRPAGE;
//SFRPAGE=UART0_PAGE;
ES0=0;
if (RI0 == 1)
{
if( UART_Buffer_Size == 0) // If new word is entered
{
UART_Input_First = 0;
}
RI0 = 0; // Clear interrupt flag
Byte = SBUF0; // Read a character from UART
UART_R_Buffer[UART_Input_First] = Byte; // Store in array
UART_Buffer_Size++; // Update array's size
UART_Buffer_Lenth=UART_Buffer_Size;
UART_Input_First++; // Update counter
//SFRPAGE = TIMER01_PAGE;
TH1 = (65535-24000000/BAUDRATE)/256;
TL1 = (65535-24000000/BAUDRATE)%256;
TR1 = 1; //开启定时器1
ET1 = 1; //开启定时器1中断
SFRPAGE=SFRPAGE_save;
}
RI0=0; //清除接收标志
UART_Buffer_Size=0;
SFRPAGE = CONFIG_PAGE;
SFRPAGE=SFRPAGE_save;
}
//-----------------------------------------------------------------------------
// 定时器1中断
//-----------------------------------------------------------------------------
INTERRUPT (TIMER1_ISR, INTERRUPT_TIMER1)
{
TR1 = 0;//停止定时器1
ET1 = 0;//禁止定时器1中断
ES0 = 0;//关闭串口中断
TF1 = 0;//清除定时器1标志
}
0