PIC16C711によるアナログ入力&HEXデータ#include "16C711.h"// **************************************************************************** // プログラム名 :PIC16C711.c / Date:2006/02/09 Program by PIC9801 // :A/Dコンバータテストプログラム // 通信仕様など :9600bps 8 bit,no parity,2 stop bits,no flow control,no WDT // :論理を反転⇒RS232Cレベルコンバータ省略のため // :使用したコンパイラ Grich RC Inc. // デバイス、クロック:PIC16C711 running at 10 MHz // **************************************************************************** #define RS232PT 0x6 // use porta for rs232c #define RS232TXBIT 4 // bit 4 is the transmit line #define RS232RXBIT 3 // bit 3 is the receive line int trisa @ 0x85; // portA data direction register int trisb @ 0x86; // portB data direction register int adcon1 @ 0x88; // A/D control register no2 int adres @ 0x9; // A/D data port int adcon0 @ 0x8; // portA data direction register int porta @ 0x5; // portA int portb @ 0x6; // portB #define RS232PAUSE while(--delay) #define RS_DELAY1 51; #define RS_DELAY2 47; // ************************************************* // * putc に似た関数 // ************************************************* void rs232_putc(unsigned int byte) { int i,delay; int rs232pt @ RS232PT; // < スタートビットを作成 > rs232pt |= 1 << RS232TXBIT; delay = RS_DELAY1; RS232PAUSE; // < ここから1BYTE分をシリアル送信 > i = 8; while(i) { if( byte & 1 ) { rs232pt &= ~(1 << RS232TXBIT); } else { rs232pt |= 1 << RS232TXBIT; asm(nop); } // end if delay = RS_DELAY2; // rs232_delay2; RS232PAUSE; asm(rrf byte,1); // asm(nop); asm(nop); asm(nop); // make loop 22 cycles i--; } // end while // now generate two stop bits. rs232pt &= ~(1 << RS232TXBIT); delay = RS_DELAY1; RS232PAUSE; delay = RS_DELAY1; RS232PAUSE; } // end rs232_putc // ************************************************* // * printfに似た関数 RAMから // ************************************************* void rs232_ram_printf(unsigned short char * str) { while(*str) { rs232_putc(*str); str++; } // end while } // end rs232_ram_printf // ************************************************* // * printfに似た関数 ROMから // ************************************************* void rs232_printf(char * str) { while(@str) { rs232_putc(@str); str++; } // end while } // end rs232_printf // ************************************************* // * getc に似た関数 // ************************************************* unsigned char rs232_getc() { unsigned char byte; int delay,i; int rs232pt @ RS232PT; int stat @ 0x3; // wait for startbit getclabel: asm(btfss rs232pt,RS232RXBIT); goto getclabel; delay = RS_DELAY1; RS232PAUSE; delay = RS_DELAY1; asm( bcf stat,0 rrf delay,1); RS232PAUSE; // read 8 bits i = 8; while(i){ asm( rrf byte,1 ); if( rs232pt & (1 << RS232RXBIT) ) { byte &= ~(1 << 7); } else { byte |= 1 << 7; asm(nop); } // end if delay = RS_DELAY2; RS232PAUSE; i--; asm(nop); asm(nop); } return(byte); } // end rs232_getc // ************************************************* // * gets に似た関数 // ************************************************* void rs232_gets(unsigned short char * str, unsigned int max) { max--; while( max && (((*str) = rs232_getc()) != '\n')) { #ifdef ECHO_ON rs232_putc(*str); #endif if( *str == '\r' ) break; str++; max--; } // end while *str = '\0'; } // end rs232_gets // ************************************************* // * 遅延(時間はいい加減(^^;)t = 約1mSEC // ************************************************* pause(t) long t; { unsigned int d; while( t ) { for(d = 0 ; d < 255 ; d++ ); t--; } } // end pause // ********************************************* // * チャネル0~3 A/D変換 // ********************************************* convert(c) int c; { unsigned int d; switch (c) { case 0: // CH1 asm(movlw 0x81); // A/D on asm(movf adcon0); // selct break; case 1: // CH1 asm(movlw 0x89); // A/D on asm(movf adcon0); // selct break; case 2: // CH2 asm(movlw 0x91); // A/D on asm(movf adcon0); // selct break; case 3: // CH3 asm(movlw 0x99); // A/D on asm(movf adcon0); // selct break; } // end case pause(2); // delay asm(bsf adcon0,2); // A/D start wait0: asm(btfsc adcon0,2); // A/D変換終了? asm(goto wait0); asm(movf adres,0); // dataの取り込み asm(bcf adcon0,0); // A/D stop asm(movwf d); asm(clrf adcon0); return(d); } // ********************************************* // * ASCII_10進表示 0-255 // ********************************************* asc10(a) unsigned int a; { unsigned int b,c,d; b = a / 100; c = ( a - ( b*100 )) /10; d = a - (b * 100) - (c * 10); b = (b & 0x0f) | 0x30; rs232_putc(b); c = (c & 0x0f) | 0x30; rs232_putc(c); d = (d & 0x0f) | 0x30; rs232_putc(d); } // ********************************************* // メインルーチン(プログラムはここから走る) // ********************************************* void main() { unsigned int a,n; adcon1 = 0x00; trisb = 0x0f; //入出力方向設定... portb = 0xff; //最初はLEDをOFFに... // while(1) { for(n = 0 ; n < 4 ; n++ ){ a = convert(n); asc10(a); rs232_printf("\r\n"); } pause(1000); //約1秒 rs232_printf("\r\n"); } // end while } // end main :1000000054298F00093E03188C0A0C088A000F0837 :1000100082070D340A3400340D340A3400340C08DD :100020000E0695008C1F19288C098F098F0F19282F :100030008C0A8E1F20288E099109910F20288E0A84 :100040002E20951F0800950095098D09950F2928E8 :100050008D0A150808009030940030281030940064 :100060009C018D010C0D9C0D8D0D11089C020E083C :10007000031C0E0F8D020318442811089C070E085C :1000800003180E0F8D0703108F0D8C0D94031F3076 :100090001405031D3228941B51280C088D000F08ED :1000A00008001C080800103095008D0194018C0C8C :1000B0008F0C031C61281108940703188D0A0E0881 :1000C0008D070310910D8E0D950B5728140808000D :1000D0008312031106163330900090031008031D9D :1000E0006D280830A500250803198F2801301B054D :1000F00003197E288312031106128228831203112A :10010000061600002F30900090031008031D84286D :100110009B0C000000000000A503250A7328831231 :10012000031106123330900090031008031D942829 :100130003330900090031008031D9A2808008C01AA :100140001208012000380319AF2813088C00120888 :1001500001209B006820920F9F28930A9F28080087 :10016000120813040319C5289001FF3010029A01E8 :10017000031C9A0A1A080319C028900A1003B5280C :1001800001309202031C9303B028080024089B004E :1001900000301B020319D92801301B020319DD2886 :1001A00002301B020319E12803301B020319E52862 :1001B000E828831281308807E8288312893088076D :1001C000E828831291308807E8288312993088073D :1001D000023092009301B02083120815831208198F :1001E000EE2809080810A500880125080800240841 :1001F0008F008C01643091008E010F209E000D084D :100200009F001E08A3008F008C01643091008E01B6 :1002100053209E001E08240298009901031C990394 :1002200018088F0019088C000A3091008E010F20E9 :1002300098000D0899001808A20023088F008C016F :10024000643091008E015320A6000D08A7002608F7 :10025000240296009701031C970322088F008C014B :100260000A3091008E015320A0000D08A100200843 :1002700016029600031C9703210897021608A8008F :100280000F3023053038A3009B0068200F30220573 :100290003038A2009B0068200F30280530388D00D0 :1002A000A8009B0068200800831688010F30860094 :1002B0008312FF3086009D0104301D028D009A01DB :1002C0008D1B9A0A1A08031973291D08A400C62059 :1002D000AA00A400F72000309200003093009F2075 :1002E0009D0A1D035C29E830920003309300B02082 :1002F00003309200003093009F208C0129080120D8 :040300005B298129CB :00000001FF ジャンル別一覧
人気のクチコミテーマ
|