所以我在MPLABX中创建了这个程序,我主要是用来学习用C代码编写未来的项目。我想把一些硬件/寄存器的东西放到函数中,以便整洁和易于阅读。出于某种原因,我的PWM功能不断重复,尽管我做了什么我不能让它运行然后停止。
这就是我所拥有的:
#include <xc.h>
const int sineval[320] = {125, 127, 129, 131, 133, 135, 137, 139, 141, 143, 145, 146, 148, 150, 152,
154, 156, 158, 160, 161, 163, 165, 167, 169, 170, 172, 174, 176, 177, 179, 181, 182, 184, 185, 187,
188, 190, 191, 193, 194, 196, 197, 198, 200, 201, 202, 204, 205, 206, 207, 208, 209, 210, 211, 212,
213, 214, 215, 216, 217, 217, 218, 219, 219, 220, 221, 221, 222, 222, 223, 223, 223, 224, 224, 224,
225, 225, 225, 225, 225, 225, 225, 225, 225, 225, 225, 224, 224, 224, 223, 223, 223, 222, 222, 221,
221, 220, 219, 219, 218, 217, 217, 216, 215, 214, 213, 212, 211, 210, 209, 208, 207, 206, 205, 204,
202, 201, 200, 198, 197, 196, 194, 193, 191, 190, 188, 187, 185, 184, 182, 181, 179, 177, 176, 174,
172, 170, 169, 167, 165, 163, 161, 160, 158, 156, 154, 152, 150, 148, 146, 145, 143, 141, 139, 137,
135, 133, 131, 129, 127, 125, 123, 121, 119, 117, 115, 113, 111, 109, 107, 105, 104, 102, 100, 98,
96, 94, 92, 90, 89, 87, 85, 83, 81, 80, 78, 76, 74, 73, 71, 69, 68, 66, 65, 63, 62, 60, 59, 57, 56,
54, 53, 52, 50, 49, 48, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 33, 32, 31, 31, 30,
29, 29, 28, 28, 27, 27, 27, 26, 26, 26, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 27,
27, 27, 28, 28, 29, 29, 30, 31, 31, 32, 33, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46,
48, 49, 50, 52, 53, 54, 56, 57, 59, 60, 62, 63, 65, 66, 68, 69, 71, 73, 74, 76, 78, 80, 81, 83, 85,
87, 89, 90, 92, 94, 96, 98, 100, 102, 104, 105, 107, 109, 111, 113, 115, 117, 119, 121, 123};
//function prototypes
void fast_sine();
void fast_sine_off();
int main() {
OSCCONbits.SCS0 = 0; //This as 1 sets the micro to use the internal 32KHz RC Oscillator, 0 and it uses external OSC.
OSCCONbits.SCS1 = 1;
TRISA = 0xFF; //This shit has to be inside main!!!! Setting the whole 8 bit register A to inputs.
TRISD = 0xFF;
TRISCbits.RC0 = 0;
TRISCbits.RC1 = 0;
TRISCbits.RC4 = 1;
TRISCbits.RC5 = 1;
TRISCbits.RC6 = 1;
TRISB = 0x00;
//Setup Buttons
//int pb1 = PORTDbits.RD2;
//int pb2 = PORTDbits.RD3;
//int pb3 = PORTCbits.RC4;
//int pb4 = PORTCbits.RC5;
//define for delay funtion
#define _XTAL_FREQ 20000000
//sinewave function test
//for (int z = 0; z < 20; z++){
fast_sine();
//}
fast_sine_off();
/*
while (1) {
//sinewave without function
#define _XTAL_FREQ 20000000
//Sine wave
PORTCbits.RC1 = 0;
PR2 = 250; //Timer2 reset value.
T2CON = 4; //TIMER 2 ON.
CCP2CON = 60;
CCPR2L = 0;
for (int i = 0; i < 319; i++) {
__delay_us(49);
CCPR2L = sineval[i];
}
}
*/
//CCP2CON = 0;
//__delay_ms(10);
//fast_sine();
/*
if (pb4 == 1 && pb1 == 1 ){
fast_sine();
__delay_ms(100);
}
else if (pb1 == 1 && CCPR2L <= 13) {
CCPR2L = CCPR2L + 1;
LATCbits.LATC0 = 1;
__delay_ms(100);
}
else if (pb2 == 1 && CCPR2L >= 1 ){
CCPR2L = CCPR2L - 1;
LATCbits.LATC0 = 0;
__delay_ms(100);
}
else if (pb3 == 1 && PR2 >= 40){
PR2 = PR2 - 1;
LATCbits.LATC0 = 1;
__delay_ms(40);
}
else if (pb4 == 1 && PR2 <= 133 ){
PR2 = PR2 + 1;
LATCbits.LATC0 = 0;
__delay_ms(40);
}
*/
//}
}
void fast_sine() {
//#define _XTAL_FREQ 20000000
//Sine wave
PORTCbits.RC1 = 0;
PR2 = 250; //Timer2 reset value.
T2CON = 4; //TIMER 2 ON.
CCP2CON = 60;
CCPR2L = 0;
for (int i = 0; i < 319; i++) {
__delay_us(49);
CCPR2L = sineval[i];
}
LATCbits.LATC0 = 0;
}
void fast_sine_off(){
//Sine wave off
PORTCbits.RC1 = 0;
LATCbits.LATC0 = 0;
TRISCbits.RC1 = 1;
PR2 = 0; //Timer2 reset value.
T2CON = 0x00; //TIMER 2 ON.
CCP2CON = 0x00;
CCPR2L = 0;
LATCbits.LATC0 = 1;
__delay_ms(10);
LATCbits.LATC0 = 0;
__delay_ms(10);
}
答案 0 :(得分:0)
你没有告诉我们你如何推断正弦不停止。
你已经声明了两个函数(对不起,如果我错了,但代码不是很清楚,很多注释代码),fast_sine和fast_sine_off。
第一次测试: 你期望执行第一个fast_sine然后执行fast_sine_off,你还没有定义之后发生的事情,因为你让程序到达main函数的末尾。
在HiTech和新的XC编译器中,这导致执行结束,但由于这不是从终端调用的程序,执行结束通常会导致受控或不受控制的重置。由于您正在使用的PIC将重新启动,您可能会看到主代码无休止地重复。
如果你想确保控制它,我建议在调用fast_sine_off之后发出这样的指令:
while(true){ // if true is not recognized you an place while(1){
CLRWDT(); // just in case you have enabled watchdog timer, refresh it so you don't let it reset the CPU
};
第二次测试: 另一方面,您可以使用一个足够短的时间来启用看门狗定时器,以便在到达fast_sine函数执行结束之前触发它,并且您将看到会有类似的结果,无休止地执行fast_sine。
尝试禁用whatchdog计时器。
在任何两种情况下,我都建议您尝试在PICKIT3或类似工具上模拟代码,或者回退到MPLAB的模拟器,并验证您的代码是否达到了sine_off函数。如果您使用任何ICD工具甚至模拟器进行调试,您可能会非常快速地发现问题。