1442607 ランダム
 HOME | DIARY | PROFILE 【フォローする】 【ログイン】

電子こうさくの家

PIC16F84によるシリアル通信&HEXデータ

#include "16F84.h"
// ****************************************************************************
// プログラム名 :RS232C.c / Date:2006/01/16 Program by PIC9801
// 通信仕様など :9600bps 8 bit,no parity,2 stop bits,no flow control,no WDT
// :論理を反転⇒RS232Cレベルコンバータ省略のため
// :使用したコンパイラ Grich RC Inc.
// デバイス、クロック:PIC16F84 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 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
// *********************************************
// メインルーチン(プログラムはここから走る)
// *********************************************
void main()
{
unsigned int a;
char * ptr;
long i;
char inp[20];

trisb = 0x0f; //入出力方向設定...
portb = 0xff; //最初はLEDをOFFに...
//
rs232_printf("\r\nYour Name ?");
rs232_gets(inp,20);
rs232_printf("\r\n"); // 改行
rs232_ram_printf(inp);
rs232_printf("さん、こんにちは! 私はPICで~す!!");
pause(1000); //約1秒
rs232_printf("\r\nこれから私の素晴らしい力をお見せします。");
rs232_printf("\r\nなんと! LEDを点灯させて見せます。");
pause(1000);
while(1) {
rs232_printf("\r\n1で点灯0で消灯 最後に改行 ");
rs232_gets(inp,20);
if( inp[0] == '1'){
a = portb;
a = a && 0x1f;
portb = a;
}
if( inp[0] == '0'){
a = portb;
a = a || 0xe0;
portb = a;
}
} // end while
}





:100000007429B000093E0318AB0A2B088A00300897
:1000100082070D340A3459346F3475347234203405
:100020004E3461346D34653420343F3400340D3443
:100030000A3400348234B3348234F13481344134AC
:100040008234B1348234F1348234C9348234BF34DE
:100050008234CD34213420348E3484348234CD340F
:100060005034493443348234C5348134603482346A
:10007000B7342134213400340D340A348234B1349D
:100080008234EA348234A9348234E7348E348434BE
:100090008234CC34913466349034B0348234E734D2
:1000A0008234B5348234A2349734CD348234F0347F
:1000B0008234A8348C34A9348234B9348234B534CF
:1000C0008234DC348234B7348134423400340D3429
:1000D0000A348234C8348234F1348234C634213450
:1000E00020344C34453444348234F03493345F3417
:1000F000933494348234B3348234B9348234C43483
:100100008C34A9348234B9348234DC348234B73448
:100110008134423400340D340A3482345034823411
:10012000C53493345F349334943482344F348234FE
:10013000C5348F34C1349334943420348D34C53471
:100140008C34E3348234C9348934FC348D347334D0
:1001500020340034831203110616333091009103CA
:100160001108031DAF280830AF002F080319D1284C
:1001700001302E050319C028831203110612C4286A
:1001800083120311061600002F309100910311080D
:10019000031DC628AE0C000000000000AF032F0AAC
:1001A000B528831203110612333091009103110810
:1001B000031DD6283330910091031108031DDC285C
:1001C00008001208840000080319EE2812088400B1
:1001D0000008AE00AA20920A1203E12808001008C5
:1001E000AB000F0801200038031900291008AB00EC
:1001F0000F080120AE00AA208F0FEF28900AEF28E9
:1002000008008312861D01293330920092031208E0
:10021000031D0629333092000310920C920312083A
:10022000031D0E290830AE002E0803192F29831252
:10023000AF0C031108300605031921290311AF1370
:1002400024290311AF1700002F30920092031208E7
:10025000031D2629AE032E0A0000000014292F08D2
:1002600008009403140A14088C000319462901217C
:10027000AD00150884002D0880000A300002AD0092
:1002800096012D08031D960A16088C000C08031908
:100290005A29150884000D3000028C0096010C08C4
:1002A0000319960A1608031D5A29950A1503940383
:1002B000140A332915088400800108000F0810046F
:1002C000031973299201FF3012028C01031C8C0A5E
:1002D0000C0803196E29920A1203632901308F0258
:1002E000031C90035E29080083160F3086008312DA
:1002F000FF30860000308E0000308D008F000E0829
:100300009000EF2017309500143094003121003018
:100310008E000E308D008F000E089000EF201730F9
:100320009200E12000308E0011308D008F000E0809
:100330009000EF20E8308F00033090005E21003005
:100340008E0033308D008F000E089000EF20AB013F
:100350001308012000308E005E308D008F000E08E3
:100360009000EF20E8308F00033090005E210030D5
:100370008E0082308D008F000E089000EF20173025
:100380009500143094003121313017028C00930114
:100390000C080319930A13080319DA2983120608B3
:1003A000AC008C000319D6291F308C0083120C0876
:1003B000AC008600303017028C0093010C08031942
:1003C000930A13080319F12983120608AC008C0064
:1003D0000C08031DED29E0308C0083120C08AC00E2
:0603E0008600B729F22996
:00000001FF


© Rakuten Group, Inc.