jason-poul

|
分享:
▲
▼
在这部分, 不管是使用 assembly / C 都应该不会有多少差异, 因为不会因为语法的不同而导致你的Uart不能动!!
一般使用到RS-232(即Uart), 则会有Baud Rate的问题, 要先设好Buad Rate(看你是使用9600/14400/19200/38400/57600/115200... ...), 接者就要看你的datasheet, 你可以到那家厂商的网站下载sampo code, 一般就uart的部分, 都有完整的sampo code。
我有一些code可以给你参考, 其余就靠你努力了!!
#define Uart_19200 0xEE // 19200 Bps, At 11.0592 Meg. #define Uart_38400 0xF7 // 38400 Bps, At 11.0592 Meg. #define Uart_57600 0xFA // 57600 Bps, At 11.0592 Meg. #define Uart_115200 0xFD // 115200 Bps, At 11.0592 Meg
// Name : Uart_Select_Baud_Rate // US_Baud_Rate. (Can Be Uart_19200/Uart_38400/Uart_57600/Uart_115200) void Uart_Select(unsigned char US_Baud_Rate) { T2CON = 0x34; // 0011 1000, // TF2 = 0, EXF2 = 0, RCLK = 1, TCLK = 1, // EXEN2 = 1, TR2 = 0, C/T2 = 0, CP/RL2 = 0. TL2 = US_Baud_Rate; // Set Count. TH2 = 0xFF; // Set Up Count. RCAP2L = US_Baud_Rate; // Set Count. RCAP2H = 0xFF; // Set Up Count. SCON = 0x50; // 0101 0000, // SM0 = 0, SM1 = 1 (8 BIT VARIABLE), SM2 = 0 (NO MULTIMODE), REN = 1 (SERIAL RECEPTION), // TB8 = 0, RB8 = 0, TI = 0, RI = 0. ES = 1; // Enable Uart Interrupt. EA = 1; // Global Int Enable. TR2 = 1; // Timer 2 Enable. }
// Local Variable Definition #define Receive_Buffer_Size 128 // Receive Buffer Size. (0 - 255) bit Receive_Get = 0; unsigned char xdata Receive_Buffer_Index = 0; unsigned char xdata Receive_Buffer[Receive_Buffer_Size] = {0}; // Receive_Buffer.
// Name : Uart_Interrupt void Uart_Interrupt(void) interrupt 4 { if (RI == 1) { if (Receive_Get == 0) // If Receive_Buffer Not Full. { if ((SBUF >= 0x21) && (SBUF <= 0x7E)) // Only ASCII From 0x21 To 0x7E Into Receive_Buffer. { Receive_Buffer[Receive_Buffer_Index] = SBUF; Receive_Buffer_Index++;
if (Receive_Buffer_Index == Receive_Buffer_Size) Receive_Get = 1; } }
RI = 0; // Clear RI (Receive Interrupt). } if (TI == 1) { Transmit_Busy = 0; // Mark Transmit Not Busy. TI = 0; // Clear TI (Transmit Interrupt). } }
[ 此文章被jason-poul在2009-05-10 01:00重新编辑 ]
此文章被评分,最近评分记录财富:20 (by 三仙) | 理由: 因为您的参与,让程式设计更容易!! | |
|
|
|