No.2356 作者:AbrahamGwang 邮件:AbrahamGwang@yahoo.com ID:6339 登陆:1次 文章数:6篇 最后登陆IP: 最后登陆:2001/8/16 19:47:00 注册:2001/8/16 19:47:00 财富:28 发帖时间:2001/11/4 12:03:00 发贴者IP:172.149.153.82 标题:AbrahamGwang:C51编程:救命!CYGNAL C8051单片机通信2/3 摘要:No.2356C51编程:救命!CYGNAL C8051单片机通信2/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 2/3部分(因帖子有 16K限制,不能一次发完,故分为3部分), 愚夫难以应用, 敬请高手指教。 //--------------------------------------------------------------------------------------- // POLLED_TEST: SW_UART Polled Mode Test // Test code to transmit and receive 15 characters to/from the HW_UART, with SW_UART // in polled mode. // - Initializes and enables the SW_UART & HW_UART // - Clears all test variables & counters // - Sends 15 characters from the HW_UART to be received by SW_UART. // - Sends 15 characters from the SW_UART to be received by the HW_UART. // void POLLED_TEST(void){ SW_UART_INIT(); // Initialize SW_UART SW_UART_ENABLE(); // Enable SW_UART SREN = 1; // Enable SW_UART Receiver SES=0; // Disable user-level interrupt support. HWU_INIT(); // Configure HW_UART for testing routine. k=m=0; // Clear test counter variables. HW_DONE=0; // Clear transfer complete indicators SW_DONE=0; // IE |= 0x10; // Enable HW_UART interrupts. TI = 1; // Initiate a HW_UART transmit // by forcing TX interrupt. // Receive 15 characters with SW_UART; transmit with HW_UART. while(SREN){ // Run while SW_UART Receiver is // enabled. if (SRI){ // If Receive Complete: SRI = 0; // Clear receive flag. SW_BUF[k++] = RDR; // Read receive buffer. if (k==15){ // If 15 characters have been received: SREN = 0; // Disable SW_UART Receiver. } // Indicate 15 characters received. } } // Transmit 15 characters with SW_UART; receive with HW_UART. while(STXBSY); // Poll Busy flag. STXBSY = 1; // Claim SW_UART Transmitter TDR=m++; // Load TX data. CCF1=1; // Initiate first SW_UART TX // by forcing a PCA module 1 interrupt. while(!SW_DONE){ // SW_UART transmitting here; HW_UART receiving. if (STI){ // If Transmit Complete: STI = 0; // Clear transmit flag. if (m<16){ // Transmit 15 characters. STXBSY = 1; // Claim SW_UART Transmitter. TDR = m++; // Transmit, increment variable. CCF1 = 1; // Force module 1 interrupt to initiate TX. } else // If this is 15th character, SW_DONE=1; // Indicate last character transmitted. } } } //------------------------------------------------------------------------------------------ // HWU_INIT: HW_UART Initialization Routine // Sets up HW_UART for use in SW_UART testing. // - HW_UART in mode 1 // - Timer 1 used as baud rate source, clocked by SYSCLK. // void HWU_INIT(void) { PCON |= 0x80; // SMOD=1 (HW_UART uses Timer 1 overflow // with no divide down). TMOD = 0x20; // Configure Timer 1 for use by HW_UART CKCON |= 0x10; // Timer 1 derived from SYSCLK TH1 = -HW_TIME_COUNT; // Timer 1 initial value TL1 = -HW_TIME_COUNT; // Timer 1 reload value TR1 = 1; // Start Timer 1 RI=0; // Clear HW_UART receive and transmit ......
>>返回讨论的主题
|