|
用CCS 写了简单的通讯程序
#include <16F887.h>
#device ADC = 10
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=16000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,stream=MYPC)
#define RD0 PIN_D0
#define RD2 PIN_D2
#define RD3 PIN_D3
#include <string.h>
void LED_Running()
{
output_low(RD0);
delay_ms(100);
output_high(RD0);
delay_ms(100);
}
void RAD_VOLT(TD)
{
//define the value for variable;
int8 M_Volt[10];
float Value;
setup_port_a( ALL_ANALOG | VSS_VDD );
setup_adc( ADC_CLOCK_INTERNAL );
set_adc_channel(TD);
delay_us(50);
value = Read_ADC();
delay_us(50);
setup_adc(ADC_OFF);
delay_us(50);
value = 5.0*value/1023; // Measurement volatge
sprintf(M_Volt, "%1.3fV \n\r",value);
printf(M_Volt);
// return(M_Volt);
}
void init_io()
{
SET_TRIS_A(0x0F);
output_B(0x00);
output_C(0x00);
output_D(0x00);
}
void main()
{
int8 RC_flag;
unsigned int8 RC_CMD[8];
char ptRCi;
char DATA;
int8 value;
char tmpch;
long tttlong;
char bKb;
int TD;
char PD;
//int8 string[20];
ptRCi=0;
tttlong=0;
init_io();
Loop:
while(1)
{//LED Running
tttlong ++;
if (tttlong > 20000)
{output_low(RD0);}
if (tttlong > 50000)
{ output_high(RD0);
tttlong =0;
}
bKb = kbhit();
if (bKb)
{
tmpch = getc();
if (tmpch == '#')
{
ptRCi=0;
}
RC_CMD[ptRCi]=tmpch;
ptRCi ++;
ptRCi %= 8;
if (tmpch == '$')
{
// reading adc
if ( (RC_CMD[0]=='#') && (RC_CMD[1]=='R') && (RC_CMD[2]=='A') && (RC_CMD[3]=='D') && (RC_CMD[4]<='7') && (RC_CMD[5]=='$') && (ptRCi==6))
{
switch(RC_CMD[4])
{ case '0': TD=0; break;
case '1': TD=1; break;
case '2': TD=2; break;
case '3': TD=3; break;
case '4': TD=4; break;
case '5': TD=5; break;
case '6': TD=6; break;
case '7': TD=7; break;
}
RAD_VOLT(TD);
goto Loop;
}
//== i/o control for port D by line
if((RC_CMD[0]=='#') && (RC_CMD[1]=='S') && (RC_CMD[2]=='P') &&(RC_CMD[3]<='D') && (RC_CMD[4]<='7') && (RC_CMD[5]=='$') && (ptRCi==6))
{
switch(RC_CMD[4])
{ case '0'D=PIN_D0;break;
case '1'D=PIN_D1;break;
case '2'D=PIN_D2;break;
case '3'D=PIN_D3;break;
case '4'D=PIN_D4;break;
case '5'D=PIN_D5;break;
case '6'D=PIN_D6;break;
case '7'D=PIN_D7;break;
}
output_high(PD);
goto Loop;
}
//== i/o control for port D release by line
if((RC_CMD[0]=='#') && (RC_CMD[1]=='R') && (RC_CMD[2]=='P') &&(RC_CMD[3]<='D') && (RC_CMD[4]<='7') && (RC_CMD[5]=='$') && (ptRCi==6))
{
switch(RC_CMD[4])
{ case '0'D=PIN_D0;break;
case '1'D=PIN_D1;break;
case '2':PD=PIN_D2;break;
case '3':PD=PIN_D3;break;
case '4':PD=PIN_D4;break;
case '5':PD=PIN_D5;break;
case '6':PD=PIN_D6;break;
case '7':PD=PIN_D7;break;
}
output_low(PD);
goto Loop;
}
else
{printf("%S \n\r","unknowen Command, Please check");}
}
}
}
}
|
|