LPC1788的ADC和DAC使用
扫描二维码
随时随地手机看文章
#ifndef __ADC1_H_
#define __ADC1_H_
#include "common.h"
#include "delay.h"
void adc_init(u8 ch,u8 div);
u16 adc_cover(u8 ch);
#endif
#include "adc1.h"
void adc_init(u8 ch,u8 div)//最大时钟不得大于12.4M
{
//打开ADC和IO口时钟
LPC_SC->PCONP|=(1<<15)|(1<<12);
switch(ch)
{
case0:
LPC_IOCON->P0_23=0x00;
LPC_IOCON->P0_23|=(1<<0)|(1<<8)|(1<<10);//无过滤 模拟adc 开漏
break;
case1:
LPC_IOCON->P0_24=0x00;
LPC_IOCON->P0_24|=(1<<0)|(1<<8)|(1<<10);//无过滤 模拟adc 开漏
break;
case2:
LPC_IOCON->P0_25=0x00;
LPC_IOCON->P0_25|=(1<<0)|(1<<8)|(1<<10);//无过滤 模拟adc 开漏
break;
case3:
LPC_IOCON->P0_26=0x00;
LPC_IOCON->P0_26|=(1<<0)|(1<<8)|(1<<10);//无过滤 模拟adc 开漏
break;
case4:
LPC_IOCON->P0_30=0x00;
LPC_IOCON->P0_30|=(3<<0)|(1<<8)|(1<<10);//无过滤 模拟adc 开漏
break;
case5:
LPC_IOCON->P0_31=0x00;
LPC_IOCON->P0_31|=(3<<0)|(1<<8)|(1<<10);//无过滤 模拟adc 开漏
break;
case6:
LPC_IOCON->P0_12=0x00;
LPC_IOCON->P0_12|=(3<<0)|(1<<8)|(1<<10);//无过滤 模拟adc 开漏
break;
case7:
LPC_IOCON->P0_13=0x00;
LPC_IOCON->P0_13|=(3<<0)|(1<<8)|(1<<10);//无过滤 模拟adc 开漏
break;
}
LPC_ADC->CR=0X00;
LPC_ADC->CR|=(1< LPC_ADC->INTEN=0x00;//禁用全部中断 } u16 adc_cover(u8 ch) { u16 temp; LPC_ADC->CR&=~(0x000000ff); LPC_ADC->CR|=(1< LPC_ADC->CR|=1<<24;//启动转换 while(!(LPC_ADC->GDR&(1u<<31)));//等待转换结束 temp=(u16)((LPC_ADC->GDR&0x00000fff0)>>4); returntemp; } #ifndef __ADC_H_ #define __ADC_H_ #include "common.h" #define DAC_IO_CON LPC_IOCON->P0_26 void dac_init(void); void dac_set_value(u16 value); #endif #include "dac.h" void dac_init(void) { //io口初始化 DAC_IO_CON=0x00; DAC_IO_CON|=(2<<0)|(1<<16)/*|(1<<10)*/;//使能DAC LPC_DAC->CTRL=0x00;//双缓冲禁止,超时禁止 DMA禁止 LPC_DAC->CR=1<<16; // LPC_DAC->CNTVAL = 100; } void dac_set_value(u16 value) { if(value>1024) { return; } else { LPC_DAC->CR=(u16)((1<<16)|(value<<6)); } }