• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            開源之路

            憶往昔, 項羽不過江. 江東好風(fēng)光! 今振臂一呼,率甲三千, 試問天!
            posts - 86, comments - 55, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            TC中的串口編程

            Posted on 2006-07-18 13:03 江邊之鳥 閱讀(511) 評論(0)  編輯 收藏 引用
            作者:未知 來源:未知 加入時間:2004-7-20 天新軟件園
            /*==========================*/
            /*本程序由sunny編寫,如有傳載*/
            /*請注明http://sunny1979.icpcn.com */
            /*或者http://tchome.icpcn.com??*/
            #include <dos.h>
            #include <bios.h>
            #include <stdio.h>
            #include <math.h>
            #include <conio.h>
            #include <graphics.h>
            #ifdef __cplusplus
            ????#define __CPPARGS ...
            #else
            ????#define __CPPARGS
            #endif
            #define SER_RBF????????0???
            #define SER_THR????????0???
            #define SER_IER????????1????
            #define SER_IIR????????2????
            #define SER_LCR????????3???
            #define SER_MCR????????4????
            #define SER_LSR????????5????
            #define SER_MSR????????6????
            #define SER_DLL????????0????
            #define SER_DLH????????1????

            #define SER_BAUD_1200??96???
            #define SER_BAUD_2400??48
            #define SER_BAUD_9600??12
            #define SER_BAUD_19200??6
            #define SER_GP02????????8?????
            #define COM_1???????????0x3F8
            #define COM_2???????????0x2F8 /*/ base port address of port 1*/
            #define SER_STOP_1??????0?????/*/ 1 stop bit per character*/
            #define SER_STOP_2??????4?????/*/ 2 stop bits per character*/
            #define SER_BITS_5??????0?????/*/ send 5 bit characters*/
            #define SER_BITS_6??????1?????/*/ send 6 bit characters*/
            #define SER_BITS_7??????2?????/*/ send 7 bit characters*/
            #define SER_BITS_8??????3?????/*/ send 8 bit characters*/
            #define SER_PARITY_NONE 0?????/*/ no parity*/
            #define SER_PARITY_ODD??8?????/*/ odd parity*/
            #define SER_PARITY_EVEN 24????/*/ even parity*/
            #define SER_DIV_LATCH_ON 128??/*/ used to turn reg 0,1 into divisor latch*/
            #define PIC_IMR????0x21???/*/ pic's interrupt mask reg.*/
            #define PIC_ICR????0x20???/*/ pic's interupt control reg.*/
            #define INT_SER_PORT_0????0x0C??/*/ port 0 interrupt com 1 & 3*/
            #define INT_SER_PORT_1????0x0B??/*/ port 0 interrupt com 2 & 4*/
            #define SERIAL_BUFF_SIZE 128????/*/ current size of circulating receive buffer*/

            void interrupt far (*Old_Isr)(__CPPARGS);??/*/ holds old com port interrupt handler*/

            char ser_buffer[SERIAL_BUFF_SIZE];??/*/ the receive buffer*/

            int ser_end = -1,ser_start=-1;??????/*/ indexes into receive buffer*/
            int ser_ch, char_ready=0;???????????/*/ current character and ready flag*/
            int old_int_mask;???????????????????/*/ the old interrupt mask on the PIC*/
            int open_port;??????????????????????/*/ the currently open port*/
            int serial_lock = 0;????????????????/*/ serial ISR semaphore so the buffer*/
            ????????/*/ isn't altered will it is being written*/
            ????????????????????????????????????/*/ to by the ISR*/


            /*-------------寫串口-----------------*/??
            void interrupt far Serial_Isr(__CPPARGS)
            {
            serial_lock = 1;
            ser_ch = inp(open_port + SER_RBF);
            if (++ser_end > SERIAL_BUFF_SIZE-1)
            ????ser_end = 0;
            ser_buffer[ser_end] = ser_ch;

            ++char_ready;
            outp(PIC_ICR,0x20);
            serial_lock = 0;

            }


            int Ready_Serial()
            {
            return(char_ready);

            }

            /*--------------讀串口--------------*/

            int Serial_Read()
            {
            int ch;
            while(serial_lock){}
            if (ser_end != ser_start)
            ???{
            ???if (++ser_start > SERIAL_BUFF_SIZE-1)
            ???????ser_start = 0;
            ???ch = ser_buffer[ser_start];
            ??if (char_ready > 0)
            ???????--char_ready;
            ???return(ch);

            ???}
            else
            ???return(0);

            }

            /*--------------寫串口-----------------*/
            Serial_Write(char ch)
            {
            while(!(inp(open_port + SER_LSR) & 0x20)){}
            asm cli
            outp(open_port + SER_THR, ch);
            asm sti
            }

            /*-----------初始化串口---------------*/
            Open_Serial(int port_base, int baud, int configuration)
            {
            open_port = port_base;
            outp(port_base + SER_LCR, SER_DIV_LATCH_ON);
            outp(port_base + SER_DLL, baud);
            outp(port_base + SER_DLH, 0);
            outp(port_base + SER_LCR, configuration);
            outp(port_base + SER_MCR, SER_GP02);
            outp(port_base + SER_IER, 1);
            if (port_base == COM_1)
            ???{
            ???Old_Isr = _dos_getvect(INT_SER_PORT_0);
            ???_dos_setvect(INT_SER_PORT_0, Serial_Isr);
            ???printf("\nOpening Communications Channel Com Port #1...\n");

            ???}
            else
            ???{
            ???Old_Isr = _dos_getvect(INT_SER_PORT_1);
            ???_dos_setvect(INT_SER_PORT_1, Serial_Isr);
            ???printf("\nOpening Communications Channel Com Port #2...\n");
            ???}
            old_int_mask = inp(PIC_IMR);
            outp(PIC_IMR, (port_base==COM_1) ? (old_int_mask & 0xEF) : (old_int_mask & 0xF7 ));
            }
            /*-------------關(guān)閉串口--------------*/
            Close_Serial(int port_base)
            {
            outp(port_base + SER_MCR, 0);
            outp(port_base + SER_IER, 0);
            outp(PIC_IMR, old_int_mask );
            if (port_base == COM_1)
            ???{
            ???_dos_setvect(INT_SER_PORT_0, Old_Isr);
            ???printf("\nClosing Communications Channel Com Port #1.\n");
            ???}
            else
            ???{
            ???_dos_setvect(INT_SER_PORT_1, Old_Isr);
            ???printf("\nClosing Communications Channel Com Port #2.\n");
            ???}

            }

            /*-------------發(fā)送應(yīng)用----------------*/

            void main(int argc,char *argv[])
            {

            char ch,press;
            int done=0;
            FILE *fp;
            argc=2;
            argv[1]="test.cpp";
            if(argc<2)
            {
            ??printf("\nUsage:display filename.wav!!!");
            ??exit(0);
            }
            if((fp=fopen(argv[1],"r+b"))==NULL)
            {
            ??printf("cannot open the file\n");
            ??exit(0);
            }
            fseek(fp, 0, SEEK_SET);
            Open_Serial(COM_1,SER_BAUD_9600,SER_PARITY_NONE | SER_BITS_8 | SER_STOP_1);
            printf("press any key to begin sending");
            getch();
            Serial_Write(' ');
            while(!done&&ch != EOF)
            ?????{
            ch = fgetc(fp);
            if(ch==EOF) Serial_Write(27);
            Serial_Write(ch);
            if (kbhit())
            {
            ??press=getch();
            ??if (press==27)
            ??{
            ???Serial_Write(27);
            ???done=1;
            ??}
            }
            ?????}??
            Close_Serial(COM_1);
            fclose(fp);
            }

            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            久久久精品人妻无码专区不卡| 精品久久久一二三区| 国产精品久久久久久搜索| 999久久久国产精品| 亚洲午夜精品久久久久久app| 一本色道久久88精品综合| 97久久精品人人做人人爽| 午夜人妻久久久久久久久| 88久久精品无码一区二区毛片| 久久久久精品国产亚洲AV无码| 77777亚洲午夜久久多喷| 欧美亚洲国产精品久久高清| 亚洲国产成人久久精品动漫| 亚洲av成人无码久久精品| 久久亚洲国产成人影院网站| 久久国产精品久久久| 亚洲精品乱码久久久久久蜜桃图片 | 综合久久国产九一剧情麻豆| 国产99精品久久| 久久综合国产乱子伦精品免费| 手机看片久久高清国产日韩| 久久99精品久久久久久噜噜 | 久久久久亚洲AV无码网站| 麻豆久久| 亚洲精品无码久久毛片| 久久免费国产精品| 亚洲欧洲久久av| 亚洲一区精品伊人久久伊人 | 久久久久久久人妻无码中文字幕爆 | 无码乱码观看精品久久| 日本久久久久久久久久| 久久久精品人妻无码专区不卡| 精品久久久久久久久久久久久久久| 久久国产乱子精品免费女| 国产精品gz久久久| 久久免费国产精品| 久久人妻无码中文字幕| 久久无码人妻一区二区三区| 国产精品免费看久久久| A狠狠久久蜜臀婷色中文网| 久久久久久狠狠丁香|