新手入门
- 积分
- 11
- 金钱
- 11
- 注册时间
- 2018-5-17
- 在线时间
- 4 小时
|
10金钱
#include "stm32f10x.h"
#include "GPIO.h"
#include "bsp_SysTick.h"
#include "bsp_usart.h"
#include "ESP12_AT.h"
void Delay_MS(u16 dly);
void delay_us(u16 dly1);
void RCC_Configuration(void);
void GPIO_Configuration(void);
unsigned long Read_HX711(void);
void USART_Configuration(void);
int fputc(int ch,FILE *f);
int GetKey (void);
USART_InitTypeDef USART_InitStructure;
ErrorStatus HSEStartUpStatus;
/*******************************************************************************
* Function Name : main
* Description : Main program.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
int main(void)
{
u32 weigh1;
float weigh2;
#ifdef DEBUG
debug();
#endif
//------------???------------
RCC_Configuration();
GPIO_Configuration();
USART_Configuration( );
while(1)
{
Delay_MS(1000);
weigh2=Read_HX711();
weigh2=weigh2/83886.08;
weigh1=weigh2*2000;
GPIO_SetBits(GPIOA,GPIO_Pin_11);
printf("%d\n",weigh1);
}
}
/*******************************************************************************
* Function Name : Delay_Ms
* Description : delay 1 ms.
* Input : dly (ms)
* Output : None
* Return : None
*******************************************************************************/
void Delay_MS(u16 dly)
{
u16 i,j;
for(i=0;i<dly;i++)
for(j=1000;j>0;j--);
}
void delay_us(u16 dly1)
{
u16 i;
for(i=dly1;i>0;i--);
}
/*******************************************************************************
* Function Name : Delay_Ms
* Description : Read weigh
* Input : None
* Output : None
* Return : None
*******************************************************************************/
unsigned long Read_HX711(void) //?HX711????????
{
unsigned long val ;
unsigned char i = 0;
GPIO_SetBits(GPIOB,GPIO_Pin_13); //DOUT=1
delay_us(1);
GPIO_ResetBits(GPIOA,GPIO_Pin_9); //SCK=0
val=0;
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13)); //??DOUT=0
Delay_MS(15);
for(i=0;i<24;i++)
{
GPIO_SetBits(GPIOA,GPIO_Pin_9); //SCK=1
val=val<<1;
Delay_MS(15);
GPIO_ResetBits(GPIOA,GPIO_Pin_9); //SCK=0
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13)) //DOUT=1
val++;
Delay_MS(15);
}
GPIO_SetBits(GPIOA,GPIO_Pin_9);
val = val^0x800000;
Delay_MS(15);
GPIO_ResetBits(GPIOA,GPIO_Pin_9);
Delay_MS(15);
return val;
}
/*******************************************************************************
* Function Name : RCC_Configuration
* Description : Configures the different system clocks.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RCC_Configuration(void)
{
//----------????RC??-----------
RCC_DeInit(); //???????
RCC_HSEConfig(RCC_HSE_ON); //?????????
while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET); //????????????
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
FLASH_SetLatency(FLASH_Latency_2);
RCC_HCLKConfig(RCC_SYSCLK_Div1); //HCLK = SYSCLK
RCC_PCLK2Config(RCC_HCLK_Div1); //PCLK2 = HCLK
RCC_PCLK1Config(RCC_HCLK_Div2); //PCLK1 = HCLK/2
RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9); //PLLCLK = 8MHZ * 9 =72MHZ
RCC_PLLCmd(ENABLE); //Enable PLLCLK
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); //Wait till PLLCLK is ready
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //Select PLL as system clock
while(RCC_GetSYSCLKSource()!=0x08); //Wait till PLL is used as system clock source
//---------????????--------------------
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE); //??APB2???GPIOA???
//??????
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
}
/*******************************************************************************
* Function Name : GPIO_Configuration
* Description : ???GPIO??
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void GPIO_Configuration(void)
{
//CLKB5 CLRE11 DataE10
GPIO_InitTypeDef GPIO_InitStructure; //?????????
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; //??PC.10(TXD) ? PC.11
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //?????50MHZ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; //?????????
GPIO_Init(GPIOB,&GPIO_InitStructure); //???PB5
//??USART1_Tx???????
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //?????50MHZ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//?? USART1_Rx ?????
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; //??PC.10(TXD) ? PC.11
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO????50MHz
GPIO_Init(GPIOB,&GPIO_InitStructure); //???PB5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //?????50MHZ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure); //???PB5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //??PC.10(TXD) ? PC.11
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //?????50MHZ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure); //???PB5
}
/*******************************************************************************
* Function Name : USART_Configuration
* Description : ????USART1
* Input : ch,*f
* Output : None
* Return : None
*******************************************************************************/
void USART_Configuration(void)
{
//????: ??? 115200 ??? 8 ??? 1 ??? NONE
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No ;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
//?????
USART_Init(USART1, &USART_InitStructure);
//????
USART_Cmd(USART1, ENABLE);
}
/*******************************************************************************
* Function Name : fputc
* Description : ?????C?(stdio)printf?? ???——»??USART1
* Input : ch,*f
* Output : None
* Return : None
*******************************************************************************/
int fputc(int ch, FILE *f)
{
USART_SendData(USART1, (unsigned char) ch);// USART1 ???? USART2 ?
while (!(USART1->SR & USART_FLAG_TXE));
return (ch);
}
// ????
int GetKey (void) {
while (!(USART1->SR & USART_FLAG_RXNE));
return ((int)(USART1->DR & 0x1FF));
}
|
|