本文主要是介绍噪声测量仪 源代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
此项目是参加省电子竞赛,噪声测量仪项目组作品源代码。
比赛四天三晚,时间匆忙,代码有点乱,贴在这里,留念。
下位机是Contex-M0, LPC1114
/****************************************Copyright (c)****************************************************
噪声测量仪
2011年
*********************************************************************************************************/
#include "..\config.h"
#include "..\LCD.h"
//#include "..\AD7822.h"
//#include "..\UART.h"
#include "..\ARM_AD.h"
//#include "..\TLC2543.h"
#include "..\Key_and_LED.h"
#include <math.h>/*********************************************************************************************************宏定义
*********************************************************************************************************/
#define k_ADtoDB 1 //AD电压与分贝DB转换的系数
#define N_AD_aver 1 //读取多少个AD值然后取平均char GcRcvBuf[16]; /* 存放AD采集到的数据 */
//INT32U MAX=0,MIN=0;/*********************************************************************************************************
** Function name: myDelay
** Descriptions: 软件延时
*********************************************************************************************************/void myDelay (INT32U ulTime)
{INT32U i;i = 0;while (ulTime--){for(i = 0; i < 5000; i++);}
}
/*********************************************************************************************************
** ADtoDB
** 将AD到的电压转换成分贝值
*********************************************************************************************************/
void System_Start()
{Write_to_LCD(1," 噪声测量 ");str_empty(data2);str_empty(data4);Write_to_LCD(3," 2011电子竞赛 ");//myDelay(1500);// myDelay(1500);// str_empty(data2);// str_empty(data3);// str_empty(data4);Write_to_LCD(1," 噪声测量 ");Write_to_LCD(3,"噪声: dB");
}float ADtoDB()
{//ln()函数关系//int DB;//DB = 8.6525 *log(AD_value * k_ADtoDB + 0.01)+32.629; // Hz适用//DB = A_VALUE *log(AD_value * k_ADtoDB + 0.01)+TAIL_VALUE; // Hz适用//DB = 10 *log((MAX-MIN)+0.01)+14; // Hz适用DB = A_VALUE_now *log((MAX-MIN)+0.01)+TAIL_VALUE_now; // Hz适用return DB;
}INT32U OP_size = 1; //程控增益的放大倍数可选1,2,4,8; 初始选择1
void OP_control(int AD_pre) //用上一次的AD值来确定下一次可控增益的倍数
{INT32U size;if(AD_pre > 1600){size = 1;}else if(AD_pre > 800){size = 2;}else if(AD_pre > 400){size = 4;}else //AD_pre < 400{size = 8;}OP_size = size;
}
/*********************************************************************************************************
** Function name: main
** Descriptions: AD采集数据例程,P0.11采集。
** 注意:由于AD参考电压是3.3V,若直接采用P0.11采集电压,电压不得高于3.3V。
** input parameters: 无
** output parameters: 无
** Returned value: 无
*********************************************************************************************************/
float sumAD = 0; //
INT32U ulADCData; //转换为3300范围的瞬时值
int main (void)
{char LCD_item[16]="";INT32U TIME_TIP = 0;INT8U i_DB_temp=0;targetInit(); /* 初始化目标板,切勿删除 */pinInit(); /* 引脚初始化 */SYSAHBCLKCTRL |= (1ul << 6); /* 使能GPIO模块时钟 */ Ini_LCD_all();Warming_LED_Init();//TLC2543_Drive_INIT();System_Start();//uartInit(); /* 串口初始化 */ADCInit(); /* ADC模块初始化 */zyIsrSet(NVIC_ADC, (unsigned long)adc0Isr, PRIO_THREE);Key_init();//AD7822Int();//外部12位AD芯片初始化#if BURSTAD0CR |= 1ul << 16;#else AD0CR |= 1ul << 24;#endifADCFlag = 0;while(1){watch_key();if (ADCFlag == 1)//有AD数据{TIME_TIP++;ADCFlag = 0; /* 清零标志 *///i++;ulADCBuf = (ulADCBuf * 3300) / 1024; ulADCData = ulADCBuf;//while(!((OP_size == 1) && (ulADCData>1600)) )//程控增益 //后续增加{//可控增益到最大//OP_control(ulADCData);}{//sumAD = sumAD + (ulADCData - 1400)* (ulADCData -1400);//if (i == N_AD_aver){//i = 0;//sumAD = sqrt(sumAD / N_AD_aver);//sprintf(GcRcvBuf, "VIN0 = %4d mv ", ulADCBuf);//内部AD//iSendStr(GcRcvBuf); /* 将数据发送到串口显示 *///ulADCData = 0;if(TIME_TIP <50){if(MIN > ulADCBuf){MIN = ulADCBuf;}if(MAX < ulADCBuf){MAX = ulADCBuf;}ADtoDB();Warming_LED_Shine();if((DB < LOW_LIMIT) || (DB > HIGH_LIMIT)){;}else{sprintf(GcRcvBuf, "噪声:%4.2f dB", DB);//sprintf(GcRcvBuf, "噪声:%6d mV", MAX);Write_to_LCD(3,GcRcvBuf);if(MAX_DB < DB) MAX_DB = DB;if(getmax == 1){sprintf(LCD_item,"最大噪声%4.2fdB",MAX_DB);//显示振幅Write_to_LCD(4,LCD_item);}else if(show_amp == 1){i_DB_temp = i_DB+1;if(i_DB_temp == 4) i_DB_temp=0;Amp_temp = (MAX-MIN);sprintf(LCD_item," %1d次振幅:%4dmV",(i_DB_temp),Amp_temp);//显示振幅Write_to_LCD(4,LCD_item);if(Btn_1_dwon)//按下Btn_1{Correct_DB(Amp_temp);}}}}if(TIME_TIP == 50){MAX=0;MIN=3300;TIME_TIP = 0;}//LCD_Dly(5000); // 1s延时//myDelay(10);//sumAD =0;}#if BURST == 0AD0CR |= 1ul << 24;#endif}}}
}/*********************************************************************************************************End Of File
*********************************************************************************************************/
#ifndef LCD_H/*********************************************************************************************************宏定义
*********************************************************************************************************/
#define DATA 4 //P1.4号管脚
#define CLK 3 //P2.3号管脚
#define SID (1<<DATA) //数据管脚,R/W 1.4
#define SCLK (1<<CLK) //时钟管脚,E 2.3
#define SCKL_0 (GPIO2DATA &= ~(SCLK)) //SCLK = 0
#define SCKL_1 (GPIO2DATA |= SCLK) //SCLK = 1
#define SID_0 (GPIO1DATA &= ~(SID)) //SID = 0
#define SID_1 (GPIO1DATA |= SID) //SID = 1/*********************************************************************************************************全局变量
*********************************************************************************************************/
unsigned char data1[]={" "};
unsigned char data2[]={" "};
unsigned char data3[]={" "};
unsigned char data4[]={" "};const unsigned char data_empty[]={" "};/*********************************************************************************************************
** Function name: myDelay
** Descriptions: 软件延时
** input parameters: 无
** output parameters: 无
** Returned value: 无
*********************************************************************************************************/
void LCD_Dly (INT32U ulTime)
{unsigned int i,j;for(i=ulTime;i>0;i--)for(j=200;j>0;j--);
}void str_copy(unsigned char *str_dest, const unsigned char *str_source)
{int i = 0;for(i=0; i<16; i++){str_dest[i] = str_source[i];}
}
void str_empty(unsigned char *str)
{int i = 0;for(i=0; i<16; i++){str[i] = ' ';}
}
/***********************************************************************************************
** 函数名称:LCD_SendByte()
** 函数功能:发送一个字节
** 入口参数:bbyte 字节数据
** 出口参数:无
***********************************************************************************************/
void LCD_SendByte(unsigned char bbyte)
{unsigned char i;for(i=0;i<8;i++){if( (bbyte&0x80)!=0 ) SID_1;else {SID_0 ; //SID=bbyte&0x80;取出最高位}SCKL_1; //SCLK=1;LCD_Dly(1);//DELETESCKL_0; //SCLK=0;bbyte<<=1; //左移}
}
/***********************************************************************************************
** 函数名称:LCD_Write()
** 函数功能:LCD写指令或数据
** 入口参数:control_flow 数据控制流,0 为写指令 1 为写数据ddata 数据
** 出口参数:无
***********************************************************************************************/
void LCD_Write(unsigned char control_flow, unsigned char ddata)
{unsigned char start_data,Hdata,Ldata;if(0==control_flow) start_data=0xf8; //写指令else start_data=0xfa; //写数据Hdata=ddata&0xf0; //取高四位Ldata=(ddata<<4)&0xf0; //取低四位LCD_SendByte(start_data); //发送起始信号LCD_Dly(1); //延时是必须的LCD_SendByte(Hdata); //发送高四位LCD_Dly(1); //延时是必须的LCD_SendByte(Ldata); //发送低四位LCD_Dly(1); //延时是必须的
}void GPIO_LCD_init(void)
{GPIO1DIR = GPIO1DIR | SID;GPIO2DIR = GPIO2DIR | SCLK;
}/***********************************************************************************************
** 函数名称:Ini_all()
** 函数功能:初始化ARM管脚和LCD寄存器
** 入口参数:无
** 出口参数:无
***********************************************************************************************/
void Ini_LCD_all(void)
{ GPIO_LCD_init();LCD_Write(0,0x30); //8 位介面,基本指令集LCD_Write(0,0x0c); //显示打开,光标关,反白关LCD_Write(0,0x01); //清屏,将DDRAM的地址计数器归零 LCD_Write(0,0x01); //清屏LCD_Dly(10); //延时是为了等待处理完毕
}/***********************************************************************************************
** 函数名称:Write_to_LCD()
** 函数功能:在LCD上写字
** 入口参数:LCD的第几行(1~4),char *data 显示的数字的字符串首地址(字符串长度为16个ASCII)
** 出口参数:无
***********************************************************************************************/
void Write_to_LCD(int Which_Line,char *data)
{int i = 0;switch (Which_Line){case 1:{for(i=0; i<16; i++){data1[i] = data[i];}}break;case 2:{for(i=0; i<16; i++){data2[i] = data[i];}}break;case 3:{for(i=0; i<16; i++){data3[i] = data[i];}}break;case 4:{for(i=0; i<16; i++){data4[i] = data[i];}}break;}LCD_Write(0,0x80); //设置第一行显示地址000000LCD_Dly(10);for (i=0;i<16;i++)LCD_Write(1,data1[i]);LCD_Dly(10);LCD_Write(0,0x90); //设置第二行显示地址010000LCD_Dly(10);for (i=0;i<16;i++)LCD_Write(1,data2[i]);LCD_Dly(10);LCD_Write(0,0x88); //设置第三行显示地址001000LCD_Dly(10);for (i=0;i<16;i++)LCD_Write(1,data3[i]);LCD_Dly(10);LCD_Write(0,0x98); //设置第四行显示地址011000LCD_Dly(10);for (i=0;i<16;i++)LCD_Write(1,data4[i]);LCD_Dly(10);
}
#define LCD_H
#endif
//ARM_AD ,P0.11为AD输入引脚#ifndef ARM_AD_H#define BURST 0 /* AD BURST模式选择 */INT32U ADCFlag;
uint32 ulADCBuf=0;void Slow_1000ms()
{//N_AD_aver = ;
}
void Fast_125ms()
{}
/*********************************************************************************************************
** Function name: ADCInit
** Descriptions: ADC初始化
** input parameters: 无
** output parameters: 无
** Returned value: 无
*********************************************************************************************************/
void ADCInit (void)
{PDRUNCFG &= ~(0x01 << 4); /* ADC模块上电 */SYSAHBCLKCTRL |= (0x01 << 13); /* 使能ADC模块时钟 */IOCON_PIO0_11 &= ~0xBF; /* 配置PIO0_11为模拟输入模式 */IOCON_PIO0_11 |= 0x02; /* PIO0_11模拟输入通道0 */AD0CR = ( 0x01 << 0 ) | /* SEL=1,选择ADC0 */(( FAHBCLK / 880000 - 1 ) << 8 ) | /* 转换时钟880kHz */( 0 << 16 ) | /* BURST=1,使用Burst模式 */( 0 << 17 ) | /* 使用11 clocks转换 */( 0 << 24 ) | /* ADC转换停止 */( 0 << 27 ); /* 直接启动ADC转换,此位无效 */AD0INTEN = (1 << 0); /* 通道0中断使能 */
}
/*********************************************************************************************************
* Function Name: adc0Isr
* Description: ADC通道0中断处理函数
* Input: 无
* Output: 无
* Return: 无
*********************************************************************************************************/
void adc0Isr(void)
{ulADCBuf = AD0DR0; /* 读取通道0的值 */ulADCBuf = (ulADCBuf >> 6) & 0x3ff;ADCFlag = 1; /* 置标志位 */
}#define ARM_AD_H
#endif
#include <stdio.h>
#include <math.h>#define NO_BTN_DOWN -1#define Warming_LED (1<<9) //P0.9#define Btn_0 (1<<2) //P2.2
#define Btn_1 (1<<9) //p2.9
#define Btn_2 (1<<6) //p0.6
#define Btn_3 (1<<5) //p2.5#define Btn_0_dwon (0==(GPIO2DATA & Btn_0))
#define Btn_1_dwon (0==(GPIO2DATA & Btn_1))
#define Btn_2_dwon (0==(GPIO0DATA & Btn_2))
#define Btn_3_dwon (0==(GPIO2DATA & Btn_3))INT32U HIGH_LIMIT=100,LOW_LIMIT=40;//超限报警的上下限
float A_VALUE_now= 10, TAIL_VALUE_now=14; //log函数式的参数
float A_VALUE_L= 10, TAIL_VALUE_L=15; //log函数式的参数
float A_VALUE_H= 10, TAIL_VALUE_H=13; //log函数式的参数
float A_VALUE_A= 10, TAIL_VALUE_A=14; //log函数式的参数
float DB;
INT32U MAX=0,MIN=0;
INT8U show_amp= 0;
int i_DB=-1;//第几个校准数据,0~5共6个
INT8U DB_input[4]={39,39,39,39}; //输入的校准DB值
INT32U Amp[4] = {0,0,0,0}; //AD的振幅,=MAX - MIN
INT32U Amp_temp = 0;
INT8U getmax = 0;
float MAX_DB=0;void KeyDelay (INT32U ulTime)
{INT32U i;i = 0;while (ulTime--){for(i = 0; i < 5000; i++);}
}void Warming_LED_Init()
{GPIO0DIR = GPIO0DIR | Warming_LED;
}void Warming_LED_Shine()//或者换成 蜂鸣器
{static int Warming_LED_tip = 0; // tipstatic int aver_time=0;if(DB > HIGH_LIMIT)//超上限报警{aver_time = aver_time + 1;if (aver_time >10)//防止瞬时误报超限{Warming_LED_tip = 8;Write_to_LCD(3,"噪声: dB");Write_to_LCD(4," 分贝过高 ");}}else if(DB < LOW_LIMIT)//超下限报警{aver_time = aver_time + 1;if (aver_time >10){Warming_LED_tip = 2;Write_to_LCD(3,"噪声: dB");Write_to_LCD(4," 分贝过低 ");}}else{aver_time = 0;}//------------------------------------------------------if(Warming_LED_tip > 0){GPIO0DATA = GPIO0DATA | Warming_LED;//灯开}else if(Warming_LED_tip == 0)//亮灯x tip后停止{GPIO0DATA = GPIO0DATA & (~Warming_LED);//灯关Write_to_LCD(4," ");}//------------------------------------------------------if(Warming_LED_tip >= 0){Warming_LED_tip--;}
}//修改报警上下限
void KeyDown_deal_limit()
{char item[16]="";Write_to_LCD(1," 设置 ");Write_to_LCD(2," ");sprintf(item,"报警上限: %3d ",HIGH_LIMIT);Write_to_LCD(3,item);sprintf(item,"报警下限: %3d ",LOW_LIMIT);Write_to_LCD(4,item);while(1){if(0==(GPIO2DATA & Btn_0))//Btn_0按下{Write_to_LCD(1," 设置 ");Write_to_LCD(2," ");HIGH_LIMIT = (HIGH_LIMIT+1)%101;sprintf(item,"报警上限: %3d ",HIGH_LIMIT);Write_to_LCD(3,item);sprintf(item,"报警下限: %3d ",LOW_LIMIT);Write_to_LCD(4,item);}if(0==(GPIO2DATA & Btn_1))//Btn_1按下{Write_to_LCD(1," 设置 ");Write_to_LCD(2," ");sprintf(item,"报警上限: %3d ",HIGH_LIMIT);Write_to_LCD(3,item);LOW_LIMIT=(LOW_LIMIT+1)%101;sprintf(item,"报警下限: %3d ",LOW_LIMIT);Write_to_LCD(4,item);}if(0==(GPIO2DATA & Btn_3))//Btn_3按下{if(HIGH_LIMIT < LOW_LIMIT){Write_to_LCD(1," 错误 ");Write_to_LCD(2,"err:下限高于上限");Write_to_LCD(3,"请重新修改上下限");Write_to_LCD(4," ");}else{Write_to_LCD(1," 噪声测量 ");Write_to_LCD(2," ");Write_to_LCD(3," ");Write_to_LCD(4," ");KeyDelay(100);break;//return();}}}
}//高低分贝分段测试
void HighLow_test()
{INT8U HighLow_part=0;//0全段,1低段,2高段char LCD_item[16]="";Write_to_LCD(1," 分段设置 ");Write_to_LCD(2," ");Write_to_LCD(3," ");Write_to_LCD(4," ");while(1){if(0==(GPIO2DATA & Btn_3))//Btn_3按下,分段测试切换分段{KeyDelay(50);HighLow_part = (HighLow_part+1)%4;//段选择:0全段,1低段,2高段, 3原始}switch(HighLow_part){case 0:{A_VALUE_now = A_VALUE_A;TAIL_VALUE_now = TAIL_VALUE_A;Write_to_LCD(2,"当前段:全分贝段");sprintf(LCD_item," log系数:%2.2f ",A_VALUE_now);Write_to_LCD(3,LCD_item);sprintf(LCD_item,"尾加参数:%2.2f ",TAIL_VALUE_now);Write_to_LCD(4,LCD_item);break;}case 1:{A_VALUE_L = A_VALUE_A;TAIL_VALUE_L = TAIL_VALUE_A-0.5;A_VALUE_now = A_VALUE_L;TAIL_VALUE_now = TAIL_VALUE_L;Write_to_LCD(2,"当前段:低分贝段");sprintf(LCD_item," log系数:%2.2f ",A_VALUE_now);Write_to_LCD(3,LCD_item);sprintf(LCD_item,"尾加参数:%2.2f ",TAIL_VALUE_now);Write_to_LCD(4,LCD_item);break;}case 2:{A_VALUE_H = A_VALUE_A;TAIL_VALUE_H = TAIL_VALUE_A - 1.5;A_VALUE_now = A_VALUE_H;TAIL_VALUE_now = TAIL_VALUE_H;Write_to_LCD(2,"当前段:高分贝段");sprintf(LCD_item," log系数:%2.2f ",A_VALUE_now);Write_to_LCD(3,LCD_item);sprintf(LCD_item,"尾加参数:%2.2f ",TAIL_VALUE_now);Write_to_LCD(4,LCD_item);break;}case 3 :{A_VALUE_now = 10;TAIL_VALUE_now = 14;Write_to_LCD(2,"当前段: 预设 ");sprintf(LCD_item," log系数:%2.2f ",A_VALUE_now);Write_to_LCD(3,LCD_item);sprintf(LCD_item,"尾加参数:%2.2f ",TAIL_VALUE_now);Write_to_LCD(4,LCD_item);}default:{//return;}}if(0==(GPIO0DATA & Btn_2))//Btn_2按下,退出{Write_to_LCD(1," 噪声测量 ");Write_to_LCD(2," ");Write_to_LCD(3," ");Write_to_LCD(4," ");KeyDelay(100);break;}}
}
//校正分贝曲线
Correct_DB(INT32U Amp_temp)
{char LCD_item[16]="";float A_VALUE_temp[3]={0,0,0};Write_to_LCD(1," 校准 ");i_DB = (i_DB + 1) % 4;sprintf(LCD_item,"第 %1d个校准数据值",i_DB);Write_to_LCD(2,LCD_item);sprintf(LCD_item,"分贝校准:%3d dB",DB_input[i_DB]);Write_to_LCD(3,LCD_item);if(i_DB !=0 ) DB_input[i_DB] = DB_input[i_DB-1];sprintf(LCD_item,"振幅:%4d %4dmV",Amp[i_DB],Amp_temp);Write_to_LCD(4,LCD_item);while(1){if(Btn_1_dwon)//输入标准分贝值{KeyDelay(200);DB_input[i_DB] = (DB_input[i_DB] + 1) % 101;if(DB_input[i_DB] < 40){DB_input[i_DB] = 40;}sprintf(LCD_item,"分贝校准:%3d dB",DB_input[i_DB]);Write_to_LCD(3,LCD_item);}if(Btn_2_dwon)//当前幅值清0{Amp[i_DB] = Amp_temp;sprintf(LCD_item,"振幅:%4d %4dmV",Amp[i_DB],Amp_temp);Write_to_LCD(4,LCD_item);}if(Btn_3_dwon)//最终确定{A_VALUE_temp[0] = (DB_input[3] - DB_input[1]) / log(1.0*Amp[3]/Amp[1]*1.0);A_VALUE_temp[1] = (DB_input[2] - DB_input[0]) / log(1.0*Amp[2]/Amp[0]*1.0);A_VALUE_A = (A_VALUE_temp[0]+A_VALUE_temp[1])/2;TAIL_VALUE_A = DB_input[2] - A_VALUE_A*log(Amp[2]);sprintf(LCD_item," log系数:%2.2f ",A_VALUE_A);Write_to_LCD(3,LCD_item);sprintf(LCD_item,"尾加参数:%2.2f ",TAIL_VALUE_A);Write_to_LCD(4,LCD_item);A_VALUE_now = A_VALUE_A;TAIL_VALUE_now = TAIL_VALUE_A;}if(Btn_0_dwon)//退出{Write_to_LCD(1," 噪声测量 ");Write_to_LCD(2," ");Write_to_LCD(3," ");Write_to_LCD(4," ");KeyDelay(100);break;}}
}
//监控按键
void watch_key()
{if(0==(GPIO0DATA & Btn_2))//Btn_2按下{A_VALUE_now = 10;TAIL_VALUE_now = 14;HighLow_test();//分段测试}if(0==(GPIO2DATA & Btn_3))//Btn_3按下{KeyDelay(100);KeyDown_deal_limit();//进入上下限修改}if(0==(GPIO2DATA & Btn_0))//Btn_0按下{KeyDelay(100);show_amp= (show_amp+1)%2;// ==1显示振幅if(show_amp == 0){Write_to_LCD(4," ");}}if(0==(GPIO2DATA & Btn_1))//Btn_1按下, 锁定最大值{KeyDelay(200);getmax= (getmax+1)%2;if(getmax == 0){MAX_DB = 0;Write_to_LCD(4," ");}}
}void Key_init()
{GPIO2DIR = GPIO2DIR & (~Btn_0);GPIO2DIR = GPIO2DIR & (~Btn_1);GPIO0DIR = GPIO0DIR & (~Btn_2);GPIO2DIR = GPIO2DIR & (~Btn_3);/* btn[0].pFun_Button = Btn0_deal_fun;btn[1].pFun_Button = Btn1_deal_fun;btn[2].pFun_Button = Btn2_deal_fun;btn[3].pFun_Button = Btn3_deal_fun; */
}
#ifndef __AD7822_H
#define __AD7822_H#define OE (1ul << 7) //P2.7片选
#define OE_OFF() GPIO2DATA |= OE /* 高,关 */
#define OE_ON() GPIO2DATA &= ~OE /* 低,开 */#define CLK (1ul << 8) //P2.8时钟
#define CLKON() GPIO2DATA |= CLK /* 高 */
#define CLKOFF() GPIO2DATA &= ~CLK /* 低 */#define AD_bit (1<<3)
#define newBit ((GPIO0DATA & AD_bit) >>3) //P0.3读入AD的12位串行数据,newBit为最新独到的 1 bit数据
#define NUM_AD 128 //连续AD的次数,然后求平均值INT32U AD_value[NUM_AD] = {0}; //存储AD结果
INT32U This_AD = 0; //当前AD是NUM_AD的第几次ADvoid GPIO_AD_INIT()
{GPIO2DIR |= OE;GPIO2DIR |= CLK;GPIO0DIR &= ~(AD_bit);}
void AD7822Int(void)
{GPIO_AD_INIT();OE_OFF(); //片选关CLKOFF(); //时钟低
}void Read_AD_value(void)
{int i=0;OE_ON(); //片选开for(i=0; i<3; i++) //每次读取数据前的3个空时钟周期{CLKON(); //时钟高CLKOFF(); //时钟低}for(i=0; i<12; i++) //读取12位串行结果{CLKON(); //时钟高CLKOFF();//时钟低,完成下降沿AD_value[This_AD] = (AD_value[This_AD] << 1);AD_value[This_AD] = AD_value[This_AD] | newBit; //读一位数据}AD_value[This_AD] = AD_value[This_AD] / 4096 *5000;OE_OFF(); //片选关This_AD = (This_AD + 1) % NUM_AD;
}#endif
/*********************************************************************************************************宏定义和全局变量
*********************************************************************************************************/
//uint32 ad[200];/*********************************************************************************
宏
*********************************************************************************/
#define DATAIN (1ul << 2) //3.2
#define DATAIN_INIT() GPIO3DIR |= DATAIN
#define DATAINOFF() GPIO3DATA |= DATAIN
#define DATAINON() GPIO3DATA &= ~DATAIN#define DATAOUT (1ul << 5) //1.5
#define DATAOUT_INIT() GPIO1DIR &= ~ DATAOUT
#define DATAOUTOFF() GPIO1DATA |= DATAOUT
#define DATAOUTON() GPIO1DATA &= ~DATAOUT#define OE (1ul << 7) //1.7
#define OE_INIT() GPIO1DIR |= OE
#define OEOFF() GPIO1DATA |= OE
#define OEON() GPIO1DATA &= ~OE#define CLK_2543 (1ul << 6) //2.6
#define CLK_2543_INIT() GPIO2DIR |= CLK_2543
#define CLK_2543OFF() GPIO2DATA |= CLK_2543
#define CLK_2543ON() GPIO2DATA &= ~CLK_2543void myDelay2(int i)
{for(i=0; i<5000;i++);}
/*********************************************************************************************************
** Function name: timer0Init
** Descriptions: 16位定时器0初始化函数
** input parameters: 无
** output parameters: 无
** Returned value: 无
*********************************************************************************************************/
INT32U read2543()
{uint32 ad1=0;INT32U i=0;OEON();//先低电平 CLK_2543ON();//先低电平 for(i=0;i<12;i++){if((GPIO2DATA & DATAOUT)) ad1|=0x01; DATAINON();CLK_2543OFF(); myDelay2(1);CLK_2543ON(); myDelay2(1);ad1<<=1;}OEOFF();ad1>>=1;return(ad1);
}
void TLC2543_Drive_INIT(void)
{SYSAHBCLKCTRL |= (1ul << 6); /* 使能GPIO模块时钟 */DATAIN_INIT() ;DATAOUT_INIT();OE_INIT();OEOFF();CLK_2543_INIT();
}
这篇关于噪声测量仪 源代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!