自学单片机有一段时间了,做了款数控烙铁程序,其中大部份程序我先屏蔽,先搞显示正常,发现烧录后数码管是全显,显示不正常不是读取AD的数值,用K型热电偶作探头经放大100倍送AD转换,DC5V/2A供电,请大神们指点下。程序如下:
- #include"STC15.h"
- #include "intrins.h"
- #define uchar unsigned char
- #define uint unsigned int
- #define ulong unsigned long
- #define ADC_POWER 0x80 //ADCμçÔ′¿ØÖÆλ
- #define ADC_FLAG 0x10 //ADCíê3é±êÖ¾
- #define ADC_START 0x08 //ADCÆe꼿ØÖÆλ
- #define ADC_SPEEDLL 0x00 //540¸öê±Öó
- #define ADC_SPEEDL 0x20 //360¸öê±Öó
- #define ADC_SPEEDH 0x40 //180¸öê±Öó
- #define ADC_SPEEDHH 0x60 //90¸öê±Öó
- //#define GPIO_LED P3
- sbit ba=P1^3;
- sbit sh=P1^0;
- sbit ge=P1^2;
- sbit dn=P1^1;
- //sbit dian=P3^4;
- /*sfr P1M0=0x0f;
- sfr P1M1=0x00;
- sfr P3M0=0xff;
- sfr P3M1=0x00; */
- sbit up=P1^0;
- sbit down=P1^1;
- sbit heat=P1^7;
- //sbit s2=P1^2;
- //void Display(uchar dn,uchar ge,uchar sh,uchar ba);
- //void Display(uchar kemp);
- uchar makesure;//S3¼ü ¡°è·èϼü¡°
- uchar tp=0;
- uchar num=0;
- uchar ad=0;
- uchar s1,s2,s3,temp;
- uchar code LED[]={0xCF,0x0C,0xEA,0xE3,0xAC,0x67,0xE7,0x0E,0xEF,0x6F}; //数码管0-9
- uchar code wei[4]={0x08,0x04,0x02,0x01}; //位选
- //uint ADC_result6,shan_sz,shan_dw,wendu,sheding,aa,tt01,tt_xm1,tt_xm2,tt_aj,js_xm;
- //uchar sheding_bz,jiare_bz,bz1,bz2,qieh_w,tt00,jiareshu,bz_xs,tt1,ab1,ab2,fen,miao;//¶¨òå±äá¿
- ulong code KT_1[14][101]=
- {
- /*-100 0*/
- { 3568,3523,3492,3461,3430,3399,3368,3337,3305,3274,
- 3242,3211,3179,3147,3115,3082,3050,3018,2985,2953,
- 2920,2887,2854,2821,2788,2754,2721,2687,2654,2620,
- 2586,2552,2518,2484,2450,2416,2381,2347,2312,2277,
- 2243,2208,2173,2137,2102,2067,2032,1996,1961,1925,
- 1889,1853,1817,1781,1745,1709,1673,1636,1600,1563,
- 1527,1490,1453,1416,1379,1342,1305,1268,1231,1193,
- 1156,1118,1081,1043,1005, 968, 930, 892, 854, 816,
- 777 ,739 , 701, 662, 624, 585, 547, 508, 469, 431,
- 392 ,353 , 314, 275, 236, 197, 157, 118, 79, 39,
- 0
- },
-
- /*0 100*/
- { 0, 39, 79, 119, 158, 198, 238, 277, 317, 357,
- 397, 437, 477, 517, 557, 597, 637, 677, 718, 758,
- 798, 838, 879, 919, 960,1000,1041,1081,1122,1162,
- 1203,1244,1285,1325,1366,1407,1448,1489,1529,1570,
- 1611,1652,1693,1734,1776,1817,1858,1899,1940,1981,
- 2022,2064,2105,2146,2188,2229,2270,2312,2353,2394,
- 2436,2477,2519,2560,2601,2643,2684,2726,2767,2809,
- 2850,2892,2933,2975,3016,3058,3100,3141,3183,3224,
- 3266,3307,3349,3390,3432,3473,3515,3556,3598,3639,
- 3681,3722,3764,3805,3847,3888,3930,3971,4012,4054,
- 4095
- },
- /*100 200 */
- {
- 4095,4137,4178,4219,4261,4302,4343,4384,4426,4467,
- 4508,4549,4590,4632,4673,4714,4755,4796,4837,4878,
- 4919,4960,5001,5042,5083,5124,5164,5205,5246,5287,
- 5327,5368,5409,5450,5490,5531,5571,5612,5652,5693,
- 5733,5774,5814,5855,5895,5936,5976,6016,6057,6097,
- 6137,6177,6218,6258,6298,6338,6378,6419,6459,6499,
- 6539,6579,6619,6659,6699,6739,6779,6819,6859,6899,
- 6939,6979,7019,7059,7099,7139,7179,7219,7259,7299,
- 7338,7378,7418,7458,7498,7538,7578,7618,7658,7697,
- 7737,7777,7817,7857,7897,7937,7977,8017,8057,8097,
- 8137
- },
- /*200 300 */
- {
- 8137, 8177, 8216, 8256, 8296, 8236, 8376, 8416, 8456, 8497,
- 8537, 8577, 8617, 8657, 8697, 8737, 8777, 8817, 8857, 8898,
- 8938, 8978, 9018, 9058, 9099, 9139, 9179, 9220, 9260, 9300,
- 9341, 9381, 9421, 9462, 9502, 9543, 9583, 9624, 9664, 9705,
- 9745, 9786, 9826, 9867, 9907, 9948, 9989,10029,10070,10111,
- 10151,10192,10233,10274,10315,10355,10396,10437,10478,10519,
- 10560,10600,10641,10682,10723,10764,10805,10846,10887,10928,
- 10969,11010,11051,11093,11134,11175,11216,11257,11298,11239,
- 11381,11422,11463,11504,11546,11587,11628,11669,11711,11752,
- 11793,11835,11876,11918,11959,12000,12042,12083,12125,12166,
- 12207
- },
- /*300 400 */
- {
- 12207,12249,12290,12332,12373,12415,12456,12498,12539,12581,
- 12623,12664,12706,12747,12789,12831,12872,12914,12955,12997,
- 13039,13080,13122,13164,13205,13247,13289,13331,13372,13414,
- 13456,13497,13539,13581,13623,13665,13706,13748,13790,13832,
- 13874,13915,13957,13999,14041,14083,14125,14167,14208,14250,
- 14292,14334,14376,14418,14460,14502,14544,14586,14628,14670,
- 14712,14754,14796,14838,14880,14922,14964,15006,15048,15090,
- 15132,15174,15216,15258,15300,15342,15384,15426,15468,15510,
- 15552,15594,15626,15679,15721,15763,15805,15847,15889,15931,
- 15974,16016,16058,16100,16142,16184,16227,16269,16311,16353,
- 16395
- },
- /*400 500 */
- {
- 16395,16438,16480,16522,16564,16607,16649,16691,16733,16776,
- 16818,16860,16902,16945,16987,17029,17072,17114,17156,17199,
- 17241,17283,17326,17368,17410,17453,17495,17537,17580,17622,
- 17664,17707,17749,17792,17834,17876,17919,17961,18004,18046,
- 18088,18131,18173,18216,18258,18301,18343,18385,18428,18470,
- 18513,18555,18598,18640,18683,18725,18768,18810,18853,18895,
- 18938,18980,19023,19065,19108,19150,19193,19235,19278,19320,
- 19363,19405,19448,19490,19533,19576,19618,19661,19703,19746,
- 19788,19831,19873,19916,19959,20001,20044,20086,20129,20172,
- 20214,20257,20299,20342,20385,20427,20470,20512,20555,20598,
- 20640
- },
- /*500 600 */
- {
- 20640,20683,20725,20768,20811,20853,20896,20938,20981,21024,
- 21066,21109,21152,21194,21237,21280,21322,21365,21407,21450,
- 21493,21535,21578,21621,21663,21706,21749,21791,21834,21876,
- 21919,21962,22004,22047,22099,22132,22175,22218,22260,22303,
- 22346,22383,22431,22473,22516,22559,22601,22644,22687,22729,
- 22772,22815,22857,22900,22942,22985,23028,23070,23113,23156,
- 23198,23241,23284,23326,23369,23411,23454,23497,23539,23582,
- 23624,23667,23710,23752,23795,23837,23880,23923,23965,24008,
- 24050,24093,24136,24178,24221,24263,24306,24348,24391,24434,
- 24476,24519,24561,24604,24646,24689,24731,24774,24817,24859,
- 24902
- },
- /*600 700 */
- {
- 24902,24944,24987,25029,25072,25114,25157,25199,25242,25284,
- 25327,25369,25412,25454,25497,25539,25582,25624,25666,25709,
- 25751,25794,25836,25879,25921,25964,26006,26048,26091,26133,
- 26176,26218,26260,26303,26345,26387,26430,26472,26515,26557,
- 26599,26642,26684,26726,26769,26811,26853,26896,26938,26980,
- 27022,27065,27107,27149,27192,27234,27276,27318,27361,27403,
- 27445,27487,27529,27572,27614,27656,27698,27740,27783,27825,
- 27867,27909,27951,27993,28035,28078,28120,28162,28204,28246,
- 28288,28330,28372,28414,28456,28498,28540,28583,28625,28677,
- 28709,28751,28793,28835,28877,28919,28961,29002,29044,29086,
- 29128
- },
- /*700 800 */
- {
- 29128,29170,29212,29254,29296,29338,29380,29422,29464,29505,
- 29547,29589,29631,29673,29715,29756,29798,29840,29882,29924,
- 29965,30007,30049,30091,30132,30174,30216,30257,30299,30341,
- 30383,30424,30466,30508,30549,30591,30632,30674,30716,30757,
- 30799,30840,30882,30924,30965,31007,31048,31090,31131,31173,
- 31214,31256,31297,31339,31380,31422,31463,31504,31546,31587,
- 31629,31670,31712,31753,31794,31836,31877,31918,31960,32001,
- 32042,32084,32125,32166,32207,32249,32290,32331,32372,32414,
- 32455,32496,32537,32578,32619,32661,32702,32743,32784,32825,
- 32866,32907,32948,32990,33031,33072,33113,33154,33195,33236,
- 33277
- },
- /*800 900 */
- {
- 33277,33318,33359,33400,33441,33482,33523,33564,33604,33645,
- 33686,33727,33768,33809,33850,33891,33931,33972,34013,34054,
- 34095,34136,34176,34217,34258,34299,34339,34380,34421,34461,
- 34502,34543,34583,34624,34665,34705,34746,34787,34827,34868,
- 34909,34949,34990,35030,35071,35111,35152,35192,35233,35273,
- 35314,35354,35395,35435,35476,35516,35557,35597,35637,35678,
- 35718,35758,35799,35839,35880,35920,35960,36000,36041,36081,
- 36121,36162,36202,36242,36282,36323,36363,36403,36443,36483,
- 36524,36564,36604,36644,36684,36724,36764,36804,36844,36885,
- 36925,36965,37005,37045,37085,37125,37165,37205,37245,37285,
- 37325
- },
- /*900 1000 */
- {
- 37325,37365,37405,37445,37484,37524,37564,37604,37644,37684,
- 37724,37764,37803,37843,37883,37923,37963,38002,38042,38082,
- 38122,38162,38201,38241,38281,38320,38360,38400,38439,38479,
- 38519,38558,38598,38638,38677,38717,38756,38796,38836,38875,
- 38915,38954,38994,39033,39073,39112,39152,39191,39231,39270,
- 39310,39349,39388,39428,39467,39507,39546,39585,39625,39664,
- 39703,39743,39782,39821,39861,39900,39939,39979,40018,40057,
- 40096,40136,40175,40214,40253,40292,40332,40371,40410,40449,
- 40488,40527,40566,40605,40645,40684,40723,40762,40801,40840,
- 40879,40918,40957,40996,41035,41074,41113,41152,41191,41230,
- 41269
- },
- /*1000 1100 */
- {
- 41269,41308,41347,41385,41424,41463,41502,41541,41580,41619,
- 41657,41696,41735,41774,41813,41851,41890,41929,41968,42006,
- 42045,42084,42132,42161,42200,42239,42277,42316,42355,42393,
- 42432,42470,42509,42548,42586,42625,42663,42702,42740,42779,
- 42817,42856,42894,42933,42971,43010,43048,43087,43125,43164,
- 43202,43240,43279,43317,43356,43394,43482,43471,43509,43547,
- 43585,43624,43662,43700,43739,43777,43815,43853,43891,43930,
- 43968,44006,44044,44082,44121,44159,44197,44235,44273,44311,
- 44349,44387,44425,44463,44501,44539,44577,44615,44653,44691,
- 44729,44767,44805,44843,44881,44919,44957,44995,45033,45070,
- 45108
- },
- /*1100 1200 */
- {
- 45108,45146,45184,45222,45260,45297,45335,45373,45411,45448,
- 45486,45524,45561,45599,45637,45675,45712,45750,45787,45825,
- 45863,45900,45938,45975,46013,46051,46088,46126,46163,46201,
- 46238,46275,46313,46350,46388,46425,46463,46500,46537,46575,
- 46612,46649,46687,46724,46761,46799,46836,46873,46910,46948,
- 46985,47022,47059,47097,47134,47171,47208,47245,47282,47319,
- 47356,47393,47430,47468,47505,47542,47579,47616,47653,47689,
- 47726,47763,47800,47837,47874,47911,47948,47985,48021,48058,
- 48095,48132,48169,48205,48242,48279,48316,48352,48389,48426,
- 48462,48499,48536,48572,48609,48645,48682,48718,48755,48792,
- 48828
- },
- /*1200 1300 */
- {
- 48828,48865,48901,48937,48974,49010,49047,49083,49120,49156,
- 49192,49229,49265,49301,49338,49374,49410,49446,49483,49519,
- 49555,49591,49627,49663,49700,49736,49772,49808,49844,49880,
- 49916,49952,49988,50024,50060,50096,50132,50168,50204,50240,
- 50276,50311,50347,50383,50419,50455,50491,50526,50562,50598,
- 50633,50669,50705,50741,50776,50812,50847,50883,50919,50954,
- 50990,51025,51061,51096,51132,51167,51203,51238,51274,51309,
- 51344,51380,51415,51450,51486,51521,51556,51592,51627,51662,
- 51697,51773,51768,51803,51838,51873,51908,51943,51979,52014,
- 52049,52084,52119,52154,52189,52224,52259,52294,52329,52364,
- 52398
- }
-
- };
- /*****************延时子程序*****************/
- /*void delayus(uint xus)
- {
- uint i,j;
- for(i=0;i<xus;i++)
- for(j=0;j<10;j++);
- }*/
- void delay1ms(unsigned int i)
- {
- uchar j;
- while(i--)
- {
- for(j=0;j>115;j++);
- {;}
- }
- }
- /*********端口初始化********/
- /*void Init()
- {
- P1M1&=~0X40;
- P1M0|=0Xf3;
- P3M1=0X00;
- P3M0=0XFF;
- P1M0&=~0X40;//P1^6¸高阻输入
- P1M1|=0X40;
- ADC_CONTR=0X80;//启动
- ADC_START=0X08;
- delay1ms(2);
- AUXR1&=~0X04;//ADC储存方式
- ADC_CONTR=0XE1;
- } */
- void Init()
- {
- P1M0|=0x80;
- P1M1&=~0x80;
- // P1M0= 0x0f;
- // P1M1= 0x00;
- P3M0=0xff;
- P3M1=0x00;
- P5=0xff;
- P5=0xff;
- P1ASF=0x40; //设置第6通道作摸拟输入
- ADC_RES=0; //清除
- ADC_RESL=0; //清除
- ADC_CONTR=ADC_POWER|ADC_SPEEDLL; //ADC开启电源,
- delay1ms(2); //延时1ms
- }
- /***键盘检测*****/
- /*void Keyboardset(void)
- {
-
- if((s1+s2+s3)!=0)
- {
- if(s1==1)
- num=num-1;
- Display(num,tp);
- }
- if(s2==1)
- {
- num=num+1;
- Display(num,tp);
- if(num>=1000)num=1000;
- }
- if(s3==1)
- {
- if(num>tp)
- makesure=1;
- return;
- }
- } */
-
- /********按键扫描*********/
- /*void Keyask(void)
- {
- uchar tepm;
- s1=0;
- s2=0;
- s3=0;
- P1=0x30;
- P5=0xff;
- tepm=P1&0X30;
- if(temp!=0x30)
- {
- delay1ms(10);
- if(temp!=0x30)
- switch(temp)
- {
- case(0x20):s1=1;
- while(!(P1&0X10));
- break;
- case(0x10):s2=1;
- while(!(P1&0x20));
- break;
- }
- }
- P1=0X70;
- if(temp!=0x30)
- {
- delay1ms(10);
- if(temp!=0x30)
- switch(temp)
- {
- case(0x20):s3=1;
- while(!(P1&0X10));
- }
- }
- }*/
- /**********ADC初始化并采集**************/
- /*uchar getAD(void)
- {
- uchar i;
- uint sum=0;
- for(i=0;i<8;i++)
- {
- ADC_CONTR|=0X08;
- BIT3=0x08(ADC_STARTAD)
- delay(1);
- P1ASF=0x40;
- while((ADC_CONTR&0x10)==0x00);//等待转换结束
- ADC_CONTR&=~0x10;
- sum+=ADC_RES;
- }
- sum>>=3;
- return(uchar)sum; //强制转换uchar形式
- } */
- uint getAD(uchar channel)
- {
- uchar ad_finished=0;//储存AD转换标志
- uchar i;
- uint sum=0;
- for(i=0;i<8;i++)
- {
- ADC_RES=0;//¸高8清0
- ADC_RESL=0;//低2清0
- channel&=0x07;//0000 0111 清0高5位
- ADC_CONTR=0x60;//转换速率
- _nop_();
- ADC_CONTR=channel;//选择当前通道
- _nop_();
- ADC_CONTR|=0x80;//启动AD电源
- _nop_();//延时
- _nop_();
- ADC_CONTR|=0x08;//0000,1000启动转换
- ad_finished=0;
- while(ad_finished==0)//等待转换结束
- {
- ad_finished=(ADC_CONTR&0x10); //测试转换结束?
- }
- ADC_CONTR&=0xE7;//1111,0111 清ADC_FLAG位,关闭转换
- sum+=ADC_RES;
- }
- sum>>=3; //求取平均值
- return(uchar)sum; //强制转换uchar形式
- return(ADC_RES*256+ADC_RESL);//·返回AD结果
- }
- /*************êyÂë1üÏÔê¾*****************/
- void Display(uchar kemp)
- /* void Display(uchar dn,uchar ge,uchar sh,uchar ba)*/
- {
- /* uchar i;
- uint k;
- for(i=0;i<4;i++);
- {
- switch(i)
- {
- case(0):
- b=0;s=0;g=0;d=0;
- break;
- case(1):
- b=1;s=0;g=0;d=0;
- break;
- case(2):
- b=0;s=1;g=0;d=0;
- break;
- case(3):
- b=0;s=0;g=1;d=0;
- break;
- case(4):
- b=0;s=0;g=0;d=1;
- break;
- }
- GPIO_LED=LED[i];
- k=10;
- while(k--);
- GPIO_LED=0x00;
- } */
- /*uchar b,sh,g;
- b=num/100;
- sh=(num/10)%10;
- g=num%10;*/
- //P3=0x00;
- ba=1;
- sh=0;
- ge=0;
- dn=0;
- P3=LED[kemp/100]; //百位
- // delay5ms();
- ba=0;
- sh=1;
- ge=0;
- dn=0;
- P3=LED[kemp/10%10]; //十位
- // delay5ms();
- ba=0;
- sh=0;
- ge=1;
- dn=0;
- P3=LED[kemp%10]; //个位
- // delay5ms();
- ba=0;
- sh=0;
- ge=0;
- dn=1;
- P3=0x5e; //单位‘C'
- // delay5ms();
- }
-
-
- /***********主程序***********/
- void main()
- {
- uchar kemp;//dn,ge,sh,ba ;
- Init();
- P1ASF=0x40;//
- AUXR=0x04;//
- ADC_CONTR|=0x80;//
-
- //P1M0=0x0f;
-
- //P3M0=0xff;
- while(1)
- {
- Keyboardset();
- ad=getAD;
- tp=KT_1[ad/2];
- if(makesure==1)
- {
- if(num>=tp)
- {
- heat=1;
- }
- if(tp>num)
- {
- heat=0;
- }
- }
- kemp=tp|num;
- //delay10ms();
- /* ba=kemp/100; //百
- sh=kemp/10%10; //十
- ge=kemp%10; */ //¸个
- //dn=0x5e;
- Display(kemp);
- }
- }
复制代码
|