单片机是SILICON的8051F410,MAX485芯片
代码和电路图如下,程序功能是当上位机通过串口有东西发下去时,单片机会进入接收中断返回对应的值
问题是我上位机还没发东西下去,单片机就不断返回给我SBUF0的值,而且都是0
一开始分析是485芯片A,B两点间按图中所连的电压差大于0.2V导致MAX485芯片RO输出为0,但是把R9去掉以后
还是一样不停接收0,求各位大牛看看到底问题出在哪里了,感激不尽
ps:是否需要给RX增加内部弱上拉?
#include <c8051F410.h>
#define SYSCLK 24500000
#define BAUDRATE 9600
unsigned char UART_Buffer[64];
unsigned char UART_Data=0;
unsigned char Byte;
unsigned char temp;
sbit comm=P0^7;
sbit LASER=P2^6;
//P0.4 digital push_pull UART TX
//P0.5 digital open_drain UART RX
void PORT_Init(void)
{
//P2MDout=0x00;
P0MDOUT|=0x90; // Enable UTX as push-pull output
XBR0=0X01; // Enable UART on P0.4(TX) and P0.5(RX)
XBR1=0X40; // Enable crossbar and weak pull-ups
}
void SYSCLK_Init (void)
{
OSCICN=0X87; // Configure internal oscillator for 24.5MHz
RSTSRC=0X04; // Enable missing clock detector
}
// Configure the UART0 using Timer1, for <BAUDRATE> and 8-N-1.
void UART0_Init(void)
{
SCON0 = 0x10; // SCON0: 8-bit variable bit rate
// level of STOP bit is ignored
// RX enabled
// ninth bits are zeros
// clear RI0 and TI0 bits
TH1 = 0x96;
CKCON &= ~0x0B;
TL1 = TH1; // init Timer1
TMOD &= ~0xf0; // TMOD: timer 1 in 8-bit autoreload
TMOD |= 0x20;
TR1 = 1; // START Timer1
IP |= 0x10; // Make UART high priority
ES0=1;
}
void UART0_Interrupt(void)interrupt 4
{
//char i;
if (RI0==1)
{
RI0=0;
Byte=20; // Clear interrupt flag
//temp=SBUF0; // Read a character from UART
comm=0;
LASER=0;
}
}
void main(void)
{
unsigned int i, j;
PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer enable)
PORT_Init(); // Initialize Port I/O
SYSCLK_Init (); // Initialize Oscillator
UART0_Init();
EA=1;
temp=10;
LASER=1;
comm=0;
UART_Data=0;
UART_Data=14;
Byte=1;
comm=0;
while(1)
{
if(Byte==20)
{
for (i=0; i<UART_Data; i++)
{
UART_Buffer=temp;
}
comm=1;
for(i=0;i<14;i++)
{
SBUF0 = UART_Buffer;
while(!TI0);
TI0 = 0;
}
Byte=1;
comm=0;
}
}
} |