完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
我用STM32 当主机,FPGA为从机进行通信,每次发送16bit数据,目前stm32接收到的数据只有高8为准确,而低8位一直在变化,FPGA接收到的数据则不准确。spi时钟目前256分频,快一点数据就更加不对了。代码如下:
FPGA部分: entity FPGA_ARM is generic ( N : integer := 16); Port ( CLK : in STD_LOGIC; F_SCLK : in STD_LOGIC; F_SC : in STD_LOGIC; F_MOSI : in STD_LOGIC; F_MISO : out STD_LOGIC; Rcv_Flag : out STD_LOGIC; Send_Data : in STD_LOGIC_VECTOR(15 downto 0); Rcv_Data : out STD_LOGIC_VECTOR(15 downto 0)); end FPGA_ARM; architecture Behavioral of FPGA_ARM is signal cur_bit, prev_bit : integer range N-1 downto 0; begin process(F_SC,F_SCLK) begin if falling_edge(F_SCLK) then case Send_Data(cur_bit) is when '0' => F_MISO <= '0'; when '1' => F_MISO <= '1'; when others => NULL; end case; end if; end process; process(F_SCLK, F_SC) begin if F_SC = '1' then Rcv_Data <= (others => '0'); cur_bit <= N-1; else if rising_edge(F_SCLK) then case F_MOSI is when '0' => Rcv_Data(cur_bit) <= '0'; when '1' => Rcv_Data(cur_bit) <= '1'; when others => NULL; end case; Rcv_Data(cur_bit) <= F_MOSI; if cur_bit = 0 then cur_bit <= N - 1 else cur_bit <= cur_bit - 1; end if; end if; end if; end process; process(CLK, F_SC) variable td_raised : boolean; begin if F_SC = '1' then prev_bit <= N-1; td_raised := false; Rcv_Flag <= '1'; elsif F_SC ='0' then Rcv_Flag <= '0'; elsif falling_edge(CLK) then prev_bit <= cur_bit; if td_raised then --Rcv_Flag <= '0'; td_raised := false; elsif prev_bit = 0 and cur_bit = N-1 then --Rcv_Flag <= '1'; td_raised := true; end if; end if; end process; stm32部分: void SPI1_Configuration(void) { SPI_InitTypeDef SPI_InitStructure; SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI1, &SPI_InitStructure); /* Enable SPI1 */ SPI_Cmd(SPI1, ENABLE); } void SPI1_Send(uint16_t data) { SPI1_NSS_L; while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); SPI_I2S_SendData(SPI1, data); Delay(5); while((SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE) == RESET)); SPI_I2S_ClearFlag(SPI2, SPI_I2S_FLAG_RXNE); SPI1_NSS_H; } void main() { while(1) { SPI1_Send(0x4546); m = SPI_I2S_ReceiveData(SPI1); SPI1_RX_Buff[i] = m; if (i==1)i=0; else i++; Delay(20); } } |
|
相关推荐
3个回答
|
|
有做过的吗?请教一下
|
|
|
|
spi的速率一般要低于20Mhz,另外你要在接收高低八位时加一个标志位,否则接收高位后低位刚好更新,就导致数据接收错误。
|
|
|
|
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
1333 浏览 1 评论
助力AIoT应用:在米尔FPGA开发板上实现Tiny YOLO V4
1041 浏览 0 评论
2408 浏览 1 评论
2113 浏览 0 评论
矩阵4x4个按键,如何把识别结果按编号01-16(十进制)显示在两个七段数码管上?
2376 浏览 0 评论
1878 浏览 49 评论
6017 浏览 113 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-23 04:33 , Processed in 0.662633 second(s), Total 73, Slave 57 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号