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)//最大時(shí)鐘不得大于12.4M
{
//打開(kāi)ADC和IO口時(shí)鐘
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);//無(wú)過(guò)濾 模擬adc 開(kāi)漏
break;
case1:
LPC_IOCON->P0_24=0x00;
LPC_IOCON->P0_24|=(1<<0)|(1<<8)|(1<<10);//無(wú)過(guò)濾 模擬adc 開(kāi)漏
break;
case2:
LPC_IOCON->P0_25=0x00;
LPC_IOCON->P0_25|=(1<<0)|(1<<8)|(1<<10);//無(wú)過(guò)濾 模擬adc 開(kāi)漏
break;
case3:
LPC_IOCON->P0_26=0x00;
LPC_IOCON->P0_26|=(1<<0)|(1<<8)|(1<<10);//無(wú)過(guò)濾 模擬adc 開(kāi)漏
break;
case4:
LPC_IOCON->P0_30=0x00;
LPC_IOCON->P0_30|=(3<<0)|(1<<8)|(1<<10);//無(wú)過(guò)濾 模擬adc 開(kāi)漏
break;
case5:
LPC_IOCON->P0_31=0x00;
LPC_IOCON->P0_31|=(3<<0)|(1<<8)|(1<<10);//無(wú)過(guò)濾 模擬adc 開(kāi)漏
break;
case6:
LPC_IOCON->P0_12=0x00;
LPC_IOCON->P0_12|=(3<<0)|(1<<8)|(1<<10);//無(wú)過(guò)濾 模擬adc 開(kāi)漏
break;
case7:
LPC_IOCON->P0_13=0x00;
LPC_IOCON->P0_13|=(3<<0)|(1<<8)|(1<<10);//無(wú)過(guò)濾 模擬adc 開(kāi)漏
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;//啟動(dòng)轉(zhuǎn)換 while(!(LPC_ADC->GDR&(1u<<31)));//等待轉(zhuǎn)換結(jié)束 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;//雙緩沖禁止,超時(shí)禁止 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)); } }