/* * Serial port driver for 12F675/629 and 16F84,16F627A chips * using software delays. * * Copyright (C)1996 HI-TECH Software. * modify by Air_variable 2004 local version 1.05 (20040629) * Freely distributable. * Require use Hitech software version 8.05 * Require use Toolsite version 1.30.0.7 */ /* * 通信のタイミングはソフトウェアで行っています。このため、オプテマイズ * 設定で以下の設定を必要とします。 * The following setup is required in order to take out accuracy. * Project->Build Options->Project->PicC compiler * Global optimize level =1 or more ,Enable assembler optimizations * */ #include #include //RS232Cの論理 ダイレクト接続0,MAX232Cドライバなど接続は1 #define sci_polarity 0 /* Xtal frequency */ #define XTAL 4000000 /* Baud rate */ //ボーレート設定-------------------------------------------------------- #define BRATE 19200 //各ボーレートのerror rate(1bit) // 19200 52.083us -> 53us ( 98.27%) // 9600 104.167us ->104us (100.16%) // Don't change anything else #define DLY 3 // cycles per null loop #define TX_OHEAD 10 // overhead cycles per loop #define RX_OHEAD 7 // receiver overhead per loop //----------ポートの変更はここで行います------------------------------- #if defined(_12F629) || defined(_12F675) // Transmit and Receive port bits static bit TxData @ ( (unsigned)&GPIO*8+5 ); /* bit5 in GPIO */ static bit RxData @ ( (unsigned)&GPIO*8+3 ); /* bit3 in GPIO */ #define INIT_PORT TRISIO = 0x11011111 /* set up I/O direction */ #else // Transmit and Receive port bits static bit TxData @ ( (unsigned)&PORTA*8+2 ); /* bit2 in PORTA */ static bit RxData @ ( (unsigned)&PORTA*8+5 ); /* bit5 in PORTA */ #define INIT_PORT TRISA = 0x11111011 /* set up I/O direction */ #endif //---------以下は調整済みです---------------------------------------- #define DELAY(ohead) (((XTAL/4/BRATE)-(ohead))/DLY) void putch(char c) { unsigned char dly, bitno; bitno = 11; // INIT_PORT; #if sci_polarity TxData = 0; // start bit sci極性が1の場合(通常のRS232C(max232c等)) #else TxData = 1; //* start bit sci極性が0の場合(抵抗によるダイレクト接続 #endif bitno = 12; do { dly = DELAY(TX_OHEAD); /* wait one bit time */ do /* nix */ ; while(--dly); if(c & 1) #if sci_polarity //sci極性が1の場合(通常のRS232C(max232c等)) TxData = 1; #else //sci極性が0の場合(抵抗によるダイレクト接続 TxData = 0; #endif if(!(c & 1)) #if sci_polarity //sci極性が1の場合(通常のRS232C(max232c等)) TxData = 0; #else //sci極性が0の場合(抵抗によるダイレクト接続 TxData = 1; #endif c = (c >> 1) | 0x80; } while(--bitno); } char getch(void) { unsigned char c, bitno, dly; for(;;) { #if sci_polarity while(RxData) //max232c等 #else while(!RxData) //ダイレクト接続 #endif continue; /* wait for start bit */ dly = DELAY(3)/2; do /* nix */; while(--dly); #if sci_polarity if(RxData) //max232c等 #else if(!RxData) //ダイレクト接続 #endif continue; /* twas just noise */ bitno = 8; c = 0; do { dly = DELAY(RX_OHEAD); do /* nix */; while(--dly); c = (c >> 1) | (RxData << 7); } while(--bitno); #if sci_polarity #else c=~c; //論理反転 #endif return c; } } char getche(void) { char c; putch(c = getch()); return c; }