"用C ++编写内容,在微处理器的Assembly中创建方波。 无论如何,我的布局如下:
#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();
}
如果我在之前调用if
变量的a, ... g
语句之一中调用该函数,它就会排序&#39;排序&#39;作品。但是,我想是因为我在while
循环开始时启动了变量,它们的范围是while
循环的整个范围,在if
语句的内部或外部。
因此,目前我的变量未传递给我的wv_calc()
函数,因此,我的代码缺少内联变量a, b, ..., g
。
有什么明显的我做错了吗?
答案 0 :(得分:0)
查看您的代码,很多问题都是由于代码组织不当造成的。
例如,假设您设置select == 2
,您是否看到了执行顺序。它将如下,您认为如何传递到cycle,cycle_high, cycle_low, port and pinconf
。它们是未初始化的,因此它们携带的值是依赖于实现的
if (select[0]=='2'){
system("CLS");
wv_calc(cycle, cycle_high, cycle_low, port, pinconf, pinhigh, portletter);
system("PAUSE");
system("CLS");
例如,您的程序用户必须知道在设置select == '1'
之前始终将port, pinconf, pinhigh, portletter
设置为初始化select = '2'
。这对您来说可能是显而易见的,但对于以编写方式生成代码的编译器而言则不是这样。
if (select[0]=='1'){
system("CLS");
cin.ignore();
getline(cin, port);
getline(cin, port);
getline(cin, pinconf);
getline(cin, pinhigh);
portletter = port.at(4);
此外,我看不到cycle, cycle_high, cycle_low
的值被设置的位置。在致电wv_calc
<强>更新强>
以下是您需要做的事情:
void menu() {
float time;
float cycle;
float cycle_high;
float cycle_low;
float period;
float frequency;
float duty;
unsigned long long int speed;
char portletter;
string port;
string pinconf;
string pinhigh;
while (1) {
system("CLS");
//char select[1]; //why do you need an array here
char select;
menu_bs();
cin >> select;
....
....