void adc_measure(u16 ch)
{
u16 adcx;
float temp;
adcx=Get_Adc_Average(ch,100);
usb_printf("
%d
",adcx);
temp=(float)adcx*(3.3/4096);
usb_printf("
%f
",temp);
}
int main(void)
{
u16 t;
// u16 delay;
u16 data;
u16 len;
u16 times=0;
u8 usbstatus=0;
delay_init(); //ÑÓʱº¯Êý³õʼ»¯
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //ÉèÖÃNVICÖжϷÖ×é2:2λÇÀÕ¼ÓÅÏȼ¶£¬2λÏìÓ¦ÓÅÏȼ¶
uart_init(115200); //´®¿Ú³õʼ»¯Îª115200
Adc_Init(); //ADC³õʼ»¯
Relay_Init(); //relay³õʼ»¯
LED_Init(); //³õʼ»¯ÓëLEDÁ¬½ÓµÄÓ²¼þ½Ó¿Ú
delay_ms(1800);
USB_Port_Set(0); //USBÏȶϿª
delay_ms(700);
USB_Port_Set(1); //USBÔÙ´ÎÁ¬½Ó
Set_USBClock();
USB_Interrupts_Config();
USB_Init();
while(1)
{
if(usbstatus!=bDeviceState)//USBÁ¬½Ó״̬·¢ÉúÁ˸ıä.
{
usbstatus=bDeviceState;//¼Ç¼ÐµÄ״̬
if(usbstatus==CONFIGURED)
{
usb_printf("usb_connected
");
}else
{
usb_printf("usb_disconnected
");
}
}
if(USB_USART_RX_STA&0x8000)
{
len=USB_USART_RX_STA&0x3FFF;//µÃµ½´Ë´Î½ÓÊÕµ½µÄÊý¾Ý³¤¶È
usb_printf("Äú·¢Ë͵ÄÏûϢΪ:");
for(t=0;t<len;t++)
{
USB_USART_SendData(USB_USART_RX_BUF[t]);//ÒÔ×Ö½Ú·½Ê½,·¢Ë͸øUSB
}
//Ñ¡ÔñÖ´Ðй¦ÄÜ
data=(USB_USART_RX_BUF[0]-'0')*100+(USB_USART_RX_BUF[1]-'0')*10+USB_USART_RX_BUF[2]-'0';//»ñµÃÊý¾Ýdata
//m=USB_USART_RX_BUF[0]-'0';
switch(data)
{
//µçѹ¼ì²â
case 100:adc_measure(ADC_Channel_0);break;
case 101:adc_measure(ADC_Channel_1);break;
case 102:adc_measure(ADC_Channel_2);break;
case 103:adc_measure(ADC_Channel_3);break;
case 104:adc_measure(ADC_Channel_4);break;
case 105:adc_measure(ADC_Channel_5);break;
default:usb_printf("
error
");break;
case 106:adc_measure(ADC_Channel_6);break;
case 107:adc_measure(ADC_Channel_7);break;
case 108:adc_measure(ADC_Channel_8);break;
case 109:adc_measure(ADC_Channel_9);break;
case 110:adc_measure(ADC_Channel_10);break;
case 111:adc_measure(ADC_Channel_11);break;
case 112:adc_measure(ADC_Channel_12);break;
case 113:adc_measure(ADC_Channel_13);break;
case 114:adc_measure(ADC_Channel_14);break;
case 115:adc_measure(ADC_Channel_15);break;
case 116:adc_measure(ADC_Channel_16);break;
void Adc_Init(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_ADC1, ENABLE ); //ʹÄÜADC1ͨµÀʱÖÓ
RCC_ADCCLKConfig(RCC_PCLK2_Div6); //ÉèÖÃADC·ÖƵÒò×Ó6 72M/6=12,ADC×î´óʱ¼ä²»Äܳ¬¹ý14M
//PA0/PA1/PA2/PA3/PA4/PA5/PA6/PA7 ×÷ΪģÄâͨµÀÊäÈëÒý½Å
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //Ä£ÄâÊäÈëÒý½Å
GPIO_Init(GPIOA, &GPIO_InitStructure);
//PB0/PB1 ×÷ΪģÄâͨµÀÊäÈëÒý½Å
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //Ä£ÄâÊäÈëÒý½Å
GPIO_Init(GPIOB, &GPIO_InitStructure);
//PC0/PC1/PC2/PC3/PC4/PC5 ×÷ΪģÄâͨµÀÊäÈëÒý½Å
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //Ä£ÄâÊäÈëÒý½Å
GPIO_Init(GPIOC, &GPIO_InitStructure);
ADC_DeInit(ADC1); //¸´Î»ADC1,½«ÍâÉè ADC1 µÄÈ«²¿¼Ä´æÆ÷ÖØÉèΪȱʡֵ
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC¹¤×÷ģʽ:ADC1ºÍADC2¹¤×÷ÔÚ¶ÀÁ¢Ä£Ê½
ADC_InitStructure.ADC_ScanConvMode = DISABLE; //Ä£Êýת»»¹¤×÷ÔÚµ¥Í¨µÀģʽ
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //Ä£Êýת»»¹¤×÷ÔÚµ¥´Îת»»Ä£Ê½
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //ת»»ÓÉÈí¼þ¶ø²»ÊÇÍⲿ´¥·¢Æô¶¯
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //ADCÊý¾ÝÓÒ¶ÔÆë
ADC_InitStructure.ADC_NbrOfChannel = 16; //˳Ðò½øÐйæÔòת»»µÄADCͨµÀµÄÊýÄ¿
ADC_Init(ADC1, &ADC_InitStructure); //¸ù¾ÝADC_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèADCxµÄ¼Ä´æÆ÷
ADC_Cmd(ADC1, ENABLE); //ʹÄÜÖ¸¶¨µÄADC1
ADC_ResetCalibration(ADC1); //ʹÄܸ´Î»Ð£×¼
while(ADC_GetResetCalibrationStatus(ADC1)); //µÈ´ý¸´Î»Ð£×¼½áÊø
ADC_StartCalibration(ADC1); //¿ªÆôADУ׼
while(ADC_GetCalibrationStatus(ADC1)); //µÈ´ýУ׼½áÊø
// ADC_SoftwareStartConvCmd(ADC1, ENABLE); //ʹÄÜÖ¸¶¨µÄADC1µÄÈí¼þת»»Æô¶¯¹¦ÄÜ
}
//»ñµÃADCÖµ
//ch:ͨµÀÖµ 0~15
u16 Get_Adc(u8 ch)
{
//ÉèÖÃÖ¸¶¨ADCµÄ¹æÔò×éͨµÀ£¬Ò»¸öÐòÁУ¬²ÉÑùʱ¼ä
ADC_RegularChannelConfig(ADC1, ch, 16, ADC_SampleTime_239Cycles5 ); //ADC1,ADCͨµÀ,²ÉÑùʱ¼äΪ239.5ÖÜÆÚ
ADC_SoftwareStartConvCmd(ADC1, ENABLE); //ʹÄÜÖ¸¶¨µÄADC1µÄÈí¼þת»»Æô¶¯¹¦ÄÜ
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));//µÈ´ýת»»½áÊø
return ADC_GetConversionValue(ADC1); //·µ»Ø×î½üÒ»´Î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;
}
PA1一接上3.3V,其他通道都显示3.3V,怀疑是ADC_Init()有问题,但不知道那个地方修改错了,有没有了解的大神给予帮助下
这里修改下就好了,还是不懂那个1的意思
一周热门 更多>