/******SPI.C文件********/ #include "SPI_ml.h" #define SDI_1 Pinout(1,0,1) #define SDI_0 Pinout(1,0,0) #define SCK_1 Pinout(1,2,1) #define SCK_0 Pinout(1,2,0) #define SPI_SS_1 Pinout(1,5,1) #define SPI_SS_0 Pinout(1,5,0) #define I0_SDO1 (P1IN&BIT1)//备用按钮 uchar atey[10]; void Pinout(unsigned char pinx,unsigned char pin,unsigned char out) { if(pinx==1) { switch(pin) { case 0: if(out==1){ P1OUT |= BIT0;} else{P1OUT &= ~BIT0;}break; case 1: if(out==1){ P1OUT |= BIT1;} else{P1OUT &= ~BIT1;}break; case 2: if(out==1){ P1OUT |= BIT2;} else{P1OUT &= ~BIT2;}break; case 3: if(out==1){ P1OUT |= BIT3;} else{P1OUT &= ~BIT3;}break; case 4: if(out==1){ P1OUT |= BIT4;} else{P1OUT &= ~BIT4;}break; case 5: if(out==1){ P1OUT |= BIT5;} else{P1OUT &= ~BIT5;}break; case 6: if(out==1){ P1OUT |= BIT6;} else{P1OUT &= ~BIT6;}break; case 7: if(out==1){ P1OUT |= BIT7;} else{P1OUT &= ~BIT7;} break; } } else if(pinx==2) { switch(pin) { case 0: if(out==1){ P2OUT |= BIT0;} else{P2OUT &= ~BIT0;}break; case 1: if(out==1){ P2OUT |= BIT1;} else{P2OUT &= ~BIT1;}break; case 2: if(out==1){ P2OUT |= BIT2;} else{P2OUT &= ~BIT2;}break; case 3: if(out==1){ P2OUT |= BIT3;} else{P2OUT &= ~BIT3;}break; case 4: if(out==1){ P2OUT |= BIT4;} else{P2OUT &= ~BIT4;}break; case 5: if(out==1){ P2OUT |= BIT5;} else{P2OUT &= ~BIT5;}break; case 6: if(out==1){ P2OUT |= BIT6;} else{P2OUT &= ~BIT6;} break; case 7: if(out==1){ P2OUT |= BIT7;} else{P2OUT &= ~BIT7;}break; } } } void delay_spi(unsigned int x)//1us 11M { unsigned char i,c; for(i=0;i<x;i++) { c++; c++; } } /*****等待写入结束******/ void waitwiriteEND(uint x) { uint i; for(i=0;i<x;i++) { SCK_0; delay_spi(5); SCK_1; delay_spi(5); } } //初始化SPI void int_SPI() { SCK_1; //置高时钟 SPI_SS_1; } /*********发送一个字节************/ void send_SPI(unsigned char data_send) { unsigned char i,s=0; for(i=0;i<8;i++) { SCK_0; s=(data_send&0x01); if(s==1) SDI_1; else SDI_0; delay_spi(5); SCK_1; delay_spi(5); data_send>>=1; } } /*********读取一个字节************/ unsigned char read_SPI() { unsigned char i,data_read=0; for(i=0;i<8;i++) { data_read<<=1; SCK_0; delay_spi(5); SCK_1; delay_spi(5); data_read|=I0_SDO1; } return data_read; } /*********发送写入允许指令************/ void write_SR() { /*****发送第一个字节——指令1****/ SPI_SS_0; //send_SPI(byte1);//写使能06 04 send_SPI(write_1);//写使能06 04 SPI_SS_1; } /*********读取寄存器************/ void read_SR() { uchar read_sr1; //uchar read_sr2=0; /*****发送第一个字节——指令1****/ SPI_SS_0; send_SPI(read_SR_1);//读状态寄存器1 05 35 //ead_sr1=read_SPI(); // SPI_SS=1; // SPI_SS=0; // send_SPI(read_SR_2);//读状态寄存器2 05 35 // read_sr2=read_SPI(); SPI_SS_1; } /*********写入多个字节***********/ void write_data_ye(uchar *p,u32 sendAddr,uchar tx) { uchar i; write_SR(); /*****发送第一个字节——指令1****/ SPI_SS_0; send_SPI(PP);//页写数据 send_SPI((sendAddr&0xFF0000)>>16);//地址1高 send_SPI((sendAddr&0xFF00)>>8);//地址2 send_SPI(sendAddr&0xFF);//地址3低 for(i=0;i<tx;i++) send_SPI(p[i]); SPI_SS_1; } /*********读取多个字节***********/ void read_datac(u32 readAddr,uchar rx) { uchar i; /*****发送第一个字节——指令1****/ SPI_SS_0; send_SPI(read_data);//读数据 send_SPI((readAddr & 0xFF0000) >> 16);//地址1高 send_SPI((readAddr & 0xFF00) >> 8);//地址2 send_SPI(readAddr & 0xFF);//地址3低 while(rx--) { atey[i]=read_SPI(); } SPI_SS_1;//结束,关闭模块 } /*********扇区擦除***********/ void Erase_datac(u32 readAddr) { /********允许写入命令*******/ write_SR(); SPI_SS_0; send_SPI(SE);//读数据 send_SPI((readAddr & 0xFF0000) >> 16);//地址1高 send_SPI((readAddr & 0xFF00) >> 8);//地址2 send_SPI(readAddr & 0xFF);//地址3低 waitwiriteEND(40); SPI_SS_1;//结束,关闭模块 } void SPI_FLASH_BulkErase(void) { /********允许写入命令*******/ write_SR(); SPI_SS_0; send_SPI(SE);//读数据 SPI_SS_1;//结束,关闭模块 waitwiriteEND(50); } void ccc() { uint i; SPI_SS_0; send_SPI(0x9f); waitwiriteEND(36); for(i=0;i<5;i++) { // atey[i]=read_SPI(); } SPI_SS_1; SPI_SS_0; send_SPI(0xAB); for(i=5;i<10;i++) { //atey[i]=read_SPI(); } waitwiriteEND(30); SPI_SS_1; }
/*******spi.h文件**********/ #ifndef __SPI_ml__ #define __SPI_ml__ #include "main.h" /*********状态寄存器16位*************/ #define writeb 0x50 //设置写使能为挥发性 #define busy 0x01 //忙检测 #define WEL 0x02 //写使能 :低位 #define BP_0 0x04 //内存保护CMP=0 SEC=0 TB=0最高110 #define SRP_1 0x80 //写保护直到下次断电才可改变写:低位 #define SRP_3 0x01 //写控制要WP=1 可写:高位 #define SUS 0x80 //擦除状态位 为1执行擦除:高位 #define LB_0 0x00 //安全解锁 :高位 /************第一个字节为指令 集1**************/ #define write_1 0x06 //写使能 #define write_0 0x04 //写禁止 #define read_SR_1 0x05 //读状态寄存器1 #define read_SR_2 0x35 //读状态寄存器2 #define write_SR_01 0x01 //写入状态寄存器(3个字节)S7——S0 S15—S8 #define PP 0x02 //页编程 A23-A0 D7_D0(第5个字节) #define QPP 0x32 //4页编程A23—A0 #define SE 0x20 //扇区擦除4kb #define BE0 0x52 //块擦除32ke #define BE1 0xd8 //块擦除64kb #define CE 0xc7 //芯片擦除 #define EPR 0x75 //编程暂停擦除 #define PD 0xb9 //编程恢复 #define read_rese 0xff //连续读取模式 /************第一个字节为指令 集2**************/ #define read_data 0x03 //读数据 地址A23——A0 数据D7_D0(字节5) #define fast_read 0x0b //快速读取 #define read_SR_12 0xeb //快速读取4 I / O EBH A23-A0,M7-M0( 4)(X,X,X,X,D7-D0,......)(5)(D7-D0,......) //7.最低地址位必须为0(A0 = 0)8.最低4位地址位必须为0(A0,A1,A2,A3 = 0) extern uchar atey[10]; void delay_spi(unsigned int x);//1us 11M void int_SPI(); void send_SPI(unsigned char data_send); unsigned char read_SPI(); void read_datac(u32 readAddr,uchar rx);//读取多个字节 void read_SR(); void write_SR(); void write_data_ye(uchar *p,u32 sendAddr,uchar tx);//写 页 void Erase_datac(u32 readAddr);//扇区擦除 void SPI_FLASH_BulkErase(void);//全部擦除 void Pinout(unsigned char pinx,unsigned char pin,unsigned char out); void ccc(); #endif
#ifndef __CONFIG_H__ #define __CONFIG_H__ //#include "msp430x54x.h" #include "msp430G2553.h" #ifndef TRUE #define TRUE 1 #endif #ifndef FALSE #define FALSE 0 #endif //#define Debug //调试软件 #define uchar unsigned char //无符号8位整型变量 #define uint unsigned int //无符号32位整型变量 #define u32 unsigned long //无符号32位整型变量 typedef signed char int8; //有符号8位整型变量 typedef unsigned short uint16; //无符号16位整型变量 typedef signed short int16; //有符号16位整型变量 typedef signed int int32; //有符号32位整型变量 typedef float fp32; //单精度浮点数(32位长度) typedef double fp64; //双精度浮点数 #endif
/*******主函数部分**********/ #include "main.h" #include "SPI_ml.h" #define SHP 0x001 #define I0_RB (P1IN&BIT7)//上 #define I0_RU (P1IN&BIT6) #define FLASH_WriteAddress 0x00000 void Pinout(unsigned char pinx,unsigned char pin,unsigned char out);//管脚使能函数 void IO_INTI(); //IO口初始化 void BasicClock_Init(); //时钟源初始化 void IO_INTI() { //P1全输入 P1DIR = 0xfd;//1111 1101 //配置输入 P1REN |=BIT1; P1OUT|=BIT1; } void BasicClock_Init() { //设置时钟源 if(CALBC1_1MHZ == 0xFF || CALDCO_1MHZ == 0xFF) { while(1); } /*********调整DC0频率************/ BCSCTL1 = CALBC1_1MHZ; //DCO ---1MHz DCOCTL = CALDCO_1MHZ; BCSCTL3 |= LFXT1S_0; IFG1 &= ~OFIFG; BCSCTL2 |= SELM_0 + DIVM_2+DIVA_1; //MCLK使用DC01MHZ,SMCLK采用125k时钟 } void main() { WDTCTL=WDTPW+WDTHOLD;//关闭看门狗 BasicClock_Init(); //时钟初始化 // Capture_GPIO_Init(); //定时器初始化 IO_INTI(); //管脚初始化 BTS int_SPI(); //Erase_datac(FLASH_WriteAddress);//擦除模块 // write_data_ye(b,FLASH_WriteAddress,10);//写页 //read_datac(FLASH_WriteAddress,10);//读取页 //ead_SR(); ccc(); while(1) { Pinout(1,0,1); } }