|
各位同好先進們,我目前遇到一個問題就是 我從回收場取得一片 PLC的 DAC 板子(不知好壞,沒有線路) 只見到裡面的 AD7840 想到他的14bit DAC 就興奮,於是花了許多時間把線路的局部給畫了下來, 然後想想如何使用 ARDUINO 驅動他,終極目標就是想用他來當作"數位電源供應"的前端,目前,問題在於我的電路有四個DAC 而我現在只使用兩個DAC,而卻 輸出 1 時候,輸出 2 "就沒有了",(當我輸出 2 時候,輸出 1 也就沒有了!) 這電路控制使用 光耦合驅動 Data1Pin DataPin2 SyncPin ClockPin ... 我需要解決就是 兩個輸出都可以同時出現!!
線路圖
程式內容如下:
- //MERGE ENCODER AND LCM DISPLAY
- static int pinA = 2; // Our first hardware interrupt pin is digital pin 2
- static int pinB = 3; // Our second hardware interrupt pin is digital pin 3
- volatile byte aFlag = 0;
- volatile byte bFlag = 0;
- volatile long encoderPos = 0;
- volatile long oldEncPos = 0;
- volatile byte reading = 0;
- const int EncoderSW = 4;//Enter SW
- //-------------upper is encoder ------------------------------
-
- //Define 74HC595 Connections with arduio
- const int Latch=5; //LCM
- const int Data=6; //LCM
- const int Clock=7; //LCM
- const int OEnPin=8;//output enable OPA
- const int SyncPin=9;//AD7840
- const int ClockPin=10;//AD7840
- const int SDataPin=11;//AD7840
- const int SDataPin2=12;//AD78402
- const byte Volt_in =A0;
- const byte Current_in = A3;
- // const int Led=13;//Led on off
- void LCDinit(void);
- void LCDSendByte(char d,char RS);
- void LCDPrint(char Line,char string[40]);//------------------------------------
- String msg;
- boolean flagread = false;//rs232 updata
- String msgdisplay;
- char msgbuf[20];//rs232 read buffer
- char Echar_Array[40];//char array 15 character ------------------------------
- char msgdisplay_Array[40];//display setting data --------------------------------
- char V2_Array[5];
- int Echar_length;
- int oldstringlength;
- String Echar_str;
- long Echar;
- long SetData;//form rs232 PC
- boolean flag1st = true;
- int SWcount;
- int Volt_value =0;
- int Curr_value =0;
- int Volt_value2 =0;
- int Curr_value2 =0;
- int old_Volt_value;
- int old_Curr_value;
- boolean encoder_change_flag = false;
- boolean Encoder_flag = false;
- int Menu_curs;
- int Old_Menu_curs;
- static int display_time_count =0;
- int Level0 =0;
- boolean init_flag = false;
- boolean Display_flag;
- int old_value;
- int Volt_oldstringlength;
- void setup() {
- pinMode(pinA, INPUT_PULLUP); // set pinA as an input _PULLUP
- pinMode(pinB, INPUT_PULLUP); // set pinB as an input _PULLUP
- attachInterrupt(0,PinA,RISING); // set an interrupt on PinA
- attachInterrupt(1,PinB,RISING); // set an interrupt on PinB
- Serial.begin(38400); // start the serial monitor link
- //-------------------------------- encoder --------------------------
- pinMode(EncoderSW, INPUT_PULLUP);
- //------------------------------^^^-- encoder --^^^----------------------------------
- //-----------------vvv-LCM -vvv-----------------
- pinMode(Data, OUTPUT);
- pinMode(Clock, OUTPUT);
- pinMode(Latch, OUTPUT);
- //----------AD7840 ------------------
- pinMode(ClockPin,OUTPUT);//AD7840
- pinMode(SDataPin,OUTPUT);//AD7840
- pinMode(SDataPin2,OUTPUT);//AD7840
- pinMode(SyncPin,OUTPUT);//AD7840
- pinMode(OEnPin,OUTPUT);//OPA Vss
- // pinMode(Volt_in,INPUT);//analog input
- // pinMode(Current_in,INPUT);//analog input
- //------------output initial ------------
- digitalWrite(OEnPin,HIGH);
- digitalWrite(SyncPin,HIGH);
- digitalWrite(ClockPin,HIGH);
- LCDinit(); //initalize LCD in 4-bit mode
- LCDPrint(0,"> Set Voltage Hello");
- LCDPrint(1,"> Set Current start");
- LCDPrintString(0,20,"> Voltage Out 2019");
- LCDPrintString(1,20,"> Current Out Feb-28");
- }
- void loop() {
- while (!digitalRead(EncoderSW))
- while(1)
- {
- digitalWrite(OEnPin,LOW);
- GetEncoder();
- encoderPos = 0;
- Menu_curs += Echar;
- if(Old_Menu_curs != Menu_curs)
- {
- Old_Menu_curs = Menu_curs;
- Display_flag = true;
- }
-
- if((Menu_curs >4) ||(Menu_curs <0) )
- {
- Menu_curs =0;
- init_flag = true;
- }
-
- switch (Menu_curs)
- {
- case 0:
- if(Display_flag)
- {
- LCDPrintString(0,0, "^ Set Voltage ");
- LCDWriteChar(0,0,'^');
- LCDWriteChar(1,0,'>');
- LCDWriteChar(0,20,'>');
- LCDWriteChar(1,20,'>');
- Display_flag = false;
- }
- Level0 = Read_Encoder_SW();//Read encoder sw to Level0
- if(Level0 ==1)
- {
- boolean Level1=true;
- LCDWriteChar(0,1,'>');
- while(Level1)
- {
- GetEncoder();
- Volt_value +=Echar;
- encoderPos = 0;
- if(Volt_value != old_value)
- {
- old_value = Volt_value;
- boolean UpdateLCD_flag = true;
- if(UpdateLCD_flag)
- {
- String V2_str = String(Volt_value);
- V2_str.toCharArray(V2_Array,5);
- LCDPrintString(0,14,V2_Array);
- int Echar_length= V2_str.length();
- if (oldstringlength != Echar_length)
- {
- oldstringlength= Echar_length;
- LCDPrintString(0,13," ");//clean some digit
- LCDPrintString(0,14,V2_Array);//------------------
- }
- Echar=0;
- int Volt_value100 = Volt_value * 231;//MAX IS 163
- AD7840driv(SDataPin,Volt_value100);
- UpdateLCD_flag = false;
- }
- }
- int Level0level1= Read_Encoder_SW();
- if (Level0level1 ==1)
- {
- Level0 =0;
- Level1 = false;
- LCDWriteChar(0,1,' ');
- }
- }
- }
- break;
-
- case 1:
- if(Display_flag)
- {
- LCDPrintString(1,0, "^ Set Current ");
- LCDWriteChar(0,0,'>');
- LCDWriteChar(1,0,'^');
- LCDWriteChar(0,20,'>');
- LCDWriteChar(1,20,'>');
- Display_flag = false;
- }
- Level0 = Read_Encoder_SW();//Read encoder sw to Level0
- if(Level0 ==1)
- {
- boolean Level1=true;
- LCDWriteChar(1,1,'>');
- while(Level1)
- {
- GetEncoder();
- Curr_value +=Echar;
- encoderPos = 0;
- if(Curr_value != old_value)
- {
- old_value = Curr_value;
- boolean UpdateLCD_flag = true;
- if(UpdateLCD_flag)
- {
- String V2_str = String(Curr_value);
- V2_str.toCharArray(V2_Array,5);
- LCDPrintString(1,14,V2_Array);
- int Echar_length= V2_str.length();
- if (oldstringlength != Echar_length)
- {
- oldstringlength= Echar_length;
- LCDPrintString(1,13," ");//clean some digit
- LCDPrintString(1,14,V2_Array);//------------------
- }
- Echar=0;
- int Curr_value100 = Curr_value *232;
- AD7840driv(SDataPin2,Curr_value100);
- UpdateLCD_flag = false;
- }
- }
- int Level0level1= Read_Encoder_SW();
- if (Level0level1 ==1)
- {
- Level0 =0;
- Level1 = false;
- LCDWriteChar(1,1,' ');
- }
- }
- }
- break;
- case 2:
- if(Display_flag)
- {
- LCDPrintString(0,20, "^ Voltage Out");
- LCDWriteChar(0,0,'>');
- LCDWriteChar(1,0,'>');
- LCDWriteChar(0,20,'^');
- LCDWriteChar(1,20,'>');
- Display_flag = false;
- }
- Level0 = Read_Encoder_SW();//Read encoder sw to Level0
- if(Level0 ==1)
- {
- boolean Level1=true;
- LCDWriteChar(0,21,'>');
- while(Level1)
- {
- // GetEncoder();
- Volt_value2 =analogRead(Volt_in);
- // encoderPos = 0;
- if(Volt_value2 != old_Volt_value)
- {
- old_Volt_value = Volt_value2;
- boolean UpdateLCD_flag = true;
- if(UpdateLCD_flag)
- {
- String V2_str = String(Volt_value2);
- V2_str.toCharArray(V2_Array,5);
- LCDPrintString(0,34,V2_Array);
- int Echar_length= V2_str.length();
- if (oldstringlength != Echar_length)
- {
- oldstringlength= Echar_length;
- LCDPrintString(0,33," ");//clean some digit
- }
- Echar=0;
- // AD7840driv(SDataPin,Volt_value2);
- UpdateLCD_flag = false;
- }
- }
- int Level0level1= Read_Encoder_SW();
- if (Level0level1 ==1)
- {
- Level0 =0;
- Level1 = false;
- LCDWriteChar(0,21,' ');
- }
- }
- }
- break;
- case 3:
- if(Display_flag)
- {
- LCDPrintString(1,20, "^ Current Out");
- LCDWriteChar(0,0,'>');
- LCDWriteChar(1,0,'>');
- LCDWriteChar(0,20,'>');
- LCDWriteChar(1,20,'^');
- Display_flag = false;
- }
- Level0 = Read_Encoder_SW();//Read encoder sw to Level0
- if(Level0 ==1)
- {
- boolean Level1=true;
- LCDWriteChar(1,21,'>');
- while(Level1)
- {
- // GetEncoder();
- Curr_value2 =analogRead(Current_in);
- // encoderPos = 0;
- if(Curr_value2 != old_Curr_value)
- {
- old_Curr_value = Curr_value2;
- boolean UpdateLCD_flag = true;
- if(UpdateLCD_flag)
- {
- String V2_str = String(Curr_value2);
- V2_str.toCharArray(V2_Array,5);
- LCDPrintString(1,34,V2_Array);
- int Echar_length= V2_str.length();
- if (oldstringlength != Echar_length)
- {
- oldstringlength= Echar_length;
- LCDPrintString(1,33," ");//clean some digit
- }
- Echar=0;
- // AD7840driv(SDataPin,Volt_value2);
- UpdateLCD_flag = false;
- }
- }
- int Level0level1= Read_Encoder_SW();
- if (Level0level1 ==1)
- {
- Level0 =0;
- Level1 = false;
- LCDWriteChar(1,21,' ');
- }
- }
- }
- break;
- case 4: //display screen purpose
- if(Display_flag)
- {
- LCDclean();
- LCDPrintString(0,0, "> Set Voltage");
- LCDPrintString(1,0, "> Set Current");
- LCDPrintString(0,20,"> Voltage Out");
- LCDPrintString(1,20,"> Current Out");
- LCDWriteChar(0,0,'*');
- LCDWriteChar(1,0,'*');
- LCDWriteChar(0,20,'*');
- LCDWriteChar(1,20,'*');
- Display_flag = false;
- }
- String V2_str = String(Volt_value);
- V2_str.toCharArray(V2_Array,5);
- LCDPrintString(0,14,V2_Array);
- V2_str = String(Curr_value);
- V2_str.toCharArray(V2_Array,5);
- LCDPrintString(1,14,V2_Array);
- Volt_value2 =analogRead(Volt_in);
- V2_str = String(Volt_value2);
- V2_str.toCharArray(V2_Array,5);
- LCDPrintString(0,34,V2_Array);
- int Echar_length= V2_str.length();
- if (Volt_oldstringlength != Echar_length)
- {
- Volt_oldstringlength= Echar_length;
- LCDPrintString(0,33," ");//clean some digit
- }
- Curr_value2 =analogRead(Current_in);
- V2_str = String(Curr_value2);
- V2_str.toCharArray(V2_Array,5);
- LCDPrintString(1,34,V2_Array);
- Echar_length= V2_str.length();
- if (oldstringlength != Echar_length)
- {
- oldstringlength= Echar_length;
- LCDPrintString(1,33," ");//clean some digit
- }
- delay(300);
-
- break;
- }
- }
- }
- //=============================================================
- // 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()
- {
- 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[40])//--------------------------------------
- {
- 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 LCDCurserMove(int y,int x)
- {
- // LCDSendByte((a*4+16),0);//a=1 shift right
- if (y == 0)
- {
- LCDSendByte(0x80 + x,0);
- }
- else
- {
- LCDSendByte(0xC0 + x,0);
- }
- LCDSendByte(0x0b,0);
- }
- 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);
- }
- void PinA(){
- cli(); //stop interrupts happening before we read pin values
- reading = PIND & 0xC;
- if(reading == B00001100 && aFlag) {
- encoderPos --;
- bFlag = 0;
- aFlag = 0;
- }
- else if (reading == B00000100) bFlag = 1;
- sei(); //restart interrupts
- }
- void PinB(){
- cli(); //stop interrupts happening before we read pin values
- reading = PIND & 0xC;
- if (reading == B00001100 && bFlag) {
- encoderPos ++;
- bFlag = 0;
- aFlag = 0;
- }
- else if (reading == B00001000) aFlag = 1;
- sei(); //restart interrupts
- }
- void GetEncoder()//encode Position data from interrupt real time
- {
- if(oldEncPos != encoderPos)
- {
- Serial.println(encoderPos);
- oldEncPos = encoderPos;
- encoder_change_flag =true;
- }
- Echar = encoderPos;
- }
- //-------------------------------AD7840--------------------------------------
- void AD7840driv(int ADPin,int temp)
- {
- Serial.print(" Analog out Data = ");
- Serial.println(temp);
- // digitalWrite(SDataPin,LOW);
- digitalWrite(ADPin,LOW);
- digitalWrite(SyncPin,LOW);
- delay(1);
- int i;
- for(i=0;i<16;i++)
- {
- digitalWrite(ClockPin,HIGH);
- // delay(1);
- if((temp & 0x8000)==0x8000)
- // {digitalWrite(SDataPin,HIGH);}
- {digitalWrite(ADPin,HIGH);}
- else
- // {digitalWrite(SDataPin,LOW);}
- {digitalWrite(ADPin,LOW);}
- digitalWrite(ClockPin,LOW);
- temp=temp<<1;
- }
- // digitalWrite(SDataPin,LOW);
- digitalWrite(ADPin,LOW);
- digitalWrite(SyncPin,HIGH);
- delay(1);
- digitalWrite(ClockPin,HIGH);
- }
- int Read_Encoder_SW(void)
- {
- if(!digitalRead(EncoderSW))//
- {
- delay(50);//50
- if(!digitalRead(EncoderSW))
- {
- return 1;
- flagread =true;
- }
- }
- // Serial.println("return ");//important this have delay time
- delay(150);//150
- }
複製代碼
補充內容 (2019-3-11 12:10 AM):
已經解決!!
|
|