本人新手,编写了一段TM7705 AD转换的程序,采用了返回值调用方式,编译倒是通过了,仅出现了一条警告“: statement is unreachable”,提示程序无法达到这。本人想向大家请教,如何正确在主函数中调用子函数的返回值呢。主程序如下:
#include "public.h"
#include "AD7705.h"
#include "sys
tick.h"
#include "gui.h"
#include "led.h"
#include "printf.h"
/****************************************************************************
* Function Name : main
* Description : Main program.
* Input : None
* Output : None
* Return : None
****************************************************************************/
u16 intvalue;
int main()
{
u8 dat[6],j;
u16 value;
float ad;
TFT_Init(); //TFT³õʼ»¯
//AD7705_AllInit(); //ADC³õʼ»¯
LED_Init(); //LED¶Ë¿Ú³õʼ»¯
printf_init();//printf³õʼ»¯
TFT_ClearScreen(BLACK);
GUI_Show12ASCII(10,10,"This is a ADC1-Channel1-PA1 Check!",YELLOW,BLACK);
GUI_Show12ASCII(10,100,"The AD Value is:",YELLOW,BLACK);
while(1)
{
AD7705_AllInit(); //ADC³õʼ»
}
if(j>1)
警告提示无法执行到这
{
j=0;
GPIO_SetBits(GPIOC,GPIO_Pin_0);
}
else
{
j++;
GPIO_ResetBits(GPIOC,GPIO_Pin_0);
}
intvalue= ReadAD7705();
delay_ms(100);
intvalue=intvalue/10;
ad=value*3.3/65536;
intvalue=(u16)(ad*100);
dat[0]=intvalue/100+0x30;
dat[1]='.';
dat[2]=intvalue%100/10+0x30;
dat[3]=intvalue%100%10+0x30;
dat[4]='V';
dat[5]=' ';
GUI_Show12ASCII(160,100,dat,RED,BLACK);
}
调用的TM7705采样子程序
#include "public.h"
u8 SPIx_ReadWriteByte(u8 TxData);
void RCC_Configuration(void);
void SPIx_Init(void);
void Delay(vu32 nCount)
{
for(; nCount != 0; nCount--);
}
void GPIO_Configuration(void)//DRDY
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
/********ÅäÖÃϵͳʱÖÓ,ʹÄܸ÷ÍâÉèʱÖÓ***********/
void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA
|RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO
|RCC_APB2Periph_SPI1, ENABLE );
}
/********AD7705³õʼ»¯º¯Êý***********/
void AD7705_Init(void)
{
unsigned char i ;
for(i = 0; i < 100; i++)
{
SPIx_ReadWriteByte(0xFF); //³ÖÐøDIN¸ßµçƽд²Ù×÷£¬»Ö¸´AD7705½Ó¿Ú
}
SPIx_ReadWriteByte(0x20) ; //ͨµÀ1 ,ÏÂÒ»¸öдʱÖӼĴæÆ÷
Delay(1000);
SPIx_ReadWriteByte(0x0E) ; //дʱÖӼĴæÆ÷ÉèÖøüÐÂËÙÂÊΪ200Hz
Delay(1000);
SPIx_ReadWriteByte(0x10) ; //ͨµÀ1 ,ÏÂÒ»¸öдÉèÖüĴæÆ÷
Delay(1000);
SPIx_ReadWriteByte(0x44) ; //дÉèÖüĴæÆ÷ ,ÉèÖóÉË«¼«ÐÔ¡¢ÎÞ»º³å¡¢ÔöÒæΪ1¡¢Â˲¨Æ÷¹¤×÷¡¢×ÔУ׼
Delay(1000000);
}
/********¶Á16λÊý¾Ý************/
u16 ReadAD7705_16BitValue(void)
{
unsigned long DataL = 0;
unsigned long DataH = 0;
unsigned long Ret = 0;
DataH = SPIx_ReadWriteByte(0xFF);
DataH = DataH << 8;
DataL = SPIx_ReadWriteByte(0xFF);
Ret = DataH | DataL;
return(Ret) ;
}
/********¶ÁÈ¡AD7705µÚһͨµÀÊý¾Ý************/
u16 ReadAD7705(void)
{
u8 i;
unsigned long Ret = 0;
for(i=0;i<10;i++)
{
SPIx_ReadWriteByte(0x38) ; //ÉèÖöÁµ±Ç°Í¨µÀÊý¾Ý
while(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_4 == 1));
Ret =Ret+ ReadAD7705_16BitValue();
}
return Ret;
需要在主函数中调用该采样值并处理
}
/****************************SPI³õʼ»¯*************************/
//SPI¿Ú³õʼ»¯
//ÕâÀïÕëÊǶÔSPI1µÄ³õʼ»¯
void SPIx_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure SPI1 pins: SCK, MISO and MOSI */ //SCLK,DIN,DOUT
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure I/O for Flash Chip select */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //SPI CS
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* Deselect the FLASH: Chip Select high */
GPIO_SetBits(GPIOC,GPIO_Pin_5);
GPIO_SetBits(GPIOC,GPIO_Pin_4);
SPI_Cmd(SPI1, DISABLE); //²»Ê¹ÄÜSPIÍâÉè ×Ô¼º¼ÓÉÏ
/* SPI1 configuration */
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //ÉèÖÃSPIµ¥Ïò»òÕßË«ÏòµÄÊý¾Ýģʽ:SPIÉèÖÃΪ˫ÏßË«ÏòÈ«Ë«¹¤
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //ÉèÖÃSPI¹¤×÷ģʽ:ÉèÖÃΪÖ÷SPI
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //ÉèÖÃSPIµÄÊý¾Ý´óС:SPI·¢ËͽÓÊÕ8λ֡½á¹¹
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; //Ñ¡ÔñÁË´®ÐÐʱÖÓµÄÎÈ̬:ʱÖÓÐü¿Õ¸ß
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //Êý¾Ý²¶»ñÓÚµÚ¶þ¸öʱÖÓÑØ
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSSÐźÅÓÉÓ²¼þ£¨NSS¹Ü½Å£©»¹ÊÇÈí¼þ£¨Ê¹ÓÃSSI룩¹ÜÀí:ÄÚ²¿NSSÐźÅÓÐSSIλ¿ØÖÆ
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; //¶¨Ò岨ÌØÂÊÔ¤·ÖƵµÄÖµ:²¨ÌØÂÊÔ¤·ÖƵֵΪ256
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //Ö¸¶¨Êý¾Ý´«Êä´ÓMSBλ»¹ÊÇLSBλ¿ªÊ¼:Êý¾Ý´«Êä´ÓMSBλ¿ªÊ¼
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRCÖµ¼ÆËãµÄ¶àÏîʽ
SPI_Init(SPI1, &SPI_InitStructure); //¸ù¾ÝSPI_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèSPIx¼Ä´æÆ÷
//SPI1->CR1|=1<<6; //SPIÉ豸ʹÄÜ
/* Enable SPI1 */
SPI_Cmd(SPI1, ENABLE); //ʹÄÜSPIÍâÉè
SPIx_ReadWriteByte(0xff);//Æô¶¯´«Êä
}
//SPIx ¶Áдһ¸ö×Ö½Ú
//TxData:ҪдÈëµÄ×Ö½Ú
//·µ»ØÖµ:¶ÁÈ¡µ½µÄ×Ö½Ú
u8 SPIx_ReadWriteByte(u8 TxData)
{
GPIO_ResetBits(GPIOC,GPIO_Pin_5);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); //¼ì²éÖ¸¶¨µÄSPI±ê־λÉèÖÃÓë·ñ:·¢ËÍ»º´æ¿Õ±ê־λ
/* Send byte through the SPI1 peripheral */
SPI_I2S_SendData(SPI1, TxData); //ͨ¹ýÍâÉèSPIx·¢ËÍÒ»¸öÊý¾Ý
/* Wait to receive a byte */
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); //¼ì²éÖ¸¶¨µÄSPI±ê־λÉèÖÃÓë·ñ:½ÓÊÜ»º´æ·Ç¿Õ±êÖ¾
/* Return the byte read from the SPI bus */
GPIO_SetBits(GPIOC,GPIO_Pin_5);
return SPI_I2S_ReceiveData(SPI1); //·µ»Øͨ¹ýSPIx×î½ü½ÓÊÕµÄÊý¾Ý
}
/********AD7705ÍêÕû³õʼ»¯º¯Êý***********/
void AD7705_AllInit(void)
{
RCC_Configuration();
GPIO_Configuration();
SPIx_Init();
AD7705_Init();
}
一周热门 更多>