ได้แล้วครับ
ไม่มีใครตอบเลย
ดังนั้น ครั้นได้ดังหมาย เราก็เผยแพร่ได้เพราะเป็นสิทธิของเรา
เพื่อ การพัฒนา ของ สังคมเราครับ
pic16f877+MCP4922 2 ตัว
วงจร ออกแบบเองเด้อ เพราะถ้าออกแบบไม่เป็น Code ข้างล่างจะไม่มีประโยชน์ครับ
ถ้าตอนที่ผมขอนะ ได้มาแบบนี้ ต่อยอด สบายครับ
//////////////// UPDATE 05-03-2012 ///////////// /1/2012 ///////////////
//////////////// PIC16F877A ///////////////
//////////////// UPDATE 08-03-2012 /////////////
/* LOAD TO TRITON */
//////////////// UPDATE 15-03-2012 /////////////
//////////////// UPDATE 18-03-2012 /////////////
//// ONLY UP RAIL AND MAP NOT TO UP PEDAL ////
#include <built_in.h>
#define beep1 PORTC.F2 //BC548 BUZZER 5V
#define temp_relay PORTB.F1
/* ********** CHANGE MODE SW PORTB.F0 ********** */
unsigned long int vout1,vout2,vout3,vout4,rail_in, map_in,
rail_ref,map_ref,pedal1,pedal2,pedal3,pedal_in,pedal,pedal_ref;
char y=0,valible=0,i4=0,y1=0;
// MCP4922 Configuration Constants '
//***********************************'
const char rail_conf = 0x50; //Channel A Buffered x2Gain
const char map_conf = 0xD0; //Channel B Buffered x2Gain
const char pedal1_conf = 0x50; //Channel A Buffered x2Gain
const char pedal2_conf = 0xD0; //Channel B Buffered x2Gain
///**** Kick down control Rail & Map ****///
const char SHDN = 0x00; //Channel A & B Output = High Impedance
void DAC_OUT1(char config,int output)
{
// PORTC.F0=0; // Enable Chip Select
PORTC=0x02;
SPI_Write(config+Hi(output));
SPI_Write(Lo(output));
PORTC=0x03;
}
void DAC_OUT2(char config,int output2)
{
PORTC=0x01;
SPI_Write(config+Hi(output2));
SPI_Write(Lo(output2));
PORTC=0x03;
}
void beep2()
{
beep1=1; delay_ms(10);beep1=0;delay_ms(90);
}
void beep3()
{
beep1=1; delay_ms(150);beep1=0;delay_ms(150);
}
void rail_up()
{
{
if(rail_in<317){vout1=rail_ref-valible;}// < 1.55 V
//else if((rail_in>=317)&&(rail_in<327)){vout1=rail_ref-(7+valible);}//1.55-1.613
else if((rail_in>=317)&&(rail_in<337)){vout1=rail_ref-(12+valible);}//1.613-1.657
//else if((rail_in>=337)&&(rail_in<348)){vout1=rail_ref-(17+valible);}//1.657-1.687
else if((rail_in>=337)&&(rail_in<358)){vout1=rail_ref-(21+valible);}//1.687-1.75
//else if((rail_in>=358)&&(rail_in<368)){vout1=rail_ref-(25+valible);}//1.75-1.8
else if((rail_in>=358)&&(rail_in<378)){vout1=rail_ref-(29+valible);}//1.8-1.89
//else if((rail_in>=378)&&(rail_in<389)){vout1=rail_ref-(33+valible);}//1.89-1.995
else if((rail_in>=378)&&(rail_in<399)){vout1=rail_ref-(37+valible);}//1.995-2.029
//else if((rail_in>=399)&&(rail_in<409)){vout1=rail_ref-(41+valible);}//2.029-2.102
else if((rail_in>=399)&&(rail_in<419)){vout1=rail_ref-(44+valible);}//2.102
//else if((rail_in>=419)&&(rail_in<429)){vout1=rail_ref-(40+valible);}//2.102
else if((rail_in>=419)&&(rail_in<440)){vout1=rail_ref-(36+valible);}//2.102
//else if((rail_in>=440)&&(rail_in<450)){vout1=rail_ref-(32+valible);}//2.102
else if((rail_in>=440)&&(rail_in<460)){vout1=rail_ref-(28+valible);}//2.102
//else if((rail_in>=460)&&(rail_in<470)){vout1=rail_ref-(24+valible);}//2.102
else if((rail_in>=460)&&(rail_in<481)){vout1=rail_ref-(20+valible);}//2.102
//else if((rail_in>=481)&&(rail_in<650)){vout1=rail_ref-(12+valible);}//2.102
else if((rail_in>=481)&&(rail_in<800)){vout1=rail_ref-(6+valible);}//2.102
else{vout1=rail_ref-(2+valible);}
}
}
void map_up()
{
if(map_in>245&&map_in<333){vout2=(map_ref+40);}
else if(map_in>=333&&map_in<419){vout2=(map_ref+50);}
else if(map_in>=419&&map_in<505){vout2=(map_ref+60);}
else if(map_in>=505&&map_in<591){vout2=(map_ref+90);}//569
else if(map_in>=591&&map_in<777){vout2=(map_ref+100);}//640
//else if(map_in>=677&&map_in<777){vout2=(map_ref+110);}//711
else{vout2=map_ref;}
}
void pedal_up()
{
if(pedal_in>225&&pedal_in<286){vout3=(pedal_ref+30);} //1.1-1.4 v
else if(pedal_in>=286&&pedal_in<368){vout3=(pedal_ref+50);} //1.4-1.8 v
else if(pedal_in>=368&&pedal_in<450){vout3=(pedal_ref+70);} //1.8-2.2 V
else if(pedal_in>=450&&pedal_in<859){vout3=(pedal_ref+100);} //2.2-2.6 V
//else if(pedal_in>=532&&pedal_in<859){vout3=(pedal_ref+110);} //2.6-3.0 V
//else if(pedal_in>=613&&pedal_in<695){vout3=(pedal_ref+75);} //3.0-3.4 V
//else if(pedal_in>=695&&pedal_in<777){vout3=(pedal_ref+80);} //3.4-3.8 V
//else if(pedal_in>=777&&pedal_in<859){vout3=(pedal_ref+85);} //3.8-4.2 V
else if(pedal_in>=859){vout3=(pedal_ref+100);} //4.2-5.0 V
else{vout3=pedal_ref;}
}
void input_sensor()
{
if(PORTB.F0==0){i4++; beep3();delay_ms(250);}// NORMAL ON 4.5V
if(i4>3){i4=0;}
if(i4==1){vout1=rail_ref;}
if(i4==2){valible=50;rail_up();}
if(i4==3){valible=120;rail_up();}
// if(i4==4){valible=80;rail_up();}
// if(i4==0){valible=0;beep3();i4++;}
rail_in=Adc_read(0);
map_in=Adc_read(1);
pedal_in=Adc_read(2);
rail_ref=rail_in*2;
map_ref=map_in*2;
pedal_ref=pedal_in*2;
map_up();
pedal_up();
}
void dac_output()
{
if(PORTC==0x01){vout1=(vout1+30);vout2=(vout1-10);}
if(map_in>=777||vout2>1555){vout2=1555;}
DAC_OUT1(rail_conf,vout1); // Output on channel A
DAC_OUT1(map_conf,vout2); // Output on channel B
// delay_ms(150);
vout4=vout3/2;
if(vout4>=1022){vout4=1022;}
DAC_OUT2(pedal1_conf,vout3); // Output on channel A
DAC_OUT2(pedal2_conf,vout4); // Output on channel B
}
void counter_t()
{
y++;
if(y<25){pedal1=Adc_Read(2);}
if(y==25){pedal2=Adc_Read(2);}
if(y>25){y=0;}
delay_ms(10);
}
void main()
{
TRISA=0XFF;
TRISC=0X00;
TRISB=0X01;
PORTB=0x00;
Spi_Init();
PORTC=0x03;
DAC_OUT1(SHDN,0);
DAC_OUT2(SHDN,0);
// beep3();beep2();beep3();
while(1)
{
counter_t();
{
if(pedal2>pedal1+23)
{
loop3:
temp_relay=1;
y1++;
i4=4;
input_sensor(); dac_output();
pedal3=Adc_Read(2);
if(y1>60){beep2();y1=0;}
else{beep1=0;}
while(pedal3>=pedal2){goto loop3;}
}
else {
input_sensor();
if(i4==1){ vout2=map_ref;temp_relay=0;} //only for safe petrol
if(i4==2){ vout2=map_ref-125;temp_relay=0;} //only for safe petrol
else{temp_relay=1;}
dac_output();
}
}
}
}