[英]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.