打印
[STM32F1]

用IO口模拟SPI的问题

[复制链接]
1843|8
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
luciusis|  楼主 | 2015-3-14 10:02 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
硬件接线是PB6接MOSI,PB7接CS,PB8接CLK,实现与bc7277的通信,驱动数码管显示,不考虑按键为什么一直没有点亮
#include "stm32f10x.h"
void GPIO_Configuration(void);
void SPI_Send_Byte(uint8_t);


int main(void)
{
        GPIO_Configuration();
        __nop();
        GPIO_ResetBits(GPIOB, GPIO_Pin_7);//CS = 0;
        __nop();
        SPI_Send_Byte(0x1b);
        __nop();
        SPI_Send_Byte(0x11);
        GPIO_SetBits(GPIOB, GPIO_Pin_7);//CS = 1;
        __nop();
        __nop();
        __nop();
        while(1);
}




void GPIO_Configuration()
{
       
        GPIO_InitTypeDef GPIO_InitStructure;
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
        /* MOSI-PB6,CS-PB7,CLK-PB8*/
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8;   
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOB, &GPIO_InitStructure);
        GPIO_SetBits(GPIOB, GPIO_Pin_7);         //CS=1
        GPIO_SetBits(GPIOB, GPIO_Pin_8);         //CLK=1
}


沙发
luciusis|  楼主 | 2015-3-14 10:04 | 只看该作者
/*发送一个字节*/
void SPI_Send_Byte(uint8_t byte)
{
        uint8_t i;
  GPIO_SetBits(GPIOB, GPIO_Pin_8);  //CLK=1
        __nop(); //CSÖÃÁãºóÑÓʱ>T/2  
        for(i=0;i<8;i++)
        {
               
                if (byte&0x80)
                        GPIO_SetBits(GPIOB, GPIO_Pin_6);  //mosi=1
                else
                        GPIO_ResetBits(GPIOB, GPIO_Pin_6);  //mosi=0
                byte<<=1;
                __nop();
                GPIO_ResetBits(GPIOB, GPIO_Pin_8);//sclk = 0;
                __nop();
                __nop();
                GPIO_SetBits(GPIOB, GPIO_Pin_8);  //sclk=1
                __nop();
               
        }
        __nop();
        __nop();
        __nop();//
       
}


使用特权

评论回复
板凳
chenyu988| | 2015-3-14 10:15 | 只看该作者
SPI极性,相位是否设置正确

使用特权

评论回复
地板
luciusis|  楼主 | 2015-3-14 10:39 | 只看该作者
chenyu988 发表于 2015-3-14 10:15
SPI极性,相位是否设置正确

软件模拟的SPI就是根据BC7277的时序写发送函数就可以了吧?

使用特权

评论回复
5
luciusis|  楼主 | 2015-3-14 10:47 | 只看该作者
BC7277手册

bc7277.pdf

973.53 KB

使用特权

评论回复
6
NE5532| | 2015-3-14 17:17 | 只看该作者
CS或者叫片选线怎么控制的?没有看到。

使用特权

评论回复
7
chenyu988| | 2015-3-14 17:23 | 只看该作者
CS低有效啊

使用特权

评论回复
8
mmuuss586| | 2015-3-14 19:57 | 只看该作者

好像没啥问题啊;
你实际看到的波形什么样子,用示波器看下;

使用特权

评论回复
9
jekey| | 2015-3-16 21:10 | 只看该作者
速度太高了,看bc7227最高才64K的通信速率。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

4

帖子

0

粉丝