今天在调STC12的AD功能时发现一个问题,我从P1.0到P1.6一个一个试过去,但到P1.7的时候就问题了,虽然P1.0到P1.6精度不高,但用P1.7口时,数码管一直显示4.9, 我一直找不到问题所在,求大神帮帮忙,我改的只有黑体字。。
单片机程序如下:
- #include "reg51.h"
- #include "intrins.h"
- typedef unsigned char uchar;
- typedef unsigned int uint;
- float temp,v;
- /*Declare SFR associated with the ADC */
- sfr ADC_CONTR = 0xBC; //ADC control register
- sfr ADC_RES = 0xBD; //ADC hight 8-bit result register
- sfr ADC_LOW2 = 0xBE; //ADC low 2-bit result register
- sfr P1ASF = 0x9D; //P1 secondary function contr ol register
- sbit Max7219_pinCLK = P2^2;
- sbit Max7219_pinCS = P2^1;
- sbit Max7219_pinDIN = P2^0;
- uchar ge ,shi, bai;
- uchar code shuzi[]={0x7e,0x30,0x6d,0x79,0x33,0x5b,0x5f,0x70,0x7f,0x7b};
- /*Define ADC operation const for ADC_CONTR*/
- #define ADC_POWER 0x80 //ADC power control bit
- #define ADC_FLAG 0x10 //ADC complete flag
- #define ADC_START 0x08 //ADC start control bit
- #define ADC_SPEEDLL 0x00 //540 clocks
- #define ADC_CHS 0x07; // AD通道选择 (P1.0 - 0X01, P1.1 - 0X02 .... P1.6 - 0x06 ), 到P1.7就有问题了。。。
- void delay(uint n);
- void InitADC();
- void Dispaly();
- void Write_Max7219_byte(uchar DATA)
- {
- uchar i;
- Max7219_pinCS=0;
- for(i=8;i>=1;i--)
- {
- Max7219_pinCLK=0;
- Max7219_pinDIN=DATA&0x80;
- DATA=DATA<<1;
- Max7219_pinCLK=1;
- }
- }
- void Write_Max7219(uchar address,uchar dat)
- {
- Max7219_pinCS=0;
- Write_Max7219_byte(address); //写入地址,即数码管编号
- Write_Max7219_byte(dat); //写入数据,即数码管显示数字
- Max7219_pinCS=1;
- }
- void Init_MAX7219(void)
- {
- Write_Max7219(0x09, 0x00); //译码方式:BCD码
- Write_Max7219(0x0a, 0x05); //亮度
- Write_Max7219(0x0b, 0x07); //扫描界限;8个数码管显示
- Write_Max7219(0x0c, 0x01); //掉电模式:0,普通模式:1
- Write_Max7219(0x0f, 0x00); //显示测试:1;测试结束,正常显示:0
- }
- void voltage_display()
- { uchar u;
- u = v;
- ge = u%10;
- shi = (u%100)/10;
- bai = (u%1000)/100;
- Write_Max7219(1,0x01);
- Write_Max7219(2,0x01);
- Write_Max7219(3,shuzi[ge]);
- Write_Max7219(4,shuzi[shi]);
- Write_Max7219(5,shuzi[bai]);
- Write_Max7219(6,0x01);
- Write_Max7219(7,0x01);
- Write_Max7219(8,0x01);
- }
- void main()
- {
- Init_MAX7219();
- InitADC(); //Init ADC sfr
- IE = 0xa0; //Enable ADC interrupt and Open master interrupt switch //Start A/D conversion
- while (1)
- {
- voltage_display();
- }
- }
- /*---------------------------- ADC interrupt service routine ----------------------------*/
- void adc_isr() interrupt 5 using 1
- {
- ADC_CONTR &= !ADC_FLAG; //Clear ADC interrupt flag
- temp = ADC_RES;
- v = temp*0.1/256*500;
- ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ADC_START | ADC_CHS;
- }
- /*---------------------------- Initial ADC sfr ----------------------------*/
- void InitADC( )
- { P1ASF = 0X80; //Set P1.7 as analog input port
- // P1.0 - 0X01 P1.1 - 0X02
- // P1.2 - 0X04 P1.3 - 0X08
- // P1.4 - 0X10 P1.5 - 0X20
- // P1.6 - 0X40
- ADC_RES = 0; //Clear previous result
- ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ADC_START | ADC_CHS;
- delay(2); //ADC power-on delay and Sta rt A/D conversion
- }
- /*---------------------------- Software delay function ----------------------------*/
- void delay(uint n)
- { uint x;
- while (n--)
- { x = 5000;
- while (x--);
- }
-
- }
复制代码
|