ccs c 如何接收RS232?
if( kbhit() ) // 有人敲 PC 終端機的鍵盤{putc ( keyin = getc() );// echo 回終端機
if ( keyin == 'u' ) {...}
else if( keyin == 't' ) {...}
else {...}
}
再給你個完整的,軟硬件 RS232 範例
#include <16F877.H> // <18F452.H> // device header file from CCS-C
#use delay(clock=20000000)
#use rs232(baud=38400, xmit=PIN_C4, rcv=PIN_C5) // 軟件 RS232
char get_RS232_B(int16 time_slice)
{int16 timeout=0; char cmd; time_slice*=100;
while(!kbhit()&&(++timeout<time_slice)) delay_us(10);
if(kbhit())
{
cmd = getc(); // 從 PIN_C5 接收
printf("\n\r RS232_B: got your command, cmd =%C", cmd); // 送到 PIN_C4 去
return(cmd);
} else return(0);
}
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)// 硬件 RS232
void main()
{
char cmd, i=0;
printf("\n\n\r Press a key after each line ... ");
getc();
printf("\n\r You can get RS232 baud rate up to 115200 ... "); getc();
printf("\n\r Send keyboard command while LED flashing ... "); getc();
printf("\n\r via RS232_A (RC6, 7) or RS232_B (RC4, 5) ... "); getc();
printf("\n\r A 5 Hz high/low signal at RA5 (LED) starts now ... press a key ... ");
puts(" "); // CCS automatically add \n\r in puts() !
while ( TRUE )
{
if ( kbhit() )
{cmd = getc();
printf("\n\r got your command from RS232_A, cmd_%u :%C", ++i, cmd);
}
output_high(PIN_A5); delay_ms(100);
output_low( PIN_A5);// delay_ms(100);
if ( ( cmd= get_RS232_B( 100 ) ) != 0 )// 100 msec for RS232_B
printf("\n\r got some command from RS232_B, cmd_%u :%C", ++i, cmd); // 送到 PIN_C6 去
}
}
页:
[1]