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

📄 spitongxun.c

📁 SPI串行通讯的chengxulizi。
💻 C
字号:
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------

#include <c8051f120.h>                 // SFR declarations
#include <stdio.h>
#include <absacc.h>
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F12x
//-----------------------------------------------------------------------------

sfr16 DP       = 0x82;                 // data pointer
sfr16 RCAP2    = 0xca;                 // Timer2 capture/reload
sfr16 RCAP3    = 0xca;                 // Timer3 capture/reload
sfr16 RCAP4    = 0xca;                 // Timer4 capture/reload
sfr16 TMR2     = 0xcc;                 // Timer2
sfr16 TMR3     = 0xcc;                 // Timer3
sfr16 TMR4     = 0xcc;                 // Timer4

//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------
#define uint unsigned int
#define uchar unsigned char
#define WRITE 0x00                  // SMBus WRITE command
#define READ  0x01                  // SMBus READ command

// Device addresses (7 bits, lsb is a don't care)
#define CHIP_SELECT 0xA0                 // Device address for chip A

 


//-----------------------------------------------------------------------------------
//Global VARIABLES
//-----------------------------------------------------------------------------------
char COMMAND;                       // Holds the slave address + R/W bit for
                                    // use in the SMBus ISR.
char WORD;                          // Holds data to be transmitted by the SMBus
                                    // OR data that has just been received.
bit SM_BUSY;                        // This bit is set when a send or receive
                                    // is started. It is cleared by the
                                    // ISR when the operation is finished.

unsigned char HIGH_ADD,LOW_ADD;
char BYTE_NUMBER;
uchar xdata image_adr[296][218];


sbit LED = P1^6;                       // LED='1' means ON
sbit SW1 = P3^7;                       // SW1='0' means switch pressed
sbit LED1=P2^4;
sbit LED2=P2^5;
sbit LED3=P2^6;
sbit LED4=P2^7;
sbit KEY1=P3^2;
sbit KEY2=P3^3;
sbit CE_RAM1=P2^1;
sbit CE_RAM2=P2^2;
sbit A16=P2^0;
sbit WAIT=P1^3;
sbit WP=P1^4;
sbit RST2102=P1^5;


//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------
void Oscillator_Init();
void PORT_Init (void);
void EMI_Init(); 
void UART0_Init (void);
void Timer1_Init();
void SPI_Init();
void MBF310_Init();
uchar MBF310_Read();
void SERIAL_Output(int m);
void MBF310_Image();
void MBF310_FIFO();
uchar Rram;
//-----------------------------------------------------------------------------
// Global VARIABLES
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------

void main (void) {
   int i,m;
   WDTCN = 0xde;                       // disable watchdog timer
   WDTCN = 0xad;
   Oscillator_Init();
   
   EMI_Init();                    // initialize oscillator
   PORT_Init ();                       // initialize crossbar and GPIO
   
   UART0_Init();   
   Timer1_Init();
   SPI_Init();
   A16=1;
   CE_RAM1=0;
   CE_RAM2=0;
   EA = 1;       //enable inerrupts
   MBF310_Init();   
  for(i=0;i<30000;i++);
     LED1=0;
  LED2=0;
  LED3=0;
  LED4=0;
     for(i=0;i<30000;i++);
  MBF310_Image();
 
    if(KEY1==1)
 {
   LED1=1;   
   LED3=1;
   }
 
   SERIAL_Output(0);
  LED1=1;
  LED2=1;
  LED3=1;
  LED4=1;  
   while (1) 
   {      
      for(i=0;i<=500;i++);
      if(KEY1==0) LED1=0;
      else LED1=1;
      if(KEY2==0) LED2=0;
      else LED2=1;      
   }
}


//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// This routine configures the crossbar and GPIO ports.
//
void PORT_Init (void)
{
   char SFRPAGE_SAVE = SFRPAGE;        // Save Current SFR page

   SFRPAGE = CONFIG_PAGE;              // set SFR page

   XBR0= 0x07;                  //Enable UART0,SPI0,SMB0
   XBR1= 0x04;                  //Enalbe INT0
   XBR2= 0x44;                    // Enable crossbar and weak pull-up
                                      
   //P0MDOUT=0x00;
   P0MDOUT |= 0x01;                    // Set TX1 pin to push-pull
   P1MDOUT |= 0x99;                    // Set P1.6(LED) to push-pull
   P2MDOUT = 0x07;
   P3MDOUT = 0x00;                    //漏极开路
   P3=0xFF;                 //High Impendance and Data in

   P4MDOUT = 0xFF;
   P5MDOUT = 0xFF;
   P6MDOUT = 0xFF;
   P7MDOUT = 0xFF;
   SFRPAGE = SFRPAGE_SAVE;             // Restore SFR page
}

//-----------------------------------------------------------------------------
// UART1_Init
//-----------------------------------------------------------------------------
//
// Configure the UART1 using Timer1, for <baudrate> and 8-N-1.
//
 
 
void EMI_Init()
{   char SFRPAGE_SAVE = SFRPAGE; 
    SFRPAGE   = EMI0_PAGE;
    EMI0CF    = 0x3C;
    EMI0TC    = 0xCF;
    SFRPAGE = SFRPAGE_SAVE;   
}

//-----------------------------------------------------------------------------
// UART0_Init
//-----------------------------------------------------------------------------
//
// Configure the UART0 using Timer1, for <baudrate> and 8-N-1.
//
void UART0_Init (void)
{  char SFRPAGE_SAVE = SFRPAGE; 
    SFRPAGE   = UART0_PAGE;
    SCON0     = 0x50;
 SFRPAGE = SFRPAGE_SAVE; 
}
/******************************************************************************/
void Oscillator_Init()
{
    int i = 0;
    
 char SFRPAGE_SAVE = SFRPAGE;        // Save Current SFR page

    SFRPAGE = CONFIG_PAGE;      
    OSCXCN    = 0x67;
    for (i = 0; i < 3000; i++);  // Wait 1ms for initialization
    while ((OSCXCN & 0x80) == 0);
    PLL0CN    = 0x04;       //PLL 参考时钟源为外部振荡器
    SFRPAGE   = LEGACY_PAGE;
    FLSCL     = 0x90;             //FLASH 存储器定时预分频器 FLRT:FLASH 读时间。01:SYSCLK <= 50 MHz
    SFRPAGE   = CONFIG_PAGE;
    PLL0CN    |= 0x01;         //PLL 偏置发生器被使能。要使PLL 工作,该位必须为‘1’
    PLL0DIV   = 0x01;
    PLL0FLT   = 0x27;        //PLL 滤波器寄存器
    PLL0MUL   = 0x04;
    for (i = 0; i < 15; i++);  // Wait 5us for initialization
    PLL0CN    |= 0x02;
    while ((PLL0CN & 0x10) == 0);
    CLKSEL    = 0x02;
 OSCICN    = 0x00;
 SFRPAGE = SFRPAGE_SAVE; 
}

void MBF310_Init()
{   
    char SFRPAGE_SAVE = SFRPAGE;        // Save Current SFR page
    SFRPAGE=SPI0_PAGE;
    NSSMD0=0; 
  
    SPIF = 0;
    SPI0DAT =0x02;   //Write command
    while (SPIF == 0);    
 SPIF = 0;  
    SPI0DAT =0x48;   //CTRLB`S Address
    while (SPIF == 0); 
 SPIF = 0;
    SPI0DAT =0x07;   //CTRLB`S data
    while (SPIF == 0); 
 NSSMD0=1; 
 NSSMD0=0;  
    SPIF = 0;
    SPI0DAT =0x02;   //Write command
    while (SPIF == 0);
 SPIF = 0;
    SPI0DAT =0x50;   //CTRLC`S Address 
    while (SPIF == 0);
    SPIF = 0;
    SPI0DAT =0x03;
    while (SPIF == 0);    
 NSSMD0=1; 
 NSSMD0=0; 
    SPIF = 0;
    SPI0DAT =0x02;   //Write command
    while (SPIF == 0);
 SPIF = 0;
    SPI0DAT =0x30;   //DTR`S Address 
    while (SPIF == 0);
    SPIF = 0;
    SPI0DAT =0x0f;
    while (SPIF == 0);
 NSSMD0=1; 
    //for(i=0;i<10;i++);

 NSSMD0=0;
    //for(i=0;i<10;i++);
    SPIF = 0;
    SPI0DAT =0x02;   //Write command
    while (SPIF == 0);
 SPIF = 0;
    SPI0DAT =0x38;   //DCR`S Address 
    while (SPIF == 0);
    SPIF = 0;
    SPI0DAT =0x08;
    while (SPIF == 0);
 NSSMD0=1; 
 NSSMD0=0; 
    SPIF = 0;
    SPI0DAT =0x02;   //Write command
    while (SPIF == 0);
 SPIF = 0;
    SPI0DAT =0x60;   //PGC`S Address 
    while (SPIF == 0);
    SPIF = 0;
    SPI0DAT =0x06;
    while (SPIF == 0);
    NSSMD0=1; 
 SFRPAGE = SFRPAGE_SAVE;  
}

/*void MBF310_FIFO()
{   int i,j,ii;
 NSSMD0=0; 
    for(i=0;i<10;i++);
    SPIF = 0;
    SPI0DAT =0x02;   //Write command
    while (SPIF == 0);
 SPIF = 0;
    SPI0DAT =0x98;   //FIFO`S Address 
    while (SPIF == 0);
    SPIF = 0;
    SPI0DAT =0x01;
    while (SPIF == 0);
    NSSMD0=1; 
    for(i=0;i<10;i++);
 NSSMD0=0;
 for(i=0;i<10;i++);
 SPIF=0;
 SPI0DAT=0x02;     //write command
    while(SPIF==0);
 SPIF=0;
 SPI0DAT=0x40;
 while(SPIF==0);
 SPIF=0;
 SPI0DAT=0x02;
 while(SPIF==0);
 for(ii=0;ii<10000;ii++);
 for(i=0;i<8;i++){    
       //NSSMD0=0;
    //for(i=0;i<10;i++);     
    for(j=0;j<218;j++){   
    image_adr[j]=MBF310_Read;
    //imagedata++;
    }       
     }
  NSSMD0=1;
  for(i=0;i<10;i++);
}*/
void MBF310_Image()
{   int i,j,ii;//jj;  
    char SFRPAGE_SAVE = SFRPAGE;        // Save Current SFR page
    SFRPAGE=SPI0_PAGE;   
 NSSMD0=1; 
    for(i=0;i<10;i++);
 NSSMD0=0;
 for(i=0;i<10;i++);
 SPIF=0;
 SPI0DAT=0x02;     //write command
    while(SPIF==0);
 SPIF=0;
 SPI0DAT=0x40;
 while(SPIF==0);
 SPIF=0;
 SPI0DAT=0x02;
 while(SPIF==0);
    for(ii=0;ii<9000;ii++);
 NSSMD0=1; 
 for(i=0;i<8;i++){ 
    NSSMD0=0;   
 //for(i=0;i<10;i++);
      SPIF = 0;
      SPI0DAT =0x03;   //Read command
      while (SPIF == 0); 
   SPIF = 0;  
   SPI0DAT=0x40;
   while(SPIF==0);
   SPIF=0; 
   SPI0DAT =0x00;
      while (SPIF == 0);
     for(j=0;j<218;j++){ 
       // while (SPIF == 0);  
     image_adr[j]=MBF310_Read();
     SPIF=0;
    // NSSMD0=1;
       // for(i=0;i<10;i++);
    }
        NSSMD0=1;
     }
 SFRPAGE = SFRPAGE_SAVE; 
}
uchar MBF310_Read()
{   uchar temp;  
 SFRPAGE=SPI0_PAGE;
 NSSMD0=0;   
 //for(i=0;i<10;i++);
    SPIF = 0;
    SPI0DAT =0x03;   //Read command
    while (SPIF == 0); 
 SPIF = 0;  
 SPI0DAT=0x40;
 while(SPIF==0);
 SPIF=0; 
 SPI0DAT =0x00;
    while (SPIF == 0);
    temp=SPI0DAT;
 return temp; 
}

void SERIAL_Output(int m)
{   int i,j;       
 char SFRPAGE_SAVE = SFRPAGE;        // Save Current SFR page
    SFRPAGE = UART0_PAGE;
 for(i=0;i<8;i++)
 {
     for(j=0;j<218;j++)
  {
     SBUF0=image_adr[m*8+i][j];
     //SBUF0=0x55;
     while(TI0==0);
           TI0=0;
    }
 } 
 SFRPAGE = SFRPAGE_SAVE; 
}
void SPI_Init()
{   char SFRPAGE_SAVE = SFRPAGE;        // Save Current SFR page
    SFRPAGE=SPI0_PAGE;
    SPI0CFG=0x47;
    SPI0CN=0x0F;
 SPI0CKR=0x10;
    SFRPAGE = SFRPAGE_SAVE; 
}
void Timer1_Init()
{   char SFRPAGE_SAVE = SFRPAGE; 
    SFRPAGE   = TIMER01_PAGE;
 PCON      &=0x7F;
    TMOD      = 0x20;
 CKCON     = 0x00;
 TL1       = 0xFF;
    TH1       = 0xFF;
    TR1=1;
 SFRPAGE = SFRPAGE_SAVE; 
}

⌨️ 快捷键说明

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