|
|||||||||||
| 技術(shù)交流 | 電路欣賞 | 工控天地 | 數(shù)字廣電 | 通信技術(shù) | 電源技術(shù) | 測控之家 | EMC技術(shù) | ARM技術(shù) | EDA技術(shù) | PCB技術(shù) | 嵌入式系統(tǒng) 驅(qū)動(dòng)編程 | 集成電路 | 器件替換 | 模擬技術(shù) | 新手園地 | 單 片 機(jī) | DSP技術(shù) | MCU技術(shù) | IC 設(shè)計(jì) | IC 產(chǎn)業(yè) | CAN-bus/DeviceNe |
尋找軟串口的源代碼 |
| 作者:luya 欄目:單片機(jī) |
| 2樓: | >>參與討論 |
| 作者: luya 于 2005/2/1 12:58:00 發(fā)布:
軟串口 |
|
| 3樓: | >>參與討論 |
| 作者: winhiwang 于 2005/2/1 12:59:00 發(fā)布:
有人剛發(fā)過,你查一下 |
|
| 4樓: | >>參與討論 |
| 作者: jcsasm 于 2005/2/1 13:00:00 發(fā)布:
什么叫軟串口?? |
|
| 5樓: | >>參與討論 |
| 作者: luya 于 2005/2/1 14:16:00 發(fā)布:
大多是用匯編寫的,請問有沒有用C51寫的呢? |
|
| 6樓: | >>參與討論 |
| 作者: maychang 于 2005/2/1 14:17:00 發(fā)布:
老古網(wǎng)上有 |
|
| 7樓: | >>參與討論 |
| 作者: 雁舞白沙 于 2005/2/1 15:07:00 發(fā)布:
LPC932 軟串口 1200 void INT_INT0(void) interrupt 0 { unsigned CHAR BitData; bit BitChk; EA=0; EX0=0; WDT(); EX0=0; BitData=0; BitChk=1; SeriesInData=0; Delay(130); //等過起始位 while(BitData<9) { if(BitData==8) { SeriesBitNine=RXD; } if(RXD==1) { SeriesInData|=0x0080; BitChk=~BitChk; } BitData++; if(BitData<8) { SeriesInData>>=1; } Delay(90); } if(BitChk) { if(ComEn) { ComBit=1; SeriesSt=0; } else { ComEn=1; ComBit=1; SeriesSt=1; } } EX0=1; EA=1; } void DataCom(void) { if(ComBit) { //串口數(shù)據(jù)集合校驗(yàn) if(ComEn) { if(SeriesSt) { if(SeriesBufDptr<60) { if(SeriesBufDptr==0) //數(shù)據(jù)緩沖區(qū)地址指針是否為零 { if(SeriesInData==0x68) //緩沖區(qū)的第一個(gè)數(shù)據(jù)是否等于0x68*/ { ComDataBuf[0]=SeriesInData; //數(shù)據(jù)保存到緩沖區(qū)*/ SeriesBufDptr++; //緩沖區(qū)地址累加*/ } else { SeriesBufDptr=0; } } else { ComDataBuf[SeriesBufDptr]=SeriesInData; //數(shù)據(jù)保存到緩沖區(qū)*/ SeriesBufDptr++; //緩沖區(qū)地址累加*/ } } else { SeriesBufDptr=0; } if(SendOverFg) { if(ComDataBuf[ComDataBuf][9]+11]==0x16) &nb |
|
| 8樓: | >>參與討論 |
| 作者: hotpower 于 2005/2/2 0:23:00 發(fā)布:
厲害 |
|
| 9樓: | >>參與討論 |
| 作者: luya 于 2005/2/2 9:32:00 發(fā)布:
太好了!還沒有看呢,先贊一下! 很感謝! |
|
| 10樓: | >>參與討論 |
| 作者: 雁舞白沙 于 2005/2/2 11:26:00 發(fā)布:
什么厲害? |
|
| 11樓: | >>參與討論 |
| 作者: xymxym 于 2005/2/2 23:55:00 發(fā)布:
!。 我最近用T2和INT兩個(gè)中斷模擬了串口,效果還不錯(cuò),摘出里面的一部分供你參考 #include "reg52.h" #include <intrins.h> #define uCHAR unsigned CHAR #define uint unsigned int #define StartBit 0 #define EndBit 9 #define N 10 sbit F_Receive = P3^2; uCHAR data AUX_Count,AUX_Time,AUX_State,AUX_Mode,AUX_Temp; uCHAR data AUX_Buf[N]; bit AUX_Received,AUX_En; void T_Rece(void) interrupt 2 { //INT2中斷 AUX_Time = 11; if (!AUX_En) { //開始接收數(shù)據(jù)串的準(zhǔn)備工作 AUX_Count = 0; //接收字節(jié)數(shù)初始 AUX_State = 1; //假設(shè)現(xiàn)在為為起始位,則下一次應(yīng)該指向數(shù)據(jù)的第一位 AUX_Received = 0; //整個(gè)數(shù)據(jù)串接收完畢,此位置1 AUX_Buf[0] = 0x00; //接收緩沖區(qū)初始 AUX_En = SET; T2High = SysBpsH; T2Low = SysBpsL; //填入要模擬的串口的波特率位時(shí)間,由T2進(jìn)行中斷 TH2 = T2High; TL2 = T2Low; TF2 = 0; TR2 = SET; ET2 = SET; //啟動(dòng)T2進(jìn)行計(jì)數(shù)的中斷 PT2 = SET; //T2為最高權(quán) } } void Time2(void) interrupt 5 { TH2 = T2High; //定時(shí)器2每隔bps秒中斷,作為模擬串口的位時(shí)間 TL2 = T2Low; TF2 = 0; if (--AUX_Time != 0) { //位數(shù)減一 if (AUX_State == StartBit) { //現(xiàn)在的中斷剛好落在起始位狀態(tài),不作任何處理 AUX_State++; //只將狀態(tài)指向數(shù)據(jù)態(tài) } else if (AUX_State == EndBit) { //現(xiàn)在是終止位狀態(tài), AUX_State = StartBit; //將接收的狀態(tài)指向起始位,準(zhǔn)備下一個(gè)數(shù)據(jù)的接收 AUX_Buf[AUX_Count++] = AUX_Temp; //將接收的字節(jié)數(shù)據(jù)存入數(shù)組 AUX_Temp = 0x00; } else { AUX_Temp >>= 1; //不是起始位也不是終止位的狀態(tài),那只能是數(shù)據(jù)位了 if (F_Receive) AUX_Temp |= 0x80; ++AUX_State; //接收狀態(tài)指向下一態(tài) } } else { //如果計(jì)滿11位的時(shí)間,AUX_Time未再被重置,說明數(shù)據(jù)串已接收完畢 EX1 = SET; IE1 = CLR; TR2 = CLR; ET2 = CLR; PT2 = CLR; AUX_En = CLR; AUX_Received = SET; //該位置1,在主程序中查詢并調(diào)用子程序?qū)λ邮盏臄?shù)據(jù)處理 } } |
|
| 12樓: | >>參與討論 |
| 作者: luya 于 2005/2/3 14:13:00 發(fā)布:
萬分感謝! |
|
| 13樓: | >>參與討論 |
| 作者: tianle1010 于 2005/2/3 14:55:00 發(fā)布:
我給你發(fā)一個(gè),希望你有用~~ //****************************************************************************** // MSP-FET430X110 DEMO - Timer_A UART Ultra-low POWER 2400 Echo, 32KHZ ACLK // // DESCRIPTION: Use timer_A CCR0 HARDWARE OUTPUT modes and SCCI data latch to // to implement UART function @ 2400 baud. SOFTWARE does not directly read and // write to RX and TX pins, instead proper use of OUTPUT modes and SCCI data // latch are demonstrated. Use of these HARDWARE features eliminates ISR // latency effects as HARDWARE insures that OUTPUT and input bit latching and // timing are perfectly synchronised with timer_A regardless of other // SOFTWARE activity. In the Mainloop the UART function readies the UART to // receive one CHARacter and waits in LPM3 with all activity interrupt driven. // After a CHARacter has been received, the UART receive function forces exit // from LPM3 in the Mainloop which echo's BACK the received CHARacter. // ACLK = TACLK = LFXT1 = 32768, MCLK = SMCLK = DCO ~ 800K // //*An external watch CRYSTAL is required on XIN XOUT for ACLK*// // // MSP430F1121 // ----------------- // /|\| XIN|- // | | | 32KHZ // --|RST XOUT|- // | | // | CCI0A/TXD/P1.1|--------> // | | 2400 8N1 // | CCI0B/RXD/P2.2|<-------- // #define RXD 0x04 // RXD on P2.2 #define TXD 0x02 // TXD on P1.1 // Conditions for 2400 Baud SW UART, ACLK = 32768 #define Bitime_5 0x06 // ~ 0.5 bit length + small adjustment #define Bitime 0x0E // 427us bit length ~ 2341 baud unsigned int RXTXData; unsigned CHAR BitCnt; void TX_Byte (void); void RX_Ready (void); // M.Buccini // Texas Instruments, Inc // September 2003 // Built with IAR Embedded Workbench Version: 1.26B // January 2004 // Updated for IAR Embedded Workbench Version: 2.21B //****************************************************************************** #include <msp430x11x1.h> void main (void) { WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer CCTL0 = OUT; // TXD Idle as Mark TACTL = TASSEL_1 + MC_2; // ACLK, continuous mode P1SEL = TXD; // P1.1/TA0 for TXD function P1DIR = TXD; // TXD OUTPUT on P1 P2SEL = RXD; // P2.2/TA0 as RXD input // Mainloop for (;;) { RX_Ready(); // UART ready to RX one Byte _BIS_SR(LPM3_bits + GIE); // Enter LPM3 w/ interr until CHAR RXed TX_Byte(); // TX BACK RXed Byte Received } } // Function Transmits Character from RXTXData Buffer void TX_Byte (void) { BitCnt = 0xA; // Load Bit counter, 8data + ST/SP while (CCR0 != TAR) // Prevent async capture CCR0 = TAR; // Current state of TA counter CCR0 += Bitime; // Some time till first bit RXTXData |= 0x100; // Add mark stop bit to RXTXData RXTXData = RXTXData << 1; // Add space start bit CCTL0 = OUTMOD0 + CCIE; // TXD = mark = idle while ( CCTL0 & CCIE ); // Wait for TX completion } // Function Readies UART to Receive Character into RXTXData Buffer void RX_Ready (void) { BitCnt = 0x8; // Load Bit counter CCTL0 = SCS + CCIS0 + OUTMOD0 + CM1 + CAP + CCIE; // Sync, Neg Edge, Cap } // Timer A0 interrupt service routine #pragma vector=TIMERA0_VECTOR __interrupt void Timer_A (void) { CCR0 += Bitime; // Add Offset to CCR0 // RX if (CCTL0 & CCIS0) // RX on CCI0B? { if( CCTL0 & CAP ) // Capture mode = start bit edge { CCTL0 &= ~ CAP; // SWITCH from capture to compare mode CCR0 += Bitime_5; } else { RXTXData = RXTXData >> 1; if (CCTL0 & SCCI) // Get bit waiting in receive latch RXTXData |= 0x80; BitCnt --; // All bits RXed? if ( BitCnt == 0) //>>>>>>>>>> Decode of Received Byte Here <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< { CCTL0 &= ~ CCIE; // All bits RXed, disable interrupt _BIC_SR_IRQ(LPM3_bits); // Clear LPM3 bits from 0(SR) } //>>>>>>>>>> Decode of Received Byte Here <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< } } // TX else { if ( BitCnt == 0) CCTL0 &= ~ CCIE; // All bits TXed, disable interrupt else { CCTL0 |= OUTMOD2; // TX Space if (RXTXData & 0x01) CCTL0 &= ~ OUTMOD2; // TX Mark RXTXData = RXTXData >> 1; BitCnt --; } } } |
|
| 14樓: | >>參與討論 |
| 作者: luya 于 2005/2/6 20:01:00 發(fā)布:
十分感謝! |
|
| 15樓: | >>參與討論 |
| 作者: luya 于 2005/2/6 20:05:00 發(fā)布:
請問雁舞白沙 請問,雁舞白沙,你的波特率是如何設(shè)制的? |
|
| 16樓: | >>參與討論 |
| 作者: luya 于 2005/2/17 16:03:00 發(fā)布:
請您把變量定義也發(fā)給我好么,這樣更可讀一些。多謝 |
|
| 17樓: | >>參與討論 |
| 作者: 雁舞白沙 于 2005/2/18 13:04:00 發(fā)布:
倒數(shù)的關(guān)系 luya ;請問雁舞白沙 ,你的波特率是如何設(shè)制的? 雁:延時(shí)---1200的倒數(shù) |
|
| 18樓: | >>參與討論 |
| 作者: yang96381 于 2005/2/18 13:55:00 發(fā)布:
哈哈,我詳細(xì)說一下原理 以51單片機(jī)為例,晶振采用12M,波特率9600,1位起始位,8位數(shù)據(jù)位,1位停止 位,采用P1口中的任意兩只引腳,如果接收采用中斷方式,那么還得用一個(gè)外部 中斷0或1引腳,用來檢測負(fù)跳變的起始位。! 波特率9600的倒數(shù)為104微秒, 這就是發(fā)送1位所需的時(shí)間,每隔104微秒發(fā)送1位數(shù)據(jù),因?yàn)椴捎肐/O口模擬串口,所以對軟件延時(shí)十分嚴(yán)格,否則所發(fā)送或接收的數(shù)據(jù)誤碼大。適當(dāng)減少波特 率可減少誤差。 采用這種方法適合發(fā)送或接收少量的數(shù)據(jù)。! |
|
| 19樓: | >>參與討論 |
| 作者: lql441 于 2005/2/18 15:52:00 發(fā)布:
IO口模擬232通訊程序占用定時(shí)器0,晶振為11.0592MHZ /********************************************** IO口模擬232通訊程序占用定時(shí)器0,晶振為11.0592MHZ **********************************************/ #include <at89x51.h> sbit TX =P1^0; sbit RX =P1^1; #define TIMER0_ENABLE TL0=TH0; TR0=1; #define TIMER0_DISABLE TR0=0; volatile bit F_TM; void IntTimer0() interrupt 1 { F_TM=1; } /*發(fā)送一個(gè)字符*/ void SendChar(unsigned CHAR inch) { unsigned CHAR i; i=0; F_TM=0; TX=0; //start bit TIMER0_ENABLE; //啟動(dòng) while(!F_TM); while(i<8) { if(inch&1)//數(shù)據(jù)由低位到高位發(fā)送 { TX=1; } else { TX=0; } F_TM=0; while(!F_TM); i++; inch>>=1; } TX=1; F_TM=0; while(!F_TM); TIMER0_DISABLE; //停止timer } /*接收一個(gè)字符*/ unsigned CHAR ReceChar() { unsigned CHAR rch,i; TIMER0_ENABLE; F_TM=0; i=0; rch=0; while(!F_TM); //等過起始位 while(i<8) { rch>>=1; if(RX) { rch|=0x80; //數(shù)據(jù)從低位到高位接收 } i++; F_TM=0; while(!F_TM); } F_TM=0; while(!F_TM) { if(RX) { break; } } TIMER0_DISABLE; //停止timer return rch; } /*檢查是不是有起始位*/ bit StartBitOn() { return (RX==0); } void main() { TMOD=0x22; //定時(shí)器1為工作模式2(8位自動(dòng)重裝),0為模式2(8位重裝) PCON=0x00; TR0=0; //在發(fā)送或接收才開始使用 TF0=0; TH0=(256-96); //9600bps 就是1000000/9600=104.167微秒 執(zhí)行的timer是104.167*11.0592/12= 96// TL0=TH0; ET0=1; EA=1; while(1) { SendChar('e'); } } |
|
| 20樓: | >>參與討論 |
| 作者: luya 于 2005/2/18 17:49:00 發(fā)布:
多謝各位!! yang96381說的很明白,我今天下午看了一個(gè)網(wǎng)上下載的程序也有同樣的體會,多謝熱心人! |
|
|
|
| 免費(fèi)注冊為維庫電子開發(fā)網(wǎng)會員,參與電子工程師社區(qū)討論,點(diǎn)此進(jìn)入 |
Copyright © 1998-2006 m.58mhw.cn 浙ICP證030469號 |