![](/img/trans.png)
[英]Can I program a PIC 16f628a in C using Microchip's MPLAB?
[英]Is there a more ethical way of dong this?? Embedded in C - PIC16F18326 - MPLAB
必须有更好的编码方式,这一切都有效,但占用了大量空间,有没有更合乎道德的方式呢?
因此,首先,我的代码基本上会闪烁一个 LED,它还具有不同的速度设置,可以通过按钮进行编程。
所以我的程序知道它处于哪种模式我有以下代码在模式更改时会更改,例如,如果从模式 1 更改为模式 5,则 M1 将 = 0 并且 M5 将 = 1 等。
M1 = 1; // We are in Mode 1
M2 = 0;
M3 = 0;
M4 = 0;
M5 = 0;
M6 = 0;
M7 = 0;
M8 = 0;
M9 = 0;
M10 = 0;
M11 = 0;
M12 = 0;
M13 = 0;
M14 = 0;
M15 = 0;
M16 = 0;
M17 = 0;
M18 = 0;
然后当速度改变时,程序会通过这段代码 go 将速度保存到当时的任何模式。 这样我可以将不同的速度保存到不同的模式。 同样,这是很多代码,我确定我过于复杂了。 有没有更好的方法来做到这一点?
void Speeds(void)
{
if (M1 == 1) // if in mode 1 when changing speed
{
counter = S1; // save value to this mode only
}
if (M2 == 1) // if in mode 2 when changing speed
{
counter = S2; // save value to this mode only
}
if (M3 == 1) // if in mode 3 when changing speed
{
counter = S3; // save value to this mode only
}
if (M4 == 1) // if in mode 4 when changing speed
{
counter = S4; // save value to this mode only
}
if (M5 == 1) // if in mode 5 when changing speed
{
counter = S5; // save value to this mode only
}
if (M6 == 1) // if in mode 6 when changing speed
{
counter = S6; // save value to this mode only
}
if (M7 == 1) // if in mode 7 when changing speed
{
counter = S7; // save value to this mode only
}
if (M8 == 1) // if in mode 8 when changing speed
{
counter = S8; // save value to this mode only
}
if (M9 == 1) // if in mode 9 when changing speed
{
counter = S9; // save value to this mode only
}
if (M10 == 1) // if in mode 10 when changing speed
{
counter = S10; // save value to this mode only
}
if (M11 == 1) // if in mode 11 when changing speed
{
counter = S11; // save value to this mode only
}
if (M12 == 1) // if in mode 12 when changing speed
{
counter = S12; // save value to this mode only
}
if (M13 == 1) // if in mode 13 when changing speed
{
counter = S13; // save value to this mode only
}
if (M14 == 1) // if in mode 14 when changing speed
{
counter = S14; // save value to this mode only
}
}
是否可以更改使用一个变量表示不同模式的模式表示? 例如:
Mode = 0 <=> M1 = 1
Mode = 1 <=> M2 = 1
Mode = 2 <=> M3 = 1
....
Mode = 17 <=> M18 = 1
如果可能,那么 Speeds() function 中的 if-else 语句可以替换为 switch-case 语句。 然后表达式将变得简洁。 例如:
switch(Mode){
case 0:
counter = S1; // save value to this mode only
break;
case 1:
counter = S2; // save value to this mode only
break;
...
case 17:
counter = S18; // save value to this mode only
break;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.