void usart1_init(void)
{
GPIO_InitTypeDef GPIO_InitStrue;//¶¨òåGPIO½á11ìå
USART_InitTypeDef USART_InitStrue;
NVIC_InitTypeDef NVIC_InitStrue;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//ê1Äü
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_InitStrue.GPIO_Mode=GPIO_Mode_AF_PP;//íÆíì¸′óà TX¶Ë
A9
GPIO_InitStrue.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&GPIO_InitStrue);
GPIO_InitStrue.GPIO_Mode=GPIO_Mode_IN_FLOATING;//¸¡¿Õêäèë RX¶Ë
A10
GPIO_InitStrue.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&GPIO_InitStrue);
USART_InitStrue.USART_BaudRate=115200;
USART_InitStrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStrue.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
USART_InitStrue.USART_Parity=USART_Parity_No;
USART_InitStrue.USART_StopBits=USART_StopBits_1;
USART_InitStrue.USART_WordLength=USART_WordLength_8b;
USART_Init(USART1,&USART_InitStrue);
USART_Cmd(USART1,ENABLE);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//½óêü·Ç
NVIC_InitStrue.NVIC_IRQChannel=USART1_IRQn;
NVIC_InitStrue.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStrue.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStrue.NVIC_IRQChannelSubPriority=1;
NVIC_Init(&NVIC_InitStrue);
}
void Adc_INIT(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);//
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //PA1 ¶Ë¿úÅäÖÃ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //Ä£ÄaêäèëÄ£ê½
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO¿úËù¶èÎa50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); //¸ù¾Yé趨2Îêy3õê¼»ˉGPIOA1
RCC_ADCCLKConfig(RCC_PCLK2_Div8);// APB2Îa72MHZ, ADC ×î′ó½óêü14MHZ 1ê·ÖÆμòa′óóú5.5 //12MHZê±Îa′ïμ½125HZ2éÑùÆμÂ꣬ó|Îa83.5ÖüÆú μ«êÇÖ»óD142HZ
//9MHZê±Îa′ïμ½125HZ2éÑùÆμÂ꣬ó|Îa59.5ÖüÆú μ«êÇ×î½ó½üÎa132hz
ADC_InitStruct.ADC_ContinuousConvMode=DISABLE;//
ADC_InitStruct.ADC_DataAlign=ADC_DataAlign_Left;//
ADC_InitStruct.ADC_ExternalTrigConv=ADC_ExternalTrigConv_None;//2»ê1óÃía2¿′¥·¢
ADC_InitStruct.ADC_Mode=ADC_Mode_Independent;//¶àá¢Ä£ê½
ADC_InitStruct.ADC_NbrOfChannel=1;//쥶àí¨μà
ADC_InitStruct.ADC_ScanConvMode=DISABLE;//é¨ÃèÄ£ê½1رÕ
ADC_Init(ADC1, & ADC_InitStruct);//ADC1Ïà1ؽá11ì嶨òå
ADC_Cmd(ADC1, ENABLE);//ê1ÄüADC
ADC_ResetCalibration(ADC1);//ADC¸′λD£×¼ê1Äü
while (ADC_GetResetCalibrationStatus(ADC1));//μè′y¸′λD£×¼½áêø
ADC_StartCalibration(ADC1);//¿aÆôADD£×¼
while (ADC_GetCalibrationStatus(ADC1));//μè′yD£×¼½áêø
}
u16 Get_ADC(u8 ch)
{
ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_55Cycles5);
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));//»ñè¡Çé¿ö£¬μè′y×a»»½áêø
return ADC_GetConversionValue(ADC1);//
}
u16 Get_ADC_average(u8 ch, u8 times)
{
u32 temp_val=0;
u8 t;
for(t=0;t<times;t++)
{
temp_val+=Get_ADC(ch);
delay_ms(5);
}
return temp_val/times;
}
int main(void)
{
u16 adcx;
u16 adcx1;
float temp;
u8 t=0;
delay_init(); //Ñóê±oˉêy3õê¼»ˉ
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //éèÖÃNVICÖD¶Ï·Ö×é2:2λÇàÕ¼óÅÏ輶£¬2λÏìó|óÅÏ輶
usart1_init();
Adc_INIT();
while(1){
adcx=Get_ADC_average(ADC_Channel_1,10);
temp=(float)adcx*(3.3/4096);//×a»ˉÎaμçÑ1
adcx1=(int)temp;
USART_SendData(USART1,adcx1);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);
}
}
主程序不知道怎么写 求教
一周热门 更多>