|
我用的是模拟的spi(因为硬件的我也调不通),通过连接后想把JecID读出后发给串口1中在电脑上显示,求大神帮忙看看为何读不出JecID。id都读不出来更何况读写操作flash,SST25VF016B的WP#,HOLD#都接了高电平。
/****************spi.c*************************************/
#include "spi.h"
void SPI_IO_Init(void) //引脚初始化
{
P2DIR |= BIT0+BIT1+BIT2;
P2DIR &=~ BIT3;
CS_1;
CLK_0;
delay_ms(10);
}
//写入一个字节
void Spi_WriteByte(uchar data)
{
uchar i;
CLK_0; //模式0
delay_us(1);
for(i=0;i<8;i++)
{
CLK_0;
if(0x80&data)
DIO_1;
else
DIO_0;
delay_us(5);
CLK_1;
delay_us(5);
data<<=1;
}
CLK_0;
delay_us(1);
}
//读取一个字节
uchar Spi_ReadByte(void)
{
uchar i,temp=0;
CLK_0; //模式0
delay_us(1);
for(i=0;i<8;i++)
{
temp<<=1;
CLK_1;
delay_us(1);
if(DO_IN) //读取最高位,保存至最末尾,通过左移位完成整个字节
temp |= 0x01;
CLK_0;
delay_us(1);
}
CLK_0;
delay_us(1);
return temp;
}
//芯片写使能
void Write_Enable(void)
{
CS_0;
delay_us(1);
Spi_WriteByte(WRITE_ENABLE);
CS_1;
}
//读/****************spi.c*************************************/
#include "spi.h"
void SPI_IO_Init(void) //引脚初始化
{
P2DIR |= BIT0+BIT1+BIT2;
P2DIR &=~ BIT3;
CS_1;
CLK_0;
delay_ms(10);
}
//写入一个字节
void Spi_WriteByte(uchar data)
{
uchar i;
CLK_0; //模式0
delay_us(1);
for(i=0;i<8;i++)
{
CLK_0;
if(0x80&data)
DIO_1;
else
DIO_0;
delay_us(5);
CLK_1;
delay_us(5);
data<<=1;
}
CLK_0;
delay_us(1);
}
//读取一个字节
uchar Spi_ReadByte(void)
{
uchar i,temp=0;
CLK_0; //模式0
delay_us(1);
for(i=0;i<8;i++)
{
temp<<=1;
CLK_1;
delay_us(1);
if(DO_IN) //读取最高位,保存至最末尾,通过左移位完成整个字节
temp |= 0x01;
CLK_0;
delay_us(1);
}
CLK_0;
delay_us(1);
return temp;
}
//芯片写使能
void Write_Enable(void)
{
CS_0;
delay_us(1);
Spi_WriteByte(WRITE_ENABLE);
CS_1;
}
unsigned char ReadJecID(void)
{
uchar byte;
CS_0; // enable the sst flash
delay_us(1);
Spi_WriteByte(JEDEC_ID); //
Spi_WriteByte(0xFF);
byte = Spi_ReadByte();
CS_1;
return byte;
}
//读/****************spi.c*************************************/
#include "spi.h"
void SPI_IO_Init(void) //引脚初始化
{
P2DIR |= BIT0+BIT1+BIT2;
P2DIR &=~ BIT3;
CS_1;
CLK_0;
delay_ms(10);
}
//写入一个字节
void Spi_WriteByte(uchar data)
{
uchar i;
CLK_0; //模式0
delay_us(1);
for(i=0;i<8;i++)
{
CLK_0;
if(0x80&data)
DIO_1;
else
DIO_0;
delay_us(5);
CLK_1;
delay_us(5);
data<<=1;
}
CLK_0;
delay_us(1);
}
//读取一个字节
uchar Spi_ReadByte(void)
{
uchar i,temp=0;
CLK_0; //模式0
delay_us(1);
for(i=0;i<8;i++)
{
temp<<=1;
CLK_1;
delay_us(1);
if(DO_IN) //读取最高位,保存至最末尾,通过左移位完成整个字节
temp |= 0x01;
CLK_0;
delay_us(1);
}
CLK_0;
delay_us(1);
return temp;
}
//芯片写使能
void Write_Enable(void)
{
CS_0;
delay_us(1);
Spi_WriteByte(WRITE_ENABLE);
CS_1;
}
unsigned char ReadJecID(void)
{
uchar byte;
CS_0; // enable the sst flash
delay_us(1);
Spi_WriteByte(JEDEC_ID); //
Spi_WriteByte(0xFF);
byte = Spi_ReadByte();
CS_1;
return byte;
}/****************spi.c*************************************/
#include "spi.h"
void SPI_IO_Init(void) //引脚初始化
{
P2DIR |= BIT0+BIT1+BIT2;
P2DIR &=~ BIT3;
CS_1;
CLK_0;
delay_ms(10);
}
//写入一个字节
void Spi_WriteByte(uchar data)
{
uchar i;
CLK_0; //模式0
delay_us(1);
for(i=0;i<8;i++)
{
CLK_0;
if(0x80&data)
DIO_1;
else
DIO_0;
delay_us(5);
CLK_1;
delay_us(5);
data<<=1;
}
CLK_0;
delay_us(1);
}
//读取一个字节
uchar Spi_ReadByte(void)
{
uchar i,temp=0;
CLK_0; //模式0
delay_us(1);
for(i=0;i<8;i++)
{
temp<<=1;
CLK_1;
delay_us(1);
if(DO_IN) //读取最高位,保存至最末尾,通过左移位完成整个字节
temp |= 0x01;
CLK_0;
delay_us(1);
}
CLK_0;
delay_us(1);
return temp;
}
//芯片写使能
void Write_Enable(void)
{
CS_0;
delay_us(1);
Spi_WriteByte(WRITE_ENABLE);
CS_1;
}
unsigned char ReadJecID(void)
{
uchar byte;
CS_0; // enable the sst flash
delay_us(1);
Spi_WriteByte(JEDEC_ID); //
Spi_WriteByte(0xFF);
byte = Spi_ReadByte();
CS_1;
return byte;
}/****************spi.c*************************************/
#include "spi.h"
void SPI_IO_Init(void) //引脚初始化
{
P2DIR |= BIT0+BIT1+BIT2;
P2DIR &=~ BIT3;
CS_1;
CLK_0;
delay_ms(10);
}
//写入一个字节
void Spi_WriteByte(uchar data)
{
uchar i;
CLK_0; //模式0
delay_us(1);
for(i=0;i<8;i++)
{
CLK_0;
if(0x80&data)
DIO_1;
else
DIO_0;
delay_us(5);
CLK_1;
delay_us(5);
data<<=1;
}
CLK_0;
delay_us(1);
}
//读取一个字节
uchar Spi_ReadByte(void)
{
uchar i,temp=0;
CLK_0; //模式0
delay_us(1);
for(i=0;i<8;i++)
{
temp<<=1;
CLK_1;
delay_us(1);
if(DO_IN) //读取最高位,保存至最末尾,通过左移位完成整个字节
temp |= 0x01;
CLK_0;
delay_us(1);
}
CLK_0;
delay_us(1);
return temp;
}
//芯片写使能
void Write_Enable(void)
{
CS_0;
delay_us(1);
Spi_WriteByte(WRITE_ENABLE);
CS_1;
}
unsigned char ReadJecID(void)
{
uchar byte;
CS_0; // enable the sst flash
delay_us(1);
Spi_WriteByte(JEDEC_ID); //
Spi_WriteByte(0xFF);
byte = Spi_ReadByte();
CS_1;
return byte;
}/****************spi.c*************************************/
#include "spi.h"
void SPI_IO_Init(void) //引脚初始化
{
P2DIR |= BIT0+BIT1+BIT2;
P2DIR &=~ BIT3;
CS_1;
CLK_0;
delay_ms(10);
}
//写入一个字节
void Spi_WriteByte(uchar data)
{
uchar i;
CLK_0; //模式0
delay_us(1);
for(i=0;i<8;i++)
{
CLK_0;
if(0x80&data)
DIO_1;
else
DIO_0;
delay_us(5);
CLK_1;
delay_us(5);
data<<=1;
}
CLK_0;
delay_us(1);
}
//读取一个字节
uchar Spi_ReadByte(void)
{
uchar i,temp=0;
CLK_0; //模式0
delay_us(1);
for(i=0;i<8;i++)
{
temp<<=1;
CLK_1;
delay_us(1);
if(DO_IN) //读取最高位,保存至最末尾,通过左移位完成整个字节
temp |= 0x01;
CLK_0;
delay_us(1);
}
CLK_0;
delay_us(1);
return temp;
}
//芯片写使能
void Write_Enable(void)
{
CS_0;
delay_us(1);
Spi_WriteByte(WRITE_ENABLE);
CS_1;
}
unsigned char ReadJecID(void)
{
uchar byte;
CS_0; // enable the sst flash
delay_us(1);
Spi_WriteByte(JEDEC_ID); //
Spi_WriteByte(0xFF);
byte = Spi_ReadByte();
CS_1;
return byte;
}
//读JecID
unsigned char ReadJecID(void)
{
uchar byte;
CS_0; // enable the sst flash
delay_us(1);
Spi_WriteByte(JEDEC_ID); //
Spi_WriteByte(0xFF);
byte = Spi_ReadByte();
CS_1;
return byte;
}
/*******************************************
spi.h
引脚说明:CS-P2.0; CLK-P2.1; DIO-P2.2; DO-P2.3
********************************************/
#ifndef _SPI_H
#define _SPI_H
#include <msp430x14x.h>
#define uchar unsigned char
#define uint unsigned int
#define CPU_F ((double)8000000)
#define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))
#define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))
#define CS_1 P2OUT|=BIT0
#define CS_0 P2OUT&=~BIT0
#define CLK_1 P2OUT|=BIT1
#define CLK_0 P2OUT&=~BIT1
#define DIO_1 P2OUT|=BIT2
#define DIO_0 P2OUT&=~BIT2
#define DO_IN P2IN&BIT3
命令指令表定义
#define WRITE_ENABLE 0X06
#define WRITE_DISABLE 0X04
#define READ_STATUSREG 0X05
#define WRITE_STATUSREG 0X01
#define READ_DATE 0X03
#define READ_FAST 0X0B
#define READ_DOUBLEFAST 0X3B
#define WRITE_PAGE 0X02
#define CLEAR_BLOCK 0XD8
#define CLEAR_PAGE 0X20
#define CLEAR_SHIP 0XC7
#define POWER_OFF 0XB9
#define POWER_ON 0XAB
#define SHIP_ID 0X90
#define JEDEC_ID 0X9F
void SPI_IO_Init(void);
void Spi_WriteByte(uchar data);
uchar Spi_ReadByte(void);
void Write_Enable(void);
unsigned char ReadJecID(void);
#endif
/******************************main.c***********************************/
#include <msp430x14x.h>
#include"spi.h"
void Clock_Init(void)
{
uchar i;
BCSCTL1 &= ~XT2OFF; // XT2= HF XTAL
do
{
IFG1 &= ~OFIFG; // Clear OSCFault flag
for (i = 0xFF; i > 0; i--); // Time for flag to set
}
while ((IFG1 & OFIFG)); // OSCFault flag still set?
BCSCTL2 |= SELM_2+SELS; // SMCLK,MCLK= XT2 (safe)
}
void uart1_init(void)
{
P3SEL |= 0xC0; // P3.6,7 = USART0 TXD/RXD
ME2 |= UTXE1 + URXE1; // Enable USART0 TXD/RXD
UCTL1 |= CHAR; // 8-bit character
UTCTL1 |= SSEL0; // UCLK = ACLK
UBR01 = 0x03; // 32k/9600 - 3.41
UBR11 = 0x00; //
UMCTL1 = 0x4A; // Modulation
UCTL1 &= ~SWRST; // Initialize USART state machine
IE2 |= URXIE1; // Enable USART0 RX interrupt
}
void uart_send( uchar *ucpData){
// _DINT();
while(*ucpData != '\0')
{
while(!(IFG2 & UTXIFG1));
TXBUF1 =*(ucpData++);
}
// _EINT();
}
void main(void)
{
uchar i;
WDTCTL = WDTPW + WDTHOLD;
SPI_IO_Init();
uart1_init();
Clock_Init();
while(1)
{
i=ReadJecID() ;
while(!(IFG2 & UTXIFG1));
TXBUF1 =i;
}
} |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|