找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1129|回复: 0
打印 上一主题 下一主题
收起左侧

我想实现STM32F4驱动AD7705的功能,代码如下,编译没有错误,求大佬帮忙看看

[复制链接]
跳转到指定楼层
楼主
ID:797057 发表于 2020-7-13 12:42 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
100黑币
本帖最后由 有没有鱼干 于 2020-7-13 16:50 编辑

LCD显示出来的一直是最大值65535。。。。应该是数据异常。。。。。。debug的时候SPI里面数据寄存器的数值好像不太正常。。。。求大神帮帮忙。。谢谢啦!
单片机源程序如下:
  1. #include "stm32f4xx.h"
  2. #include "usart.h"
  3. #include "lcd.h"
  4. #include "delay.h"
  5. #include "led.h"
  6. #include "stdio.h"


  7. void RCC_Configuration(void);
  8. void GPIO_Configuration(void);
  9. u16 SPIx_ReadWriteByte(u16 TxData);

  10. void SPIx_Init(void);
  11. u16 ReadTM7705(void);
  12. uint8_t TM7705_Recive8Bit(void);
  13. void TM7705_Send8Bit(uint8_t _data);


  14. void Delay(vu32 nCount)
  15. {
  16.        for(; nCount != 0; nCount--);
  17. }


  18. void GPIO_Configuration(void)
  19. {
  20.         
  21.         GPIO_InitTypeDef GPIO_InitStructure;
  22.         /* Configure USART1 Rx (PB.8) as input floating  busy busy DRDY */
  23.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  24.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  25.               GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP ;
  26.                     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  27.         GPIO_Init(GPIOA, &GPIO_InitStructure);
  28. }

  29. /********ÅäÖÃÏμí3ê±Öó,ê1Äü¸÷íaéèê±Öó***********/
  30. void RCC_Configuration(void)
  31. {
  32.   SystemInit();        
  33.   RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOA, ENABLE );
  34.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1| RCC_APB2Periph_USART1, ENABLE);
  35. }

  36. /********TM77053õê¼»ˉoˉêy***********/
  37. void TM7705_Init(void)
  38. {         
  39.         unsigned char i ;
  40.         for(i = 0; i < 100; i++)
  41.     {
  42.         SPIx_ReadWriteByte(0xFF);                          //3ÖDøDIN¸ßμçƽD′2ù×÷£¬»Ö¸′AD7705½ó¿ú
  43.     }
  44.         SPIx_ReadWriteByte(0x20) ;                         //í¨μà1 ,ÏÂò»¸öD′ê±Öó¼Ä′æÆ÷
  45.         Delay(800);
  46.         SPIx_ReadWriteByte(0x02) ;                         //D′ê±Öó¼Ä′æÆ÷éèÖøüDÂËùÂêÎa200Hz
  47.         Delay(800);
  48.         SPIx_ReadWriteByte(0x10) ;                         //í¨μà1 ,ÏÂò»¸öD′éèÖüÄ′æÆ÷
  49.         Delay(800);
  50.         SPIx_ReadWriteByte(0x44) ;                         //D′éèÖüÄ′æÆ÷ ,éèÖÃ3éË«¼«DÔ¡¢ÎT»o3å¡¢ÔöòæÎa0¡¢ÂË2¨Æ÷1¤×÷¡¢×ÔD£×¼
  51.         Delay(8000);
  52. }

  53. /********¶á16λêy¾Y************/
  54. u16 ReadTM7705_16BitValue(void)
  55. {
  56.         //unsigned long
  57.                                 u16        DataL = 0;
  58.        // unsigned long
  59.                                 u16        DataH = 0;
  60.         //unsigned long
  61.                                 u16        Ret = 0;

  62.         DataH =SPIx_ReadWriteByte(0xff);
  63.               delay_ms(10);
  64.                DataH = DataH << 8;

  65.               DataL = SPIx_ReadWriteByte(0xff);
  66.         
  67.               delay_ms(10);
  68.         Ret = DataH | DataL;

  69.         return(Ret) ;                             
  70. }








  71. /********¶áè¡AD7705μúò»í¨μàêy¾Y************/
  72. u16 ReadTM7705(void)
  73. {
  74.         
  75.          // unsigned long
  76.                 u16        Ret = 0;
  77.           // GPIO_Configuration();
  78.                //  SPIx_Init();
  79.                 // TM7705_Init();
  80.               SPIx_ReadWriteByte(0x38) ;//éèÖöáμ±Ç°í¨μàêy¾Y
  81.   while(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_10==1))
  82.                                 {
  83.                                         ;
  84.                                 }            
  85.         Ret = ReadTM7705_16BitValue();
  86.         delay_ms(100);

  87.         return Ret;
  88. }


  89. /****************************SPI3õê¼»ˉ*************************/

  90. //SPI¿ú3õê¼»ˉ
  91. //ÕaàïÕëêǶÔSPI1μÄ3õê¼»ˉ
  92. void SPIx_Init(void)
  93.         {         
  94.         SPI_InitTypeDef SPI_InitStructure;
  95.         GPIO_InitTypeDef GPIO_InitStructure;
  96.                                  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOA, ENABLE);//
  97.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
  98.                                        
  99.                           GPIO_PinAFConfig(GPIOB,GPIO_PinSource3,GPIO_AF_SPI1);
  100.               GPIO_PinAFConfig(GPIOB,GPIO_PinSource4,GPIO_AF_SPI1);
  101.               GPIO_PinAFConfig(GPIOB,GPIO_PinSource5,GPIO_AF_SPI1);

  102.         /* Configure SPI1 pins: SCK, MISO and MOSI */
  103.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
  104.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;  //¸′óÃíÆíìêä3ö
  105.                                 GPIO_InitStructure.GPIO_OType=GPIO_OType_PP ;
  106.                                 //GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP ;
  107.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  108.         GPIO_Init(GPIOB, &GPIO_InitStructure);

  109.         /* Configure I/O for Flash Chip select */
  110.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;  //SPI CS
  111.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;  //¸′óÃíÆíìêä3ö
  112.                           GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP ;
  113.                           GPIO_InitStructure.GPIO_OType=GPIO_OType_PP ;
  114.                     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  115.         GPIO_Init(GPIOA, &GPIO_InitStructure);

  116.         /* Deselect the FLASH: Chip Select high */
  117.         GPIO_SetBits(GPIOA,GPIO_Pin_9);
  118.        // GPIO_SetBits(GPIOA,GPIO_Pin_10);

  119.                     SPI_Cmd(SPI1, DISABLE);
  120.                         
  121.                     /* SPI1 configuration */
  122.         SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;  
  123.         SPI_InitStructure.SPI_Mode = SPI_Mode_Master;              
  124.         SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;            
  125.         SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;               
  126.         SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;      
  127.         SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;               
  128.         SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;            
  129.         SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;      
  130.         SPI_InitStructure.SPI_CRCPolynomial = 7;         
  131.         SPI_Init(SPI1, &SPI_InitStructure);           

  132.         //SPI1->CR1|=1<<6;
  133.         /* Enable SPI1  */
  134.         SPI_Cmd(SPI1, ENABLE);

  135.         SPIx_ReadWriteByte(0xff);              
  136. }  




  137. u16 SPIx_ReadWriteByte(u16 TxData)
  138. {               
  139.         GPIO_ResetBits(GPIOA,GPIO_Pin_9);
  140.              // GPIO_ResetBits(GPIOA,GPIO_Pin_10);
  141.         while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); //
  142.         /* Send byte through the SPI1 peripheral */
  143.         SPI_I2S_SendData(SPI1, TxData); //í¨1yíaéèSPIx·¢Ëíò»¸öêy¾Y
  144.              //TM7705_Send8Bit(TxData);
  145.         /* Wait to receive a byte */
  146.         while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); //
  147.         /* Return the byte read from the SPI bus */
  148.         //TM7705_Recive8Bit()        ;                     
  149.         GPIO_SetBits(GPIOA,GPIO_Pin_9);
  150.              // GPIO_SetBits(GPIOA,GPIO_Pin_10);        

  151.         return SPI_I2S_ReceiveData(SPI2); //·μ»Øí¨1ySPIx×î½ü½óêÕμÄêy¾Y   
  152. }

  153.                         
  154.                                  




  155. /********Ö÷oˉêy************/
  156. int main(void)
  157. {         
  158. u16 adcx;
  159. float temp;

  160.         RCC_Configuration();
  161.         GPIO_Configuration();
  162.               uart_init(76800);
  163.         delay_init(168);
  164.         LCD_Init();
  165.               LED_Init();
  166.               SPIx_Init();   
  167.         TM7705_Init();
  168.         
  169.               POINT_COLOR=BLUE;
  170.               LCD_ShowString(30,110,200,16,16,"ADC_CH1_VAL:");              
  171.               LCD_ShowString(30,170,200,16,16,"ADC_CH1_VOLL:0.0000V");        
  172.                
  173. while(1)
  174.       {
  175.        //delay_ms(50);
  176.                   
  177.        adcx = ReadTM7705();  
  178.                                 
  179.                    LCD_ShowxNum(134,110,adcx,5,16,0);   
  180.                    temp=(float)adcx*(3.0/65535);         
  181.              adcx=temp;                           
  182.                    LCD_ShowxNum(134,170,adcx,1,16,0);   
  183.                    temp-=adcx;                           
  184.                    temp*=10000;                           
  185.                    LCD_ShowxNum(150,170,temp,4,16,0X80);
  186.                    LED0=!LED0;
  187.                    delay_ms(200);        

  188.         }
  189.                         
  190.                                 
  191. }
复制代码


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表