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

📄 main.c

📁 有关SPI的一段应用程序
💻 C
字号:
#include <hidef.h>      /* common defines and macros */
#include <mc9s12dg128.h>     /* derivative information */


#pragma LINK_INFO DERIVATIVE "mc9s12dg128b"

uchar DATA[20] = {
  0x00,//b159~152,an extra dummy first
  0x00,//b151~144,first 17bits is zeros
  0x13,//b143~136,b5:not select MBS_OUT;b3:disable digital test;b2:reset internally;b1:enable frame transfer of the shutter;b0:enable FPN correction
  0x04,//b135~128,b7:disable binning;b6~b5:disable sub_sampling;b4~b3:disable column testing;
  0x80,//b127~120,connect with the last 3bits of the former byte setting the Y window size,excluding last 2bits
  0x00,//b119~112,Y window start position excluding b0
  0xb4,//b111~104,setting X window size
  0x00,//b103~ 96,X window start position excluding b7,connect with the last 3bits of the next byte
  0x07,//b 95~ 88,b4~b3 set CMD_Image externally
  0xc0,//b 87~ 80,
  0x00,//b 79~ 72,
  0x00,//b 71~ 64,
  0x00,//b 63~ 56, 
  0x00,//b 55~ 48,
  0x60,//b 47~ 40,
  0xa7,//b 39~ 32,
  0xb9,//b 31~ 24,
  0x74,//b 23~ 16,
  0x30,//b 15~  8,
  0x10 //b  7~  0,
};

void Init_Spi0(void)
{
   MODRR = 0x00;
   SPI0BR = 0x02;
   SPI0CR2 = 0x10;
   SPI0CR1 = 0x5e;
   DDRB  = 0xFF;
   PORTB = 0x3C;
}

void Init_Clk(void)
{
  asm
  {
    LDAB  #1
    STAB  REFDV
    LDAB  #3
    STAB  SYNR								 ;System Clock Setting ->Bus Clk=32MHz
  Wait:
    BRCLR CRGFLG,#$08,*
    BSET  CLKSEL,#$80  				 ;Enable PLL Module
    
    LDAB  #$00								 
    STAB  PEAR								 ;Enable External Clock
  }
}

void Get_Ref(void)
{
  uint i;
  for(i=0;i<10;i++)
  {
    asm
    {
      LDAB #$00
      STAB PORTK
      NOP
      LDAB #$80
      STAB PORTK
      NOP
    }
  }
  asm
  {
    LDAB #$00
    STAB PORTK
    LDAA PORTA
    STAA Min
    STAA Max
    LDAB #$80
    STAB PORTK
  }
  for(i=0;i<175;i++)
  {
    asm
    {
      LDAB #$00
      STAB PORTK
      LDAA PORTA
      LDX  #$00
      MINM [Min,X]
      MAXM [Max,X]
      LDAB #$80
      STAB PORTK
    }
  }
  for(i=0;i<3;i++)
  {
    asm
    {
      LDAB #$00
      STAB PORTK
      NOP
      LDAB #$80
      STAB PORTK
      NOP
    }
  }
}

void Sample(void)
{
  uchar evalue,data;
  uint i,num,eflag;
  evalue = (3*Min+5*Max)/8;
  for(i=0;i<10;i++)
  {
    asm
    {
      LDAB #$00
      STAB PORTK
      NOP
      LDAB #$80
      STAB PORTK
      NOP
    }
  }
  num = 0;
  eflag = 0;
  for(i=0;i<176;i++)
  {
    asm
    {
      LDAB #$00
      STAB PORTK
      LDAA PORTA
      STAA data
      LDAB #$80
      STAB PORTK
    }
    if(data<evalue)
    {
      num++;
    }
    else
    {
      if(num>3)
      {
        eflag++;
        Position = i-(num/2);
      }
      num=0;
    }
    if(eflag>=1) break;
  }
  for(i=0;i<3;i++)
  {
    asm
    {
      LDAB #$00
      STAB PORTK
      NOP
      LDAB #$80
      STAB PORTK
      NOP
    }
  }
}

void Deliver(void)
{
  asm
  {
    BRCLR SCI0SR1,#$80,*
    LDAB  Min
    STAB  SCI0DRL
    BRCLR SCI0SR1,#$80,*
    LDAB  Max
    STAB  SCI0DRL
    BRCLR SCI0SR1,#$80,*
    LDAB  Position
    STAB  SCI0DRL
    BRCLR SCI0SR1,#$80,*
    LDAB  #$FF
    STAB  SCI0DRL
  }
}

void main(void) 
{
  uint counter;
  counter = 0x00;
  Init_Spi0();
  Init_Clk();
  counter = 0x00;
  DDRJ = 0xff;
  PTJ  = 0xff;
  for(;;)
  {
    SPI0DR = DATA[0];
    for(counter=0;counter<20;counter++)
    {
      while(!(SPI0SR&0x20))
      {}
      SPI0DR = DATA[counter];
    }
    PORTB = 0x0fc;
    PTJ = 0x01;
    asm
    {
      NOP
      NOP
      NOP
      NOP
      NOP
    }
    PTJ = 0xc0;
    for(counter=0;counter<100;counter++)
    {}
    PORTB = 0x3c;
  }
  /* put your own code here */
  EnableInterrupts;
  for(;;) {} /* wait forever */
}

⌨️ 快捷键说明

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