|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2013-10-22 17:31:51
|
显示全部楼层
如果单片机和GM8125相连,接收不到也发不出去。你看看是不是我的代码有问题?- /**************************************************************/
- /*CPU£ºAT89C51 */
- /*¾§ÌåƵÂÊ£º22.1184M */
- /*»úÆ÷ÖÜÆÚ£º0.54us */
- /*C51 */
- /**************************************************************/
- #include <stc12c5a60s2.h>
- #define DELAY_TIME 60000 //Delay time
- #define DELAY_TIME1 1
- /**********************I/O¶¨Òå***********************/
- sbit MS=P3^6; //GM8125¹¤×÷ģʽ¿ØÖÆ
- sbit RESET=P3^7; //GM8125¸´Î»Òý½Å¿ØÖÆ
- sbit SRADD0=P0^0;//GM8125½ÓÊÕµØÖ·µÚһλ
- sbit SRADD1=P0^1;//GM8125½ÓÊÕµØÖ·µÚ¶þλ
- sbit SRADD2=P0^2;//GM8125½ÓÊÕµØÖ·µÚÈýλ
- sbit STADD0=P0^4;//GM8125·¢Ë͵ØÖ·µÚһλ
- sbit STADD1=P0^5;//GM8125·¢Ë͵ØÖ·µÚ¶þλ
- sbit STADD2=P0^6;//GM8125·¢Ë͵ØÖ·µÚÈýλ
- /******************Êý¾Ý±äÁ¿¶¨Òå*****************/
- unsigned char SendBuff[5]={0xaa,0x45,0x67,0xbc,0xc9};
- unsigned char ReceiveBuff0;
- unsigned char ReceiveBuff1;
- unsigned char ReceiveBuff2;
- unsigned char ReceiveBuff3;
- unsigned char ReceiveBuff4;
- unsigned char ReceiveBuff5;
- unsigned char Contr_data;
- unsigned char ADD;
- unsigned char i=0 ,j1=0 ,j2=0 ,j3=0 ,j4=0 ,j5=0 ,c=0;
- /*********************************************/
- /*º¯ÊýÃû³Æ£ºdelay.C */
- /*º¯Êý¹¦ÄÜ£º ÑÓʱ³ÌÐò */
- /*********************************************/
- void delay(unsigned int m)
- {
- unsigned int n;
- n=0;
- while(n < m)
- {n++;}
- return;
- }
- /*********************************************/
- /*º¯ÊýÃû³Æ£ºsend0.c */
- /*º¯Êý¹¦ÄÜ£º Ñ¡Ôñ0¿ÚµØÖ· */
- /*********************************************/
- void sendwrite0()
- {
- STADD0=0;
- STADD1=0;
- STADD2=0;
- }
- void sendread0()
- {
- STADD0=0;
- STADD1=0;
- STADD2=0;
- SRADD0=0;
- SRADD1=0;
- SRADD2=0;
- }
- /*********************************************/
- /*º¯ÊýÃû³Æ£ºsend1.c */
- /*º¯Êý¹¦ÄÜ£º Ñ¡Ôñ1¿ÚµØÖ· */
- /*********************************************/
- void send1()
- {
- STADD0=1;
- STADD1=0;
- STADD2=0;
- }
- /*********************************************/
- /*º¯ÊýÃû³Æ£ºsend0.c */
- /*º¯Êý¹¦ÄÜ£º Ñ¡Ôñ2¿ÚµØÖ· */
- /*********************************************/
- void send2()
- {
- STADD0=0;
- STADD1=1;
- STADD2=0;
- }
- /*********************************************/
- /*º¯ÊýÃû³Æ£ºsend0.c */
- /*º¯Êý¹¦ÄÜ£º Ñ¡Ôñ3¿ÚµØÖ· */
- /*********************************************/
- void send3()
- {
- STADD0=1;
- STADD1=1;
- STADD2=0;
- }
- /*********************************************/
- /*º¯ÊýÃû³Æ£ºsend0.c */
- /*º¯Êý¹¦ÄÜ£º Ñ¡Ôñ4¿ÚµØÖ· */
- /*********************************************/
- void send4()
- {
- STADD0=0;
- STADD1=0;
- STADD2=1;
- }
- /*********************************************/
- /*º¯ÊýÃû³Æ£ºsend0.c */
- /*º¯Êý¹¦ÄÜ£º Ñ¡Ôñ5¿ÚµØÖ· */
- /*********************************************/
- void send5()
- {
- STADD0=1;
- STADD1=0;
- STADD2=1;
- }
- /*********************************************/
- /*º¯ÊýÃû³Æ£ºsend1_data.c */
- /*º¯Êý¹¦ÄÜ£º1¿Ú·¢ÉäÊý¾Ý
- /*********************************************/
- void send1_data()
- {
- send1();
- SBUF=ReceiveBuff1;
- while(!TI);
- TI=0;
- }
- /*********************************************/
- /*º¯ÊýÃû³Æ£ºsend2_data.c */
- /*º¯Êý¹¦ÄÜ£º2¿Ú·¢ÉäÊý¾Ý
- /*********************************************/
- void send2_data()
- {
- send2();
- SBUF=ReceiveBuff2;
- while(!TI);
- TI=0;
- }
- /*********************************************/
- /*º¯ÊýÃû³Æ£ºsend3_data.c */
- /*º¯Êý¹¦ÄÜ£º3¿Ú·¢ÉäÊý¾Ý
- /*********************************************/
- void send3_data()
- {
- send3();
- SBUF=ReceiveBuff3;
- while(!TI);
- TI=0;
- }
- /*********************************************/
- /*º¯ÊýÃû³Æ£ºsend3_data.c */
- /*º¯Êý¹¦ÄÜ£º4¿Ú·¢ÉäÊý¾Ý
- /*********************************************/
- void send4_data()
- {
- send4();
- SBUF=ReceiveBuff4;
- while(!TI);
- TI=0;
- }
- /*********************************************/
- /*º¯ÊýÃû³Æ£ºsend3_data.c */
- /*º¯Êý¹¦ÄÜ£º5¿Ú·¢ÉäÊý¾Ý
- /*********************************************/
- void send5_data()
- {
- send5();
- SBUF=ReceiveBuff5;
- while(!TI);
- TI=0;
- }
- void main()
- {
- TMOD= 0x20; //Ö¸¶¨¶¨Ê±Æ÷1¹¤×÷ÔÚ·½Ê½2
- IE = 0x90; //¿ª´®ÐпÚÖжÏ
- SCON=0xc0; //´®Ðпڹ¤×÷ÔÚ·½Ê½3
- TH1 = 0xf8; //×°È붨ʱÆ÷1³õÖµ,ÉèÖÃÖ÷»ú¹¤×÷²¨ÌØÂÊΪ7200bps
- TL1 = 0xf8;
- PCON=0x00;
- RESET=0; //¶ÔGM8125½øÐи´Î»²Ù×÷
- RESET=1;
- delay(DELAY_TIME);
- Contr_data=0xfc; //×°ÈëÃüÁî×Ö³õÖµ
- TR1=1; //Æô¶¯¶¨Ê±Æ÷1
- MS=0;
- sendwrite0();
- SBUF=Contr_data; //ÉèÖÃGM8125×Ó´®¿Ú²¨ÌØÂÊΪ19200bps£¬Ä¸´®¿Ú²¨ÌØÂÊΪ115200bps
- while(!TI);
- TI=0;
- delay(DELAY_TIME);
- REN=1;
- MS=1; //¶ÁÃüÁî×Ö
- sendread0();
- delay(DELAY_TIME);
- while (ReceiveBuff0!=Contr_data) //Ñé֤дÈëµÄÃüÁî×ÖÊÇ·ñÕýÈ·£¬²»ÕýÈ·ÔòÖØÐÂд
- {
- MS=0; //GM8125¹¤×÷ÔÚдÃüÁî×Ö¹¤×÷ģʽÏÂ
- sendwrite0();
- SBUF=Contr_data;
- while(!TI);
- TI=0;
- delay(DELAY_TIME);
- REN=1;
- MS=1; //¶ÁÃüÁî×Ö
- sendread0();
- delay(DELAY_TIME);
- }
- REN=0;
- MS=0; //ÉèÖÃGM8125¹¤×÷ÔÚ¶àͨµÀ¹¤×÷ģʽÏÂ
- TR1=0; //¶¨Ê±Æ÷1Í£Ö¹
- TH1 = 0xff; //×°È붨ʱÆ÷1³õÖµ,ÉèÖÃÖ÷¿ØMCU¹¤×÷²¨ÌØÂÊΪ115200bps
- TL1 = 0xff;
- PCON=0x80;
- TR1=1; //Æô¶¯¶¨Ê±Æ÷1
- ReceiveBuff1=0X01;
- ReceiveBuff2=0X02;
- ReceiveBuff3=0X03;
- ReceiveBuff4=0X04;
- ReceiveBuff5=0X05;
- while(1)
- {
- send1_data();
- delay(DELAY_TIME);
-
- send2_data();
- delay(DELAY_TIME);
- send3_data();
- delay(DELAY_TIME);
- send4_data();
- delay(DELAY_TIME);
- send5_data();
- delay(DELAY_TIME);
- }
- }
- void CommReceive(void) interrupt 4
- {
- int i;
- if(RI)
- {
- for(i=0;i<1;i++)
- {
- if(SRADD2==0&&SRADD1==0&&SRADD0==0)
- {
- ReceiveBuff0=SBUF; //¶ÁÃüÁî×Ö´æÈëReceiveBuff0
- break;
- }
- if(SRADD2==0&&SRADD1==0&&SRADD0==1)
- {
- ReceiveBuff1=SBUF; //¶ÁÃüÁî×Ö´æÈëReceiveBuff0
- break;
- }
- if(SRADD2==0&&SRADD1==1&&SRADD0==0)
- {
- ReceiveBuff2=SBUF; //¶ÁÃüÁî×Ö´æÈëReceiveBuff0
- break;
- }
- if(SRADD2==0&&SRADD1==1&&SRADD0==1)
- {
- ReceiveBuff3=SBUF; //¶ÁÃüÁî×Ö´æÈëReceiveBuff0
- break;
- }
- if(SRADD2==1&&SRADD1==0&&SRADD0==0)
- {
- ReceiveBuff4=SBUF; //¶ÁÃüÁî×Ö´æÈëReceiveBuff0
- break;
- }
- if(SRADD2==1&&SRADD1==0&&SRADD0==1)
- {
- ReceiveBuff5=SBUF; //¶ÁÃüÁî×Ö´æÈëReceiveBuff0
- break;
- }
- }
- RI=0;
- }
- return;
- }
复制代码 |
|