2736|4

552

帖子

3

TA的资源

纯净的硅(初级)

楼主
 

stm的SPI [复制链接]

STM32的SPI搞了2天都还没搞出来,一下程序是用STM32的SPI驱动串行DA,,TLV5608输出正弦波,问题是波形死活不出来。

  1. #include "spi.h"

  2. //spi1_NSS==>PA4
  3. //spi1_clk==>PA5
  4. //spi1_MISO==>PA6
  5. //spi1_MOSI==>PA7

  6. //LDAC=======>PA0
  7. //MODE=======>PA1
  8. //FS=======>PA3

  9. unsigned short sin[550]={ //??????
  10. 0x1FF,0x205,0x20B,0x211,0x216,0x21C,0x222,0x228,0x22E,0x233,0x239,0x23F,0x245,0x24B,0x250,0x256
  11. ,0x25C,0x262,0x267,0x26D,0x273,0x279,0x27E,0x284,0x289,0x28F,0x295,0x29A,0x2A0,0x2A5,0x2AB,0x2B0
  12. ,0x2B6,0x2BB,0x2C1,0x2C6,0x2CB,0x2D1,0x2D6,0x2DB,0x2E1,0x2E6,0x2EB,0x2F0,0x2F5,0x2FB,0x300,0x305
  13. ,0x30A,0x30F,0x314,0x318,0x31D,0x322,0x327,0x32C,0x330,0x335,0x33A,0x33E,0x343,0x347,0x34C,0x350
  14. ,0x355,0x359,0x35D,0x361,0x366,0x36A,0x36E,0x372,0x376,0x37A,0x37E,0x382,0x385,0x389,0x38D,0x390
  15. ,0x394,0x398,0x39B,0x39F,0x3A2,0x3A5,0x3A8,0x3AC,0x3AF,0x3B2,0x3B5,0x3B8,0x3BB,0x3BE,0x3C1,0x3C3
  16. ,0x3C6,0x3C9,0x3CB,0x3CE,0x3D0,0x3D3,0x3D5,0x3D7,0x3D9,0x3DC,0x3DE,0x3E0,0x3E2,0x3E4,0x3E5,0x3E7
  17. ,0x3E9,0x3EB,0x3EC,0x3EE,0x3EF,0x3F1,0x3F2,0x3F3,0x3F4,0x3F5,0x3F7,0x3F7,0x3F8,0x3F9,0x3FA,0x3FB
  18. ,0x3FB,0x3FC,0x3FD,0x3FD,0x3FD,0x3FE,0x3FE,0x3FE,0x3FE,0x3FE,0x3FE,0x3FE,0x3FE,0x3FE,0x3FE,0x3FD
  19. ,0x3FD,0x3FD,0x3FC,0x3FB,0x3FB,0x3FA,0x3F9,0x3F8,0x3F7,0x3F7,0x3F5,0x3F4,0x3F3,0x3F2,0x3F1,0x3EF
  20. ,0x3EE,0x3EC,0x3EB,0x3E9,0x3E7,0x3E5,0x3E4,0x3E2,0x3E0,0x3DE,0x3DC,0x3D9,0x3D7,0x3D5,0x3D3,0x3D0
  21. ,0x3CE,0x3CB,0x3C9,0x3C6,0x3C3,0x3C1,0x3BE,0x3BB,0x3B8,0x3B5,0x3B2,0x3AF,0x3AC,0x3A9,0x3A5,0x3A2
  22. ,0x39F,0x39B,0x398,0x394,0x390,0x38D,0x389,0x385,0x382,0x37E,0x37A,0x376,0x372,0x36E,0x36A,0x366
  23. ,0x361,0x35D,0x359,0x355,0x350,0x34C,0x347,0x343,0x33E,0x33A,0x335,0x330,0x32C,0x327,0x322,0x31D
  24. ,0x318,0x314,0x30F,0x30A,0x305,0x300,0x2FB,0x2F5,0x2F0,0x2EB,0x2E6,0x2E1,0x2DB,0x2D6,0x2D1,0x2CB
  25. ,0x2C6,0x2C1,0x2BB,0x2B6,0x2B0,0x2AB,0x2A5,0x2A0,0x29A,0x295,0x28F,0x289,0x284,0x27E,0x279,0x273
  26. ,0x26D,0x267,0x262,0x25C,0x256,0x250,0x24B,0x245,0x23F,0x239,0x233,0x22E,0x228,0x222,0x21C,0x216
  27. ,0x211,0x20B,0x205,0x1FF,0x1F9,0x1F3,0x1ED,0x1E8,0x1E2,0x1DC,0x1D6,0x1D0,0x1CA,0x1C5,0x1BF,0x1B9
  28. ,0x1B3,0x1AE,0x1A8,0x1A2,0x19C,0x197,0x191,0x18B,0x185,0x180,0x17A,0x175,0x16F,0x169,0x164,0x15E
  29. ,0x159,0x153,0x14E,0x148,0x143,0x13D,0x138,0x133,0x12D,0x128,0x123,0x11D,0x118,0x113,0x10E,0x109
  30. ,0x103,0x0FE,0x0F9,0x0F4,0x0EF,0x0EA,0x0E6,0x0E1,0x0DC,0x0D7,0x0D2,0x0CE,0x0C9,0x0C4,0x0C0,0x0BB
  31. ,0x0B7,0x0B2,0x0AE,0x0A9,0x0A5,0x0A1,0x09D,0x098,0x094,0x090,0x08C,0x088,0x084,0x080,0x07C,0x079
  32. ,0x075,0x071,0x06E,0x06A,0x066,0x063,0x05F,0x05C,0x059,0x055,0x052,0x04F,0x04C,0x049,0x046,0x043
  33. ,0x040,0x03D,0x03B,0x038,0x035,0x033,0x030,0x02E,0x02B,0x029,0x027,0x024,0x022,0x020,0x01E,0x01C
  34. ,0x01A,0x019,0x017,0x015,0x013,0x012,0x010,0x00F,0x00D,0x00C,0x00B,0x00A,0x009,0x007,0x007,0x006
  35. ,0x005,0x004,0x003,0x003,0x002,0x001,0x001,0x001,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000
  36. ,0x000,0x000,0x001,0x001,0x001,0x002,0x003,0x003,0x004,0x005,0x006,0x007,0x007,0x009,0x00A,0x00B
  37. ,0x00C,0x00D,0x00F,0x010,0x012,0x013,0x015,0x017,0x019,0x01A,0x01C,0x01E,0x020,0x022,0x025,0x027
  38. ,0x029,0x02B,0x02E,0x030,0x033,0x035,0x038,0x03B,0x03D,0x040,0x043,0x046,0x049,0x04C,0x04F,0x052
  39. ,0x056,0x059,0x05C,0x05F,0x063,0x066,0x06A,0x06E,0x071,0x075,0x079,0x07C,0x080,0x084,0x088,0x08C
  40. ,0x090,0x094,0x098,0x09D,0x0A1,0x0A5,0x0A9,0x0AE,0x0B2,0x0B7,0x0BB,0x0C0,0x0C4,0x0C9,0x0CE,0x0D2
  41. ,0x0D7,0x0DC,0x0E1,0x0E6,0x0EA,0x0EF,0x0F4,0x0F9,0x0FE,0x103,0x109,0x10E,0x113,0x118,0x11D,0x123
  42. ,0x128,0x12D,0x133,0x138,0x13D,0x143,0x148,0x14E,0x153,0x159,0x15E,0x164,0x169,0x16F,0x175,0x17A
  43. ,0x180,0x185,0x18B,0x191,0x197,0x19C,0x1A2,0x1A8,0x1AE,0x1B3,0x1B9,0x1BF,0x1C5,0x1CB,0x1D0,0x1D6
  44. ,0x1DC,0x1E2,0x1E8,0x1ED,0x1F3,0x1F9
  45. };

  46. void spi_init(void)
  47. {
  48.         RCC->APB2ENR |=(1<<2);//PORTA时钟使能
  49.         RCC->APB2ENR |=(1<<12);//SPI1时钟使能       
  50.        
  51.         GPIOA->CRL &=0x000fffff;
  52.         GPIOA->CRL |=0xbbb00000;//PA5,6,7设置为复用,
  53.         GPIOA->ODR |=(0x07<<5); //PA5,6,7上拉,
  54.        
  55.         SPI1->CR1 &=~(1<<10);//全双工模式
  56.         SPI1->CR1 |=(1<<9);//软件管理nss
  57.         SPI1->CR1 |=(1<<8);
  58.        
  59.         SPI1->CR1 |=(1<<2);//SPI设置为主机
  60.         SPI1->CR1 |=(1<<11);//16位数据格式
  61. //        SPI1->CR1 &=~(1<<11);//8位数据格式
  62.         SPI1->CR1 &=~(1<<1);//空闲时sclk为0,
  63.         SPI1->CR1 |=(1<<0);//数据采样从第二个时钟边沿开始
  64.         SPI1->CR1 |=(7<<3);        //Fsck=Fcpu/256
  65.         SPI1->CR1 &=~(1<<7);//高位在前
  66.         SPI1->CR1 |=(1<<6);        //开启SPI
  67. }

  68. void spi_write(unsigned short Txdata)
  69. {
  70. //        delay_ms(50);
  71.         SPI1->DR=Txdata;//传输开始
  72.         while(!(SPI1->SR&(1<<1)));
  73. //        GPIOA->ODR |=(1<<2);
  74. //        delay_us(10);
  75. }

  76. unsigned short tlv5608_deal(unsigned short dat,unsigned short chnnel)
  77. {
  78.         unsigned short dat_ch;
  79.         if(chnnel==9)
  80.         {
  81.                 dat=(dat&0x3ff);
  82.         }
  83.         else
  84.         {
  85.                 dat=((dat&0x3ff)<<2);
  86.         }
  87.         chnnel=((chnnel&0x0f)<<12);
  88.         dat_ch=dat|chnnel;
  89.         return dat_ch;
  90. }
复制代码

  1. //#include "stm32f10x_lib.h"
  2. #include "stm32f10x.h"
  3. #include "sys.h"
  4. #include "timer.h"
  5. #include "gpio.h"
  6. #include "spi.h"


  7. //LDAC=======>PA0
  8. //MODE=======>PA1
  9. //FS=======>PA3


  10. //www.doflye.net       
  11. /*------------------------------------------------------------
  12.                            主函数
  13. ------------------------------------------------------------*/
  14. int main()
  15. {
  16.    
  17.   unsigned int i=0;
  18.         unsigned int ff;
  19.         Stm32_Clock_Init();//系统时钟设置,,设置为72M
  20.         RCC->APB2ENR |= 0x00000001; //开启afio时钟
  21.   //AFIO->MAPR = (0x00FFFFFF & AFIO->MAPR)|0x04000000;          //关闭JTAG
  22.                                

  23.         RCC->APB2ENR|=0X0000001c;//先使能外设IO PORTa,b,c时钟
  24. //到此为止,时钟设置完成        

  25.         gpio_init();
  26.         spi_init();
  27.         GPIOA->ODR |=(1<<1);        //
  28. //        timer_init(5000,7199);//500ms中断一次,定时器的计数频率为72M/7199,计数次数为5000;
  29.          while (1)
  30.     {       
  31.         GPIOA->ODR |=(1<<2);
  32.         GPIOA->ODR &=~(1<<3);        //开片选       
  33. //        delay_us(10);
  34. spi_write(tlv5608_deal(sin[i],0));                       
  35.         i++;
  36.         if(i>549)
  37.         {
  38.                 i=0;
  39.         }
  40.         GPIOA->ODR |=(1<<3);        //关片选
  41.         GPIOA->ODR &=~(1<<0);//更新数据
  42.         }
  43. }
复制代码



此帖出自stm32/stm8论坛

最新回复

找问题要一块一块地找。 首先把SPI调出来。可以拿示波器、逻辑分析仪来看。 再看看控制信号的正确性,可以读一下DOUT引脚的数据验证。 当以上通过了,再看看TLV5608的外围有没有问题。 处理一个问题,要把它分成一块一块地解决  详情 回复 发表于 2014-12-5 20:44
点赞 关注
 

回复
举报

4996

帖子

19

TA的资源

裸片初长成(初级)

沙发
 
spi并难啊,不过要先保证SPI没有问题,在调试其他芯片。。。。
此帖出自stm32/stm8论坛

点评

主机能不能不读从机的数据呢?就是像我这样: [*]void spi_write(unsigned short Txdata) [*]{ [*]// delay_ms(50); [*] SPI1->DR=Txdata;//传输开始 [*] while(!(SPI1->SR&(1  详情 回复 发表于 2014-12-9 10:54
 
个人签名我的博客
 

回复

1万

帖子

25

TA的资源

裸片初长成(高级)

板凳
 
找问题要一块一块地找。

首先把SPI调出来。可以拿示波器、逻辑分析仪来看。
再看看控制信号的正确性,可以读一下DOUT引脚的数据验证。

当以上通过了,再看看TLV5608的外围有没有问题。

处理一个问题,要把它分成一块一块地解决
此帖出自stm32/stm8论坛

点评

主机能不能不读从机的数据呢?就是像我这样: [*]void spi_write(unsigned short Txdata) [*]{ [*]// delay_ms(50); [*] SPI1->DR=Txdata;//传输开始 [*] while(!(SPI1->SR&(1  详情 回复 发表于 2014-12-9 10:55
 
 

回复

552

帖子

3

TA的资源

纯净的硅(初级)

4
 
zhaojun_xf 发表于 2014-12-5 20:06
spi并难啊,不过要先保证SPI没有问题,在调试其他芯片。。。。
主机能不能不读从机的数据呢?就是像我这样:
  • void spi_write(unsigned short Txdata)
  • {
  • //        delay_ms(50);
  •         SPI1->DR=Txdata;//传输开始
  •         while(!(SPI1->SR&(1<<1)));
  • //        GPIOA->ODR |=(1<<2);
  • //        delay_us(10);
  • }

此帖出自stm32/stm8论坛
 
 
 

回复

552

帖子

3

TA的资源

纯净的硅(初级)

5
 
dontium 发表于 2014-12-5 20:44
找问题要一块一块地找。

首先把SPI调出来。可以拿示波器、逻辑分析仪来看。
再看看控制信号的正确性,可以读一下DOUT引脚的数据验证。

当以上通过了,再看看TLV5608的外围有没有问题。

处理一个问题,要把它分成一块一块地解决
主机能不能不读从机的数据呢?就是像我这样:
  • void spi_write(unsigned short Txdata)
  • {
  • //        delay_ms(50);
  •         SPI1->DR=Txdata;//传输开始
  •         while(!(SPI1->SR&(1<<1)));//等待发送完成
  • //        GPIOA->ODR |=(1<<2);
  • //        delay_us(10);
  • }

此帖出自stm32/stm8论坛
 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/8 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表