C51编程:救命!CYGNAL C8051单片机通信3/3
请教古班主和各路高手有关CYGNAL C8051F000 单片机与PC 通信的问题,
请解答讨论。
新近购置一台CYGNAL C8051F000单片机,感觉的确设计不俗。阅读
MANUAL 后, 发现其内置晶振可以由软件设定为2MHZ,4MHZ,
8MHZ,16MHZ。
同时也可以外接11.059200MHZ,18.432MHZ的晶振。
我想用9600,8,N,1的参数与PC通信,不知道如何编程,CYGNAL C8051F000
单片机的编程指令,是否与普通8051单片机指令相兼容?
曾经采用普通8031单片机,用以下语句,通信(6MHZ晶振,2400,8,N,1)成功:
------------------------------------------------*/
#ifndef MONITOR51
SCON = 0x50; /* SCON: mode 1, 8-bit
UART, enable rcvr */
TMOD = 0x20; /* TMOD: timer 1, mode 2, 8-bit
reload */
PCON = 0x80; /* PCON: Power control frequency
double */
TH1 = 0xf3; /* 2400 bps,
TH1: reload value /6MHz */
TL1 = 0xf3;
TR1 = 1; /* TR1: timer 1 run */
TI = 1; /* TI: set TI to send first char of UART */
#endif
------------------------------------------------*/
)
不知道是否可以如法炮制到CYGNAL系统?另外用TH1 RELOAD value 公
式TH1=256 - 2fosc/(384 X bps) [SMOD=1时];
TH1=256 - fosc/(384 X bps) [SMOD=0时], 采用11.0592MHZ晶振,
9600,8,N,1的参数, 计算得TH1=250 [SMOD=1时];
TH1=253 [SMOD=0时];
上述公式是否适用于晶振为2MHZ,4MHZ, 8MHZ,16MHZ? 菜鸟本人不
明白用上述晶振, CYGNAL系统怎样与PC通信?
以下是CYGNAL 公司提供的Keil C51通信范例AN015SW 3/3部分(因帖子有
16K限制,不能一次发完,故分为3部分), 愚夫难以应用, 敬请高手指教。
// Check Transmit interrupt; service if CCF1 is set.
else if (CCF1){
CCF1 = 0;
//
Clear interrupt flag
switch (SUTXST){
// State 0: Transmit Initiated.
// Here, the user has loaded a
byte to transmit into TDR, and set the
// module 1 interrupt to
initiate the transfer.
// - Transmit START bit
(drop SW_TX)
// - Read PCA0, add one bit
time, & store in module 1 capture registers
// for first bit.
// - Increment TX state
variable.
case 0:
SW_TX = 0;
//
Drop TX pin as START bit.
PCA_TEMP =
PCA0L; // Read PCA
counter value into
PCA_TEMP |=
(PCA0H << 8); // PCA_TEMP.
PCA_TEMP
+= TIME_COUNT; // Add 1 bit time.
PCA0CPL1 =
PCA_TEMP; // Store updated
match value into
PCA0CPH1 =
(PCA_TEMP >> 8); // module 1 capture/compare registers.
PCA0CPM1 |=
0x48; // Enable
module 1 software timer.
SUTXST++;
//
Update TX state variable.
break;
// States 1-9: Transmit Bit.
// - Output LSB of TDR
onto TX
// - Shift TDR 1 bit right.
// - Shift a '1' into MSB of
TDR for STOP bit in State 9.
// - Add 1 bit time to module
1 capture register
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
SW_TX =
(TDR & 0x01); // Output LSB of TDR onto
SW_TX pin.
TDR >>= 1;
//
Shift TDR right 1 bit.
TDR |= 0x80;
//
Shift '1' into MSB of TDR for
// STOP bit in State 9.
PCA_TEMP =
(PCA0CPH1 << 8); // Read module 1 contents into
PCA_TEMP |=
PCA0CPL1; // PCA_TEMP.
PCA_TEMP
+= TIME_COUNT; // Add 1 bit time to
PCA_TEMP
PCA0CPL1 =
PCA_TEMP; // Restore
PCA0CPL1 and PCA0CPH1
PCA0CPH1 =
(PCA_TEMP >> 8); // with the updated value
SUTXST++; // Update TX state variable.
break;
// State 10: Last bit has been
transmitted. Transmit STOP bit
// and end transfer.
// - Transmit STOP bit
// - Set TX Complete
indicator, clear Busy flag
// - Reset TX state
// - Prepare module 1 for next
transfer.
// - Trigger IE7 interrupt if
user-level interrupts enabled.
case 10:
STI = 1;
// Indicate TX complete.
SUTXST = 0;
// Reset TX state.
SW_TX = 1;
//
SW_TX should remain high.
PCA0CPM1 =
0x01; // Disable
module 1 software timer; leave
// interrupt enabled for next transmit.
if (SES){
//
If user-level interrupt support enabled:
EIE2 |= 0x20;
// Enable IE7.
PRT1IF |= 0x80; //
Trigger IE7.
}
STXBSY = 0;
// SW_UART TX free.
break;
}
}
}
//----------------------------------------------------------------------------------------
// USER_ISR: User SW_UART Interrupt Service Routine (IE7 ISR)
// If interrupt-mode test code is enabled, this ISR
// transmits 15 characters and receives 15 characters. This routine is triggered each
// time a SW_UART transmit or receive is completed.
// - Checks receive complete indicator, and services.
// - Checks transmit complete indicator, and services.
// - Checks for transmits or receives that completed during the ISR; if so, triggers
the
// interrupt again.
//
void USER_ISR(void) interrupt 19 { //
IE7 Interrupt Service Routine
PRT1IF &= ~(0x80);
// Clear IE7 interrupt flag
if (SRI){
//
If Receive Complete:
SRI = 0;
// Clear receive flag.
SW_BUF[k++] = RDR;
// Read receive buffer.
if (k==16){
//
If 15 characters have been received:
SREN=0;
// Disable SW_UART Receiver.
}
// Indicate 15 characters received.
}
else if (STI){
// If Transmit
Complete:
STI = 0;
// Clear transmit flag.
if (m<15){ // If less than 15 characters
transmitted:
STXBSY = 1; // Claim
SW_UART Transmitter.
TDR = m++;
// Increment variable,
transmit.
CCF1 = 1;
// Force module 1 interrupt
to initiate TX
}
else
SW_DONE=1;
// Indicate last character
transmitted.
}
if (STI|SRI)
// If SRI or STI
is set, re-trigger
PRT1IF |= 0x80;
// interrupt to
service.
}
//--------------------------------------------------------------------------------------
// HW_UART_ISR: Hardware UART Interrupt Service Routine
// Transmits characters from 1 to 15, and receives 15 characters.
// - Checks receive interrupt, and services.
// - Checks transmit interrupt, and services.
//
void HW_UART_ISR(void) interrupt 4 {
static char i=0;
// Transmit data
variable.
static char j=0;
// Receive data
index.
static idata char HW_BUF[20];
// Receive data buffer.
if (RI){
// If Receive Complete:
RI=0;
// Clear receive flag
HW_BUF[j++] = SBUF;
// Read receive buffer
if (j==15)
//
If 15 characters received:
REN=0;
// Disable HW_UART receiver.
}
else if (TI){
// If Transmit
Complete:
TI = 0;
// Clear transmit flag
if (i<15)
//
If characters left to transmit:
SBUF=i++;
//
Increment variable, transmit.
else
// If 15 characters transmitted,
HW_DONE=1;
// Indicate HW TX finished.
}
}
// End of File
………………………………………………………………………………………
………………………………………………………………
发表时间:2001年11月4日12:06:00