- // 20190505 reprogram display dspLine1[] dspLine2[]
- // 20190507 RS232 DATA read
- // 20190511 fine tune program
- # include <EEPROM.h>
- //Define 74HC595 Connections with arduio
- const int Latch=5; //BLACK
- const int Data=6;//GREEN
- const int Clock=7;//BLUE
- char V2_Array[5];
- //char Addrmodule[3]= "ad";//----------------------0503
- int oldstringlength;
- int KS; // key switch press
- int KSC;
- byte Krs=0,Kls=0,Kdec=0,Kinc=0,Kenter=0,Ksel =0;
- boolean Khold = false;
- byte oldStringlength;
- const int keyport = A0;
- byte oldcaseB = 0 ;
- byte caseB;
- byte caseC;
- byte oldcaseC;
- byte oldCase =0;
- byte caseA = 0;
- int incB = 0;
- int incB_old = 1;
- int chr15;
- //int Set_value =0;
- //int Set_valueA=0;
- char moduleAddr[2];//--------------0503
- String OutPrintStr;
- //-------------------------------------------------
- struct CMD_indicate {
- char *sel_No;
- char *name;
- boolean mod;//enter data need...
- };
- //------------------20190505------------------------------
- char *dspLine1;//total display matrix line1 不能使用 dspLine1[24] 會異常
- char *dspLine2;
- int incC=0;
- char *sel_No_buf;//use store for display dspLine2
- struct CMD_indicate caseAs[] =
- {
- {"%AANNTTCCFF","Set Configure ",1},//0
- {"@AAHI+00.000","Set Hi Alarm ",1},//11
- {"@AALO+00.000","Set Lo Alarm ",1},//12
- {" AANN ","Change Addr AA new=NN",1},//13
- {"#AA","Read input data",0},//1
- {"$AA0","Span calibrate ",0},
- {"$AA1","Offset calibrate ",0},
- {"@AAEAT","Enable Alarm",0},
- {"@AADA","disable Alarm",0},//5
- {"@AACA","clear Latch alarm",0},
- {"@AARH","Read Hi Alarm ",0},
- {"@AARL","Read Lo Alarm ",0},
- {"@AARE","Read Event count",0},
- {"@AACE","clear Event count ",0},//10
- {"$AA2","Read Config para ",0},//14
- {"@AADI","Read digit I/O status",0}//15
- };
- char Num_hex[]= {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
- int Num_hexNo0 = 0 ;
- int Num_hexNo1 = 0 ;
- char Set_AA[2];
- char Set_NN[2];
- void LCDinit(void);
- void LCDPrint(char Line,char string[24]);
- void LCDPrintString(int x,int y, char string[]);
- void LCDWriteChar(int x,int y,char Data);
- void LCDCursorMove(int y,int x);
- byte Keyscan(void);
- void dispCase (int caseA);
- void dispCaseB(void);
- boolean level1 =0;
- boolean level2 =0;
- boolean OK_flag = false;//when OK key is pressed
- boolean level3 = 0;
- boolean Set_value_sign = false;
- unsigned long startTime = 0;
- int Maxtime = 3000;
- //-----------20190507 RS232 receive ----------------
- char *msgbuf ="HI HOW ARE YOU"; // buff of rs232 read in from PC or ADAM 4012
- int strleng;//read in string length
- boolean read_OK_flag;
- /*
- 把字串轉成陣列
- 把陣列轉成字串
- 讀取RS232送過來的字串存到陣列裡面以'\n'為結束
- */
- void RS232read()
- {
- int k=0;
- byte chr;
- if (Serial.available() > 0)
- {
- while((chr = Serial.read()) != '\n')
- {
- msgbuf[k]=chr;
- k++;
- }
- }
- strleng = k;
- }
- // load structure data to buff
- // replace to buff AA to 02
- // set index to 6
- // modify index 6[0] to index6[2]
- //re display
- // incB chr15 Num_hex[posi]
- //int posi;//position of dspLine2[]
- void CleanLine1(void)
- {
- LCDPrint(1," ");//0505 clean over data
- }
- void CleanLine0(void)
- {
- LCDPrint(0," ");//0505 clean over data
- }
- //------------------------------------------------
- void setup() {
- moduleAddr[0] = EEPROM.read(0);
- moduleAddr[1] = EEPROM.read(1);
- pinMode(Data, OUTPUT);
- pinMode(Clock, OUTPUT);
- pinMode(Latch, OUTPUT);
- Serial.begin(9600); // start the serial monitor link
- LCDinit(); //initalize LCD in 4-bit mode
- LCDPrint(0,"WELCOME ADAM-4012-485 ");
- LCDPrint(1," 4-20mA TO RS485 --2019 ");
- // Serial.println("#02");
- }
- void loop() {
- delay(2000);
- // Serial.println("write to --------1->>> ");// -----------NANO-
- LCDclean();
- LCDPrint(0," Read Data #02 per 3 Sec");
- startTime = millis();
- level1 = true;
- // Serial.println(startTime);//--------------NANO
- while(level1){ //------------------------------------------level 1
- if(millis() - startTime > Maxtime )//timer 1sec
- {
- Serial.println("#02");// send out for inquire data
- startTime = millis();
- delay(200);
- RS232read();
- // if(msgbuf[0] =='>' || msgbuf[0] =='!' )
- {
- CleanLine1();
- String Annn = String (msgbuf);
- Annn.substring(0,9);
- Annn.toCharArray(msgbuf,9);
- LCDPrint(1,msgbuf);//---------------------
- }
- }
- caseA = Keyscan();
- if (caseA != oldCase)
- {
- dispCase(caseA);
- oldCase = caseA;
- }
- switch (caseA)
- {
- case 1://next
- LCDPrint(0," Main Function ");
- LCDPrint(1," < key no function ");
- break;
- case 2://previous
- LCDPrint(0," Main Function ");
- LCDPrint(1," > key no function ");
- break;
- case 4://up
- level2 = true;
- if(OK_flag)
- {
- LCDPrint(0,"OK was pressed! ");
- OK_flag = false;
- }
- else
- {
- LCDPrint(1," ");
- }
- while(level2) //---------------------------------------level 2
- {
- caseB = Keyscan();
- if(caseB != oldcaseB)
- {
- dispCaseB();//display structure caseAs[] content
- oldcaseB = caseB;//not more key same character
- }
- switch (caseB)
- {
- case 1:
- incB ++;
- if((incB <0) ||(incB >16))
- {incB = 16;}
- caseB_stepDisp();
- CleanLine1();
- break;
- case 2:
- incB --;
- if((incB <0) ||(incB >16))
- {incB = 0;}
- caseB_stepDisp();
- CleanLine1();
- break;
- case 4:
- incB ++;
- if((incB <0) ||(incB >16))
- {incB = 16;}
- caseB_stepDisp(); //display step
- CleanLine1();
- break;
- case 8://ok process
- dispCaseB();//display structure data on LCM
- LCDWriteChar(0,22,'o');
- OK_process();//-------------------------------------Level 3
- delay(200);
- RS232read();
- // Serial.print("strleng = ");
- // Serial.println(strleng);
- // if(msgbuf[0] =='>' || msgbuf[0] =='!' )
- {
- CleanLine1();
- // LCDPrintString(1,3,msgbuf);//0511
- // Serial.print("strleng= ");
- // Serial.println(strleng);
- LCDWriteChar(1,20,(strleng/10+48));//list string length
- LCDWriteChar(1,21,(strleng%10+48));
- read_OK_flag = true;
- }
- break;
- case 16:
- incB --;
- if((incB <0) ||(incB >16))
- {incB = 0;}
- //LCDPrintString(0,17,"+");//clean some digit
- caseB_stepDisp();
- CleanLine1();
- break;
- case 32:
- level2 = false;
- break;
- }
- }
- break;
- case 8://ok
- LCDPrint(0," Select Function by ^ v ");
- OK_flag = true;
- break;
- case 16://down
- LCDPrint(0," @AADA ---Alarm off ");
- break;
- case 32://return
- LCDPrint(0," Main Function ");
- break;
- }
- delay(100);
- }
- }
- void caseB_stepDisp(void)//
- {
- LCDWriteChar(0,19,'+');//clean some digit
- LCDWriteChar(0,20,((incB/10)+48));//display item no
- LCDWriteChar(0,21,((incB%10)+48));
- }
- // read structure --- caseAs[]
- void OK_process(void)
- {
- boolean next_treat = caseAs[incB].mod;
- chr15 = 0; //initial data
- if(next_treat)//enter next data with v ^ < > OK Ret.
- {
- level3 = true;
- while(level3)
- {
- LCDWriteChar(0,23,(chr15+48));//display cursor
- caseC = Keyscan();
- if(caseC != oldcaseC)
- {
- oldcaseC = caseC;//not more key same character
- }
- switch (caseC)
- {
- case 1:
- dspLine1[22]='<'; //0505
- chr15 --;
- if(chr15 <0)
- {chr15 = 0;}
- break;
- case 2:
- dspLine1[22]='>'; //0505
- chr15 ++;
- if(chr15 >10)
- {chr15 =10;}
- break;
- case 4:
- dspLine1[22]='^';
- if(incB ==3)
- {//set AA-NN
- Num_hexNo0++;
- if(Num_hexNo0 < 0)
- {
- Num_hexNo0 =0;
- }
- if(Num_hexNo0 > 15)
- {
- Num_hexNo0 =15;
- }
- Set_NN[chr15] = Num_hex[Num_hexNo0];
- dspLine2[(chr15)+3] = Set_NN[chr15];
- }
- else if(incB ==0)
- {//for%AANNTTCCFF
- incC ++;
- dspLine2[(chr15)] = Num_hex[incC];
- LCDPrint(1,dspLine2);
- }
- else
- {
- incC ++;// 0505 inc parameter @AAHI+00.000 use incC
- if(chr15 ==0)
- {
- Set_value_sign = !Set_value_sign;
- if(Set_value_sign)
- {
- dspLine2[5] = '+';//0505
- }
- else
- {
- dspLine2[5] = '-';//0505
- }
- }
- else
- {
- dspLine2[(chr15+5)] = Num_hex[incC];
- }
- }
- LCDPrint(1,dspLine2);
- break;
- case 8:
- LCDPrint(0," level 3 process ok ");
- if(incB ==3)
- {
- EEPROM.write(0,Set_NN[0]);
- EEPROM.write(1,Set_NN[1]);
- }
- level3 = false;
- break;
- case 16:
- dspLine1[22]='v';
- if(incB ==3)
- {//set AA-NN
- Num_hexNo0--;
- if(Num_hexNo0 < 0)
- {
- Num_hexNo0 =0;
- }
- if(Num_hexNo0 > 15)
- {
- Num_hexNo0 =15;
- }
- Set_NN[chr15] = Num_hex[Num_hexNo0];
- dspLine2[(chr15)+3] = Set_NN[chr15];
- }
- else if(incB ==0)
- {//for%AANNTTCCFF
- incC --;
- dspLine2[(chr15)] = Num_hex[incC];
- LCDPrint(1,dspLine2);
- }
- else
- {
- incC --;// 0505 inc parameter @AAHI+00.000 use incC
- if(chr15 ==0)
- {
- Set_value_sign = !Set_value_sign;
- if(Set_value_sign)
- {
- dspLine2[5] = '+';//0505
- }
- else
- {
- dspLine2[5] = '-';//0505
- }
- }
- else
- {
- dspLine2[(chr15+5)] = Num_hex[incC];
- }
- }
- LCDPrint(1,dspLine2);
- break;
- case 32:
- level3 = false;
- break;
- }
- }
- }
- ReplaceAA_NN();//replace AA by EEPROM data and Send out to RS232
- }
- void ReplaceAA_NN(void)//20190504 replace AA by moduleAddr set by #13 AA-NN
- {
- // OutPrintStr = caseAs[incB].sel_No;
- OutPrintStr = String (dspLine2);
- OutPrintStr.replace("AA",moduleAddr);
- Serial.println(OutPrintStr);// output rs232 the command data
- CleanLine1();//clean LCM 1 display
- }
- //display structure caseAs[] content
- void dispCaseB(void)
- {
- // Serial.println("dispCaseB");
- if(incB != incB_old)
- {
- incB_old = incB;//new change
- CleanLine0();
- }
- LCDPrint(0,caseAs[incB].name);
- LCDWriteChar(0,22,' ');
- dspLine2 = caseAs[incB].sel_No;
- LCDPrintString(1,0,caseAs[incB].sel_No);
- if(read_OK_flag)//read rs232 data
- {
- read_OK_flag = false;
- String caseAs_sel_No = caseAs[incB].sel_No;
- int sel_No_length= caseAs_sel_No.length();
- String msgbuf_trim = String(msgbuf);
- msgbuf_trim.substring(0,strleng-sel_No_length);
- msgbuf_trim.toCharArray(msgbuf,strleng-sel_No_length);
- LCDPrintString(1,(sel_No_length),msgbuf);
- }
- }
- void dispCase (int nnnn)
- {
- String V2_str = String(nnnn);
- V2_str.toCharArray(V2_Array,4);
- byte newString_length= V2_str.length();
- if (oldStringlength != newString_length)
- {
- // LCDclean();//---------------------0505
- oldstringlength= newString_length;
- LCDPrintString(1,22," ");//clean some digit
- }
- LCDPrintString(1,22,V2_Array);
- }
- //=============================================================
- // Send Data or Command to LCD
- //=============================================================
- void LCDSendByte(char d,char RS)
- {
- char dH,dL,temp;
- //Keep Data on upper nybble
- dH = d & 0xF0; //Get MSB
- dL = d & 0x0F;
- dL = d << 4; //Get LSB
- //Send MSB with E=clock
- temp=0;
- temp=dH | RS | 0x02; //MSB With RS+E bit
- ShiftData(temp);
- //Send MSB with E=0
- temp=0;
- temp=dH | RS; //MSB With RS bit
- ShiftData(temp);
- //Send LSB with E=clock
- temp=0;
- temp=dL | RS | 0x02; //MSB With RS+E bit
- ShiftData(temp);
- //Send LSB with E=0
- temp=0;
- temp=dL | RS; //MSB With RS bit
- ShiftData(temp);
- }
- //=============================================
- void ShiftData(char temp)
- {
- int i;
- for(i=0;i<8;i++)
- {
- if((temp & 0x80)==0x80) //Send 1-bit to shift register
- {digitalWrite(Data,HIGH);}
- else
- {digitalWrite(Data,LOW);}
- digitalWrite(Clock,LOW);
- digitalWrite(Clock,HIGH);
- temp=temp<<1;
- }
- //Latch the data
- digitalWrite(Latch,LOW);
- delay(1);
- digitalWrite(Latch,HIGH);
- }
- //=================================================================
- // LCD Display Initialization Function
- //=================================================================
- void LCDinit(void)
- {
- int count;
- char t[]={0x43,0x03,0x03,0x02,0x28,0x01,0x0C,0x06,0x02,0x02};
- for (count = 0; count <= 9; count++)
- {
- LCDSendByte(t[count],0); //Command RS=0
- }
- }
- //=================================================================
- // Display Line on LCD at desired location Function
- //=================================================================
- void LCDPrint(char Line,char string[24])//--------------------------------------
- {
- int len,count;
- if(Line==0) {
- LCDSendByte(0x80,0); //Command RS=0 Move Cursor to Home
- }
- else {
- LCDSendByte(0xC0,0); //Command RS=0 Move Cursor to Second Line LCDSendByte(0xC0,0);
- }
- len = strlen(string);
- for (count=0;count<len;count++)
- {
- LCDSendByte(string[count],1); //Data RS=1
- }
- }
- //=================================================================
- void LCDclean(void)
- {
- LCDSendByte(0x01,0);
- }
- void LCDCursorMove(int y,int x)//=====================================>
- {
- if (y == 0)
- {
- LCDSendByte(0x80 + x,0);
- }
- else
- {
- LCDSendByte(0xC0 + x,0);
- }
- // LCDSendByte(0x14,0);//input mode
- LCDSendByte(0x0E,0);// display control mode cursor
- }
- void LCDPrintString(int x,int y, char string[])
- {
- int count, len;
- // summ = x;
- if (x == 0)
- {
- LCDSendByte(0x80+y ,0);
- }
- else
- {
- LCDSendByte(0xC0+y,0);
- }
- len = strlen(string);
- for (count=0;count<len;count++)
- {
- LCDSendByte(string[count],1); //Data RS=1
- }
- }
- void LCDWriteChar(int x,int y,char Data)
- {
- if (x == 0)
- {
- LCDSendByte(0x80 + y,0);
- }
- else
- {
- LCDSendByte(0xC0 + y,0);
- }
- LCDSendByte(Data,1);
- }
- void LCDhome(void)
- {
- LCDSendByte(0x02,0);
- }
- void LCDDisp(void)
- {
- LCDSendByte(0x0f,0);
- }
- byte Keyscan(void)// There are 6 keys shiftL shiftR Up Down Enter Select
- {
- byte kkk;
- KS = analogRead(keyport);//Read(A0); //key scan read port3
- if(KS >640)//key no pressed
- {
- kkk = 64;
- }
- if((KS<639))// is press &&(Khold == false)
- {
- delay(20);
- KS=Mavg();
- KSC=Mavg();
- // Serial.println(KS);
- if(abs(KS-KSC)<10) //key press correctly
- {
- if((KS<600)&&(KS>550)) //1st key > press (KS<600)&&(KS>531)
- {Krs=1;Kls=0;Kdec=0; Kinc=0;Kenter = 0; Ksel=0;
- } //shift right
- else if((KS<550)&&(KS>525))//2nd key < press KS<530)&&(KS>501)
- {Krs=0;Kls=1;Kdec=0; Kinc=0;Kenter = 0; Ksel=0;} //shift left
- else if((KS<525)&&(KS>480))//3rd key Inc press((KS<500)&&(KS>451
- {Krs=0;Kls=0;Kdec=1; Kinc=0;Kenter = 0; Ksel=0;} //key down
- else if((KS<480) &&(KS>400)) //4th key Dec press (KS<450) &&(KS>361)
- {Krs=0;Kls=0;Kdec=0; Kinc=1;Kenter = 0; Ksel=0;}//key up
- else if((KS<400)&&(KS>100))//5th key "VoI" press (KS<360)&&(KS>100)
- {Krs=0;Kls=0;Kdec=0; Kinc=0;Kenter = 1; Ksel=0;}
- else if(KS<100)// 6 th key "ON/OFF " press
- {Krs=0;Kls=0;Kdec=0; Kinc=0;Kenter = 0; Ksel=1;}
- // Serial.println(KS);
- kkk= Krs*1+Kls*2+Kinc*16+Kdec*4+Kenter*8+Ksel*32;
- // Serial.println(kkk);
- while(analogRead(keyport)<640) {} //0502 modify for wait key relaese
- }
- else //2 times check KS != KSC
- {
- kkk = 64;
- }
- }
- delay(50);
- return kkk;
- }
- int Mavg(void)
- // read 5 times data and average 讀5次求平均值
- // an for analog input no.
- {
- byte at;
- int M;
- M=0;
- for (at=0;at<3;at++)
- {
- M=analogRead(keyport)+M;//analog 3 input
- delay(50);
- }
- M=M/3;
- return M;
- }