1099b6fd1bebc2f5
获得 0 次赞
帮助过0人
#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(GPIOBGPIO_PinSource3GPIO_AF_SPI1); GPIO_PinAFConfig(GPIOBGPIO_PinSource4GPIO_AF_SPI1); GPIO_PinAFConfig(GPIOBGPIO_PinSource5GPIO_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(GPIOAGPIO_Pin_9); // GPIO_SetBits(GPIOAGPIO_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(GPIOAGPIO_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(GPIOAGPIO_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(301102001616"ADC_CH1_VAL:"); LCD_ShowString(301702001616"ADC_CH1_VOLL:0.0000V"); while(1) { //delay_ms(50); adcx = ReadTM7705(); LCD_ShowxNum(134110adcx5160); temp=(float)adcx*(3.0/65535); adcx=temp; LCD_ShowxNum(134170adcx1160); temp-=adcx; temp*=10000; LCD_ShowxNum(150170temp4160X80); LED0=!LED0; delay_ms(200); }}LCD显示出来的一直是最大值65535。。。。应该是数据异常。。。。。。debug的时候SPI里面数据寄存器的数值好像不太正常。。。。求大神帮帮忙。。谢谢啦!