单片机源程序如下:
- /****************************************************************
- 文件名:Main.c
- 功能描述:主程序
- ****************************************************************/
- #define GLOBAL_CLK 1
- #include <stdlib.h>
- #include <string.h>
- #include "def.h"
- #include "option.h"
- #include "2440addr.h"
- #include "2440lib.h"
- #include "2440slib.h"
- #include "Uart.h"
- void SPI_Init(void);
- void WriteByte_MMA7455L(U8 addr,U8 dat);
- U8 ReadByte_MMA7455L(U8 addr);
- static U32 cpu_freq;
- static U32 UPLL;
- static void cal_cpu_bus_clk(void)
- {
- U32 val;
- U8 m, p, s;
- val = rMPLLCON; //MPLLCON上电初始值为0x00096030
- m = (val>>12)&0xff; //结果为0x96=150
- p = (val>>4)&0x3f; //结果为0x03=3
- s = val&3; //结果为0
- //(m+8)*FIN*2 不要超出32位数!
- FCLK = ((m+8)*(FIN/100)*2)/((p+2)*(1<<s))*100;//"1<<s"表示令1左移s位,即可得2^s
- val = rCLKDIVN;
- m = (val>>1)&3;
- p = val&1;
- val = rCAMDIVN;
- s = val>>8;
- switch (m) {
- case 0:
- HCLK = FCLK;
- break;
- case 1:
- HCLK = FCLK>>1;
- break;
- case 2:
- if(s&2)
- HCLK = FCLK>>3;
- else
- HCLK = FCLK>>2;
- break;
- case 3:
- if(s&1)
- HCLK = FCLK/6;
- else
- HCLK = FCLK/3;
- break;
- }
- if(p)
- PCLK = HCLK>>1;
- else
- PCLK = HCLK;
- if(s&0x10)
- cpu_freq = HCLK;
- else
- cpu_freq = FCLK;
- val = rUPLLCON;
- m = (val>>12)&0xff;
- p = (val>>4)&0x3f;
- s = val&3;
- UPLL = ((m+8)*FIN)/((p+2)*(1<<s));
- UCLK = (rCLKDIVN&8)?(UPLL>>1):UPLL;
- }
- void Main(void)
- {
- U8 key,sta;
- int i;
- U32 mpll_val = 0;
- i = 2 ;
- switch ( i ) {
- case 0: //200
- key = 12;
- mpll_val = (92<<12)|(4<<4)|(1);
- break;
- case 1: //300
- key = 13;
- mpll_val = (67<<12)|(1<<4)|(1);
- break;
- case 2: //400M
- key = 14;
- mpll_val = (92<<12)|(1<<4)|(1);
- break;
- case 3: //440!!!
- key = 14;
- mpll_val = (102<<12)|(1<<4)|(1);
- break;
- default:
- key = 14;
- mpll_val = (92<<12)|(1<<4)|(1);
- break;
- }
- //rMPLLCON赋值0x5c011
- ChangeMPllValue((mpll_val>>12)&0xff, (mpll_val>>4)&0x3f, mpll_val&3);
- ChangeClockDivider(key, 12); //key=14
- cal_cpu_bus_clk();
-
- Port_Init(); //IO端口初始化
- SPI_Init();//SPI初始化
- MMA7455L_INIT();//MMA7455L初始化
- Uart_Init();//串口初始化
- Uart_Select(0);
- Delay(5); //延时10ms让IIC和SPI准备就绪
- Uart_Printf("CLKCON=%x!",rCLKCON);
- Uart_Printf("开始发送数据!\n\n");
- //对MMA7455L的0x16寄存器写配置数据0x05
- WriteByte_MMA7455L(0x16,0x05);
- //读回0x16寄存器的配置值,判断SPI通信是否成功.
- sta=ReadByte_MMA7455L(0x16);
- Uart_Printf("sta=0x%x",sta);
- while(1)
- {
- }
- }
- void WriteByte_MMA7455L(U8 addr,U8 dat)
- {
- int n;
- //片选CS置低,使能MMA7455为SPI模式
- rGPFDAT &= 0xef;
- Delay(1);
-
- Uart_Printf("SPI状态寄存器的数据为0x%x!\n",rSPSTA0);
- while(!(rSPSTA0&0x01==0x01)) ;
- //高位置1写操作
- rSPTDAT0 = 0x80|((addr & 0x3f)<<1);
- Uart_Printf("SPI状态寄存器的数据为0x%x!\n",rSPSTA0);
- Uart_Printf("写操作地址发送成功!\n");
- for(n=0;n<20;n++);
- Uart_Printf("SPI状态寄存器的数据为0x%x!\n",rSPSTA0);
- while(!(rSPSTA0&0x01==0x01)) ;
- rSPTDAT0 = dat; //写数据
- Uart_Printf("SPI状态寄存器的数据为0x%x!\n",rSPSTA0);
- Uart_Printf("写操作数据发送成功!\n");
- for(n=0;n<20;n++);
- //片选CS置高电平,结束SPI通信
- rGPFDAT |= 0xff;
- Delay(1);
- }
- U8 ReadByte_MMA7455L(U8 addr)
- {
- int n;
- U8 Re_Dat;
- //片选CS置低电平,使能MMA7455L为SPI模式
- rGPFDAT &= 0xef;
- Delay(1);
- Uart_Printf("SPI状态寄存器的数据为0x%x!\n",rSPSTA0);
- while(!(rSPSTA0&0x01==0x01)) ;
- //读操作高位清0
- rSPTDAT0 = (addr & 0x3f)<<1;
- Uart_Printf("SPI状态寄存器的数据为0x%x!\n",rSPSTA0);
- Uart_Printf("读操作地址发送成功!\n");
- for(n=0;n<20;n++);
- Uart_Printf("SPI状态寄存器的数据为0x%x!\n",rSPSTA0);
- while(!(rSPSTA0&0x01==0x01));
- Re_Dat = rSPRDAT0;
- Uart_Printf("SPI状态寄存器的数据为0x%x!\n",rSPSTA0);
- for(n=0;n<20;n++);
- //片选CS置高电平,结束SPI通信
- rGPFDAT |= 0xff;
- Delay(1);
- //返回读取数据
- return Re_Dat;
- }
- //MMA7455L初始化
- void MMA7455L_INIT(void )
- {
- MMA_QUEUE.MMA_HEAD=0;
- MMA_QUEUE.MMA_CUR=0;
- SPI_WriteREG(0x16,0x05);//级别模式
- delay_nus(10);
- SPI_WriteREG(0x18,0x20);
- delay_nus(10);
- SPI_WriteREG(0x19,0x00);
- delay_nus(10);
- SPI_WriteREG(0x1a,0x00);
- delay_nus(10);
- }
- //SPI初始化
- void SPI_Init(void)
- {
- //激活时钟控制器的SPI位
- rCLKCON |= 1 <<18;
- rSPCON0 = (0<<6)|(0<<5)|(1<<4)|(1<<3)|(0<<2)|(0<<1)|1;
- //查询模式;SCK使能;主机模式;时钟低电平有效;格式A;普通模式;
- //MMA7455的SPI的传输波特率在工作电压小于2.4V时最高可达4MHz,大于2.4V时可达8MHz
- //SPI波特率为50MHz/2/(24+1)=1MHz
- rSPPRE0 = 24;
- }
复制代码
所有资料51hei提供下载:
第4章.zip
(13.24 KB, 下载次数: 6)
|