找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1765|回复: 5
收起左侧

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

[复制链接]
ID:797057 发表于 2020-7-13 16:48 | 显示全部楼层 |阅读模式
100黑币
#include "stm32f4xx.h"
#include "usart.h"
#include "lcd.h"
#include "delay.h"
#include "led.h"
#include "stdio.h"


void RCC_Configuration(void);
void GPIO_Configuration(void);
u16 SPIx_ReadWriteByte(u16 TxData);

void SPIx_Init(void);
u16 ReadTM7705(void);
uint8_t TM7705_Recive8Bit(void);
void TM7705_Send8Bit(uint8_t _data);


void Delay(vu32 nCount)
{
       for(; nCount != 0; nCount--);
}


void GPIO_Configuration(void)
{

        GPIO_InitTypeDef GPIO_InitStructure;
        /* Configure USART1 Rx (PB.8) as input floating  busy busy DRDY */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
              GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP ;
                    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
}

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

/********TM77053õê¼»ˉoˉêy***********/
void TM7705_Init(void)
{         
        unsigned char i ;
        for(i = 0; i < 100; i++)
    {
        SPIx_ReadWriteByte(0xFF);                        
    }
        SPIx_ReadWriteByte(0x20) ;                    
        Delay(800);
        SPIx_ReadWriteByte(0x02) ;                  
        Delay(800);
        SPIx_ReadWriteByte(0x10) ;                        
        Delay(800);
        SPIx_ReadWriteByte(0x44) ;                        
        Delay(8000);
}

/********¶á16λêy¾Y************/
u16 ReadTM7705_16BitValue(void)
{
        //unsigned long
                                u16        DataL = 0;
       // unsigned long
                                u16        DataH = 0;
        //unsigned long
                                u16        Ret = 0;

        DataH =SPIx_ReadWriteByte(0xff);
              delay_ms(10);
               DataH = DataH << 8;

              DataL = SPIx_ReadWriteByte(0xff);

              delay_ms(10);
        Ret = DataH | DataL;

        return(Ret) ;                             
}

/********¶áè¡AD7705μúò»í¨μàêy¾Y************/
u16 ReadTM7705(void)
{


                u16        Ret = 0;

                 SPIx_ReadWriteByte(0x38) ;
  while(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_10==1))
                                {
                                        ;
                                }            
        Ret = ReadTM7705_16BitValue();
        delay_ms(100);

        return Ret;
}


/****************************SPI3õê¼»ˉ*************************/
void SPIx_Init(void)
        {         
        SPI_InitTypeDef SPI_InitStructure;
        GPIO_InitTypeDef GPIO_InitStructure;
       RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOA, ENABLE);//
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);

              GPIO_PinAFConfig(GPIOB,GPIO_PinSource3,GPIO_AF_SPI1);
              GPIO_PinAFConfig(GPIOB,GPIO_PinSource4,GPIO_AF_SPI1);
              GPIO_PinAFConfig(GPIOB,GPIO_PinSource5,GPIO_AF_SPI1);

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

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

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

                    SPI_Cmd(SPI1, DISABLE);

                    /* SPI1 configuration */
        SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;  
        SPI_InitStructure.SPI_Mode = SPI_Mode_Master;              
        SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;            
        SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;               
        SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;      
        SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;               
        SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;            
        SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;      
        SPI_InitStructure.SPI_CRCPolynomial = 7;         
        SPI_Init(SPI1, &SPI_InitStructure);           

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

        SPIx_ReadWriteByte(0xff);              
}  



u16 SPIx_ReadWriteByte(u16 TxData)
{               
        GPIO_ResetBits(GPIOA,GPIO_Pin_9);        
        while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); //

        /* Send byte through the SPI1 peripheral */
        SPI_I2S_SendData(SPI1, TxData)
        while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); //

        /* Return the byte read from the SPI bus */               
        GPIO_SetBits(GPIOA,GPIO_Pin_9);  

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


/********Ö÷oˉêy************/
int main(void)
{         
u16 adcx;
float temp;

        RCC_Configuration();
        GPIO_Configuration();
              uart_init(76800);
        delay_init(168);
        LCD_Init();
              LED_Init();
              SPIx_Init();   
        TM7705_Init();

              POINT_COLOR=BLUE;
              LCD_ShowString(30,110,200,16,16,"ADC_CH1_VAL:");              
              LCD_ShowString(30,170,200,16,16,"ADC_CH1_VOLL:0.0000V");        

while(1)
      {
       //delay_ms(50);

       adcx = ReadTM7705();  

                   LCD_ShowxNum(134,110,adcx,5,16,0);   
                   temp=(float)adcx*(3.0/65535);         
             adcx=temp;                           
                   LCD_ShowxNum(134,170,adcx,1,16,0);   
                   temp-=adcx;                           
                   temp*=10000;                           
                   LCD_ShowxNum(150,170,temp,4,16,0X80);
                   LED0=!LED0;
                   delay_ms(200);        

        }


}


LCD显示出来的一直是最大值65535。。。。应该是数据异常。。。。。。debug的时候SPI里面数据寄存器的数值好像不太正常。。。。求大神帮帮忙。。谢谢啦!


回复

使用道具 举报

ID:282850 发表于 2020-7-14 14:55 | 显示全部楼层
把AD7705的输入端短接、把PGA调到最小试试,其它可能的问题也会是SPI程序错误
回复

使用道具 举报

ID:797057 发表于 2020-7-14 16:50 | 显示全部楼层
f556 发表于 2020-7-14 14:55
把AD7705的输入端短接、把PGA调到最小试试,其它可能的问题也会是SPI程序错误

短接了还是跟原来一样。。。。。请问PGA是怎么调的。。。
回复

使用道具 举报

ID:801878 发表于 2020-7-28 10:19 | 显示全部楼层
、PGA相关的参数      bitmap_merge_area_size             -->位图合并区分配的值     create_bitmap_area_size            -->创建位图分配的值     hash_area_size                     -->主要用于hash join时分配的大小     sort_area_size                     -->排序所用的内存总量,影响one-pass,multi-pass,optimal     sort_area_retained_size            -->排序后在内存中保存排序信息的内存总量     这几个参数在Oracle 9i之前被使用,在Oracle 9i可以对这几个参数进行手动分配,亦可以由系统自动分配。
回复

使用道具 举报

ID:809311 发表于 2020-8-8 17:17 | 显示全部楼层
一边调一边看。
回复

使用道具 举报

ID:522756 发表于 2020-8-11 10:15 | 显示全部楼层
我只能说,你等着吧
就放一个工程出来,也不知道具体原因出在哪里,就等着别人给你解决
老铁,你应该有一套自己的思路,然后哪里出了问题,别人好针对性的给你解决思路
比如说LCD,你直接说显示不了,别人怎帮你
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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