[讨论] 关于单片机的程序问题

anrewanrew   2010-4-14 22:59 楼主


下面是我找的一个关于数控电压源的程序。 然后发现我所做的跟他的单片机上的接口不同 不同的地方就是接数码管和接DAC0832的接口换了 但是我看了程序,只有提及到接数码管的接口程序,没有接DAC0832接口的程序的, 请问那我应该怎么修改好呢,急! 谢谢各位! #include "reg51.h" #include "math.h" #define uint unsigned int #define uchar unsigned char //#include "intrins.h" #define led_sec P1 #define dac_adr 0x7fff //DAC0832的地址 sbit inc_key = P3^0; //"+" sbit dec_key = P3^1; //"-" sbit step_key = P3^2; //调整键 sbit ten = P3^3; //数码管的十位 sbit one = P3^4; //数码管的个位 sbit point = P3^5; //数码管的小数位 char xdata dacreg _at_ dac_adr; //define the dac0832's address bit flag0 = 0; uchar arry10[10] = { 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; uchar arry1[10] = { 0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10}; uchar arry0[10] = { 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; uchar * ptr10 = arry10; uchar * ptr1 = arry1; uchar * ptr0 = arry0; //arry10[10] = { 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //arry1[10] = { 0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10}; //arry0[10] = { 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //**************************initial function********************************** //中断初始化 void ini_int(void) { inc_key = 1; dec_key = 1; EA = 1; EX0 = 1; IT0 = 1; //边沿触发方式 } //**************************delay function************************************ //延时动态扫描 void delay(uint i) //delay cycle is 20us { uint j=0; for (;j } //**************************buffer************************************** //根据地址查表得出要显示的电压值 buffer(uchar led10,uchar led1,uchar led0) { ten = 0; led_sec = *(led10 + ptr10); delay(500); // delay 10ms ten = 1; one = 0; led_sec = *(led1 + ptr1); delay(500); one = 1; point = 0; led_sec = *(led0 + ptr0); delay(500); point = 1; } //**************************display the output voltage****************** //BCD码到十进制的转换 void disp_out(unsigned char dat) { uchar dig_10,dig_1,dig_0,temp,temp1; temp = dat; dig_10 = temp/100; temp1 = temp%100; dig_1 = temp1/10; dig_0 = temp%10; buffer(dig_10,dig_1,dig_0); } //**********************main function*********************************** void main() { unsigned char dac_dat,dac_temp; uchar inc_key_temp = 1; uchar dec_key_temp = 1; dac_dat = 0xd; //flag0 = 0; // initialising the flag bit dac_temp = dac_dat; dacreg = dac_temp; //set the output voltage to 1.3V ini_int(); //interrupt initialising disp_out(dac_temp); while (1 /*~dec_key | ~inc_key*/) { //判断有没有键按下 if ( inc_key == 0 ) { delay(600); inc_key_temp = (inc_key == 0) ? 0 : 1; //按键释放前一直扫描显示,如果按键释放则跳出并 "+" 1 do { disp_out(dac_temp); } while (inc_key == 0); } else if( dec_key == 0 ) { delay(600); dec_key_temp = (dec_key == 0) ? 0 : 1; do { disp_out(dac_temp); } while (dec_key == 0); } else { ; } if ( ~flag0 & ~dec_key_temp & (dac_temp>13)) // decrease and step is 0.1V { dec_key_temp = 1; dac_dat = dac_dat - 1; dac_temp = dac_dat; dacreg = dac_temp; disp_out(dac_temp); } else if ( ~flag0 & ~inc_key_temp & (dac_dat<161)) // increase and step is 0.1V { inc_key_temp = 1; dac_dat = dac_dat + 1; dac_temp = dac_dat; dacreg = dac_temp; disp_out(dac_temp); } else if ( flag0 & ~dec_key_temp & (dac_dat>23)) //decrease and step is 1V { dec_key_temp = 1; dac_dat = dac_dat - 10; dac_temp = dac_dat; dacreg = dac_temp; disp_out(dac_temp); } else if (flag0 & ~inc_key_temp & (dac_dat<151)) // increase and step is 1V { inc_key_temp = 1; dac_dat = dac_dat + 10; dac_temp = dac_dat; dacreg = dac_temp; disp_out(dac_temp); } else { disp_out(dac_temp); } } } //*************************interrupt function************************** void int0() interrupt 0 using 1 { flag0 = ~flag0; }

 

本帖最后由 paulhyde 于 2014-9-15 09:27 编辑

回复评论 (1)



在有需要调节电压的功能键的地方进行调节电压就可以了

 

本帖最后由 paulhyde 于 2014-9-15 09:27 编辑
南京璞晓电子   www.cpx0.com需要 msn:njlianjian@hotmail.com
点赞  2010-4-15 10:57
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复