用2片STM8S003K3
单片机进行SPI
通信实验:
发送程序没有问题,能够发送显示(LED表示),接收通过中断接收数据,但总是没有反应(LED显示)。
我检查程序、硬件连接,还是不知道哪里出错。。。有谁知道原因,麻烦帮助一下,谢谢啦。。。
发送程序:
- /* MAIN.C file
- *
- * Copyright (c) 2002-2005 STMicroelectronics
- */
- #include "stm8s.h"
- #include "STM8S003K3.h"
- #include "stm8s_bitsdefine.h"
- #include "stm8s_clk.h"
- //#include "delay.h"
- u8 Data[8],dataIndex=0;
- //void SPI_MasterInit(void);
- void Delay(unsigned int t);
- //void IO_Init(void);
- void main(void)
- {
- u8 i;
- //CLK_CKDIVR = 0x18; //频率设为2MHz
- //IO_Init();
- //SPI_MasterInit();
- for(i=0;i<8;i++)
- {
- Data=~(0x01<SR 0x02)==0); //判断发送缓器非空则等待
- SPI_SendData(Data[dataIndex++]);//将数据写入发送缓冲器
- Delay(50000);
- }
- }
- void Delay(unsigned int t)
- {
- while(t--);
- }
- # ifdef USE_FULL_ASSERT
- void assert_failed(u8 *file,u32 line)
- {
- while(1)
- {
- }
- }
- #endif
接收程序:
- /* MAIN.C file
- *
- * Copyright (c) 2002-2005 STMicroelectronics
- */
- #include "stm8s.h"
- #include "STM8S003K3.h"
- #include "stm8s_bitsdefine.h"
- #include "stm8s_clk.h"
- //#include "delay.h"
- u8 Data[8],dataIndex=0;
- //void SPI_MasterInit(void);
- void Delay(unsigned int t);
- //void IO_Init(void);
- void main(void)
- {
- //CLK_CKDIVR = 0x18; //频率设为2MHz
- //IO_Init();
- //SPI_MasterInit();
- GPIO_Init(GPIOD, GPIO_PIN_ALL, GPIO_MODE_OUT_PP_HIGH_SLOW);
- GPIO_Init(GPIOC, GPIO_PIN_5|GPIO_PIN_6, GPIO_MODE_IN_PU_NO_IT);//PC5 PC6 设置上拉输入
- GPIO_Init(GPIOC, GPIO_PIN_7, GPIO_MODE_OUT_PP_HIGH_FAST); //PC7 推挽输出
- /*
- SPI初始化:高位发送先
- 波特率为主时钟1/4
- 从机模式
- CPOL=0
- CPHA=0
- 全双工模式
- 软件管理NSS
- CRC校验多项式为0x07(注意不可为0x00)
- */
- SPI_Init(SPI_FIRSTBIT_MSB,
- SPI_BAUDRATEPRESCALER_4,
- SPI_MODE_SLAVE,
- SPI_CLOCKPOLARITY_LOW,
- SPI_CLOCKPHASE_1EDGE,
- SPI_DATADIRECtiON_2LINES_FULLDUPLEX,
- SPI_NSS_SOFT,
- 0x07);
- SPI_ITConfig(SPI_IT_RXNE, ENABLE);//开接收缓冲器非空中断
- SPI_Cmd(ENABLE); //使能SPI
- _asm("rim"); //开启总中断
- //rim();
- while(1)
- {
- }
- }
- # ifdef USE_FULL_ASSERT
- void assert_failed(u8 * file,u32 line)
- {
- while(1)
- {
- }
- }
- #endif
STM8s.it.C程序如下
- #include "stm8s_it.h"
- extern u8 Data[8],dataIndex;
- INTERRUPT_HANDLER(SPI_IRQHandler, 10)
- {
- /* In order to detect unexpected events during development,
- it is recommended to set a breakpoint on the following instruction.
- */
- if((SPI->SR 0x01)!=0) //判断接收器缓冲器非空标志位RXNE是否为1
- {
- if(dataIndex==8)
- dataIndex=0;
- Data[dataIndex]=SPI->DR; //读取接收到数据
- GPIOD->ODR=Data[dataIndex++]; //用LED显示数据
- }
- }