跨届大侠
北京龙邱智能科技有限公司
- 积分
- 10332
- 威望
- 2905
- 贡献
- 6151
- 兑换币
- 4515
- 注册时间
- 2008-6-7
- 在线时间
- 638 小时
|
<>/**************************************************************************************<BR>龙丘MC9S12(DG128)多功能开发板 <BR>Designed by Chiu Sir<BR>E-mail:chiusir@163.com<BR>软件版本:V1.1<BR>最后更新:2008年11月28日<BR>相关信息参考下列地址:<BR>博客: <A target=_blank href="http://longqiu.21ic.org/">http://longqiu.21ic.org</A><BR>淘宝店:<A target=_blank href="http://shop36265907.taobao.com/">http://shop36265907.taobao.com</A><BR>------------------------------------<BR>Code Warrior 4.6<BR>Target : MC9S12DG128B<BR>Crystal: 16.000Mhz<BR>busclock: 8.000MHz<BR>pllclock:16.000MHz</P><P>本程序主要包括以下功能:<BR>1.设置锁相环和总线频率;<BR>2.初始化SPI为400kbps;<BR>3.SPI读函数和写函数。<BR>*****************************************************************************************/<BR>#include <hidef.h> /* common defines and macros */<BR>#include <mc9s12dg128.h> /* derivative information */<BR>#pragma LINK_INFO DERIVATIVE "mc9s12dg128b"</P><P>//----------------------------------------------------- <BR>//延时1毫秒 <BR>void Dly_ms(int ms)<BR>{<BR> int ii,jj;<BR> if (ms<1) ms=1;<BR> for(ii=0;ii<ms;ii++)<BR> for(jj=0;jj<2670;jj++); //busclk:16MHz--1ms<BR>}<BR>//----------------------------------------------------- <BR>//SPI写一个字节<BR>void SPI_Write_Byte(U8 val)<BR>{<BR> SPI0DR = val;<BR> while(!(SPI0SR_SPIF));<BR> return;<BR>}<BR>//----------------------------------------------------- <BR>//SPI读一个字节 <BR>U8 SPI_Read_Byte(void)<BR>{ <BR> while(!(SPI0SR_SPIF));<BR> return SPI0DR;<BR>}<BR>//----------------------------------------------------- <BR>//SPI初始化,速率400kbps <BR>void SPI_Init(void) <BR>{<BR> DDRT = 0xff;<BR> MODRR = 0x00; <BR> SPI0CR2 = 0x10;<BR> SPI0CR1 = 0x5e; <BR> SPI0BR = 0x42; //BR=busclk/((SPPR + 1)· 2^(SPR + 1))=16000/(5*8)=400k <BR>}<BR>//----------------------------------------------------- <BR>//总线频率为16M <BR>void setbusclock(void)<BR>{ <BR> CLKSEL=0X00; //disengage PLL to system<BR> PLLCTL_PLLON=1; //turn on PLL<BR> SYNR=1; <BR> REFDV=1; //pllclock=2*osc*(1+SYNR)/(1+REFDV)=32MHz;<BR> _asm(nop); //If PLL is selected (PLLSEL=1), Bus Clock = PLLCLK / 2=16M <BR> while(!(CRGFLG_LOCK==1)) <BR> _asm(nop); //when pll is steady ,then use it;<BR> CLKSEL_PLLSEL =1; //engage PLL to system; <BR>}<BR>//====================================================== <BR>void main(void) {<BR> unsigned char spich=0;<BR> EnableInterrupts;<BR> <BR> setbusclock(); <BR> SPI_Init();<BR> <BR> for(;;)<BR> {<BR> Dly_ms(200);<BR> SPI_Write_Byte(0xff);<BR> spich=SPI_Read_Byte();<BR> /*<BR> 在这里添加你的代码!这是我SD卡操作的原函数,绝对好用!<BR> */ <BR> } <BR>}<BR>需要完整工程请单击:<A target=_blank href="http://blog.21ic.com/UploadFiles/2008-11/282233989683.rar">demoSPI.rar</A></P> |
|