1
2
43
新手上路
#include <STC/STC8H.H> #include <intrins.h> #ifdef _VSCODE #define INTERRUPT(n) #define bit char #else #define INTERRUPT(n) interrupt n #endif typedef unsigned char u8; typedef unsigned int u16; #define SS P10 // SS管脚 #define LED P11 // 测试用灯 bit fg_spi_busy; // spi忙碌标识 char fg_spi_dma_idel; // dma spi空闲标识 u8 xdata buf[] = {0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80}; // 数据缓冲区 #define BUF_LENG sizeof(buf) / sizeof(u8); // 缓冲区字节数 void spi_init() { SPCTL = 0x50; // 使能SPI、设置为主机模式 SPSTAT = 0xc0; // 清理中断标志、写冲突标识 IE2 = 0X02; // 使能SPI中断 } void dma_spi_init() { DMA_SPI_CFG |= 0X80; // 使能DMA_SPI中断 DMA_SPI_CFG |= 0X40; // 允许DMS_SPI发送数据 DMA_SPI_CFG &= ~0X20; // 禁止DMS_SPI接收数据 DMA_SPI_CR |= 0X80; // 使能SPI_DMA功能 DMA_SPI_STA = 0; // 清理中断、数据覆盖、数据丢弃标识 DMA_SPI_AMT = BUF_LENG - 1; // 设置需要传输的字节数 DMA_SPI_TXA = buf; // 设置需要传输的数据的地址 DMA_SPI_CFG2 = 0; // SS脚控制 } // 启动DMA_SPI主机模式 void dma_spi_host_start() { DMA_SPI_CR |= 0X40; } // 启动DMA_SPI从机模式 void dma_spi_cclent_start() { DMA_SPI_CR |= 0X20; } void main_init() { P_SW2 |= 0x80; // 允许使用XFR P0M0 = 0x00; P0M1 = 0x00; P1M0 = 0x00; P1M1 = 0x00; P2M0 = 0x00; P2M1 = 0x00; P3M0 = 0x00; P3M1 = 0x00; P4M0 = 0x00; P4M1 = 0x00; P5M0 = 0x00; P5M1 = 0x00; LED = 1; SS = 1; fg_spi_busy = 0; fg_spi_dma_idel = 1; spi_init(); dma_spi_init(); EA = 1; } // 通过SPI发送1个字节数据 void spi_send(u8 c) { while (fg_spi_busy) ; fg_spi_busy = 1; SS = 0; // 拉低从机 SS管脚 SPDAT = c; // 发送测试数据 } // char i = 0; void main() { main_init(); while (1) { if (fg_spi_dma_idel) { LED = 1; // 熄灯 fg_spi_dma_idel = 0; // 设置为忙碌 dma_spi_host_start(); // 开始工作 SPDAT = 0; // 需要触发一次SPI//??? } } } // SPI中断服务 void spi_main() INTERRUPT(9) { SPSTAT = 0xc0; // 清清中断标志等 SS = 1; // 拉高SS fg_spi_busy = 0; // 不再忙碌 // LED = !LED; // 闪烁 } // DMA中断服务 这段代码需要拓展中断的辅助 void spi_dma_main(void) INTERRUPT(13) { if (DMA_SPI_STA & 0x04) // SPI_DMA 中断 { DMA_SPI_STA &= ~0x04; // 清除标识 fg_spi_dma_idel = 1; // 发送完毕,设置为空闲 LED = 0; // 亮灯 } // 每批次触发一次 } 复制代码
使用道具 举报 送花
3
2031
910
等待验证会员
86
5979
1万
超级版主
Debu*** 发表于 2025-7-26 13:41 设置模式,复位FIFO和指针,设置地址和长度,启动DMA就行了,为什么要赋值SPDAT 使用DMA操作SPI就不需要再 ...
// 启动DMA_SPI主机模式 void dma_spi_host_start() { DMA_SPI_TXA = buf; // 设置需要传输的数据的地址 DMA_SPI_AMT = BUF_LENG - 1; // 设置需要传输的字节数 DMA_SPI_CR |= 0x01; // 清理FIFO DMA_SPI_CR |= 0X40; // 启动 }复制代码
80
2983
6363
荣誉版主
无情的代码机器
erci*** 发表于 2025-7-26 15:27 SSIG 一般配1。 可以参考库函数DMA例程
本版积分规则 发表回复 回帖后跳转到最后一页
|手机版|小黑屋|深圳国芯人工智能有限公司 ( 粤ICP备2022108929号-2 )
GMT+8, 2025-8-24 08:17 , Processed in 0.123091 second(s), 77 queries .
Powered by Discuz! X3.5
© 2001-2025 Discuz! Team.