使用Keil 注册过的 IDE-Version: 礦ision3 V3.34 Copyright (c) Keil Elektronik GmbH / Keil Software, Inc. 1995 - 2006
License Information: mao hall ailansoft LIC=EXT9L-0FAGJ-ANFAZ-Y4U1E-SBXW4-CKWPP
Tool Version Numbers: Toolchain Path: E:\Program Files\Keil\C51\BIN\ C Compiler: C51.Exe V8.06 Assembler: A51.Exe Linker/Locator: BL51.Exe V6.05 Librarian: LIB51.Exe V4.24 Hex Converter: OH51.Exe V2.6 CPU DLL: S8051.DLL V3.10 Dialog DLL: DP51.DLL V2.48f Target DLL: BIN\VDM51.DLL Dialog DLL: TP51.DLL V2.48f
下面是我的程序::
#include <reg52.h>
#define uchar unsigned char #define uint unsigned int
sbit LED0 = P1^0; sbit LED1 = P1^1; sbit Sw = P1^2; uchar buffer[8]; uchar sign = 0; uchar rev_num = 0;
void delay(uint time) { int i = 1000; for(time;time>0;time--) for(i;i>0;i--); }
void UART_init() /*串口初始化*/ { SCON = 0x50; /*串行口工作方式1,10位异步收发*/ TMOD =0x20; /* TMOD: timer 1, mode 2, 8-bit reload */ TH1 = 243; /* TH1: reload value for 2400 baud @ 6MHz */ TL1 = 243; PCON = 0x80; /*波特率加倍*/ RI = 0; //清收发标志 TI = 0; TR1 = 1; /*启动T/C1*/ }
void serial() interrupt 4 { RI=0; buffer[rev_num] = SBUF; rev_num += 1; if(rev_num > 6) { sign = rev_num; rev_num &= 0x00; } }
void main() {
uchar i=0; P0 = 0; P2 = 0; P1 = 0; LED0 = 1; LED1 = 1; Sw = 1; UART_init(); EA=1; ES=1; //开串口中断 while(1) { if(sign>0 ) { SBUF = 'A'; while(!TI); TI = 0; for(i=0;i< sign;i++) { SBUF = buffer; TI = 0; while(!TI); SBUF = i + 0x30; TI = 0; while(!TI); }
sign &= 0x00; LED1 = ~LED1; sign &= 0x00; delay(1000); }else{ delay(20000); LED0 = ~LED0; LED1 = 1; } } }
这是整个代码,我想从PC发一个字符串,然后单片机接收到再把整个字符串发给PC机,但是这样实际却不是这样. 比如我发送abcdefg,单片机发回来是ggggggg,并且一直隔几百ms就发送一次,停不下来.并且并不是执行整个for循环,只发送g,而不发送'A'. 请问各位,问题出在哪里呢????
|