同样的程序,在STM32上不能运行 在MSP430上能运行
两个代码是非常的相似,但是MSP430里读出 状态寄存器=0x02,STM32读出的数据会发生变化,不知道是什么原因。
下面分别贴代码,希望大家能指点指点!!!
MSP430下的程序,参考别人的
- #define MB85SR64_DLY 30
- #define MB85RS64_PORT_IN P2IN
- #define MB85RS64_PORT_OUT P2OUT
- #define MB85RS64_CS BIT0
- #define MB85RS64_WP BIT5
- #define MB85RS64_SCK BIT3
- #define MB85RS64_SI BIT2
- #define MB85RS64_SO BIT1
- #define SET_MB85RS64_CS MB85RS64_PORT_OUT|=MB85RS64_CS
- #define SET_MB85RS64_WP MB85RS64_PORT_OUT|=MB85RS64_WP
- #define SET_MB85RS64_SCK MB85RS64_PORT_OUT|=MB85RS64_SCK
- #define SET_MB85RS64_SI MB85RS64_PORT_OUT|=MB85RS64_SI
- #define SET_MB85RS64_SO MB85RS64_PORT_OUT|=MB85RS64_SO
- #define CLR_MB85RS64_CS MB85RS64_PORT_OUT&=~MB85RS64_CS
- #define CLR_MB85RS64_WP MB85RS64_PORT_OUT&=~MB85RS64_WP
- #define CLR_MB85RS64_SCK MB85RS64_PORT_OUT&=~MB85RS64_SCK
- #define CLR_MB85RS64_SI MB85RS64_PORT_OUT&=~MB85RS64_SI
- #define CLR_MB85RS64_SO MB85RS64_PORT_OUT&=~MB85RS64_SO
- #define Read_MB85RS64_SO (MB85RS64_PORT_IN&MB85RS64_SO)==MB85RS64_SO
- //寄存器相关
- #define MB85RS64_WREN 0x06//Set Write Enable Latch 0000 0110
- #define MB85RS64_WRDI 0x04//Reset Write Enable Latch 0000 0100B
- #define MB85RS64_RDSR 0x05//Read Status Register 0000 0101B
- #define MB85RS64_WRSR 0x01//Write Status Register 0000 0001B
- #define MB85RS64_READ 0x03//Read Memory Code 0000 0011B
- #define MB85RS64_WRITE 0x02//Write Memory Code 0000 0010B
- void Write_data(unsigned int address,unsigned char * data,unsigned int number)
- {
- unsigned int temp_address=0;
- unsigned char i=0;
- //Set Write Enable Latch
- CLR_MB85RS64_CS;
- Write(MB85RS64_WREN);
- SET_MB85RS64_CS;
- temp_address=address&0x1FFF;
- //Write Memory Code
- CLR_MB85RS64_CS;
- Write(MB85RS64_WRITE);
- Write((temp_address>>8)&0xFF);
- Write(temp_address&0xFF);
- for(i=0;i<number;i++)
</number;i++) - {
- Write(* data);
- data++;
- }
- SET_MB85RS64_CS;
- }
- void Read_data(unsigned int address,unsigned char * data,unsigned int number)
- {
- unsigned int temp_address=0;
- unsigned char i=0;
- temp_address=address&0x1FFF;
- //Read Memory Code
- CLR_MB85RS64_CS;
- Write(MB85RS64_READ);
- Write((temp_address>>8)&0xFF);
- Write(temp_address&0xFF);
- for(i=0;i<number;i++)
</number;i++) - {
- * data=Read();
- data++;
- }
- SET_MB85RS64_CS;
- }
- void Init_PORT(void)
- {
- P2OUT = 0x00; P2SEL &= 0xD0;
- P2SEL2 &= 0xD0;
- P2DIR |= BIT0 + BIT2 + BIT3 + BIT5; //
- P2DIR &= ~BIT1; //
- P2REN |= BIT1;
- }
- void main(void)
- {
- volatile unsigned int i;
- char uart_tx_buf[30];
- unsigned char rd_data;
- unsigned char rd;
- unsigned char rd_test = 0;
- WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
- Init_PORT();
- Init_uart0();
- sendStr("Init
");
- Init_PORT();
- while(1)
- {
- //Set Write Enable Latch
- CLR_MB85RS64_CS;
- Write(MB85RS64_WREN);
- SET_MB85RS64_CS;
- //Read
- CLR_MB85RS64_CS;
- Write(MB85RS64_RDSR);
- rd_data=Read();
- SET_MB85RS64_CS;
- sprintf(uart_tx_buf,"stat1==0x%02X
",rd_data);
- sendStr(uart_tx_buf);
- //Set Write Enable Latch
- CLR_MB85RS64_CS;
- Write(MB85RS64_WRDI);
- SET_MB85RS64_CS;
- //Read
- CLR_MB85RS64_CS;
- Write(MB85RS64_RDSR);
- rd_data=Read();
- SET_MB85RS64_CS;
- sprintf(uart_tx_buf,"stat1==0x%02X
",rd_data);
- sendStr(uart_tx_buf);
- DelayXms(1000);
- }
- }
[color=rgb(51, 102, 153) !important]复制代码
STM32下的程序:
- #define CLR_MB85RS64_CS GPIO_WriteBit(GPIOC, SPI_MB85RS64_CS, Bit_RESET)
- #define SET_MB85RS64_CS GPIO_WriteBit(GPIOC, SPI_MB85RS64_CS, Bit_SET)
[color=rgb(51, 102, 153) !important]复制代码
- void MySPI_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(SPI_GPIO_RCC, ENABLE);
- GPIO_InitStructure.GPIO_Pin = SPI_SCK | SPI_MOSI;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(SPI_PORT, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = SPI_MISO;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- //GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(SPI_PORT, &GPIO_InitStructure);
- GPIO_SetBits(SPI_PORT, SPI_SCK | SPI_MOSI);
- }
- unsigned char Read(void)
- {
- unsigned char i=0,data=0;
- GPIO_WriteBit(SPI_PORT, SPI_SCK, Bit_RESET);
- for(i=0;i<8;i++)
- {
- data<<=1;
- GPIO_WriteBit(SPI_PORT, SPI_SCK, Bit_SET);;
- DelayXus(MB85SR64_DLY);
- //if(Read_MB85RS64_SO)
- if(GPIO_ReadInputDataBit(SPI_PORT, SPI_MISO))
- {data|=1;}
- GPIO_WriteBit(SPI_PORT, SPI_SCK, Bit_RESET);
- DelayXus(MB85SR64_DLY);
- }
- return data;
- }
- void Write(unsigned char data)
- {
- unsigned char i=0;
- GPIO_WriteBit(SPI_PORT, SPI_SCK, Bit_RESET);
- for(i=0;i<8;i++)
- {
- if((data&0x80)==0x80)
- {GPIO_WriteBit(SPI_PORT, SPI_MOSI, Bit_SET);}
- else
- {GPIO_WriteBit(SPI_PORT, SPI_MOSI, Bit_RESET);}
- data<<=1;
- GPIO_WriteBit(SPI_PORT, SPI_SCK, Bit_SET);
- DelayXus(MB85SR64_DLY);
- GPIO_WriteBit(SPI_PORT, SPI_SCK, Bit_RESET);
- }
- }
- MySPI_GPIO_Init();
- MySPI_MB85RS64_CS_Init();
- MySPI_MB85RS64_CS_H;
- while(1)
- {
- //Set Write Enable Latch
- CLR_MB85RS64_CS;
- Write(MB85RS64_WREN);
- SET_MB85RS64_CS;
- //Read
- CLR_MB85RS64_CS;
- Write(MB85RS64_RDSR);
- rd_data=Read();
- SET_MB85RS64_CS;
- printf("stat1==0x%02X
",rd_data);
- //Set Write Enable Latch
- CLR_MB85RS64_CS;
- Write(MB85RS64_WRDI);
- SET_MB85RS64_CS;
- //Read
- CLR_MB85RS64_CS;
- Write(MB85RS64_RDSR);
- rd_data=Read();
- SET_MB85RS64_CS;
- printf("stat2==0x%02X
",rd_data);
- DelayXms(1000);
- }
[color=rgb(51, 102, 153) !important]复制代码
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
原来是这样啊 懂了 谢谢
一周热门 更多>