|
pc机程序
- /**************************************************
- * Serial communication between PC and 51 board
- *
- * function: A running hores controlled by PC
- * send one byte data to 51 board every one
- * second
- *
- * compile: gcc -o main main.c
- * eviroment: ubuntu 7.10
- * compiler: gcc
- *
- * baud: 1200
- * parit: N
- * databits: 8
- * stopbit: 1
- ***************************************************/
- #include /*鏍囧噯杈撳叆杈撳嚭瀹氫箟*/
- #include /*鏍囧噯鍑芥暟搴撳畾涔?/
- #include /*Unix 鏍囧噯鍑芥暟瀹氫箟*/
- #include
- #include
- #include /*鏂囦欢鎺у埗瀹氫箟*/
- #include /*PPSIX 缁堢?鎺у埗瀹氫箟*/
- #include /*閿欒?鍙峰畾涔?/
- #define FALSE -1
- #define TRUE 0
- struct termio
- { unsigned short c_iflag; /* 杈撳叆妯″紡鏍囧織 */
- unsigned short c_oflag; /* 杈撳嚭妯″紡鏍囧織 */
- unsigned short c_cflag; /* 鎺у埗妯″紡鏍囧織*/
- unsigned short c_lflag; /* local mode flags */
- unsigned char c_line; /* line discipline */
- unsigned char c_cc[NCCS]; /* control characters */
- };
- /**
- * *@brief 璁剧疆涓插彛閫氫俊閫熺巼
- * *@param fd 绫诲瀷 int 鎵撳紑涓插彛鐨勬枃浠跺彞鏌?
- * *@param speed 绫诲瀷 int 涓插彛閫熷害
- * *@return void
- * */
- int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,
- B38400, B19200, B9600, B4800, B2400, B1200, B300, };
- int name_arr[] = {38400, 19200, 9600, 4800, 2400, 1200, 300, 38400,
- 19200, 9600, 4800, 2400, 1200, 300, };
- void set_speed(int fd, int speed){
- int i;
- int status;
- struct termios Opt;
- tcgetattr(fd, &Opt);
- for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++) {
- if (speed == name_arr[i]) {
- tcflush(fd, TCIOFLUSH);
- cfsetispeed(&Opt, speed_arr[i]);
- cfsetospeed(&Opt, speed_arr[i]);
- status = tcsetattr(fd, TCSANOW, &Opt);
- if (status != 0) {
- perror("tcsetattr fd1");
- return;
- }
- tcflush(fd,TCIOFLUSH);
- }
- }
- }
- /**
- * *@brief 璁剧疆涓插彛鏁版嵁浣嶏紝鍋滄?浣嶅拰鏁堥獙浣?
- * *@param fd 绫诲瀷 int 鎵撳紑鐨勪覆鍙f枃浠跺彞鏌?
- * *@param databits 绫诲瀷 int 鏁版嵁浣? 鍙栧??涓?7 鎴栬??
- * *@param stopbits 绫诲瀷 int 鍋滄?浣? 鍙栧?间负 1 鎴栬??
- * *@param parity 绫诲瀷 int 鏁堥獙绫诲瀷 鍙栧?间负N,E,O,,S
- * */
- int set_Parity(int fd,int databits,int stopbits,int parity)
- {
- struct termios options;
- if ( tcgetattr( fd,&options) != 0) {
- perror("SetupSerial 1");
- return(FALSE);
- }
- options.c_cflag &= ~CSIZE;
- switch (databits) /*璁剧疆鏁版嵁浣嶆暟*/
- {
- case 7:
- options.c_cflag |= CS7;
- break;
- case 8:
- options.c_cflag |= CS8;
- break;
- default:
- fprintf(stderr,"Unsupported data size\n"); return (FALSE);
- }
- switch (parity)
- {
- case 'n':
- case 'N':
- options.c_cflag &= ~PARENB; /* Clear parity enable */
- options.c_iflag &= ~INPCK; /* Enable parity checking */
- break;
- case 'o':
- case 'O':
- options.c_cflag |= (PARODD | PARENB); /* 璁剧疆涓哄?鏁堥獙*/
- options.c_iflag |= INPCK; /* Disnable parity checking */
- break;
- case 'e':
- case 'E':
- options.c_cflag |= PARENB; /* Enable parity */
- options.c_cflag &= ~PARODD; /* 杞?崲涓哄伓鏁堥獙*/
- options.c_iflag |= INPCK; /* Disnable parity checking */
- break;
- case 'S':
- case 's': /*as no parity*/
- options.c_cflag &= ~PARENB;
- options.c_cflag &= ~CSTOPB;break;
- default:
- fprintf(stderr,"Unsupported parity\n");
- return (FALSE);
- }
- /* 璁剧疆鍋滄?浣?/
- switch (stopbits)
- {
- case 1:
- options.c_cflag &= ~CSTOPB;
- break;
- case 2:
- options.c_cflag |= CSTOPB;
- break;
- default:
- fprintf(stderr,"Unsupported stop bits\n");
- return (FALSE);
- }
- /* Set input parity option */
- if (parity != 'n')
- options.c_iflag |= INPCK;
- tcflush(fd,TCIFLUSH);
- options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/
- options.c_oflag &= ~OPOST; /*Output*/
- options.c_cc[VTIME] = 150; /* 璁剧疆瓒呮椂15 seconds*/
- options.c_cc[VMIN] = 0; /* Update the options and do it NOW */
- if (tcsetattr(fd,TCSANOW,&options) != 0)
- {
- perror("SetupSerial 3");
- return (FALSE);
- }
- return (TRUE);
- }
- int OpenDev(char *Dev)
- {
- int fd = open( Dev, O_RDWR ); //| O_NOCTTY | O_NDELAY
- if (-1 == fd)
- {
- perror("Can't Open Serial Port");
- return -1;
- }
- else
- return fd;
- }
- int main(int argc, char **argv){
- int fd;
- int nread, nwrite;
- unsigned char buff[512];
- char *dev = "/dev/ttyS0"; //涓插彛1
- fd = OpenDev(dev);
- if ( fd == -1 ){
- perror("OpenDev");
- exit(0);
- } else {
- printf("DevOpened\n");
- }
- set_speed(fd,1200);
- if (set_Parity(fd,8,1,'N') == FALSE) {
- printf("Set Parity Error\n");
- exit (0);
- } else {
- printf("Parity set successfully\n");
- }
- buff[0] = 0x01;
- while (1)
- {
- //nread = read(fd, buff, sizeof(buf));
- nwrite = write(fd, buff, 1);
- if ( buff[0] == 0x00 )
- buff[0] = 0x01;
- else
- buff[0] = buff[0] << 1;
-
- if ( nwrite < 0 ){
- perror("write");
- } else {
- printf("%d bytes written\n", nwrite);
- }
- sleep(1);
- }
- close(fd);
- exit (0);
- }
复制代码
51板程序
- /*******************************************
- * serial communication
- * send and receive
- *
- * written by Manio
- * 04/27/08
- *
- * PC settings:
- * baud: 1200
- * parit: N
- * databits: 8
- * stopbit: 1
- *
- * BUG:
- * send or receive process may be interrupted
- * by receive or send interput ,and confuse
- * data appear
- *******************************************/
- #include
- void send_char_com(unsigned char ch) ;
- void delay( void );
- int main( void )
- {
- SP = 0x50;
- TMOD = 0x20;
- SCON = 0x50;
- TH1 = 0xE6;
- TL1 = 0xE6;
- EA = 1;
- ES = 1;
- TR1 = 1;
- while (1){
- send_char_com( 0x83 );
- delay();
- }
- return 0;
- }
- void delay( void )
- {
- int i;
- for ( i = 0; i < 10000; ++i );
- }
- void serial_rev( void ) interrupt 4
- {
- P1 = SBUF;
- RI = 0;
- }
- //向串口发送一个字符
- void send_char_com(unsigned char ch)
- {
- SBUF=ch;
- //TI will be set and a interrupt will be requested after one frame is sent
- //TI must be clear after sending
- while(TI==0);
- TI=0;
- }
复制代码 |
|