中级会员
- 积分
- 379
- 威望
- 205
- 贡献
- 114
- 兑换币
- 126
- 注册时间
- 2016-10-20
- 在线时间
- 30 小时
|
//**********AD采样**********//
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
unsigned int read_data[4]={0}; //采样变量
const byte LedData[18]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6,0xee,0x3e,0x9c,0x7a,0x9e,0x8e,0xce,0x6e} ;
/* 0 1 2 3 4 5 6 7 8 9 A B C D E F P H */
const byte Error[5] = {0xee,0xfc,0xee,0xee,0x9e};
//==============================================================
void delay(void)
{
unsigned int loop_i, loop_j;
for (loop_i=0; loop_i< 500; loop_i++)
{
for (loop_j=0; loop_j<500;loop_j++)
{
;
}
}
}
//=============================================================================
void spiDELAY(void)
{
unsigned int loop_i;
for (loop_i=0; loop_i< 100; loop_i++)
{
;
}
}
//=============================================================================
void spiWRITEbyte(unsigned int spiDATA){
unsigned int i =0;
for(i;i<8;i++){
PTP_PTP2 =0;
spiDELAY();
if(spiDATA%2){
PTP_PTP1=1;
}
else {
PTP_PTP1=0;
}
spiDELAY();
spiDATA = spiDATA/2;
PTP_PTP2 = 1;
spiDELAY();
}
PTP_PTP2 = 1;
}
//=============================================================================
void ledCONTROL(dword displayDATA){
unsigned int spiBYTE01,spiBYTE02,spiBYTE03,spiBYTE04,spiBYTE05;
spiBYTE05 = displayDATA /10000; //得到万位
spiBYTE04 = displayDATA /1000 - displayDATA /10000 *10 ; //获得千位
spiBYTE03 = displayDATA /100 - displayDATA /1000 *10 ; //获得百位
spiBYTE02 = displayDATA /10 - displayDATA /100 *10 ; //获得个位
spiBYTE01 = displayDATA % 10 ; //获得个位
// spiWRITEbyte(spiDATA);
spiWRITEbyte(LedData[spiBYTE01]);
spiWRITEbyte(LedData[spiBYTE02]);
spiWRITEbyte(LedData[spiBYTE03]);
spiWRITEbyte(LedData[spiBYTE04]);
spiWRITEbyte(LedData[spiBYTE05]);
}
//---------------------------------------------------------
void xianshi(void){
unsigned int i,spiDATA,spiLENGTH;
DDRP_DDRP1 =1;
DDRP_DDRP2 =1;
DDRK_BIT1 =1;
PORTK_BIT1 =1;
PTP_PTP1 =0;
PTP_PTP2 =0;
spiDATA = 0xFC ; //显示0
spiLENGTH = 8;
//显示5个0.
for(i=0;i<6;i++) {
spiWRITEbyte(spiDATA);
}
}
//---------------------------------------------------------
void ad_init(void){
ATD0CTL2 =0x80;
ATD0CTL3 =0x40; //atd0
ATD0CTL4 =0x81;
ATD1CTL2 =0x80;
ATD1CTL3 =0x40; //atd1
ATD1CTL4 =0x81;
// ATD0CTL5 = 0xA6; //连续转换 //通用模拟信号通道一
// ATD0CTL5 = 0x86; //单次转换
// ATD1CTL5 = 0xA6; //连续转换 //通用模拟信号通道二
// ATD1CTL5 = 0x86; //单次转换
// ATD1CTL5 = 0xA7; //连续转换 //光敏采样输入
// ATD0CTL5 = 0x87; //单次转换
// ATD0CTL5 = 0xA7; //连续转换 //TMP36温度测量
// ATD0CTL5 = 0x87; //单次转换
}
//------------------------------------------------------------------------------
void data_get(){
ATD1CTL5 = 0xA6;
while(!ATD1STAT0_SCF==1);
read_data[0]=ATD1DR0L; //电位器AD1旋钮 AN14
ATD1STAT0_SCF=1;
ATD0CTL5 = 0xA6;
while(!ATD0STAT0_SCF==1);
read_data[1]=ATD0DR0L; //电位器AD2旋钮 AN6
ATD0STAT0_SCF=1;
ATD0CTL5 = 0xA7; //光敏采样输入 AN7
while(!ATD0STAT0_SCF==1);
read_data[2]=ATD0DR0L;
ATD0STAT0_SCF=1;
ATD1CTL5 = 0xA7; //温度采样输入 AN15
while(!ATD1STAT0_SCF==1);
read_data[3]=ATD1DR0L;
ATD1STAT0_SCF=1;
}
void main(void)
{
ad_init();
xianshi();
EnableInterrupts;
for(;;)
{
data_get();
ledCONTROL(read_data[3]);
delay();
_FEED_COP();
}
}
|
|