我正在编写一个显示以下7 SEGMENT LCD温度的代码:
我正在使用 PIC24FJ64GA006 Micro。
我只能使用PORTB [13:6],PORTD [0:7],PORTE [0:7]端口引脚。
你能帮我找到更优雅的方法来替换下面的代码吗?
void SegLCD_Set(uint8_t Value,uint8_t Digit)
{
switch(Digit)
{
case 1:
if(Value==0x00){ LATB = (LATB & ~(0x7F<<6)) | (0x3F<<6); }
if(Value==0x01){ LATB = (LATB & ~(0x7F<<6)) | (0x0C<<6); }
if(Value==0x02){ LATB = (LATB & ~(0x7F<<6)) | (0x5B<<6); }
if(Value==0x03){ LATB = (LATB & ~(0x7F<<6)) | (0x5E<<6); }
if(Value==0x04){ LATB = (LATB & ~(0x7F<<6)) | (0x6C<<6); }
if(Value==0x05){ LATB = (LATB & ~(0x7F<<6)) | (0x76<<6); }
if(Value==0x06){ LATB = (LATB & ~(0x7F<<6)) | (0x77<<6); }
if(Value==0x07){ LATB = (LATB & ~(0x7F<<6)) | (0x1C<<6); }
if(Value==0x08){ LATB = (LATB & ~(0x7F<<6)) | (0x7F<<6); }
if(Value==0x09){ LATB = (LATB & ~(0x7F<<6)) | (0x7E<<6); }
break;
case 2:
if(Value==0x00){ LATD = (LATD & ~(0xFF)) | (0x3F); }
if(Value==0x01){ LATD = (LATD & ~(0xFF)) | (0x0C); }
if(Value==0x02){ LATD = (LATD & ~(0xFF)) | (0x5B); }
if(Value==0x03){ LATD = (LATD & ~(0xFF)) | (0x5E); }
if(Value==0x04){ LATD = (LATD & ~(0xFF)) | (0x6C); }
if(Value==0x05){ LATD = (LATD & ~(0xFF)) | (0x76); }
if(Value==0x06){ LATD = (LATD & ~(0xFF)) | (0x77); }
if(Value==0x07){ LATD = (LATD & ~(0xFF)) | (0x1C); }
if(Value==0x08){ LATD = (LATD & ~(0xFF)) | (0x7F); }
if(Value==0x09){ LATD = (LATD & ~(0xFF)) | (0x7E); }
break;
case 3:
if(Value==0x00){ LATE = (LATE & ~(0xFF)) | (0x3F); }
if(Value==0x01){ LATE = (LATE & ~(0xFF)) | (0x0C); }
if(Value==0x02){ LATE = (LATE & ~(0xFF)) | (0x5B); }
if(Value==0x03){ LATE = (LATE & ~(0xFF)) | (0x5E); }
if(Value==0x04){ LATE = (LATE & ~(0xFF)) | (0x6C); }
if(Value==0x05){ LATE = (LATE & ~(0xFF)) | (0x76); }
if(Value==0x06){ LATE = (LATE & ~(0xFF)) | (0x77); }
if(Value==0x07){ LATE = (LATE & ~(0xFF)) | (0x1C); }
if(Value==0x08){ LATE = (LATE & ~(0xFF)) | (0x7F); }
if(Value==0x09){ LATE = (LATE & ~(0xFF)) | (0x7E); }
break;
default:
LATB = (LATB & ~(0x7F<<6)) | (0x3F<<6);
LATD = (LATD & ~(0xFF)) | (0x3F);
LATE = (LATE & ~(0xFF)) | (0x3F);
break;
};
}//End SegLCD_Set
谢谢!
答案 0 :(得分:1)
我不确定它是否更优雅,但这可行...
void SegLCD_Set(uint8_t Value,uint8_t Digit)
{
unsigned char values[] = {0x3f, 0x0c, 0x5b, 0x5e, 0x6c, 0x76, 0x77, 0x1c, 0x7f, 0x7e};
switch(Digit)
{
case 1:
LATB = (LATB & ~(0x7F<<6)) | (values[Value]<<6);
break;
case 2:
LATD = (LATD & ~(0xFF)) | (values[Value]);
break;
case 3:
LATE = (LATE & ~(0xFF)) | (values[Value]);
break;
default:
LATB = (LATB & ~(0x7F<<6)) | (0x3F<<6);
LATD = (LATD & ~(0xFF)) | (0x3F);
LATE = (LATE & ~(0xFF)) | (0x3F);
break;
};
}