?? ft_water.c
字號:
#include <msp430xw42x.h>
#include <math.h>
#define uchar unsigned char
//#define PSM_Version_UG // PSM state machine from the User's Guide is used.
#define PSM_Version_Advanced // advanced PSM state machine is used.
#define __msp430xW42x
//const unsigned char seg[]={0xeb,0x0a,0xc7,0xe5,0x6c,0xad,0xaf,0xe0,0xef,0xed,0xee,0x2f,0x8b,0x67,0x8f,0x8e};//段碼表
const unsigned char seg[]={0x7d,0x05,0x3e,0x7a,0x63,0x5b,0x5f,0x70,0x7f,0x7b};
#ifdef PSM_Version_UG
/*** Processing State Machine of the Scan Interface module description ***/
const unsigned char PSM[] = {
0x00, // no rotation
0x03, // turns right (+1)
0x0C, // turns left (-1)
0x49, // error
0x00, // turns left
0x01, // no rotation
0x48, // error
0x09, // turns right
0x00, // turns right
0x41, // error
0x08, // no rotation
0x09, // turns left
0x40, // error
0x01, // turns left
0x08, // turns right
0x09 // no rotation
};
#endif
#ifdef PSM_Version_Advanced
/*** Processing State Machine (advanced version) ***/
const unsigned char PSM[] = {
0x00, // state 0
0x13, // state 1
0x0C, // state 2
0x4D, // state 3
0x04, // state 4
0x01, // state 5
0x4C, // state 6
0x5B, // state 7
0x52, // state 8
0x45, // state 9
0x08, // state 10
0x0D, // state 11
0x44, // state 12
0x05, // state 13
0x5A, // state 14
0x09, // state 15
0x10, // state 16
0x13, // state 17
0x4C, // state 18
0x5B, // state 19
0x44, // state 20
0x11, // state 21
0x5A, // state 22
0x1B, // state 23
0x12, // state 24
0x53, // state 25
0x18, // state 26
0x4D, // state 27
0x52, // state 28
0x45, // state 29
0x1A, // state 30
0x19 // state 31
};
#endif
const unsigned long hanzhi[]={0,478,899,1321,1741,2162,2582,3002,3422,3841,4261,4680,5099,5518,5967,6355,6774,7193,7611,8029,8448,8866,9284,9702,10120,10538,10956,11374,11792,12210,12628,13046,13463,13881,14299,14717,15135,15552,15970,16388,16806,17224,17641,18059,18477,18895,19313,19731,20149,20567,20985,21403,21821,22239,22657,23075,23494,23912,24330,24748,25167,25585,26004,26422,26841,27259,27678,28097,28515,28934,29353,29772,30191,30610,31029,31448,31868,32287,32706,33126,33545,33965,34385,34804,35224,35644,36064,36484,36904,37325,37745,38165,38584,39007,39427,39848,40269,40690,41111,41533,41954};
const unsigned long midu[]={0,10002,10002,10002,10002,10002,10002,10001,10001,10000,9999,9998,9997,9996,9995,9993,9992,9990,9988,9986,9984,9982,9980,9978,9975,9973,9970,9967,9965,9962,9959,9956,9953,9949,9946,9943,9939,9936,9932,9928,9924,9921,9917,9913,9909,9904,9900,9896,9891,9887,9883,9878,9873,9869,9864,9859,9854,9849,9844,9839,9834,9829,9824,9818,9813,9808,9802,9797,9791,9786,9780,9774,9768,9763,9757,9751,9745,9739,9733,9726,9720,9714,9708,9701,9695,9688,9682,9675,9669,9662,9655,9649,9642,9635,9628,9621,9614,9607,9600,9593,9586};
// 100 500 1000 1500 2000 2500 3000
const unsigned int water_pulse[]={385,385,387,389,391,392,395,398,401,403,406,406,406,407,407,408,408,409,409,410,410,410,410,411,411,411,411,411,411,411,411};
unsigned char dis_buff[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};//顯示緩存單元
unsigned char kset_v=0,kup_v=0,kdown_v=0,flag_ca=0;//鍵值緩存
unsigned long ljrl_30s=0,ljll,ljll_30s=0,ll=0,lj,counter=0,bz=0,bz1=0,bz2=0,js=0,hs=0,js1=0,hs1=0,js_hz=0,hs_hz=0,hz_cha=0,js_wendu=0,hs_wendu=0;
no_init unsigned long ljrl,ljrl_3,ljll_5;
no_init unsigned int flag_qlinit;
char write_buf[12];
char read_buf[12];
unsigned char halfsec=0,minitue=0,hour=0,date=1,month=1,year=4,col=0,lljy_jishu=0,sec30s=0,sec10s=0,sec20s=0,sifcount=0,siftp=0,siftp2=0,siftp1=255,siftp3=0;
unsigned int cap_reg=0,cap_reg1=0,cap_reg2=0,bz_cha=0,js_cha=0,hs_cha=0,counter1=0,siftp4=0,countertp=0,dacr_l1=0,dacr_l2=0,dacl1_low=0,dacl1_high=0,dacl2_low=0,dacl2_high=0,dacl1_mid=0,dacl2_mid=0,pt_js0=0,pt_js50=0,pt_js100=0,pt_hs0=0,pt_hs50=0,pt_hs100=0,js_ad=0,hs_ad=0;
double pt_ajs=0,pt_bjs=0,pt_ad0js=0,pt_ad50js=0,pt_ad100js=0,wendu_js=0;//進水參數
double pt_ahs=0,pt_bhs=0,pt_ad0hs=0,pt_ad50hs=0,pt_ad100hs=0,wendu_hs=0;//回水參數
unsigned int dacr_l1l2[]={872,692,782,602,827,737,647,557,849,804,760,715,669,625,580,860,838,815,793,771,749,726,704,681,658,636,613,591,569,866,854,844,832,821,809,799,788,777,766,754,743,732,721,709,698,687,675,664,653,642,631,619,608,597,586,575,563,551,545,539,880,886,892,898,904,910};
unsigned int cntlow_intl1=0,cnthigh_intl1=0,cntlow_intl2=0,cnthigh_intl2=0,cntlowl1_int=0,cntlowl2_int=0;
unsigned char lowl1_int2=0,highl1_int2=0,lowl1_int10=0,highl1_int10=0,lowl2_int2=0,highl2_int2=0,lowl2_int10=0,highl2_int10=0;
unsigned char flag_l1mid=0,flag_l2mid=0,flag_key5s=0,cnt_key5s=0,flag_10sll=0,cnt_jycishu=0,flag_intl1=0,flag_intl2=0,flag_dac=0,flag_l1=0,flag_l2=0,flag_l1h=0,flag_l2h=0,cnt_l1mid,cnt_l2mid;
void delay(unsigned int v);
void delay(unsigned int v)
{
while(v!=0)v--;
}
unsigned char keycode(void)//鍵值子程序
{
unsigned char x;
x=(P1IN&0x10);
return(x);
}
#pragma vector=PORT1_VECTOR
__interrupt void PORT1(void)//P1中斷
{
if((P1IN&0x10)==0)
{
delay(1000);
if((P1IN&0x10)==0)
{
kset_v++;
if(kset_v>2)
{
kset_v=0;
}
}
}
P1IFG&=~BIT4;
LPM3_EXIT;
}
unsigned long disp_int(unsigned long ljtemp)//主顯拆分子程序
{
unsigned char ljjm;
ljjm=ljtemp%10;
dis_buff[7]=seg[ljjm];
ljtemp=ljtemp/10;
ljjm=ljtemp%10;
dis_buff[6]=seg[ljjm];
ljtemp=ljtemp/10;
ljjm=ljtemp%10;
dis_buff[5]=seg[ljjm];
ljtemp=ljtemp/10;
ljjm=ljtemp%10;
dis_buff[4]=seg[ljjm];
ljtemp=ljtemp/10;
ljjm=ljtemp%10;
dis_buff[3]=seg[ljjm];
ljtemp=ljtemp/10;
ljjm=ljtemp%10;
dis_buff[2]=seg[ljjm];
ljtemp=ljtemp/10;
ljjm=ljtemp%10;
dis_buff[1]=seg[ljjm];
ljtemp=ljtemp/10;
ljjm=ljtemp%10;
dis_buff[0]=seg[ljjm];
if(dis_buff[0]==0x7d)
{
dis_buff[0]=0x00;
if(dis_buff[1]==0x7d)
{
dis_buff[1]=0x00;
if(dis_buff[2]==0x7d)
{
dis_buff[2]=0x00;
if(dis_buff[3]==0x7d)
{
dis_buff[3]=0x00;
if(dis_buff[4]==0x7d)
{
dis_buff[4]=0x00;
}
}
}
}
}
return(0);
}
void write_Flash(char *addr,char *buf,int len)
{
unsigned int cnt;
FCTL2=FWKEY+FSSEL0+FN0;
FCTL1=FWKEY+ERASE;
FCTL3=FWKEY;
*addr=0;
FCTL1=FWKEY+WRT;
for(cnt=0;cnt<len;cnt++)
{
*(addr+cnt)=*(buf+cnt);
}
FCTL1=FWKEY;
FCTL3=FWKEY+LOCK;
}
void read_Flash(char *addr,char *rbuf,int len)
{
unsigned int cnt;
for(cnt=0;cnt<len;cnt++)
{
*(rbuf+cnt)=*(addr+cnt);
}
FCTL3=FWKEY+LOCK;
}
#pragma vector=TIMER1_A0_VECTOR
__interrupt void Timer_A1(void)//TIMER1定時0.5S中斷
{
halfsec=halfsec+1;
sec30s=sec30s+1;
sec10s=sec10s+1;
sec20s+=1;
col^=1;
if((P1IN&0x10)==0)
{
cnt_key5s+=1;
if(cnt_key5s>8)
{
if(flag_qlinit!=1)
{
ljrl=0;
ljll=0;
lj=0;
ljrl_3=0;
ljll_5=0;
flag_qlinit=1;
}
}
}
else
{
cnt_key5s=0;
}
if(halfsec==120)
{
halfsec=0;
minitue=minitue+1;
if(minitue==60)
{
minitue=0;
hour=hour+1;
if(hour==24)
{
hour=0;
}
}
}
LPM3_EXIT;
}
#pragma vector=TIMER0_A1_VECTOR
__interrupt void Timer_A0(void)
{
switch(TAIV)
{
case 2:LPM0_EXIT;break;
case 4:break;
case 10:LPM0_EXIT;break;
default:break;
}
}
#pragma vector=SCANIF_VECTOR
__interrupt void SCAN_IF(void)
{
// SIFCTL1&=~0x0060;
// LPM3_EXIT;
siftp+=1;
// siftp2+=1;
siftp3+=1;
siftp4+=1;
if (SIFCTL3 & 0x01)
{
if(flag_intl1==0)
cnthigh_intl1+=1;
flag_intl1=1;
}
else
{
if(flag_intl1==1)
cntlow_intl1+=1;
flag_intl1=0;
}
if (SIFCTL3 & 0x02)
{
if(flag_intl2==0)
cnthigh_intl2+=1;
flag_intl2=1;
}
else
{
if(flag_intl2==1)
cntlow_intl2+=1;
flag_intl2=0;
}
siftp1=SIFCNT-sifcount;
if(siftp1==4)
{
counter+=1;
counter1+=1;
siftp3=0;
sifcount=SIFCNT;
}
SIFCTL1&=~0x0060;
}
void main(void)
{ int i;
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
// SCFI0=FN_2;
FLL_CTL0=XCAP14PF;
// SCFQCTL=74;
TA1CTL=TASSEL0+TACLR; //timera1 initial
TA1CCTL0=CCIE;
TA1CCR0=16384;
TA1CTL|=MC0;
P1DIR=0x25;
P1IE|=BIT4;
P1IES|=BIT4;
P1OUT&=~BIT2;
P2DIR=0x7e;
P2SEL=0x40;
P2OUT=0X00;
P2OUT|=BIT2;
P2SEL |= 0x80; // P2.7 => SIFCLKG
// P2OUT=0X00;
// P2OUT&=~BIT1;
delay(500);
P6DIR=0xff;
P6OUT&=~BIT6;
P6SEL |= 0x03;
// countertp=SIFCNT;
// Initialize Timing State Machine
SIFTSM0 = 0x8800;
SIFTSM1 = 0x002C; // DAC=off, CA=off, 1xSIFCLK, excitation SIFCH.0
SIFTSM2 = 0x0c24; // DAC=off, CA=off, 1xACLK SIFCH.0
SIFTSM3 = 0x2934; // DAC=on, CA=on, 4xSIFCLK SIFCH.0
SIFTSM4 = 0x4934; // DAC=on, CA=on, 14xSIFCLK SIFCH.0
SIFTSM5 = 0x2974; // DAC=on, CA=on, 14xSIFCLK SIFCH.0
SIFTSM6 = 0x002D; // DAC=off, CA=off, 1xSIFCLK, excitation SIFCH.1
SIFTSM7 = 0x0c25; // DAC=off, CA=off, 1xACLK SIFCH.1
SIFTSM8 = 0x2935; // DAC=on, CA=on, 4xSIFCLK SIFCH.1
SIFTSM9 = 0x4935; // DAC=on, CA=on, 14xSIFCLK SIFCH.1
SIFTSM10 =0x2975; // DAC=on, CA=on, 14xSIFCLK SIFCH.1
SIFTSM11 = 0x0220; // stop
// Initialize DAC digital control values
SIFDACR0 = 0x0200; // SIFCH.0 - set to maximum value
SIFDACR1 = 0x0200;
SIFDACR2 = 0x0200; // SIFCH.1 - set to maximum value
SIFDACR3 = 0x0200;
// Initialize Scan Interface Control Registers
SIFCTL2 = 0x0140; // DAC and comparator are controlled by SIFTSMx
for (i=0;i<=857;i++); // wait loop. SIFVCCEx has to settle
SIFCTL3 = 0x4000; // SIF0OUT selected for S1, SIF1OUT selected for S2
// (S1 and S2 are input signals of Processing State Machine)
SIFCTL4 = 0x30f0; // initialize PSM counters
SIFCTL5 = 0x0045; // SIFCLK is controlled by SIFTSMx
// SIFCLK = 1MHz
SIFPSMV = (unsigned int) &PSM; // Definition of PSM vector
SIFCTL1 |= 0x1001; // switch on Scan Interface. enable interrupt
sifcount=SIFCNT;
P2OUT&=~BIT1;
delay(1000);
read_Flash((char*)0x1000,read_buf,5);
dacr_l1=read_buf[0];
dacr_l1=(dacr_l1<<8);
dacr_l1|=read_buf[1];
dacr_l2=read_buf[2];
dacr_l2=(dacr_l2<<8);
dacr_l2|=read_buf[3];
if(read_buf[4]==1)
{
flag_dac=15;
SIFDACR0=dacr_l1+2;
SIFDACR1=dacr_l1-2;
SIFDACR2=dacr_l2+2;
SIFDACR3=dacr_l2-2;
}
_EINT();
while(1)
{
WDTCTL =WDT_ARST_1000;
if(sec10s>19)
{
i=1;
// counter+=SIFCNT;
xhh:if(counter>(water_pulse[i]*i/72))
{
if(i>29)
{
goto xhh1;
}
else
{
i++;
goto xhh;
}
}
xhh1: ll=(counter*720/water_pulse[i]);
ljll_30s=(counter*2000/water_pulse[i]);
ljll_5+=ljll_30s;
ljll=ljll_5/1000;
counter=0;
// cntlowl1_int=0;
// cntlowl2_int=0;
flag_ca=0;
sec10s=0;
}
if(flag_dac==0)
{
if(sec30s>5)
{
if(cntlow_intl1<2)
{
if(flag_l1==0)
{
SIFDACR0=dacr_l1l2[cnt_l1mid];
SIFDACR1=dacr_l1l2[cnt_l1mid];
cnt_l1mid+=1;
cnt_jycishu+=1;
if(cnt_l1mid>64)
{
cnt_l1mid=0;
cnt_jycishu=0;
flag_dac=15;
SIFDACR0=dacr_l1+2;
SIFDACR1=dacr_l1-2;
SIFDACR2=dacr_l2+2;
SIFDACR3=dacr_l2-2;
}
else
{
siftp=0;
siftp2=siftp;
cntlow_intl1=0;
cntlow_intl2=0;
cnthigh_intl1=0;
cnthigh_intl2=0;
dacl1_mid=SIFDACR0;
sec30s=0;
sec20s=0;
sec10s=0;
}
}
else
{
SIFDACR0+=2;
SIFDACR1+=2;
dacl1_low=SIFDACR0;
flag_l1|=0x03;
// flag_dac|=0x01;
siftp=0;
siftp2=siftp;
cntlow_intl1=0;
cntlow_intl2=0;
cnthigh_intl1=0;
cnthigh_intl2=0;
cnt_jycishu+=1;
lowl1_int2+=1;
if(lowl1_int2>15)
{
lowl1_int2=0;
lowl1_int10=0;
lowl2_int2=0;
lowl2_int10=0;
highl1_int2=0;
highl1_int10=0;
highl2_int2=0;
highl2_int10=0;
flag_dac=0;
flag_l1=0;
flag_l2=0;
flag_l1h=0;
flag_l2h=0;
cnt_l1mid=0;
cnt_l2mid=0;
flag_10sll=0;
siftp=0;
siftp2=0;
cnt_jycishu=0;
flag_dac=15;
SIFDACR0=dacr_l1+2;
SIFDACR1=dacr_l1-2;
SIFDACR2=dacr_l2+2;
SIFDACR3=dacr_l2-2;
}
sec30s=0;
sec20s=0;
sec10s=0;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -