其次,你把相同的数据通过单片机发到PC上,你看看接收到的数据和你先前发出去的是否一致。
最后,根据比较结果修改单片机通讯协议。
如果,协议没有问题,那就是你程序中其他地方的错了。
另外,你的波特率没问题。
#include "Include/c8051f120.h"// SFR declarations#include "Include/system.h"// SFR declarations
void UART0_Init (void)
{
unsigned char T1_PRESCALE
unsigned char T1_RELOAD
unsigned long BAUD
BAUD = 38400
if ((SYSCLK/BAUD/32/1)<256)
{
T1_PRESCALE = 0x10
T1_RELOAD = -((SYSCLK/BAUD/16/1+1)/2)
}
else if ((SYSCLK/BAUD/32/4)<256)
{
T1_PRESCALE = 0x01
T1_RELOAD = -((SYSCLK/BAUD/16/4+1)/2)
}
else if ((SYSCLK/BAUD/32/12)<256)
{
T1_PRESCALE = 0x00
T1_RELOAD = -((SYSCLK/BAUD/16/12+1)/2)
}
else if ((SYSCLK/BAUD/32/48)<256)
{
T1_PRESCALE = 0x02
T1_RELOAD = -((SYSCLK/BAUD/16/48+1)/2)
}
SFRPAGE = UART0_PAGE
SCON0 = 0x50 // SCON0: 8-bit variable bit rate
// RX enabled
// SCON0 |= 0xd0 // SCON0: 8-bit variable bit rate
SFRPAGE = TIMER01_PAGE
CKCON &= ~0x1B // clear T1M, SCA1, SCA0
CKCON |= T1_PRESCALE // set T1M, SCA1, SCA0 according to BAUD
TH1 = T1_RELOAD // reload value calculated from BAUD
TL1 = T1_RELOAD // also load into TL1
TMOD &= ~0xf0 // clear T1 bits in TMOD
TMOD |= 0x20 // set TMOD for 8 bit reload
TR1 = 1 // START Timer1
//added by edward to test the priority settings of UART0. If this line is here, means good.
IP |= 0x10
SFRPAGE = UART0_PAGE
TI0 = 1 // Indicate TX0 ready
ES0 = 1
}
void UartRun(void)
{
unsigned char Receive_userData
if (RI0)
{
Receive_userData = SBUF0
UART0_SEND(Receive_userData)
RI0 = 0
}
if (TI0)
{
TI0 = 0
}
}
void UART0_ISR (void) interrupt 4
{
_push_(SFRPAGE)
SFRPAGE = UART0_PAGE
UartRun()
_pop_(SFRPAGE)
}
这个是串口输入什么就显示什么的例子,如果需要打印的话你可以加入打印的函数print()
{UartRun()
}
放在主函数体里就可以
1,硬件连接 一般有 串口方式 和 并口方式,设计时 由打印机确定。2,直接根据硬件连接状态 编程就可以了。
3,打印机 应该 有说明书,里面有数据 传送方式,
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)