本帖最后由 有没有鱼干 于 2020-7-13 16:50 编辑
LCD显示出来的一直是最大值65535。。。。应该是数据异常。。。。。。debug的时候SPI里面数据寄存器的数值好像不太正常。。。。求大神帮帮忙。。谢谢啦!
单片机源程序如下:
- #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); //3ÖDøDIN¸ßμçƽD′2ù×÷£¬»Ö¸′AD7705½ó¿ú
- }
- SPIx_ReadWriteByte(0x20) ; //í¨μà1 ,ÏÂò»¸öD′ê±Öó¼Ä′æÆ÷
- Delay(800);
- SPIx_ReadWriteByte(0x02) ; //D′ê±Öó¼Ä′æÆ÷éèÖøüDÂËùÂêÎa200Hz
- Delay(800);
- SPIx_ReadWriteByte(0x10) ; //í¨μà1 ,ÏÂò»¸öD′éèÖüÄ′æÆ÷
- Delay(800);
- SPIx_ReadWriteByte(0x44) ; //D′éèÖüÄ′æÆ÷ ,éèÖÃ3éË«¼«DÔ¡¢ÎT»o3å¡¢ÔöòæÎa0¡¢ÂË2¨Æ÷1¤×÷¡¢×ÔD£×¼
- 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)
- {
-
- // unsigned long
- u16 Ret = 0;
- // GPIO_Configuration();
- // SPIx_Init();
- // TM7705_Init();
- SPIx_ReadWriteByte(0x38) ;//éèÖöáμ±Ç°í¨μàêy¾Y
- while(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_10==1))
- {
- ;
- }
- Ret = ReadTM7705_16BitValue();
- delay_ms(100);
- return Ret;
- }
- /****************************SPI3õê¼»ˉ*************************/
- //SPI¿ú3õê¼»ˉ
- //ÕaàïÕëêǶÔSPI1μÄ3õê¼»ˉ
- 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);
- // GPIO_ResetBits(GPIOA,GPIO_Pin_10);
- while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); //
- /* Send byte through the SPI1 peripheral */
- SPI_I2S_SendData(SPI1, TxData); //í¨1yíaéèSPIx·¢Ëíò»¸öêy¾Y
- //TM7705_Send8Bit(TxData);
- /* Wait to receive a byte */
- while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); //
- /* Return the byte read from the SPI bus */
- //TM7705_Recive8Bit() ;
- GPIO_SetBits(GPIOA,GPIO_Pin_9);
- // GPIO_SetBits(GPIOA,GPIO_Pin_10);
- 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);
- }
-
-
- }
复制代码
|