中级会员
- 积分
- 274
- 金钱
- 274
- 注册时间
- 2016-7-15
- 在线时间
- 88 小时
|
楼主 |
发表于 2016-8-1 12:08:49
|
显示全部楼层
[mw_shl_code=c,true]#include "stm32f10x.h"
#include "stm32f10x_usart.h"
#include "misc.h"
#define ADC1_DR_Address ((uint32_t)0x4001244C)
const double Vref_ = 3.3;
__IO uint16_t ADCConvertedValue;
unsigned char TxBuf[10] = " \r\n";
unsigned char TxBuf1[50] = " ¶Ë¿úμçÑ1ÖμÎa \r\n\r\n";
unsigned char TxBuf2[50] = " hello \r\n\r\n";
unsigned char TxBuf3[50] = " ½óêÕμ½êy¾Y \r\n\r\n";
void RCC_Configuration(void);
void ADC1_Config(void);
void USART1_Config(void);
void USART1_NVIC_Config(void);
static unsigned int rec_temp=0;
int main(void)
{
unsigned int i_tmp = 0,i=0;
double ADC_Infact_V = 0;
RCC_Configuration();
ADC1_Config();
USART1_Config();
for( i = 0; TxBuf != '\0'; i++)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET);
USART_SendData(USART1 , TxBuf);
}
for( i = 0; TxBuf1 != '\0'; i++)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET);
USART_SendData(USART1 , TxBuf1);
}
while (1)
{
if(rec_temp == 'a')
{
rec_temp = 0;
{
i_tmp = ADCConvertedValue;
ADC_Infact_V = ((double)i_tmp)*Vref_/4095*1000;
i_tmp = (unsigned int)ADC_Infact_V;
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET);
USART_SendData(USART1 , 0x30+i_tmp/1000%10);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET);
USART_SendData(USART1 , '.');
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET);
USART_SendData(USART1 , 0x30+i_tmp/100%10);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET);
USART_SendData(USART1 , 0x30+i_tmp/10%10);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET);
USART_SendData(USART1 , 0x30+i_tmp%10);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET);
USART_SendData(USART1 , 'V');
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET);
USART_SendData(USART1 , '\r');
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET);
USART_SendData(USART1 , '\n');
i_tmp = ADCConvertedValue;
}
}
}
}
void RCC_Configuration(void)
{
RCC_ADCCLKConfig(RCC_PCLK2_Div4);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC, ENABLE);
}
void USART1_NVIC_Config(void)
{
NVIC_InitTypeDef USART_NVIC_InitStructuer;
USART_NVIC_InitStructuer.NVIC_IRQChannel = USART1_IRQn;
USART_NVIC_InitStructuer.NVIC_IRQChannelSubPriority = 0;
USART_NVIC_InitStructuer.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&USART_NVIC_InitStructuer);
}
void USART1_Config(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
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_ITConfig(USART1,USART_IT_RXNE, ENABLE);
USART_ITConfig(USART1,USART_IT_TXE, DISABLE);
USART_Cmd(USART1, ENABLE);
USART1_NVIC_Config();
}
void ADC1_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOC, &GPIO_InitStructure);
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADCConvertedValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 1;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
DMA_Cmd(DMA1_Channel1, ENABLE);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_55Cycles5);
ADC_DMACmd(ADC1, ENABLE);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
rec_temp = USART_ReceiveData(USART1);
if(rec_temp == 'a' )
{
for( int i = 0; TxBuf3 != '\0'; i++)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET);
USART_SendData(USART1 , TxBuf3);
}
}
}
}
[/mw_shl_code] |
|