MSP430F149定時(shí)數(shù)據(jù)采集程序
#define RS_H P5OUT |=BIT3 //Data Register is selected
#define RS_L P5OUT &=~BIT3 //Instruction Register is selected
#define READ P5OUT |= BIT2 //讀LCD
#define WRITE P5OUT &= ~BIT2 //寫(xiě)LCD
#define EN_H P5OUT |= BIT1 //Enable Read/Write Operation
#define EN_L P5OUT &=~BIT1 //Disable Read/Write Operation
unsigned char a[]="AD&DA change!";
int AD_result;
void Init_CLK(void); //時(shí)鐘初始化函數(shù)
void Init_ADC(void); //AD轉(zhuǎn)換函數(shù)
void Init_TimerA(void);
void DATransfer(unsigned DA_data);//DA轉(zhuǎn)換子函數(shù)
void Init_Port()
{
P3SEL=0x00; //LCD所需端口設(shè)置
P3DIR=0xFF;
P3OUT&=~BIT5; //74HC245的DIR為低電平,輸入方向由B端到A端
P5SEL=0x00; //P5輸出
P5DIR=0xFF;
P5OUT=0x00;
P4SEL=0x00; //P4輸出
P4DIR=0xFF;
P4OUT=0x00;
P6SEL=0xF0; //P6.4-p6.5作為AD輸入通道,P6.0-P6.3作為一般的IO口
P6DIR=0xFF;
P6OUT=0x00;
}
void Delay(unsigned int i) //延時(shí)子程序
{
while(i--); //延時(shí)時(shí)間為i*5+6個(gè)時(shí)鐘周期
}
void Write_Command(unsigned char command )
{
P4OUT=command;
RS_L; //選擇程序寄存器
WRITE; //允許寫(xiě)操作
EN_H; //液晶使能
Delay(50);//延時(shí)
EN_L; //液晶讀寫(xiě)操作禁止
Delay(50);
}
void Write_Data(unsigned char data )
{
P4OUT=data;
RS_H; //選擇數(shù)據(jù)寄存器
WRITE; //允許寫(xiě)操作
EN_H; //液晶使能
Delay(50); //延時(shí)
EN_L ; //液晶讀寫(xiě)操作禁止
Delay(50);
}
void Clear_Second_Line() //用于刪除2*16字符液的晶第二行的函數(shù)
{
int i=16;
Write_Command(0xC0); //設(shè)置DDRAM的初始地址
while(i--)
{
P4OUT=0x20;
RS_H; //選擇數(shù)據(jù)寄存器
WRITE; //允許寫(xiě)操作
EN_H; //液晶使能
Delay(50); //延時(shí)
EN_L ; //液晶讀寫(xiě)操作禁止
Delay(50);
}
}
void Init_LCD(void)
{
Write_Command( 0x38 );//8位總線接口,雙行顯示,5*7點(diǎn)陣型
Delay(100);
Write_Command(0x0F);//開(kāi)顯示,開(kāi)光標(biāo),光標(biāo)閃爍
Delay(100);
Write_Command(0x01);//清屏,將空格字符ASCII碼20H寫(xiě)入到所有的DDRAM單元
Delay(2500);
Write_Command(0x06);//設(shè)置進(jìn)入方式,DDRAM自增,整個(gè)顯示右移
Delay(100);
Write_Command(0x80);//設(shè)置DDRAM的初始地址
}
void Init_ADC(void)
{
//P6SEL = BIT7; //設(shè)置P6.7為模擬輸入A7
ADC12CTL0 &= ~(ENC); //設(shè)置ENC為0,從而可以修改ADC12寄存器的值
ADC12CTL0 += ADC12ON+MSH; //開(kāi)A/D轉(zhuǎn)換,允許多次采樣
ADC12MCTL7 = EOS+INCH_7;//ADC12MCTL7的輸入通道為A7,參考電壓分別為AVSS和AVCC
ADC12CTL1 = 0X00; //轉(zhuǎn)換的起始地址為:ADCMEM7
ADC12CTL1 = CSTARTADD0+CSTARTADD1+CSTARTADD2;
ADC12CTL1 += SHP; //采樣脈沖來(lái)自采樣定時(shí)器
ADC12CTL1 += CONSEQ_2; //單通道多次轉(zhuǎn)換
ADC12CTL1 += ADC12SSEL0; //ADC12內(nèi)核時(shí)鐘源為ACLK
ADC12IE = 0x00; //關(guān)閉各個(gè)通道的轉(zhuǎn)換中斷
ADC12CTL0 |= ENC; //使能ADC轉(zhuǎn)換
return;
}
void Init_TimerA(void)
{
TACTL = TASSEL0 + TACLR; // 選擇ACLK:32K,清除TAR
TACTL +=ID1; // 1/8 SMCLK
TACTL +=ID0;
CCTL0 = CCIE; // CCR0 中斷允許
CCR0 = 2000; // 時(shí)間間隔為 0.5S
TACTL |= MC0; // 增記數(shù)模式
return;
}
void Init_CLK(void)
{
unsigned int i;
BCSCTL1 = 0X00; //將寄存器的內(nèi)容清零
//XT2震蕩器開(kāi)啟
//LFTX1工作在低頻模式
//ACLK的分頻因子為1
do
{
IFG1 &= ~OFIFG; // 清除OSCFault標(biāo)志
for (i = 0x20; i > 0; i--);
}
while ((IFG1 & OFIFG) == OFIFG); // 如果OSCFault =1
BCSCTL2 = 0X00; //將寄存器的內(nèi)容清零
BCSCTL2 += SELM1; //MCLK的時(shí)鐘源為T(mén)X2CLK,分頻因子為1
BCSCTL2 += SELS; //SMCLK的時(shí)鐘源為T(mén)X2CLK,分頻因子為1
}
#pragma vector=TIMERA0_VECTOR //定時(shí)器A0的中斷函數(shù)
__interrupt void TimerA_ISR(void)
{
float AD_data;
char a;
char b;
int m;
int n;
// 讀出轉(zhuǎn)換結(jié)果
P5OUT^=BIT6;
while((ADC12IFG & BIT7)==0) ; //判斷有沒(méi)有轉(zhuǎn)換結(jié)束
_NOP();
ADC12CTL0 &= ~ENC; // 關(guān)閉轉(zhuǎn)換
P5OUT^=BIT5;
AD_result = ADC12MEM7; //讀到AD轉(zhuǎn)換的12位二進(jìn)制數(shù)
DATransfer(AD_result);
if((ADC12IFG & BIT7)==0)
{P5OUT^=BIT4;}
AD_data=AD_result*3.3/4095; //將讀到的12位二進(jìn)制數(shù)轉(zhuǎn)化為對(duì)應(yīng)的電壓
m=(int)AD_data;
n=(int)(AD_data*10-m*10);
a=48+m; //將符點(diǎn)型的電壓值轉(zhuǎn)化為字符數(shù),便于液晶顯示
b=48+n;
Clear_Second_Line(); //刪除16*2字符液晶的第二行數(shù)據(jù)
Write_Command(0x06); // 設(shè)置液晶進(jìn)入方式,DDRAM自增,整個(gè)顯示右移
Delay(100);
Write_Command(0xC0); //設(shè)置液晶DDRAM的初始地址
Write_Data(a);
Delay(100);
Write_Command(0x06);//設(shè)置進(jìn)入方式,DDRAM自增,整個(gè)顯示右移
Write_Data(.);
Delay(100);
Write_Command(0x06);//設(shè)置進(jìn)入方式,DDRAM自增,整個(gè)顯示右移
Delay(100);
Write_Data(b);
ADC12CTL0 |= ENC; // 允許AD轉(zhuǎn)換
ADC12CTL0|=ADC12SC; //開(kāi)始新的轉(zhuǎn)換
}
void DATransfer(unsigned DA_data) //DAC轉(zhuǎn)換子程序
{
int i;
unsigned int da_data;
P6OUT&=~BIT0; //CS為低電平
for(i=0;i<16;i++)
{
P6OUT&=~BIT2; //SCLK為低電平
P6OUT&=~BIT1;
da_data=0x3000+(DA_data&0x0fff); //送入待轉(zhuǎn)換的數(shù)字量,并選中A通道輸出
da_data=((da_data>>(15-i))&0x0001)<<1;
P6OUT|=da_data ; //輸出一位二進(jìn)制數(shù)
Delay(10);
P6OUT|=BIT2; //SCLK為高電平
Delay(10);
}
P6OUT&=~BIT2; //SCLK為低電平
P6OUT|=BIT0; //CS為高電平,開(kāi)始轉(zhuǎn)換數(shù)據(jù)
Delay(10);
}
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // 關(guān)閉看門(mén)狗
_DINT(); // 關(guān)閉中斷
Delay(50000);
Init_Port();
Init_LCD();
Init_CLK(); // 初始化
Init_ADC();
Init_TimerA();
_EINT(); // 打開(kāi)中斷
for(unsigned int i=0;a!=看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();