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一切正常。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
下面是SX1276与STM32使用SPI通信的初始化代码示例: ``` #include <SPI.h> #include <LoRa.h> #define NSS_PIN PA4 #define NRESET_PIN PB0 #define DIO0_PIN PB1 void setup() { Serial.begin(9600); while (!Serial); // Initialize NSS, NRESET, and DIO0 pins pinMode(NSS_PIN, OUTPUT); pinMode(NRESET_PIN, OUTPUT); pinMode(DIO0_PIN, INPUT); // Reset the SX1276 module digitalWrite(NRESET_PIN, LOW); delay(10); digitalWrite(NRESET_PIN, HIGH); delay(10); // Initialize SPI communication SPI.begin(); SPI.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE0)); // Set the SPI clock frequency to 10MHz // Initialize LoRa module LoRa.setPins(NSS_PIN, NRESET_PIN, DIO0_PIN); if (!LoRa.begin(433E6)) { Serial.println("Starting LoRa failed!"); while (1); } } void loop() { Serial.print("Sending packet: "); Serial.println(counter); // send packet LoRa.beginPacket(); LoRa.print("hello "); LoRa.print(counter); LoRa.endPacket(); counter++; delay(5000); } ``` 需要注意的是,在STM32中需要在程序中指定NSS、NRESET和DIO0引脚的映射。在这里,我们使用PA4作为NSS引脚,PB0作为NRESET引脚,PB1作为DIO0引脚。在初始化时,我们需要将这些引脚设置为输入或输出,并设置NRESET引脚为低电平,然后再将其设置为高电平以重置SX1276模块。在SPI通信方面,我们需要在程序中指定SPI时钟频率和数据传输模式。在这里,我们将时钟频率设置为10MHz,数据传输模式设置为SPI_MODE0。最后,我们初始化LoRa模块,设置NSS、NRESET和DIO0引脚,并在433MHz频率下开始通信。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值