標(biāo)題:
基于MSP430的數(shù)字存儲(chǔ)示波器的設(shè)計(jì)與實(shí)現(xiàn)程序
[打印本頁(yè)]
作者:
390422202
時(shí)間:
2018-11-7 17:32
標(biāo)題:
基于MSP430的數(shù)字存儲(chǔ)示波器的設(shè)計(jì)與實(shí)現(xiàn)程序
單片機(jī)源程序如下:
/*******************************************
方案1需要按復(fù)位鍵即每次只能采樣128次
方案2加一個(gè)軟件觸發(fā),從固定位置觸發(fā)
各種中斷形成一個(gè) 循環(huán)。∩弦粋(gè)中斷完成再觸發(fā)下一個(gè)中斷
********************************************/
#include <msp430x16x.h>
#include "math.h"
extern unsigned int N_point; //點(diǎn)數(shù)
extern unsigned char M_grade; //階數(shù)
#define PI 3.14159265358979323
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#define Num_of_Results 129
#define CPU_F ((double)8000000)
#define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))
#define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))
float data_R[128]; //實(shí)部
float data_I[128]; //虛部
uint N_point=128;
uchar M_grade=7;
uint results[32];
uchar fre[4];
uchar mami[7]={"-0000mv"};
uchar h0[] = {"頻率"};
uchar h1[] = {"正在保存...."};
uchar h2[] = {"歡迎使用本示波器"};
uchar h3[] = {"保存等待"};
uchar h4[] = {"不可保存"};
uchar h5[] = {"****************"};
uint count,ad,start,end,biger,longer;
ulong sum,max=0,min=4095,zhi;
uchar index =0,h=1,temp,flag=1,m=0,c=1;
uchar tem=0,num=0;//中斷函數(shù)中的計(jì)數(shù)變量,定義成全局
uchar tempH,tempL,color=1;
uint wavey[128];
#define LCD_DataIn P4DIR=0x00 //數(shù)據(jù)口方向設(shè)置為輸入
#define LCD_DataOut P4DIR=0xff //數(shù)據(jù)口方向設(shè)置為輸出
#define LCD2MCU_Data P4IN
#define MCU2LCD_Data P4OUT
#define LCD_CMDOut P6DIR|=0xFF //P3口的低三位設(shè)置為輸出
#define LCD_RS_H P6OUT|=BIT3 //P3 .0
#define LCD_RS_L P6OUT&=~BIT3 //P3.0
#define LCD_RW_H P6OUT|=BIT4 //P3.1
#define LCD_RW_L P6OUT&=~BIT4 //P3.1
#define LCD_EN_H P6OUT|=BIT5 //P3.2
#define LCD_EN_L P6OUT&=~BIT5 //P3.2
/*******************************************
函數(shù)名稱(chēng):int_CAP()
功 能:初始化觸發(fā)器!只打開(kāi)觸發(fā)引腳P13
參 數(shù):無(wú)
返回值 :無(wú)
********************************************/
void int_CAP()
{
P1SEL = 0x0C;//P12P13作為捕獲輸入端子
P1DIR = 0x00;
TACCTL1|=CM_2+SCS+CAP;//P12下升沿觸發(fā)捕獲,同步模式 不開(kāi)中斷
TACCTL2|=CM_1+SCS+CAP+CCIE;//P13上升沿觸發(fā)捕獲,同步模式
TACTL|=TASSEL_2+MC_2;//SMCLK作為計(jì)數(shù)脈沖,不分頻
}
/*******************************************
函數(shù)名稱(chēng):Clock_Init()
功 能:初始化時(shí)鐘
參 數(shù):無(wú)
返回值 :無(wú)
********************************************/
void Clock_Init() //時(shí)鐘初始化
{
uchar i;
BCSCTL1&=~XT2OFF; //打開(kāi)XT振蕩器
BCSCTL2|=SELM1+SELS+DIVS_3; //MCLK為8MHZ,SMCLK為1MHZ
do
{
IFG1&=~OFIFG; //清除震蕩標(biāo)志
for(i=0;i<100;i++)
_NOP(); //延時(shí)等待
}
while((IFG1&OFIFG)!=0); //如果標(biāo)志為1,則繼續(xù)循環(huán)等待
IFG1&=~OFIFG;
}
/*******************************************
函數(shù)名稱(chēng):int_adc()
功 能:初始化ADC
參 數(shù):無(wú)
返回值 :無(wú)
********************************************/
void int_adc()
{
P6SEL |= 0x07; // P6.0 P6.1P6.2ADC option select
P6DIR |=0XF8;
ADC12CTL0 = SHT0_2 + ADC12ON+REF2_5V+REFON+MSH; // Set sampling time, turn on ADC12
ADC12CTL1 = SHP+CONSEQ_3;// Use sampling timer
ADC12MCTL0 = 0X00;//參考電壓 AVCC AVSS
ADC12MCTL1 = INCH_1;
ADC12MCTL2 = INCH_2+EOS;//選擇通路,EOS停止第一輪轉(zhuǎn)化
ADC12IE = 0x04; //中斷最后一個(gè)使能 // Enable interrupt
ADC12CTL0 |= ENC; // 轉(zhuǎn)化 enabled
}
/*******************************************
函數(shù)名稱(chēng):Delay_1ms
功 能:延時(shí)約1ms的時(shí)間
參 數(shù):無(wú)
返回值 :無(wú)
********************************************/
void Delay_1ms(void)
{
uchar i;
for(i = 150;i > 0;i--) _NOP();
}
/*******************************************
函數(shù)名稱(chēng):fft_128()
功 能:fft變換
參 數(shù):無(wú)
返回值 :無(wú)
********************************************/
void fft_128()
{ float tab_sin_128[128]={ //正玄表 旋轉(zhuǎn)因子所在
0.0000, 0.0491, 0.0980, 0.1467,0.1951,
0.2430, 0.2903, 0.3369, 0.3827,
0.4276, 0.4714, 0.5141, 0.5556, 0.5957,
0.6344, 0.6716, 0.7071, 0.7410,
0.7730, 0.8032, 0.8315, 0.8577, 0.8819,
0.9040, 0.9239, 0.9415, 0.9569,
0.9700, 0.9808, 0.9892, 0.9952, 0.9988,
1.0000, 0.9988, 0.9952, 0.9892,
0.9808, 0.9700, 0.9569, 0.9415, 0.9239,
0.9040, 0.8819, 0.8577, 0.8315,
0.8032, 0.7730, 0.7410, 0.7071, 0.6716,
0.6344, 0.5957, 0.5556, 0.5141,
0.4714, 0.4276, 0.3827, 0.3369, 0.2903,
0.2430, 0.1951, 0.1467, 0.0980,
0.0491, 0.0000,-0.0491,-0.0980,-0.1467,
-0.1951,-0.2430,-0.2903,-0.3369,
-0.3827,-0.4276,-0.4714,-0.5141,-0.5556,
-0.5957,-0.6344,-0.6716,-0.7071,
-0.7410,-0.7730,-0.8032,-0.8315,-0.8577,
-0.8819,-0.9040,-0.9239,-0.9415,
-0.9569,-0.9700,-0.9808,-0.9892,-0.9952,
-0.9988,-1.0000,-0.9988,-0.9952,
-0.9892,-0.9808,-0.9700,-0.9569,-0.9415,
-0.9239,-0.9040,-0.8819,-0.8577,
-0.8315,-0.8032,-0.7730,-0.7410,-0.7071,
-0.6716,-0.6344,-0.5957,-0.5556,
-0.5141,-0.4714,-0.4276,-0.3827,-0.3369,
-0.2903,-0.2430,-0.1951,-0.1467,
-0.0980,-0.0491};
int i;
int L;// the grade deal with now
int J;//the point deal with now
int B;//蝶形兩左翅 相距的距離
int var;//臨時(shí)變量
int P,Q;//cos sin 在sintab中的位置 旋轉(zhuǎn)因子
float data_R_J,data_I_J,data_R_JB,data_I_JB;//臨時(shí)變量 存放data_R。。等
for (i=0;i<N_point;i++)
{
data_R[i]=(float)wavey[i];
}
for(L=1;L<=M_grade;L++)//L級(jí)
{
B=1;
var=L-1;while(var>0){B=2*B;var--;}//B=2^(L-1);
for(i=0;i<B;i++) // 交叉蝶形中的第i個(gè)蝶形 第J=J+2*B個(gè)交叉蝶形 因?yàn)闆](méi)兩個(gè)交叉蝶形相距2*B
{
P=1;var=M_grade-L;
while(var>0){P=2*P;var--;}
P=P*i; //P=J*2^(M-L) ;J=i now
if(P<3*N_point/4)
Q=P+N_point/4;
else Q=P-3*N_point/4;
for(J=i;J<N_point;J=J+2*B)//128 N_point/2 第J個(gè)交叉蝶形
{
data_R_J=data_R[J];
data_I_J=data_I[J];
data_R_JB=data_R[J+B];
data_I_JB=data_I[J+B];//
data_R[J]=data_R_J+data_R_JB*tab_sin_128[Q]+data_I_JB*tab_sin_128[P];
data_I[J]=data_I_J+data_I_JB*tab_sin_128[Q]-data_R_JB*tab_sin_128[P];
data_R[J+B]=data_R_J-data_R_JB*tab_sin_128[Q]-data_I_JB*tab_sin_128[P];
data_I[J+B]=data_I_J-data_I_JB*tab_sin_128[Q]+data_R_JB*tab_sin_128[P];
}
}
}
}
/*******************************************
函數(shù)名稱(chēng):Delay_Nms
功 能:延時(shí)N個(gè)1ms的時(shí)間
參 數(shù):n--延時(shí)長(zhǎng)度
返回值 :無(wú)
********************************************/
void Delay_Nms(uint n)
{
uint i;
for(i = n;i > 0;i--) Delay_1ms();
}
/*******************************************
函數(shù)名稱(chēng):Write_Cmd
功 能:向液晶中寫(xiě)控制命令
參 數(shù):cmd--控制命令
返回值 :無(wú)
********************************************/
void Write_Cmd(uchar cmd)
{
uchar lcdtemp = 0;
LCD_RS_L;
LCD_RW_H;
LCD_DataIn;
do //判忙
{
LCD_EN_H;
_NOP();
lcdtemp = LCD2MCU_Data;
LCD_EN_L;
}
while(lcdtemp & 0x80);
LCD_DataOut;
LCD_RW_L;
MCU2LCD_Data = cmd;
LCD_EN_H;
_NOP();
LCD_EN_L;
}
/*******************************************
函數(shù)名稱(chēng):Write_Data
功 能:向液晶中寫(xiě)顯示數(shù)據(jù)
參 數(shù):dat--顯示數(shù)據(jù)
返回值 :無(wú)
********************************************/
void Write_Data(uchar dat)
{
uchar lcdtemp = 0;
LCD_RS_L;
LCD_RW_H;
LCD_DataIn;
do //判忙
{
LCD_EN_H;
_NOP();
lcdtemp = LCD2MCU_Data;
LCD_EN_L;
}
while(lcdtemp & 0x80);
LCD_DataOut;
LCD_RS_H;
LCD_RW_L;
MCU2LCD_Data = dat;
LCD_EN_H;
_NOP();
LCD_EN_L;
}
uchar readData(void)
{
uchar lcdtemp = 0;
unsigned char RData;
MCU2LCD_Data=0xff;
LCD_RS_L;
LCD_RW_H;
LCD_DataIn;
do //判忙
{
LCD_EN_H;
_NOP();
lcdtemp = LCD2MCU_Data;
LCD_EN_L;
}
while(lcdtemp & 0x80);
LCD_RS_H;
LCD_RW_H;
LCD_EN_L;
LCD_EN_H;
RData=LCD2MCU_Data;
LCD_EN_L;
return RData;
}
/***********************************************************
函數(shù)名: drawPoint
函數(shù)說(shuō)明:畫(huà)點(diǎn)
傳入?yún)?shù):打點(diǎn)位置(x0,y0);color=1,點(diǎn)亮;color=0,擦除
傳出參數(shù):無(wú)
返回值: 無(wú)
**********************************************************/
void Draw_point(unsigned char x,unsigned char y,unsigned char color)
{
uchar row,collum,cbite;
Write_Cmd(0x34);
Write_Cmd(0x36);
collum=x>>4;
cbite=x&0x0f;
if(y<32)
row=y;
else
{row=y-32;
collum+=8;
}
Write_Cmd(0x80+row);
Write_Cmd(0x80+collum);
readData();
tempH=readData();
tempL=readData();
Write_Cmd(0x80+row);
Write_Cmd(0x80+collum);
if (color)
{
if(cbite<8)
{
tempH|=(1<<(7-cbite));
}
else
{
tempL|=(1<<(15-cbite));
}
}
else
{
if(cbite<8)
{
tempH&=~(1<<(7-cbite));
}
else
{
tempL&=~(1<<(15-cbite));
}
}
Write_Data(tempH);
Write_Data(tempL);
Write_Cmd(0x30);
}
/***********************************************************
函數(shù)名: drawRowLine
函數(shù)說(shuō)明:畫(huà)水平線
傳入?yún)?shù):(x0,y0),水平線的起點(diǎn);(x1,y0)水平線的終點(diǎn)
color=1,點(diǎn)亮;color=0,擦除
傳出參數(shù):無(wú)
返回值: 無(wú)
**********************************************************/
void drawRowLine(uchar x0,uchar y0,uchar x1,uchar color)
{
do
{
Draw_point(x0, y0, color); // 逐點(diǎn)顯示,描出垂直線
x0++;
}
while(x1>=x0);
}
/***********************************************************
函數(shù)名: drawCollumLine
函數(shù)說(shuō)明:畫(huà)豎直線
傳入?yún)?shù):(x0,y0),豎直線的起點(diǎn);(x1,y0)豎直線的終點(diǎn);
color=1,點(diǎn)亮;color=0,擦除
傳出參數(shù):無(wú)
返回值: 無(wú)
************************************************************/
void drawCollumLine(uchar x0,uchar y0,uchar y1,uchar color)
{
while (y0<=y1)
{
Draw_point(x0,y0,color);
y0++;
}
}
/***********************************************************
函數(shù)名: drawLine
函數(shù)說(shuō)明:使用Bresenham法,畫(huà)任意兩點(diǎn)間的直線
傳入?yún)?shù):(x0,y0),豎直線的起點(diǎn);(x1,y1)豎直線的終點(diǎn) color=1,點(diǎn)亮;color=0,擦除
傳出參數(shù):無(wú)
返回值: 無(wú)
**********************************************************/
void drawLine(uchar x0,uchar y0,uchar x1,uchar y1,uchar color)
{
uchar tempx,tempy;
if(x0>x1) // 對(duì)x0、x1大小進(jìn)行排列,以便畫(huà)圖
{
tempx = x1;
x1 = x0;
x0 = tempx;
}
if(y0>y1)
{
tempy=y0;
y0=y1;
y1=tempy;
}
int dx; // 直線x軸差值變量
int dy; // 直線y軸差值變量
char dx_sym; // x軸增長(zhǎng)方向,為-1時(shí)減值方向,為1時(shí)增值方向
char dy_sym; // y軸增長(zhǎng)方向,為-1時(shí)減值方向,為1時(shí)增值方向
int dx_2; // dx*2值變量,用于加快運(yùn)算速度
int dy_2; // dy*2值變量,用于加快運(yùn)算速度
int di; // 決策變量
dx = x1-x0; // 求取兩點(diǎn)之間的差值
dy = y1-y0;
if (dx<0) dx_sym=-1;
else
{
if(dx>0) dx_sym=1;
else
{
drawCollumLine(x0,y0,y1,color);
return;
}
}
if(dy>0) dy_sym=1;
else
{
if(dy<0) dy_sym=-1;
else
{
drawRowLine(x0,y0,x1,color);
return;
}
}
dx=dx_sym*dx;
dy=dy_sym*dy;
dx_2=dx*2;
dy_2=dy*2;
if(dx>=dy)
{
di=dy_2-dx;
while(x0!=x1)
{
Draw_point(x0,y0,color);
x0+=dx_sym;
if(di<0) di+=dy_2;
else {di+=dy_2-dx_2;y0+=dy_sym;}
}
Draw_point(x0,y0,color);
}
else
{
di=dx_2-dy;
while(y0!=y1)
{
Draw_point(x0,y0,color);
y0+=dy_sym;
if(di<0) di+=dx_2;
else {di+=dx_2-dy_2;x0+=dx_sym;}
}
Draw_point(x0,y0,color);
}
}
/*******************************************
函數(shù)名稱(chēng):Disp_HZ
功 能:控制液晶顯示漢字
參 數(shù):addr--顯示位置的首地址
pt--指向顯示數(shù)據(jù)的指針
num--顯示字符個(gè)數(shù)
返回值 :無(wú)
********************************************/
void Disp_HZ(uchar addr,const uchar * pt,uchar num)
{
uchar i;
Write_Cmd(addr);
for(i = 0;i < (num*2);i++)
Write_Data(*(pt++));
}
/****************************
數(shù)字
*****************************/
void Disp_SZ(uchar addr,const uchar * pt,uchar num)
{
uchar i;
Write_Cmd(addr);
for(i = 0;i < num;i++)
Write_Data(*(pt++));
}
void Ini_Lcd(void)
{
//LCD_CMDOut; //液晶控制端口設(shè)置為輸出
// Delay_Nms(500);
Write_Cmd(0x30); //基本指令集
Delay_1ms();
Write_Cmd(0x02); // 地址歸位
Delay_1ms();
Write_Cmd(0x0c); //整體顯示打開(kāi),游標(biāo)關(guān)閉
Delay_1ms();
Write_Cmd(0x01); //清除顯示
Delay_1ms(); Write_Cmd(0x06); //游標(biāo)右移
Delay_1ms();
Write_Cmd(0x80); //設(shè)定顯示的起始地址
}
/*******************************************
函數(shù)名稱(chēng):Clear_GDRAM
功 能:清除液晶GDRAM中的隨機(jī)數(shù)據(jù)
參 數(shù):無(wú)
返回值 :無(wú)
********************************************/
void Clear_GDRAM()
{
uchar i,j,k;
Write_Cmd(0x34); //打開(kāi)擴(kuò)展指令集
i = 0x80;
for(j = 0 ;j < 32;j++)
{
Write_Cmd(i++);
Write_Cmd(0x80);
for(k = 0;k < 16;k++)
{
Write_Data(0x00);
}
}
i = 0x80;
for(j = 0;j < 32;j++)
{
Write_Cmd(i++);
Write_Cmd(0x88);
for(k = 0;k < 16;k++)
{
Write_Data(0x00);
}
}
Write_Cmd(0x30); //回到基本指令集
}
/*******************************************
函數(shù)名稱(chēng):Draw_TX
功 能:顯示一個(gè)16*16大小的圖形
參 數(shù):Yaddr--Y地址
Xaddr--X地址
dp--指向圖形數(shù)據(jù)存放地址的指針
返回值 :無(wú)
********************************************/
void Draw_TX(uchar Yaddr,uchar Xaddr,const uchar * dp)
{
uchar j;
uchar k=0;
// Write_Cmd(0x01); //清屏,只能清除DDRAM
Write_Cmd(0x34); //使用擴(kuò)展指令集,關(guān)閉繪圖顯示
for(j=0;j<16;j++)
{
Write_Cmd(Yaddr++); //Y地址
Write_Cmd(Xaddr); //X地址
Write_Data(dp[k++]);
Write_Data(dp[k++]);
}
Write_Cmd(0x36); //打開(kāi)繪圖顯示
// Write_Cmd(0x30); //回到基本指令集模式
}
/*******************************************
函數(shù)名稱(chēng):FRE()
功 能:對(duì)定時(shí)器內(nèi)的數(shù)據(jù)進(jìn)行處理,得出頻率
參 數(shù):
返回值 :無(wú)
********************************************/
void choose()
{
zhi = (zhi*3200)/4095;
zhi=zhi/2;
mami[1]=zhi/1000+0X30;
mami[2]=(zhi%1000)/100+0X30;
mami[3]=(zhi%1000)%100/10+0X30;
mami[4]=(zhi%1000)%100%10+0X30;
Disp_SZ(0x8d,mami+1,6);
Disp_SZ(0x98,mami,7);
max=0;
min=4095;
}
/*******************************************
函數(shù)名稱(chēng):FRE()
功 能:對(duì)定時(shí)器內(nèi)的數(shù)據(jù)進(jìn)行處理,得出頻率
參 數(shù):
返回值 :無(wú)
********************************************/
void FRE()
{ uint temp2;
uchar temp1;
temp2=1000000/sum;
temp1=temp2/1000;
fre[0]=temp1+0x30;
temp2=temp2-temp1*1000;
temp1=temp2/100;
fre[1]=temp1+0x30;
temp2=temp2-temp1*100;
temp1=temp2/10;
fre[2]=temp1+0x30;
temp2=temp2-temp1*10;
fre[3]=temp2+0x30;
}
/*******************************************
函數(shù)名稱(chēng):COMPA()
功 能:比較器
參 數(shù):
返回值 :無(wú)
********************************************/
void COMPA()
{
P2SEL = 0x1c; //P24 P23 P22分別為比較器輸入輸出
P2DIR = 0x04; //
CACTL1 = CAON+CARSEL+CAREF_0 ; // 使用外部參考電壓 一腳可以接地
CACTL2 = P2CA0+P2CA1+CAF; // 使用 CA0 CA1
}
/*******************************************
函數(shù)名稱(chēng):timer()
功 能:計(jì)數(shù)器處理
參 數(shù):
返回值 :無(wú)
********************************************/
void timer()
{
TACCTL1&=~CCIE;
P1IE=(BIT0+BIT4+BIT5+BIT6+BIT7);
flag=0;
tem++;
switch(tem)
{
case 1:{start=TACCR1;}break;
case 2:end=TACCR1;
if(end>start)
{
results[num]=(end-start);
}
else
{
results[num]=(65536-start+end);
}
tem=0;
num++;
break;
}
Delay_1ms();
TACCTL1|=CCIE;//以前是關(guān)總中斷 現(xiàn)在試試關(guān)定時(shí)器中斷
if(num==32)
{ uchar i;
for(i = 0; i < num; i++)
{
sum+=results[i];
}
sum >>=5;
FRE();
Disp_SZ(0x8A,fre,4);
sum=0;
num=0;
TACCTL1&=~CCIE;
Disp_HZ(0x9c,h3,4);
delay_ms(1000);
Disp_HZ(0x9c,h4,4);
P1IE&=~(BIT0+BIT4+BIT5+BIT6+BIT7);
//ADC12IE |= 0x01;
ADC12CTL0 |= ENC;//只能單位置位
TACCTL2 |=CCIE;
//_EINT();
}
}
/*******************************************
函數(shù)名稱(chēng):Key
功 能:存儲(chǔ)
參 數(shù):
返回值 :無(wú)
********************************************/
void Key(void)
{
P1DIR&=~(BIT0+BIT4+BIT5+BIT6+BIT7);
P1SEL&=~(BIT0+BIT4+BIT5+BIT6+BIT7);
P1IES = BIT0+BIT4+BIT5+BIT6+BIT7;
}
/*******************************************
函數(shù)名稱(chēng):change
功 能:改變頻率和放大倍數(shù)
參 數(shù):
返回值 :無(wú)
********************************************/
void change()
{
zhi=max-min;
P5SEL = 0X00;
P5DIR = 0XFF;
ADC12CTL0 &= ~ENC;//置位0,DIV才能改
if(longer<512)
{
ADC12CTL1 = ADC12DIV_0+SHP+CONSEQ_3;
}
else if(longer<1024)
{
ADC12CTL1 = ADC12DIV_1+SHP+CONSEQ_3;
}
else if(longer<1536)
{
ADC12CTL1 = ADC12DIV_2+SHP+CONSEQ_3;
}
else if(longer<2048)
{
ADC12CTL1 = ADC12DIV_3+SHP+CONSEQ_3;
}
else if(longer<2560)
{
ADC12CTL1 = ADC12DIV_4+SHP+CONSEQ_3;
}
else if(longer<3072)
{
ADC12CTL1 = ADC12DIV_5+SHP+CONSEQ_3;
}
else if(longer<3584)
{
ADC12CTL1 = ADC12DIV_6+SHP+CONSEQ_3;
}
else if(longer<4096)
{
ADC12CTL1 = ADC12DIV_7+SHP+CONSEQ_3;
}
ADC12CTL0 |= ENC;
////////
if(biger<256)
{
P5OUT = 0x06;
zhi=zhi/156;
}
else if(biger<512)
{
P5OUT = 0x0e;
zhi=zhi/143;
}
else if(biger<786)
{
P5OUT = 0x16;
zhi=(zhi*27)/2600;
}
else if(biger<1024)
{
P5OUT = 0x1e;
zhi=(zhi*8)/1005;
}
else if(biger<1280)
{
P5OUT = 0x26;
zhi=zhi/78;
}
else if(biger<1536)
{
P5OUT = 0x2e;
//zhi=(zhi*9)/435;
}
else if(biger<1792)
{
P5OUT = 0x36;
zhi=(zhi*5)/119;
}
else if(biger<2048)
{
P5OUT = 0x3e;
zhi=(zhi*20)/97;
}
else if(biger<2304)
{
P5OUT = 0x07;
zhi=(zhi*15)/293;
}
else if(biger<2560)
{
P5OUT = 0x0f;
zhi=(zhi*44)/775;
}
else if(biger<2816)
{
P5OUT = 0x17;
zhi=(zhi*90)/1001;
}
else if(biger<3072)
{
P5OUT = 0x1f;
zhi=(zhi*75)/312;
}
else if(biger<3328)
{
P5OUT = 0x27;
zhi=(zhi*5)/48;
}
else if(biger<3584)
{
P5OUT = 0x2f;
zhi=(zhi*700)/4318;
}
else if(biger<3840)
{
P5OUT = 0x37;
zhi=(zhi*100)/322;
}
else if(biger<4096)
{
P5OUT = 0x3f;
zhi=(zhi*1000)/607;
}
}
/*******************************************
函數(shù)名稱(chēng):write_Seg
功 能:存儲(chǔ)數(shù)據(jù)到某段FLASH
參 數(shù):
返回值 :無(wú)
********************************************/
void write_Seg (uint add)
{
uchar *Flash_ptr; // Flash pointer
uchar i;
Flash_ptr = (uchar *)add; // Initialize Flash pointer
FCTL1 = FWKEY + ERASE; // Set Erase bit
FCTL3 = FWKEY;
// Clear Lock bit
*Flash_ptr = 0; // Dummy write to erase Flash segment空寫(xiě)
FCTL1 = FWKEY + WRT; // Set WRT bit for write operation
for (i=0; i<128; i++)
{
*Flash_ptr++ = wavey[i]; // Write value to flash
}
FCTL1 = FWKEY; // Clear WRT bit
FCTL3 = FWKEY + LOCK; // Set LOCK bit
}
/*******************************************
函數(shù)名稱(chēng):read_Seg
功 能:read_某段FLASH
參 數(shù):
返回值 :無(wú)
********************************************/
void read_Seg(uint add)
{
uchar *Flash_ptr; // Segment A pointer
uchar i;
Flash_ptr = (uchar *)add;
//FCTL1 = FWKEY + ERASE; // Set Erase bit
//FCTL3 = FWKEY; // Clear Lock bit
*Flash_ptr = 0; // Dummy write to erase Flash segment B
//FCTL1 = FWKEY + WRT; // Set WRT bit for write operation
for (i=0; i<128; i++)
{
wavey[i] = *Flash_ptr++;
}
FCTL1 = FWKEY; // Clear WRT bit
FCTL3 = FWKEY + LOCK; // Set LOCK bit
}
/***************************主函數(shù)*************************/
void main( void )
{
P6SEL = 0x00;
LCD_CMDOut;
WDTCTL = WDTPW + WDTHOLD; //關(guān)狗
Key();
Clock_Init();
int_adc();
COMPA();
_EINT();
Ini_Lcd(); //初始化液晶
Clear_GDRAM();
Disp_SZ(0x80,h5,16);
Disp_HZ(0x90,h2,8);
Disp_SZ(0x88,h5,16);
delay_ms(1000);
Write_Cmd(0x01); //清除漢字顯示
Delay_1ms();
Disp_HZ(0x88,h0,2);
//while(1)
//{
_NOP();
int_CAP(); //波形顯示完成后在初始化
// }
// _BIS_SR(CPUOFF + GIE); // LPM0, ADC12_ISR will force exit
//write_Seg(0x1080);
}
/*******************************************
函數(shù)名稱(chēng):ADC中斷
功 能:進(jìn)入中斷程序,處理采集到的數(shù)據(jù)
參 數(shù):
返回值 :無(wú)
********************************************/
#pragma vector=ADC_VECTOR // ADC12 interrupt service rout ine
__interrupt void ADC12ISR (void)
{ ADC12IE &= ~ 0x04; // Enable interrupt關(guān)中斷不可以!。。!
TACCTL2&=~CCIE;//
uchar y=0;
if(max<ADC12MEM2)
{
max=ADC12MEM2;
}
if(min>ADC12MEM2)
{
min=ADC12MEM2;
}
ad = ADC12MEM2;
biger = ADC12MEM1;
longer = ADC12MEM0;
ad >>=7;
wavey[index++] = ad;
if(index==127)
{
index=0;
ADC12CTL0 &= ~ ADC12SC;
ADC12CTL0 &= ~ENC;//停止轉(zhuǎn)化
Clear_GDRAM();
for (m=0;m<127;m++)
{
uchar Oldx,Oldy ;
Draw_point((m*h),(31*(h-1)/h+wavey[y]/h),1);
Oldx=(uchar)(m*h);
Oldy=(uchar)(31*(h-1)/h+wavey[y]/h);
drawLine(Oldx,Oldy,((m+1)*h+0),(31*(h-1)/h+wavey[++y]/h),1);
}
change();//把轉(zhuǎn)換通道換一下
choose();
TACCTL1|=CCIE;
_NOP();
}
ADC12IE |= 0x04;
}
/*******************************************
函數(shù)名稱(chēng):定時(shí)器中斷
功 能:讀取定時(shí)器數(shù)據(jù)
參 數(shù):
返回值 :無(wú)
********************************************/
#pragma vector=TIMERA1_VECTOR
__interrupt void Timer_A(void)
{
switch( TAIV )
{
case 2: {timer();}break;
case 4: ADC12CTL0 |= ADC12SC; break; //重新打開(kāi)使能 32次轉(zhuǎn)換完成之后再開(kāi)始ADC
case 10: break;
}
}
#pragma vector=PORT1_VECTOR
__interrupt void KeyBord(void)
{
if((P1IFG&BIT7)!=0)
{
Write_Cmd(0x01); //清除漢字顯示
Delay_1ms();
Disp_HZ(0x88,h0,2);
P1IFG&=~BIT5;//清處中斷標(biāo)志
P1IFG&=~BIT6;//清處中斷標(biāo)志
P1IFG&=~BIT7;//清處中斷標(biāo)志
}
else if((P1IFG&BIT5)!=0)
{
Clear_GDRAM();
Write_Cmd(0x01); //清除顯示
Delay_1ms();
uchar y=0;
read_Seg(0x1000);
for (m=0;m<127;m++)
{
uchar Oldx,Oldy ;
Draw_point((m*h),(31*(h-1)/h+wavey[y]/h),1);
Oldx=(uchar)(m*h);
Oldy=(uchar)(31*(h-1)/h+wavey[y]/h);
drawLine(Oldx,Oldy,((m+1)*h+0),(31*(h-1)/h+wavey[++y]/h),1);
}
}
else if((P1IFG&BIT6)!=0)
{ Clear_GDRAM();
Write_Cmd(0x01); //清除顯示
Delay_1ms();
uchar y=0;
read_Seg(0x1080);
for (m=0;m<127;m++)
{
uchar Oldx,Oldy ;
Draw_point((m*h),(31*(h-1)/h+wavey[y]/h),1);
Oldx=(uchar)(m*h);
Oldy=(uchar)(31*(h-1)/h+wavey[y]/h);
drawLine(Oldx,Oldy,((m+1)*h+0),(31*(h-1)/h+wavey[++y]/h),1);
}
}
else if((P1IFG&BIT4)!=0)
{
Clear_GDRAM();
Write_Cmd(0x01); //清除顯示
Delay_1ms();
Disp_HZ(0x80,h1,6);
if(c==1)
{
write_Seg (0x1000);
c=2;
}
else if(c==2)
{
write_Seg (0x1080);
c=1;
}
//Disp_HZ(0x88,h2,8);
delay_ms(1000);
Write_Cmd(0x01); //清除漢字顯示
Delay_1ms();
Disp_HZ(0x88,h0,2);
P1IFG&=~BIT4;//清處中斷標(biāo)志
} else if((P1IFG&BIT0)!=0)
{
Clear_GDRAM();
Write_Cmd(0x01); //清除顯示
Delay_1ms();
int i;
for(i=0;i<N_point;i++)
{
data_I[i]=0;
}
fft_128();
for(i=0;i<128;i++)
{
wavey[i]=(unsigned int)sqrt(data_R[i]*data_R[i]+data_I[i]*data_I[i])/32;
}
for(i=0;i<128;i++)
{ //drawCollumLine(i,(31*(h-1)/h+wavey[i]/h),31,1);
Draw_point(i,1,1);
Draw_point(i,(31*(h-1)/h+wavey[i]/h),1);
drawLine(i,(31*(h-1)/h+wavey[i]/h),i,1,1);
}
_NOP();
delay_ms(1000);
Disp_HZ(0x88,h0,2);
P1IFG&=~BIT0;//清處中斷標(biāo)志
}
}
復(fù)制代碼
所有資料51hei提供下載:
基于MSP430的數(shù)字存儲(chǔ)示波器的設(shè)計(jì)與實(shí)現(xiàn)程序.docx
(22.28 KB, 下載次數(shù): 14)
2018-11-7 17:31 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
作者:
admin
時(shí)間:
2018-11-8 03:08
補(bǔ)全原理圖或者詳細(xì)說(shuō)明一下電路連接即可獲得100+黑幣
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1