51单片机的基于DS18B20温度计改共阳极共阴极

此单片机共阳数码管(蓝色的那个数码管)改共阴数码管该怎么在C上进行改动,或者把2个数码管拆成2个单独的数码管,求问各位

img

#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit SEG1 = P3^0;
sbit SEG2 = P3^1;
sbit SEG3 = P3^2;
sbit LED = P3^3;
sbit DQ = P3^5;
int num1;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77};
uchar Temp_Value[]= {0x00,0x00};     //从DS18B20读取的温度值
bit DS18B20_IS_OK = 1;               //传感器正常标志
//--------------------------
//延时
//-------------------------
void Delay (uint x)
{
 while(--x);
}
void display(int num)
{
    uchar shi,ge;
    if(num>99 || num<-99)
    {
        SEG1=SEG2=1;
        P2=0xbf;LED=1;Delay(2000);
        SEG1=0;Delay(2000);    SEG2=0;
        SEG3=0;P2=0x40;Delay(2000);SEG3=1;
            
    }
    else
    {

         if(num>=0)
            LED=0;
        else
            {LED=1;num=-num;}

        shi=num/10;
        ge=num%10;
        
        SEG1=1;
        P2=~table[shi];
        Delay(2000);
        SEG1=0;
    
        SEG2=1;
        P2=~table[ge];
        Delay(2000);
        SEG2=0;
                   
        SEG3=0;
        P2=0x8f;
        Delay(2000);
        SEG3=1;        
    }
}
uchar Init_DS18B20()
{  //初始化DS18B20
    uchar status;
    DQ = 1;
    Delay(8);
    DQ = 0;
    Delay(90);
    DQ = 1;
    Delay(8);
    status = DQ;
    Delay(100); 
    DQ = 1;
    return status;
}
uchar ReadOneByte()
{     //读一字节
    uchar i,dat = 0;
    DQ = 1; 
    for (i = 0; i < 8; i++)
    {
       DQ = 0;dat >>= 1; DQ = 1;
       if(DQ)  dat |= 0x80; Delay(30); DQ = 1;
      }
     return dat;
}

void WriteOneByte(uchar dat)
{    //写一字节
      uchar i;
      for (i = 0; i < 8; i++)
      {
        DQ = 0; DQ = dat & 0x01; Delay(5); DQ = 1; dat>>=1;
       }
}
void Read_Temperature()
{
   if( Init_DS18B20() == 1)           //DS18B20故障 
      DS18B20_IS_OK = 0;
   else 
      WriteOneByte(0xCC);             //跳过序列号
      WriteOneByte(0x44);             //启动温度转换
      Init_DS18B20(); 
      WriteOneByte(0xCC);             //跳过序列号
      WriteOneByte(0xBE);             //读取温度寄存器
      Temp_Value[0] = ReadOneByte();  //温度低8位
      Temp_Value[1] = ReadOneByte();  //温度高8位
}
void Display_Temperature()
{
     char ng = 0;             //负数标识及负号显示位置
     //如果为负数则取反加1,并设置负号标识记负号显示位置
     if ( (Temp_Value[1] & 0xF8) == 0xF8 )
     {
         Temp_Value[1] = ~Temp_Value[1];
         Temp_Value[0] = ~Temp_Value[0] + 1;
         if (Temp_Value[0] == 0x00)  Temp_Value[1]++;
         ng = 1; 
      }
      Temp_Value[0] = ((Temp_Value[0] & 0xF0)>>4)|((Temp_Value[1] & 0x07)<<4);
        ng=ng? -Temp_Value[0] : Temp_Value[0];
           num1=ng;
       
       //刷新显示若干时间
   //  for (i = 0;i < 30; i++)
    //    {

            display(num1);
    //    }
}
void main(void)
{
   Read_Temperature();
   Delay(50000);
   Delay(50000);
   while(1)
   {
       display(num1);
       Read_Temperature();
       Display_Temperature(); 
    }
}


段码和位码1开0关

共阳改共阴首先要对code table[]的码表按bit取反,然后在main的while循环中单独调用 display(int num),显示0,1,2.。。9看看两种型号的数码管位码是否是一一对应的,否则就要更改code table[],正确了在显示你的动态温度值,应该就没有问题了

对原来的段选和片选取反,也就是SEG1 SEG2 SEG3 P2 的赋值都加上取反~。

uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77};
按位取反