【发布时间】:2016-06-07 00:59:02
【问题描述】:
“用 C++ 编写一些东西以在汇编中为微处理器创建方波。 反正我的布局如下:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void code_calc(float cyc, float cyc_high, float cyc_low, string prt, string pncnf, string pnhgh, char prtlttr) {
ofstream outfile("sqrwv.txt");
if (outfile.is_open()) {
cout << "\n\nWriting to file...\n\n"<<prt<<pncnf<<cyc_low;
outfile << "Sqrwv MOVLW 0x02 ; Crude count variable\n"
<< " MOVWF count4 ; Store in COUNT4 to generate wait of 0.5s\n"
<< " BSF STATUS, RP0 ;\n"
<< " BCF STATUS, RP1 ; Select bank 1\n"
<< " MOVLW B'"<<pncnf<<"' ; Write / output only on pin 1\n"
<< " MOVWF TRIS"<<prtlttr<< " ; so portb is now(wwww wwww)\n"
<< "sqhigh BCF STATUS, RP0 ;\n"
<< " BCF STATUS, RP1 ; Select bank 0\n"
<< " BSF "<<prt<<", "<<pnhgh<<" ; Set pin 1 of port B high\n"
<< " CALL Delay ; Wait 0.1s\n"
<< " DECFSZ count4, 1 ; Decrement COUNT4 to generate\n"
<< " GOTO sqhigh ; a flat high signal of 0.5s\n"
<< " MOVLW 0x02 ; Crude count variable again\n"
<< " MOVWF count5 ; \n"
<< "sqlow NOP ; NOP to ensure correct cycles on high and low\n"
<< " NOP ; mirroring the BCFs in HIGH\n"
<< " BCF "<<prt<<", "<<pnhgh<<" ; Set pin 1 of port B low\n"
<< " NOP ;\n"
<< " NOP ;\n"
<< " NOP ; 93 cycles low\n"
<< " CALL Delay ; Wait 0.1s\n"
<< " DECFSZ count5, 1 ; Decrement COUNT4 to generate a flat low signal\n"
<< " GOTO sqlow ; Repeat 4 more times, giving 0.5s\n"
<< " GOTO Sqrwv ; If count4 is now zero, just go back to high - forever 83 counts\n";
outfile.close();
}
else cout << "Couldn't open file. Try again.";
}
// outputs menu text when called
void menu_bs() {
cout << " ::::::::::::::::::::::::::::::::::::::::::\n"
<< " :: Square Wave Generator with Assembly ::\n" // initiate the menu
<< " ::::::::::::::::::::::::::::::::::::::::::\n\n"
<< "Follow the instructions in the list, "
<< "and check for output in sqrwv.txt\n\n"
<< "1. Some important information\n"
<< "2. Enter your clock speed\n" // give the user various options
<< "3. Enter your wave period\n"
<< "4. Or Enter your wave frequency\n"
<< "5. Configure your port and pins\n"
<< "6. Enter your duty cycle\n"
<< "7. Generate your code\n"
<< "8. Exit\n\n" // allow the user to exit using the
<< "Choice: \n";
}
void menu() {
while (1) {
system("CLS");
char select[1]; // use as menu select variable
menu_bs();
unsigned long long int speed;
float time;
float cycle;
float cycle_high;
float cycle_low;
float period;
float frequency;
float duty;
char portletter;
string port;
string pinconf;
string pinhigh;
cin >> select;
if (select[0] == '1')
{
system("CLS");
cin.ignore();
cout << "There are a few things that you should note: \n"
<< "- Designed for use with P16F877A\n"
<< "- You can enter a delay time of 3.4e +/- 38\n"
<< "- For cycle counts <=1277, you only need to reserve for count1\n"
<< "- For cycle counts <=261125, you need to reserve both count1 and count2\n"
<< "- For cycle counts <=62620865, you need to reserve count1, count2 and count3\n"
<< "- The upper cycle limit for the code created is 62620861 cycles\n"
<< " - This would give a delay of about 62 seconds on a 1MHz clock\n";
system("PAUSE");
system("CLS");
}
if (select[0] == '2')
{
system("CLS");
cout << "Your clock speed\n\n";
cout << "Enter your clock speed in integer form: e.g. 5000000\n\n";
cin >> speed;
cout << "\n\nYou chose " << speed << "\n\n";
system("PAUSE");
system("CLS");
}
else if (select[0] == '3')
{
system("CLS");
cout << "Your wave period\n\n";
cout << "Enter your wave period in decimal form or integer: 3.5\n\n";
cin >> time;
cout << "\n\n";
cycle = time * speed;
cout << "\n\nYou chose a period of " << time << "s\n\n";
cout << "This corresponds to " << cycle << " cycles for one period.\n\n";
system("PAUSE");
system("CLS");
}
else if (select[0] == '4')
{
system("CLS");
cout << "Your wave frequency\n\n";
cout << "Enter your wave frequency in decmial form or integer: 0.2\n\n";
cin >> time;
cout << "\n\n";
cycle = speed / time;
cout << "\n\nYou chose a frequency of " << time << "Hz\n\n";
cout << "This corresponds to " << cycle << " cycles for your frequency\n\n";
system("PAUSE");
system("CLS");
}
else if (select[0] == '5')
{
system("CLS");
cout << "Your port configuration\n\n";
cout << "Enter one port which you'd like to mess with: e.g. PORTA, PORTB, PORTC\n\n";
cin.ignore();
getline(cin, port);
cout << "\n\n";
cout << "Now enter the write read configuration of the pins on your port: \n e.g. 00000000 = write all, 00001111 = write four, read 4\n\n";
getline(cin, pinconf);
cout << "\n\n";
cout << "Now enter the pin you'd like to toggle high/low: e.g. 1-7\n\n";
getline(cin, pinhigh);
pinhigh = pinhigh;
portletter = port.at(4);
if (pinconf.length() == 8 && port.length() == 5) {
cout << "\n\nYou're configured to: " << port << "(" << pinconf << ")\n\n";
cout << "You'll be using the TRIS" << port.at(4) << " register and will be toggling pin " << pinhigh<<endl<<endl;
} else cout << "\n\nYour port should be five characters long and your pin \nconfiguration should be eight characters because you have \neight pins. Try again.\n\n";
system("PAUSE");
system("CLS");
}
else if (select[0] == '6')
{
system("CLS");
cout << "Your wave duty cycle\n\n";
cout << "Enter your duty cycle as a decmial (not percentage): e.g. 50% duty cycle = 0.5\n\n";
cin >> duty;
cout << "\n\n";
if (duty >= 1 || duty <= 0) {
cout << "\n\nYou can't have a duty cycle greater than or less than 100% or 0% respectively.\n\n";
system("PAUSE");
system("CLS");
}
else {
cycle_high = cycle * duty;
cycle_low = cycle * (1 - duty);
cout << "\n\nYou chose a duty cycle of " << duty * 100 << "%\n\n";
cout << "This corresponds to " << cycle_high << " cycles for your logic high.\n";
cout << "This corresponds to " << cycle_low << " cycles for your logic low.\n\n";
system("PAUSE");
system("CLS");
}
}
else if (select[0] == '7')
{
system("CLS");
cout << cycle << cycle_high << port << pinconf;
code_calc(cycle, cycle_high, cycle_low, port, pinconf, pinhigh, portletter);
system("PAUSE");
system("CLS");
}
else if (select[0] == '8') // quit option
{
system("CLS");
exit(0);
system("PAUSE");
system("CLS");
}
else
{
system("CLS");
cout << "You have entered an invalid input, please try again.\n";
cout << "Try to enter a number corresponding to the menu item.\n";
system("PAUSE");
system("CLS");
}
}
}
int main() {
menu();
}
如果我在调用a, ... g 变量的先前if 语句之一中调用该函数,则它“排序”有效。但是,我认为因为我在 while 循环的开头启动了变量,所以它们的范围是整个 while 循环,在 if 语句的内部或外部。
所以,目前我的变量没有被传递给我的wv_calc() 函数,因此,我的代码没有内联变量a, b, ..., g。
有什么明显的我做错了吗?
【问题讨论】:
-
您的问题是关于带有
while循环的代码。你为什么要发布这个函数? -
尝试使用更有意义的变量名。我猜你重用了变量。
-
while 循环代码是不言自明的,我认为有人能够发现变量范围/传递变量的错误,但我现在将发布
while代码。 -
wv_calc甚至不使用您传递的大部分变量。 -
@Barmar,变量名是为了帖子的简单,我的实际程序中有引用周期数、频率、占空比和周期等的变量名。