STM8S 使用SPI方式与LoRa通讯卡死

开发环境:IAR for STM8

芯片:STM8S105K4

内容:因为之前用STM8S105K4开发过CC1101RF模块用的是io模拟spi。因此开发LoRa也用了io模拟spi结果LoRa初始化没问题,但是一发包/收包就会死。

io模拟spi如下

void LORA_delay(unsigned int n)
{
    unsigned int m=0;
    
    for(m=0; m<n; m++);
    for(m=0; m<n; m++);
}

unsigned char LORA_MISO_IN_FUN(void)
{
    unsigned int i = 10000;
    while(i--)
    {
        if(!LORA_MISO_IN())
        return 0;
    }
    return 1;
}

unsigned char  LORA_ReadWriteByte(unsigned char value)
{
    unsigned char temp=0x00, count=0x00;
    LORA_SCLK_LOW();//将时钟线拉低
    LORA_delay(1);
    for(count=0x80; count>0; count>>=1) //数据从高到低写出和读入
    {
        if(value & count)
        {
            LORA_MOSI_HIGH();
        }
        else
        {
            LORA_MOSI_LOW();            
        }
        LORA_delay(1);
        LORA_SCLK_HIGH();//将时钟线置高,数据写入模块
        LORA_delay(1);
        if(LORA_MISO_IN())
        {
            temp |= count;//从模块读出数据
        }
        LORA_delay(1);
        LORA_SCLK_LOW();//将时钟线拉低
        LORA_delay(1);
    }
    return temp;
}

void SX1276Write( uint8_t addr, uint8_t data )
{
  addr=addr|0x80;
  LORA_CSN_LOW();                           //片选致能
  while(LORA_MISO_IN());
    //if(LORA_MISO_IN_FUN())
    //return;
  LORA_ReadWriteByte(addr);                 //命令写入
  LORA_ReadWriteByte(data);                //写数据
  LORA_CSN_HIGH();                          //片选禁能
}


void SX1276Read( uint8_t addr, uint8_t *data )
{
        addr=addr&0x7F;
        LORA_CSN_LOW();               //片选致能
    while(LORA_MISO_IN());
    //if(LORA_MISO_IN_FUN())
    //return;
    LORA_ReadWriteByte(addr);    //地址
    data[0] = LORA_ReadWriteByte(0x00);//读出数据
    LORA_CSN_HIGH();                        //片选禁能
}

void SX1276WriteBuffer( uint8_t addr, uint8_t *buffer, uint8_t size )
{
    uint8_t count = 0x0;
        addr=addr|0x80;
        LORA_CSN_LOW();   //片选致能
    while(LORA_MISO_IN());
    //if(LORA_MISO_IN_FUN())
    //return;
    LORA_ReadWriteByte(addr); //地址 + 连续写命令
        for (count=0; count<size; count++)
    {
        LORA_ReadWriteByte(buffer[count]);   //写入数据
    }
    LORA_CSN_HIGH();  //片选禁能
}

void SX1276ReadBuffer( uint8_t addr, uint8_t *buffer, uint8_t size )
{
    unsigned char count = 0x0 ;
        addr=addr&0x7F;
        LORA_CSN_LOW();               //片选致能
    while(LORA_MISO_IN());
    //if(LORA_MISO_IN_FUN())
    //return;
    LORA_ReadWriteByte(addr);    //地址
        for (count = 0; count < size; count++)
    {
        buffer[count] = LORA_ReadWriteByte(0x00);//读出数据
    }
    LORA_CSN_HIGH();                        //片选禁能
}

总结:

最终还是没找到原因,不过我用硬件spi调试LoRa一切正常。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值