⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 串口直接收多字节写到2416.c

📁 24c16 收发程序 8051单片机
💻 C
字号:
#include <Reg52.h>

sbit LED_RX= 0xb5;
#define MAXNUM 30


unsigned char i;
unsigned char j;
unsigned char k,l;
unsigned char idata myarray[MAXNUM];


void Serial_Initial(void);
unsigned char ReceiveOneByteFromRS232(void);
void SendSerialOneByte(unsigned char ch);
void I2cWriteToC16(unsigned char No,unsigned char addr,unsigned char data1);
void WaitForUS(unsigned int val);
void I2cStart(void);
void I2cStop(void); 
bit  I2cSendByteToC16(unsigned char data2);
void _nop_(void);
sbit ISCL  = P1^0;
sbit ISDA  = P1^2;




void main(void)
{ 
 //上电后慢闪两次,等待串口输入, 在字节序列最后加上结束符ed,送出后快闪三次结束,字符数统计在00位置.  
  for(i=0;i<4;i++)
      {
        LED_RX =!LED_RX;
        for(j=0;j<20;j++)
         {
          WaitForUS(50000);
          WaitForUS(50000);
         }
      } 

      Serial_Initial(); 

 l=0;
 while(myarray[l]!=0xed)
  { 
    myarray[l]=ReceiveOneByteFromRS232();
    if (myarray[l]!=0xed)
      {        
        I2cWriteToC16(0Xa0,0X01+l,myarray[l]);           
        WaitForUS(5000);
        l++;
      }
   }  
      I2cWriteToC16(0Xa0,0X00,l);  WaitForUS(5000);

 for(i=0;i<6;i++)
      {
        LED_RX =!LED_RX;
        for(j=0;j<10;j++)
         {
          WaitForUS(50000);
          WaitForUS(50000);
         }
      }          
while(1);
}










void Serial_Initial(void)
{ SCON=0x50;
	PCON=0x00;
	TMOD=0x20;   
  TH1= 0xFD; 
  TL1= 0xFD;
}

unsigned char ReceiveOneByteFromRS232(void)
{
   unsigned int w;
   unsigned char tmp;
   Serial_Initial();
   TR1=1;
   while(RI==0);
   tmp=SBUF;
   RI=0;
   TR1=0; 
   return tmp;
}


void SendSerialOneByte(unsigned char ch)
{
	Serial_Initial();
	TR1 =1;
	SBUF = ch;
	while(TI==0);
	TI=0;
	TR1=0;
}




void I2cWriteToC16(unsigned char No,unsigned char addr,unsigned char data1)
{
    I2cStart();
    I2cSendByteToC16(No);
    I2cSendByteToC16(addr);
    I2cSendByteToC16(data1);
    I2cStop();
   
}

void I2cStart(void)
{
     ISDA=1;    
     ISCL=1;                 
     ISDA=0;               
     _nop_();     
     ISCL=0;   
} 

bit I2cSendByteToC16(unsigned char data2)
{
   unsigned char BitCounter;
    unsigned char bdata tmp;
    bit ack;
    tmp=data2;                                       /*choice device*/
    for(BitCounter=0;BitCounter<8;BitCounter++)
      {
       ISDA=(bit)(tmp&0x80);              
       ISCL=1;
       _nop_();        
       ISCL=0;
       _nop_();      
       tmp<<=1;
      }    
    ISDA=1;             
    _nop_();  
    ISCL=1;      
    _nop_();      
    ack=ISDA;  
    ISCL=0;     
    _nop_();
    return ack;
}      

void I2cStop(void)
{   
 ISDA=0;   
    ISCL=1;   
    _nop_();
    ISDA=1;
    _nop_();
    _nop_();  
    _nop_();
    _nop_();
}





void WaitForUS(unsigned int val)
{
   unsigned int tmp;

   tmp  = 65536-val;
   TL0  = tmp & 0xff;
   TH0  = (tmp>>8)&0xff;
   TF0  = 0;
   TR0  = 1;       
   while(TF0==0);  
   TR0  = 0;       
   TF0  = 0;
}





⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -