各位大神,我用的是C8051F330的單片機,串口發送和接收都沒有問題,我在進入串口中斷后,讀串口,把串口讀到的值賦給全局變量time,如果讀到的是F1,延時函數就delay()就是5s,但是好像不起作用,不知道是不是time的作用域有問題?
#include <c8051f330.h> // SFR declarations
#include <stdio.h>
//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------
#define SYSTEMCLOCK 24500000 // SYSCLK frequency in Hz
#define BAUDRATE 9600 // Baud rate of UART in bps
//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------
void SYSCLK_Init (void);
void UART0_Init (void);
void PORT_Init (void);
void Timer2_Init (int);
//-----------------------------------------------------------------------------
// Global Variables
//-----------------------------------------------------------------------------
char Byte,flag;
unsigned char temp1;
unsigned int time;
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void delay()
{
unsigned int i,j;
for(i=0;i<time;i++)//100和30000約1s
{
for(j=0;j<30000;j++)//30000是0.008s
{
}
}
}
void main (void)
{
P1=0x00;
temp1=0x00;
time=1500;
PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer
// enable)
PORT_Init(); // Initialize Port I/O
SYSCLK_Init (); // Initialize Oscillator
UART0_Init();
EA = 1;
while(1)
{
if(temp1<101)//<100
{
P1=temp1;
SBUF0=temp1;
while(!TI0);
TI0=0;
delay();
temp1++;
}
else if(temp1==101)
{
temp1=0x80;
for(temp1=0x80;temp1<0xe5;temp1++)
{
P1=temp1;
delay();
SBUF0=temp1;
while(!TI0);
TI0=0;
}
temp1=0x00;
}
}
}
void PORT_Init (void)
{
P0MDOUT |= 0x10; // Enable UTX as push-pull output
P1MDOUT |= 0xff;
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 |= 0x03; // Configure internal oscillator for
// its maximum frequency
RSTSRC = 0x04; // Enable missing clock detector
}
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
if (SYSTEMCLOCK/BAUDRATE/2/256 < 1) {
TH1 = -(SYSTEMCLOCK/BAUDRATE/2);
CKCON &= ~0x0B; // T1M = 1; SCA1:0 = xx
CKCON |= 0x08;
} else if (SYSTEMCLOCK/BAUDRATE/2/256 < 4) {
TH1 = -(SYSTEMCLOCK/BAUDRATE/2/4);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 01
CKCON |= 0x01;
} else if (SYSTEMCLOCK/BAUDRATE/2/256 < 12) {
TH1 = -(SYSTEMCLOCK/BAUDRATE/2/12);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 00
} else {
TH1 = -(SYSTEMCLOCK/BAUDRATE/2/48);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 10
CKCON |= 0x02;
}
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; // Enable UART0 interrupts
}
void UART0_Interrupt (void) interrupt 4
{
unsigned int ab;
if(RI0==1)
{
RI0 = 0; // Clear interrupt flag
Byte = SBUF0; // Read a character from UART
P1=Byte;
SBUF0=Byte;
while(!TI0);
TI0=0;
if(Byte==0xF1)
{
time=500;
}
else if(Byte==0xF2)
{
time=1000;
}
else if(Byte==0xF3)
{
time=1500;
}
else if(Byte==0xF4)
{
time=2000;
}
else
{
P1=Byte;
}
flag=1;
}
}
|