Last active
September 21, 2015 07:03
-
-
Save unicore32/e824bf0f767381a5bd29 to your computer and use it in GitHub Desktop.
PIC16F1827でインタープラン(株)製のIM920を使って通信するための実験プログラム(要CCS C Compiler)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// DEFINE port | |
#define portTX PIN_B2 | |
#define portRX PIN_B1 | |
#define portENABLE PIN_B0 | |
#define portBUSY PIN_A4 | |
#define portRESET PIN_B3 | |
#include <16f1827.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#fuses INTRC_IO, NOWDT, NOPROTECT, PUT, NOBROWNOUT, NOMCLR, NOLVP | |
#use delay(CLOCK = 32000000) | |
#use rs232(BAUD = 19200, XMIT = portTX, RCV = portRX, PARITY = N, STOP = 1, BITS = 8) | |
#use fast_io(A) | |
#use fast_io(B) | |
char rcvBUFF[40]; | |
int rcvCOUNT = 0; | |
int rcvFLAG = 0; | |
int sendFLAG = 0; | |
int oldADATA = 0b00000000; | |
void sendDATA(int ADATA) | |
{ | |
if(input(portBUSY) == 1) | |
sendFLAG = 1; | |
else | |
{ | |
printf("TXDT %X000000\r\n", ADATA); | |
//printf("AB,0001,00:%X,00,00,00,00,00,00,00\r\n", ADATA); | |
sendFLAG = 0; | |
} | |
oldADATA = ADATA; | |
} | |
int convDATA(char *buffDATA, long *outputDATA) | |
{ | |
char s1[3], s2[3], s3[3], s4[3]; | |
strncpy(s1, buffDATA+11, 2); | |
strncpy(s2, buffDATA+14, 2); | |
strncpy(s3, buffDATA+17, 2); | |
strncpy(s4, buffDATA+20, 2); | |
s1[2] = "\0"; | |
s2[2] = "\0"; | |
s3[2] = "\0"; | |
s4[2] = "\0"; | |
outputDATA[0] = strtoul(s1, (char **) NULL, 16); | |
outputDATA[1] = strtoul(s2, (char **) NULL, 16); | |
outputDATA[2] = strtoul(s3, (char **) NULL, 16); | |
outputDATA[3] = strtoul(s4, (char **) NULL, 16); | |
} | |
#int_rda | |
void isr_rcv() | |
{ | |
char rcvDATA; | |
rcvDATA = getc(); | |
rcvBUFF[rcvCOUNT] = rcvDATA; | |
rcvCOUNT++; | |
if (rcvDATA == 0x0A) | |
{ | |
rcvCOUNT = 0; | |
rcvFLAG = 1; | |
} | |
else if (rcvCOUNT >= 40) | |
{ | |
rcvCOUNT = 0; | |
memset(rcvBUFF, '\0', 40); | |
} | |
} | |
void main() | |
{ | |
setup_oscillator(OSC_8MHZ|OSC_PLL_ON); | |
set_tris_a(0b00011111); // A0~A4 IN | |
set_tris_b(0b00000010); // B4~B7 OUT(LED) | |
output_a(0); | |
output_b(0); | |
enable_interrupts(INT_RDA); | |
enable_interrupts(GLOBAL); | |
unsigned int inADATA; | |
unsigned long outDATA[4]; | |
while(1) | |
{ | |
inADATA = input_a() & 0b00001111; | |
if(inADATA != oldADATA || sendFLAG == 1) | |
sendDATA(inADATA); | |
if (rcvFLAG == 1) | |
{ | |
convDATA(rcvBUFF, outDATA); | |
output_b((outDATA[0] << 4) & 0b11110000); | |
memset(rcvBUFF, '\0', 40); | |
rcvFLAG = 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment