main.c
来自「DG128 SPI 接口程序 在飞思卡尔DG128完成SPI协议的通信」· C语言 代码 · 共 52 行
C
52 行
#include <hidef.h> /* common defines and macros */
#include <mc9s12dg128.h> /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12dg128b"
//写、读一个字节
U8 SPI_RdWr_Byte(U8 val)
{
SPI0DR = val;
while(!(SPI0SR_SPIF));
return SPI0DR;
}
U8 SPI_ReadByte(void)
{
SPI0DR = 0xff;
while(!(SPI0SR_SPIF));
return SPI0DR;
}
void SPI_Init(void)
{
DDRT = 0xff;
MODRR = 0x00;
SPI0CR2 = 0x10;
SPI0CR1 = 0x5e;
//SPI0BR = 0x02; //BR=2M
SPI0BR = 0x42; //BR=busclk/((SPPR + 1)· 2^(SPR + 1))=16000/(5*8)=400k
}
void setbusclock(void)
{
CLKSEL=0X00; //disengage PLL to system
PLLCTL_PLLON=1; //turn on PLL
SYNR=1;
REFDV=1; //pllclock=2*osc*(1+SYNR)/(1+REFDV)=32MHz;
_asm(nop); //If PLL is selected (PLLSEL=1), Bus Clock = PLLCLK / 2=16M
while(!(CRGFLG_LOCK==1))
_asm(nop); //when pll is steady ,then use it;
CLKSEL_PLLSEL =1; //engage PLL to system;
}
void main(void) {
/* put your own code here */
EnableInterrupts;
setbusclock();
SPI_Init();
for(;;){
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?