[STM32F1] 用IO口模拟SPI的问题

[复制链接]
2050|8
 楼主| luciusis 发表于 2015-3-14 10:02 | 显示全部楼层 |阅读模式
硬件接线是PB6接MOSI,PB7接CS,PB8接CLK,实现与bc7277的通信,驱动数码管显示,不考虑按键为什么一直没有点亮
  1. #include "stm32f10x.h"
  2. void GPIO_Configuration(void);
  3. void SPI_Send_Byte(uint8_t);


  4. int main(void)
  5. {
  6.         GPIO_Configuration();
  7.         __nop();
  8.         GPIO_ResetBits(GPIOB, GPIO_Pin_7);//CS = 0;
  9.         __nop();
  10.         SPI_Send_Byte(0x1b);
  11.         __nop();
  12.         SPI_Send_Byte(0x11);
  13.         GPIO_SetBits(GPIOB, GPIO_Pin_7);//CS = 1;
  14.         __nop();
  15.         __nop();
  16.         __nop();
  17.         while(1);
  18. }




  19. void GPIO_Configuration()
  20. {
  21.        
  22.         GPIO_InitTypeDef GPIO_InitStructure;
  23.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  24.         /* MOSI-PB6,CS-PB7,CLK-PB8*/
  25.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8;   
  26.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  27.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  28.         GPIO_Init(GPIOB, &GPIO_InitStructure);
  29.         GPIO_SetBits(GPIOB, GPIO_Pin_7);         //CS=1
  30.         GPIO_SetBits(GPIOB, GPIO_Pin_8);         //CLK=1
  31. }


 楼主| luciusis 发表于 2015-3-14 10:04 | 显示全部楼层
  1. /*发送一个字节*/
  2. void SPI_Send_Byte(uint8_t byte)
  3. {
  4.         uint8_t i;
  5.   GPIO_SetBits(GPIOB, GPIO_Pin_8);  //CLK=1
  6.         __nop(); //CSÖÃÁãºóÑÓʱ>T/2  
  7.         for(i=0;i<8;i++)
  8.         {
  9.                
  10.                 if (byte&0x80)
  11.                         GPIO_SetBits(GPIOB, GPIO_Pin_6);  //mosi=1
  12.                 else
  13.                         GPIO_ResetBits(GPIOB, GPIO_Pin_6);  //mosi=0
  14.                 byte<<=1;
  15.                 __nop();
  16.                 GPIO_ResetBits(GPIOB, GPIO_Pin_8);//sclk = 0;
  17.                 __nop();
  18.                 __nop();
  19.                 GPIO_SetBits(GPIOB, GPIO_Pin_8);  //sclk=1
  20.                 __nop();
  21.                
  22.         }
  23.         __nop();
  24.         __nop();
  25.         __nop();//
  26.        
  27. }


chenyu988 发表于 2015-3-14 10:15 | 显示全部楼层
SPI极性,相位是否设置正确
 楼主| luciusis 发表于 2015-3-14 10:39 | 显示全部楼层
chenyu988 发表于 2015-3-14 10:15
SPI极性,相位是否设置正确

软件模拟的SPI就是根据BC7277的时序写发送函数就可以了吧?
 楼主| luciusis 发表于 2015-3-14 10:47 | 显示全部楼层
BC7277手册

bc7277.pdf

973.53 KB, 下载次数: 3

NE5532 发表于 2015-3-14 17:17 | 显示全部楼层
CS或者叫片选线怎么控制的?没有看到。
chenyu988 发表于 2015-3-14 17:23 | 显示全部楼层
CS低有效啊
mmuuss586 发表于 2015-3-14 19:57 | 显示全部楼层

好像没啥问题啊;
你实际看到的波形什么样子,用示波器看下;
jekey 发表于 2015-3-16 21:10 | 显示全部楼层
速度太高了,看bc7227最高才64K的通信速率。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

4

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部