为什么我的 PIC 的 LED 不闪烁?
我正在尝试开始使用 pic24,特别是 PIC24FJ64GA002,并且我查看了寄存器等数据表,但我仍然无法让它闪烁 LED。当我通过调试运行它时,它运行正确,但是当我尝试在图片上实际运行它时,它似乎根本无法运行。
我正在使用外部振荡器,特别是 8MHZ 振荡器,连接到引脚 9(OSCI) 和 10 (OSCO)。 Mplab 中的编译器是 C30。
数据表链接为:http://ww1.microchip.com/downloads/en/DeviceDoc /39881D.pdf
代码如下
//include basic header definition
#include <p24FJ64GA002.h>
//config
_CONFIG2(0x0200);
_CONFIG1(0x0800);
int i;
//main loop
int main(void)
{
OSCCON = 0x2280; //select external OSC, no PLL
AD1PCFG = 0xFFFF; //set to all digital I/O
TRISA = 0x0000; //configure all PortA as output
while(1) //Loop forever
{
LATAbits.LATA0 = 1; //RA0 = 1
Wait();
LATAbits.LATA0 = 1; //RA0 = 1
Wait();
}
}
int Wait(void) // gives me a nice delay of 1/3rd a second or so
{
for (int i = 0; i < 30000; i++)
{
for (int i = 0; i < 30; i++);
}
}
I am trying to get started with pic24's, specifically the PIC24FJ64GA002, and I have looked in the datasheet at the registers and whatnot, but I still cannot get it to blink the leds. When I run it via debug it runs correctly, but when I try to actually run it on the pic it seems to not run at all.
I am using an external Oscillator, a 8MHZ Oscillator specifically, connected to pins 9(OSCI) and 10 (OSCO). Compiler is C30 in Mplab.
Datasheet link is: http://ww1.microchip.com/downloads/en/DeviceDoc/39881D.pdf
The code is below
//include basic header definition
#include <p24FJ64GA002.h>
//config
_CONFIG2(0x0200);
_CONFIG1(0x0800);
int i;
//main loop
int main(void)
{
OSCCON = 0x2280; //select external OSC, no PLL
AD1PCFG = 0xFFFF; //set to all digital I/O
TRISA = 0x0000; //configure all PortA as output
while(1) //Loop forever
{
LATAbits.LATA0 = 1; //RA0 = 1
Wait();
LATAbits.LATA0 = 1; //RA0 = 1
Wait();
}
}
int Wait(void) // gives me a nice delay of 1/3rd a second or so
{
for (int i = 0; i < 30000; i++)
{
for (int i = 0; i < 30; i++);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你需要去嗨,等等,然后瞧,等等……你只是去嗨,等等,嗨,等等。
You need to go hi, wait, then lo, wait... you are just going hi, wait, hi, wait.
您使用什么优化级别进行编译?如果它仅在调试中工作,则优化器可能会将整个
Wait()
函数减少为无操作。尝试声明“volatile int i”。What optimization level are you compiling with? If it's only working in debug, it's possible the optimizer is reducing the whole
Wait()
function to a no-op. Try declaring `volatile int i'.谢谢大家的帮助,但我设置的配置位错误,当我在 mplab 的配置编辑器中设置它们时一切正常。
感谢您的帮助!
Thank you guys for all your help, but it was that I set the config bits wrong, when I set them in the config editor in mplab all works well.
Thank you for all your help!