演示STC89xx系列单片机串行口功能 (8-bit/9-bit)
Posted 互联星空
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了演示STC89xx系列单片机串行口功能 (8-bit/9-bit)相关的知识,希望对你有一定的参考价值。
1 /*------------------------------------------------------------------------------------*/ 2 /* --- STC MCU Limited ---------------- ---------------------------------------*/ 3 /* ---演示STC89xx系列单片机串行口功能 (8-bit/9-bit) ----------------*/ 4 /* --- Mobile: (86)13922809991 -----------------------------------------------*/ 5 /* --- Fax: 86-755-82905966 ---------------------------------------------------*/ 6 /* --- Tel: 86-755-82948412 ----------------------------------------------------*/ 7 /* --- Web: www.STCMCU.com -----------------------------------------------*/ 8 /*如果要在程序中使用?在文章中引用该程序, -----------------------*/ 9 /*请在程序中?文章中注明使用了STC的资料及程序 -----------*/ 10 /*-------------------------------------------------------------------------------------*/ 11 #include "reg51.h" 12 #include "intrins.h" 13 14 typedef unsigned char BYTE; 15 typedef unsigned int WORD; 16 17 #define FOSC 18432000L /* System frequency 系统频率 */ 18 #define BAUD 9600 /* UART baudrate 波特率 */ 19 20 21 /*Define UART parity mode*/ 22 #define NONE_PARITY 0 /* None parity 奇偶*/ 23 #define ODD_PARITY 1 /* Odd parity 奇*/ 24 #define EVEN_PARITY 2 /* Even parity 偶*/ 25 #define MARK_PARITY 3 /* Mark parity */ 26 #define SPACE_PARITY 4 /* Space parity */ 27 28 29 #define PARITYBIT EVEN_PARITY /* Testing even parity 测试*/ 30 31 sbit bit9 = P2 ^ 2; /* P2.2 show UART data bit9 */ 32 bit busy; 33 34 void SendData( BYTE dat ); 35 36 37 void SendString( char *s ); 38 39 40 void main() 41 { 42 #if (PARITYBIT == NONE_PARITY) 43 SCON = 0x50; /* 8-bit variable UART */ 44 45 #elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY) 46 SCON = 0xda; /* 9-bit variable UART, parity bit initial to 1 */ 47 #elif (PARITYBIT == SPACE_PARITY) 48 SCON = 0xd2; /* 9-bit variable UART, parity bit initial to 0 */ 49 50 51 #endif 52 53 TMOD = 0x20; /* Set Timer1 as 8-bit auto reload mode */ 54 //TH1 = TL1 = -(FOSC/12/32/BAUD); /* Set auto-reload vaule */ 55 TH1 = 0xfd; 56 TL1 = 0xfd; 57 TR1 = 1; /* Timer1 start run */ 58 ES = 1; /* Enable UART interrupt */ 59 EA = 1; /* Open master interrupt switch */ 60 61 SendString( "STC89-90xx\r\nUart Test !\r\n" ); 62 while ( 1 ); 63 } 64 65 66 /*---------------------------- 67 * UART interrupt service routine 68 * ----------------------------*/ 69 void Uart_Isr() interrupt 4 using 1 70 { 71 if ( RI ) 72 { 73 RI = 0; /* Clear receive interrupt flag */ 74 P0 = SBUF; /* P0 show UART data */ 75 bit9 = RB8; /* P2.2 show parity bit */ 76 } 77 78 79 if ( TI ) 80 { 81 TI = 0; /* Clear transmit interrupt flag */ 82 busy = 0; /* Clear transmit busy flag */ 83 } 84 } 85 86 87 /*---------------------------- 88 * Send a byte data to UART 89 * Input: dat (data to be sent) 90 * Output:None 91 * ----------------------------*/ 92 void SendData( BYTE dat ) 93 { 94 while ( busy ); /* Wait for the completion of the previous data is sent */ 95 ACC = dat; /* Calculate the even parity bit P (PSW.0) */ 96 if ( P ) /* Set the parity bit according to P */ 97 98 99 { 100 #if (PARITYBIT == ODD_PARITY) 101 TB8 = 0; /* Set parity bit to 0 */ 102 #elif (PARITYBIT == EVEN_PARITY) 103 TB8 = 1; /* Set parity bit to 1 */ 104 105 106 #endif 107 }else { 108 #if (PARITYBIT == ODD_PARITY) 109 TB8 = 1; /* Set parity bit to 1 */ 110 #elif (PARITYBIT == EVEN_PARITY) 111 TB8 = 0; /* Set parity bit to 0 */ 112 #endif 113 } 114 busy = 1; 115 SBUF = ACC; /* Send data to UART buffer */ 116 } 117 118 119 /*---------------------------- 120 * Send a string to UART 121 * Input: s (address of string) 122 * Output:None 123 * ----------------------------*/ 124 void SendString( char *s ) 125 { 126 while ( *s ) /* Check the end of the string */ 127 128 { 129 SendData( *s++ ); /* Send current char and increment string ptr */ 130 } 131 }
以上是关于演示STC89xx系列单片机串行口功能 (8-bit/9-bit)的主要内容,如果未能解决你的问题,请参考以下文章