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

📄 main.c

📁 基于LPC2100的CAN转串口的源码
💻 C
字号:


//********************************************************************************************************/
#include "config.h"
#include ".\CAN\IncludeCAN.h"
//for(i=0;i<=73;i++);//8us
//for(i=0;i<=219;i++);//24us
 //-------------------------------------------
void LEDGlares()
{
  int i;
  //PINSEL0=0x00000000;
  PINSEL1=0x00000000;
  IODIR = 0x000C0000;
  while(1)
 {
  IO0SET= 0x000C0000;
  for(i=0;i<=1000000;i++);//140ms
  
  IO0CLR= 0x000C0000;
  for(i=0;i<=1000000;i++);
 
 }
}
//-----------------------------------------
void LED1()
{
 PINSEL1=0x00000000;
 IODIR = 0x000C0000;
 IO0SET= 0x000C0000;
 IO0CLR= 0x000C0000;


}
//------------------------------------------
void LED2()
{
 PINSEL1=0x00000000;
 IODIR = 0x000C0000;
 IO0SET= 0x00040000;
}
//--------------------------------------------------------------------------------
void USBWriteCMD(uint32 CMD)              
{
   int i;
   IO0DIR=0x0000ff72; // set data p0.8-15 ,wr,rd,a0 p0.1.4,5,6 as output 
  
  // IO0SET=0x00000002;  //cs=1
  // IO0CLR=0x00000002;  //cs=0
   IO0CLR=0x00000010;//a0=0
   //IO0SET=0x00000010;  //a0=1
   IO0SET=0x00000040;  //wr=1
   IO0SET=0x00000020;  //rd=1
   
  // CMD=CMD<<8;
   IO0PIN=CMD; 
  
   IO0SET=0x00000010;  //a0=1
   for(i=0;i<=73;i++); //8us
   IO0CLR=0x00000040;  //wr=0
  // for(i=0;i<=73;i++); //8us
   IO0SET=0x00000040;  //wr=1
  // for(i=0;i<=73;i++); //8us 
  
   
   IO0CLR=0x00000010;  //a0=0
   IO0SET=0x00000040;  //wr=1
   IO0SET=0x00000020;  //rd=1
   //IO0SET=0x00000002;  //cs=1
}
//---------------------------------------------------------------------
void USBWriteData(uint32 Data)              
{
  int i;
  IO0DIR=0x0000ff72; // set data p0.8-15 ,wr,rd,a0 p0.0.4,5,6 as output    
  
 // IO0SET=0x00000002;  //cs=1
  // IO0CLR=0x00000002;  //cs=0
  //IO0SET=0x00000010;  //a0=1
  IO0CLR=0x00000010;  //a0=0
  IO0SET=0x00000040;  //wr=1
  IO0SET=0x00000020;  //rd=1
  
 // Data=Data<<8;
  IO0PIN=Data;
  
  //IO0CLR=0x00000010;  //a0=0
 // for(i=0;i<=73;i++); //8us
  IO0CLR=0x00000040;  //wr=0
  for(i=0;i<=73;i++); //8us
  IO0SET=0x00000040;  //wr=1
 // for(i=0;i<=73;i++); //8us 
  
  IO0SET=0x00000010;  //a0=1
  IO0SET=0x00000040;  //wr=1
  IO0SET=0x00000020;  //rd=1
  // IO0SET=0x00000002;  //cs=1
 }
//-----------------------------------------
uint32 USBReadData( )
{
  int i;
  uint32 Data;
 
 IO0DIR=0x00000072;  // set data p0.8-15 as input ,wr,rd,a0 p0.0.4,5,6 as output
  
 // IO0SET=0x00000002;  //cs=1
 //  IO0CLR=0x00000002;  //cs=0
  IO0CLR=0x00000010;  //a0=0
  IO0SET=0x00000040;  //wr=1
  IO0CLR=0x00000020;  //rd=0
 
 //delay10us(1);       // 10us
  Data=IO0PIN;
 // Data=Data>>8;
  for(i=0;i<=73;i++);  //8us
  IO0SET=0x00000020;   //rd=1
  return(Data);
  
  IO0SET=0x00000010;  //a0=1
  IO0SET=0x00000040;  //wr=1
  IO0SET=0x00000020;  //rd=1
 // IO0SET=0x00000002;  //cs=1
}
//-----------------------------------------
InitCH372()
{
 int i;
 uint32 Data;
 USBWriteCMD(0x00000500);              
 for(i=0;i<=400000;i++);//delay 40ms
 while(1)
 {
  USBWriteCMD(0x00000500);              
  for(i=0;i<=400000;i++);//delay 40ms
 
  USBWriteCMD(0x00000600);              
   
  USBWriteData(0x0000aa00);              
  
  Data=USBReadData( );

 Data=(Data>>8);
  Data=(Data&0x000000ff);
  if(Data==0x00000055)
    // LEDGlares();
      
      break;
  }

 USBWriteCMD(0x00001500);//set usb mode 2
  
 USBWriteCMD(0x00000200);              
 //LED1();
}
//----------------------------------------
int main (void)
{
uint32 Data;

IO0DIR=0x0000ff72; // set data p0.8-15 ,wr,rd,a0 p0.1.4,5,6 as output

PINSEL0 = 0x0000c000;			// 设置IO口工作模式,P0.7设置为EINT2
PINSEL1 = 0x00000000;
 
 IO0CLR=0x00000010;  //a0=0
 IO0SET=0x00000040;  //wr=1
 IO0SET=0x00000020;  //rd=1
 IO0SET=0x00000002;  //cs=1
 
 IO0CLR=0x00000002;  //cs=0
 LEDGlares();
 InitCH372(); 
//LED1();
//USBWriteCMD(0x00000002);
/*IO0DIR=0x0000ff70;
Data=0x000000aa;
 Data=Data<<8;
  IO0PIN=Data;
  t=IO0PIN;
  t=t>>8;
  t=(t&0x000000ff);
   if(t==0x000000aa)
     {
      LEDGlares();
      
      }*/
}

/*********************************************************************************************************
**                            End Of File
********************************************************************************************************/

⌨️ 快捷键说明

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