完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
嗨,我正在研究一个项目,使用APIC12F1822 8引脚微控制器的LED闪光灯。我已经设置了几种不同的选通模式,现在我想用一个按钮开关作为输入,在3个选通模式和关断状态之间切换。我用附加的示意图来显示开关上的硬件去抖动,它连接到引脚RA2,它可以被设置为A。边沿触发中断,我想用一个积极的边缘触发中断。在读取数据表后,我使用ITCON和IOCAP寄存器在RA2上建立了一个正边缘中断的中断。我有一个ISR读取IOCAF标志位寄存器,并在检测到中断时增加选通模式,但是ISR永远不会被调用。我一直在使用我的PICtiT3(在注释掉了Ay-DelayySMS()函数来避免挂起之后)通过代码,并且它从不进入ISR。我认为问题可能是由TrISA寄存器引起的,我认为RA2(我使用的边沿触发中断)应该被设置为输入,但是我在数据表中找不到任何东西来告诉我在TrISA中我应该用RA0做什么。我试着把它设置为输入和输出,没有变化。有人能建议我如何解决这个问题吗?以下是我的代码:
以上来自于百度翻译 以下为原文 Hi, I am working on a project that strobes a LED using a PIC12F1822 8 pin microcontroller. I have set up a few different strobe patterns that work and now I want to use a button switch as an input to switch between the 3 strobe patterns and the off state. I am using hardware debouncing on the switch as shown by the attached schematic, which is connected to pin RA2 which can be set up as a edge triggered interrupt and I want to use a positive edge to trigger the interrupt. After reading the datasheet I set up the interrupts using the INTCON and IOCAP registers for a positive edge interrupt on RA2. I have an ISR that reads the IOCAF flag bit register and increments the strobe mode when the interrupt is detected. However the ISR doesn't ever get called. I have been stepping through the code using my PICkit3 (after commenting out the __delay_ms() functions to avoid hanging) and it never steps into the ISR. I think maybe the problem is being caused by the TRISA register, I think RA2 (the edge triggered interrupt I am using) should be set as an input, but I can't seem to find anything in the datasheet to tell me what I should do with RA0 in TRISA. I have tried setting it as both an input and an output, with no change. Can anyone suggest how I can fix this problem? Here is my code: /* * First PIC version of WatchOutLights * * File: main_1.c * * Author: root * * Created on December 14, 2017, 11:28 PM */ // PIC12F1822 Configuration Bit Settings // 'C' source line config statements // CONFIG1 #pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin) #pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled) #pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR) #pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled) #pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled) #pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled) #pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin) #pragma config IESO = ON // Internal/External Switchover (Internal/External Switchover mode is enabled) #pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled) #pragma config WDTE = OFF // CONFIG2 #pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off) #pragma config PLLEN = OFF // PLL disable #pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset) #pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.) #pragma config LVP = OFF // Low-Voltage Programming Enable (Low-voltage programming enabled) // #pragma config statements should precede project file includes. // Use project enums instead of #define for ON and OFF. #include #include #define _XTAL_FREQ 31000 // 31kHz long unsigned int lastDebounceTime = 0; long debounceDelay = 50; char strobeMode = 1; uint8_t ioc = 0; // used to read IOC interrupt into /******************************************** * Function Prototypes *********************************************/ void initPort(); void initInterrupts(); void main(void) { initPort(); initInterrupts(); while(1) { switch(strobeMode) { case 0: LATAbits.LATA5 = 0; // turn off LED break; case 1: LATAbits.LATA5 = 1; // strobe pattern #1 __delay_ms(50); LATAbits.LATA5 = 0; __delay_ms(500); LATAbits.LATA5 = 1; __delay_ms(50); LATAbits.LATA5 = 0; __delay_ms(120); LATAbits.LATA5 = 1; __delay_ms(50); LATAbits.LATA5 = 0; __delay_ms(1000); break; case 2: { // strobe pattern #2 uint8_t i; for (i=0; i<=3; i++) { LATAbits.LATA5 = 1; __delay_ms(50); LATAbits.LATA5 = 0; __delay_ms(100); } LATAbits.LATA5 = 0; break; __delay_ms(400); } case 3: // strobe pattern #3 LATAbits.LATA5 = 1; __delay_ms(500); LATAbits.LATA5 = 0; __delay_ms(500); break; } } return; } void initPort() { // initialise PORTA OSCCON = 0b10000011; // 31kHz TRISA = 0x04; // PortA 000000 RA5 is LED, RA2 is Button Switch Interrupt // Pin 2 as INT input, pin 5 as LED output LATAbits.LATA0 = 0; // tie unused pins to GND LATAbits.LATA1 = 0; LATAbits.LATA4 = 0; LATAbits.LATA5 = 0; // turn off LED } void initInterrupts() { INTCON = 0b10001000; // enable Global Interrupt Enable, // disable Peripheral Interrupt Enable. // disable Timer0 Overflow Interrupt Enable // disable INT External Interrupt Enable // enable Interrupt On Change Enable // set Timer0 Interrupt Flag to 0 // set External INT Interrupt to 0 // set IOC flag bit to 0 IOCAP = 0b00000100; // enable only RA2 as PinChange Int } void interrupt isr() { INTCON = 0x00; // disable Interrupts to avoid multiple calls ioc = IOCAF & 0b00000100; // read bit 2 only IOCAF = 0x00; // reset IntOnChange register so other interrupts can be caught strobeMode++; INTCON = 0b10001000; // enable Global Interrupt Enable and Interrupt On Change again } Attached Image(s) |
|
相关推荐
16个回答
|
|
以上来自于百度翻译 以下为原文 void interrupt isr() { ioc = IOCAF & 0b00000100; // read bit 2 only IOCAF = 0x00; // reset IntOnChange register so other interrupts can be caught strobeMode++; } |
|
|
|
正如NKurzman所说,加上:每当你写一个变量到ISR内部,然后在ISR外部读取它,加上,添加到你的IntPoT()函数,没有RAI,RA2将在模拟模式下,所以总是读为零,所以永远不会产生中断。他的。在“PATA寄存器部分”,特别是段落末尾的盒装注释中,查看“122.1安塞拉寄存器”的段落。这对于所有具有模拟能力输入的PIC都是正确的。它捕获所有的第一个计时器。
以上来自于百度翻译 以下为原文 As NKurzman said, plus: volatile char strobeMode = 1; This is required whenever you write to a variable inside an ISR, and read it outside the ISR. Plus, add to your initPort() function ANSELA = 0; // switch all PORTA pins to digital mode. Without that, RA2 will be in analog mode, so will always read as zero, so will never generate an interrupt. The datasheet DOES tell you all about this. Have a look at the paragraph "12.2.1 ANSELA REGISTER" in the "PORTA register section, and particularly the boxed Note at the end of the paragraph. This is true for all PICs that have analog capable inputs. It catches all first-timers out |
|
|
|
谢谢你们俩。非常感谢您的帮助。我想我是如此专注于TrISA寄存器和数据表的中断部分,我完全错过了安塞拉寄存器。
以上来自于百度翻译 以下为原文 Thank you both. Your help is greatly appreciated. I think I was so focused on the TRISA register and the interrupt section of the datasheet I completely missed the ANSELA register. |
|
|
|
现在我试着让它进入省电模式,如果我在微芯片网站上读到,对于XC8我应该使用睡眠(),然后等待一个中断来唤醒它。然而,当我在电流表0中挂上我的电表来检查当前的抽签时,我大约6Ma。我改变了CAS。e 0语句到:
以上来自于百度翻译 以下为原文 Now I'm trying to make it enter powersave mode if (strobeMode == 0) I read at Microchips website that for XC8 I should use SLEEP(); which will then wait for a interrupt to wake it. However when I hook up my ammeter to check the current draw when in strobeMode 0, I'm getting about 6mA. I altered my case 0 statement to: case 0: LATAbits.LATA5 = 0; // turn off LED SLEEP(); // save power until interrupt triggered break; |
|
|
|
6Ma在哪里?PIC没有太多的运行,监管者画出电流(很多)和连接PIC的任何东西。
以上来自于百度翻译 以下为原文 6Ma where? The PIC does not draw that much running. Regulators draw current ( some a lot) and anything connected to the PIC. |
|
|
|
我正在测量我的5V调节器的输出电流。我已经把我的PACKIT3从我的电脑上断开了,但是它仍然连接到我正在使用的PIC12F1822上,这样就可以解释一些当前的绘制。当我到家的时候,我会检查没有PICTIT3连接到PIC。
以上来自于百度翻译 以下为原文 I was measuring the output current of my 5V regulator. I had disconnected my PICkit3 from my computer, but it was still connected to the PIC12F1822 I am using, so that may account for some of the current draw. When I get home I'll check without the PICkit3 connected to the PIC. |
|
|
|
如果你的调节器是一所老学校7805,他们有很高的静态电流。
以上来自于百度翻译 以下为原文 if your regulator is an old school 7805, they have high quiescent current. |
|
|
|
我现在的理解是,PIC只会画出它需要的电流,虽然我可能错了。我正在使用我的ARDUINO克隆OSEPP UNO克隆的5V电源轨作为PIC的电源输入。我刚刚从PIC12F1822断开了我的PICTIT3,我的万用表显示PIC在睡眠命令中画出了27 UA,这仍然比数据表似乎说的要多。应该画(30Na1.1.8V)。在运行过程中,PIC画出大约3Ma,但是它驱动LED,所以大部分电流都将达到这个值。
以上来自于百度翻译 以下为原文 My current understanding is that the PIC will only draw as much current as it needs, although I may be quite wrong. I am using the 5V power rails of my Arduino clone OSEPP Uno clone as the power input to the PIC. I just disconnected my PICkit3 from the PIC12f1822 and my multimeter shows that the PIC is drawing 27uA during the sleep command, which is still more than the nA that the datasheet seems to be saying it should be drawing (30nA @ 1.8V). During operation the PIC is drawing about 3mA, however it is driving a LED, so most of the current would be going to that. |
|
|
|
以防万一我的代码引起问题,在这里。在SLMOBMODE 0,它应该睡觉。
以上来自于百度翻译 以下为原文 Just in case my code is causing problems, here it is. In strobeMode 0, it should be sleeping. /* * First PIC version of WatchOutLights * * File: main_1.c * * Author: root * * Created on December 14, 2017, 11:28 PM */ // PIC12F1822 Configuration Bit Settings // 'C' source line config statements // CONFIG1 #pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin) #pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled) #pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR) #pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled) #pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled) #pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled) #pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin) #pragma config IESO = ON // Internal/External Switchover (Internal/External Switchover mode is enabled) #pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled) #pragma config WDTE = OFF // CONFIG2 #pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off) #pragma config PLLEN = OFF // PLL disable #pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset) #pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.) #pragma config LVP = OFF // Low-Voltage Programming Enable (Low-voltage programming enabled) // #pragma config statements should precede project file includes. // Use project enums instead of #define for ON and OFF. #include #include #define _XTAL_FREQ 31000 // 31kHz long unsigned int lastDebounceTime = 0; long debounceDelay = 50; volatile char strobeMode = 0; uint8_t ioc = 0; // used to read IOC interrupt into /******************************************** * Function Prototypes *********************************************/ void initPort(); void initInterrupts(); void main(void) { initPort(); initInterrupts(); while(1) { if (strobeMode > 3){ strobeMode = 0; } switch(strobeMode) { case 0: LATAbits.LATA5 = 0; // turn off LED SLEEP(); // save power until interrupt triggered NOP(); break; case 1: LATAbits.LATA5 = 1; // strobe pattern #1 __delay_ms(50); LATAbits.LATA5 = 0; __delay_ms(500); LATAbits.LATA5 = 1; __delay_ms(50); LATAbits.LATA5 = 0; __delay_ms(120); LATAbits.LATA5 = 1; __delay_ms(50); LATAbits.LATA5 = 0; __delay_ms(1000); break; case 2: { // strobe pattern #2 uint8_t i; for (i=0; i<=3; i++) { LATAbits.LATA5 = 1; __delay_ms(50); LATAbits.LATA5 = 0; __delay_ms(100); } LATAbits.LATA5 = 0; __delay_ms(400); break; } case 3: // strobe pattern #3 LATAbits.LATA5 = 1; __delay_ms(500); LATAbits.LATA5 = 0; __delay_ms(500); break; } } return; } void initPort() { // initialise PORTA OSCCON = 0b10000011; // 31kHz TRISA = 0x04; // PortA 000000 RA5 is LED, RA2 is Button Switch Interrupt // Pin 2 as INT input, pin 5 as LED output LATAbits.LATA0 = 0; // tie unused pins to GND LATAbits.LATA1 = 0; LATAbits.LATA4 = 0; LATAbits.LATA5 = 0; // turn off LED ANSELA = 0; // switch all PORTA pins to digital mode. } void initInterrupts() { INTCON = 0b10001000; // enable Global Interrupt Enable, // disable Peripheral Interrupt Enable. // disable Timer0 Overflow Interrupt Enable // disable INT External Interrupt Enable // enable Interrupt On Change Enable // set Timer0 Interrupt Flag to 0 // set External INT Interrupt to 0 // set IOC flag bit to 0 IOCAP = 0b00000100; // enable only RA2 as PinChange Int } void interrupt isr() { ioc = IOCAF & 0b00000100; // read bit 2 only IOCAF = 0x00; // reset IntOnChange register so other interrupts can be caught strobeMode++; } |
|
|
|
我现在使用SpkFalm LiPoost升压转换器,由2节AA电池供电,为PIC提供3.3V电源。LiPoost升压转换器的静态电流小于55μA,但PIC具有相同的电流消耗,当我使用我的ARDUNO作为电源(25UA)时。我的ARDUNO使用MC3269—5G 800毫安LDO稳压器,静态电流为20mA,5V,但是一些电流将被车载硬件使用。因此,当PIC处于睡眠状态时,电流(和先前)的电流消耗是25UA,不管使用哪种电源。PIC使用30Na @ 1.8V。我可以正确地说,如果它运行在更高的电压(3.3V),它会消耗更少的电流,还是会更多?我也尝试过从昨晚2V使用5V电源和分压器运行。我去掉了LED的电流限制电阻(1.8V的正向电压),但是我不能让它运行。
以上来自于百度翻译 以下为原文 I am now using a Sparkfun Lipower Boost Converter being powered by 2 AA batteries as the power source providing 3.3V to the PIC. The LiPower Boost Converter has a quiescent current of less than 55uA, but the PIC has the same current consumption as when I was using my Arduino as a power supply (25uA). My Arduino uses a MC33269-5G 800mA LDO regulator with a quiescent current of 20mA at 5V, but some current would be used by the onboard hardware. So the current (and previous) current consumption when the PIC is sleeping is 25uA, regardless of which power source is being used. The according to the datasheet the PIC uses 30nA @ 1.8V. Would I be correct to say that if it is running at a higher voltage (3.3V) that it would consume less current, or would it be more? Also I tried running it from 2V last night using a 5V supply and a voltage divider. I removed the current limiting resistor for the LED (forward voltage of 1.8V) but I couldn't get it to run. |
|
|
|
你的配置设置是什么?如何配置所有PIC管脚(输入/输出,高/低,模拟/数字),它们连接到什么?任何由PIC I/O引脚提供的电流增加了你的消耗量。
以上来自于百度翻译 以下为原文 More What are your CONFIG settings? How have you configured all your PIC pins (in/out, high/low, analog/digital), and what are they connected to? Any current sourced by a PIC I/O pin adds to your consumption. |
|
|
|
这是我当前的代码。如果我把中断在RANK PIN(RA2)设置为输出或输入,我不确定是否使用TrISA在ItItPoT()中是否重要。
以上来自于百度翻译 以下为原文 Here is my current code. I'm not sure if in initPort() using TRISA whether it matters if I set my interrupt on change pin (RA2) as an output or input. /* // PIC12F1822 Configuration Bit Settings // 'C' source line config statements // CONFIG1 #pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin) #pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled) #pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR) #pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled) #pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled) #pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled) #pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin) #pragma config IESO = ON // Internal/External Switchover (Internal/External Switchover mode is enabled) #pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled) #pragma config WDTE = OFF // CONFIG2 #pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off) #pragma config PLLEN = OFF // PLL disable #pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset) #pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.) #pragma config LVP = OFF // Low-Voltage Programming Enable (Low-voltage programming enabled) // #pragma config statements should precede project file includes. // Use project enums instead of #define for ON and OFF. #include #include #define _XTAL_FREQ 31000 // 31kHz //long unsigned int lastDebounceTime = 0; //long debounceDelay = 50; volatile char strobeMode = 0; uint8_t ioc = 0; // used to read IOC interrupt into /******************************************** * Function Prototypes *********************************************/ void initPort(); void initInterrupts(); void main(void) { initPort(); initInterrupts(); while(1) { if (strobeMode > 3){ strobeMode = 0; } switch(strobeMode) { case 0: LATAbits.LATA5 = 0; // turn off LED SLEEP(); // save power until interrupt triggered NOP(); break; case 1: LATAbits.LATA5 = 1; // strobe pattern #1 __delay_ms(50); LATAbits.LATA5 = 0; __delay_ms(500); LATAbits.LATA5 = 1; __delay_ms(50); LATAbits.LATA5 = 0; __delay_ms(120); LATAbits.LATA5 = 1; __delay_ms(50); LATAbits.LATA5 = 0; __delay_ms(1000); break; case 2: { // strobe pattern #2 uint8_t i; for (i=0; i<=3; i++) { LATAbits.LATA5 = 1; __delay_ms(50); LATAbits.LATA5 = 0; __delay_ms(100); } LATAbits.LATA5 = 0; __delay_ms(400); break; } case 3: // strobe pattern #3 LATAbits.LATA5 = 1; __delay_ms(500); LATAbits.LATA5 = 0; __delay_ms(500); break; } } return; } void initPort() { // initialise PORTA OSCCON = 0b10000011; // 31kHz TRISA = 0x04; // PortA 000100 RA5 is LED, RA2 is Button Switch Interrupt // Pin 2 as INT input, pin 5 as LED output LATAbits.LATA0 = 0; // tie unused pins to GND LATAbits.LATA1 = 0; LATAbits.LATA4 = 0; LATAbits.LATA5 = 0; // turn off LED ANSELA = 0; // switch all PORTA pins to digital mode. } void initInterrupts() { INTCON = 0b10001000; // enable Global Interrupt Enable, // disable Peripheral Interrupt Enable. // disable Timer0 Overflow Interrupt Enable // disable INT External Interrupt Enable // enable Interrupt On Change Enable // set Timer0 Interrupt Flag to 0 // set External INT Interrupt to 0 // set IOC flag bit to 0 IOCAP = 0b00000100; // enable only RA2 as PinChange Int } void interrupt isr() { ioc = IOCAF & 0b00000100; // read bit 2 only IOCAF = 0x00; // reset IntOnChange register so other interrupts can be caught strobeMode++; } |
|
|
|
什么是连接到引脚?它是否被驱动到一个有效的高或低水平?这就打开了PLL,使用了HfTimoC的3KHz。你试过写0来代替这个寄存器吗?还要注意,BOR电路也需要电源。禁用BOR的最小功率。
以上来自于百度翻译 以下为原文 What is connected to the pin? Is it being driven to a valid high or low level? OSCCON = 0b10000011; // 31kHz This turns on the PLL, and uses 31kHz from the HFINTOSC. Have you tried writing zero to this register instead? Also note, the BOR circuit takes power too. Disable the BOR for minimum power. |
|
|
|
它可以帮助使用100欧姆电阻器(和旁路电容器)从调节器的输出到PIC的VDD。如果你的DMM可以读取100 UV(199.9毫伏范围),你可以看到1 UA/计数。你可以用一个更高的电阻来看到Na。
以上来自于百度翻译 以下为原文 It may help to use a 100 ohm resistor (and bypass capacitor) from the output of the regulator to the PIC's Vdd. If your DMM can read 100 uV (199.9 mV range) you can see 1 uA/count. You can use a higher value resistor to see nA. |
|
|
|
以上来自于百度翻译 以下为原文 BOREN = ON That also uses current. anything you leave on will contribute to the "Base Sleep Current" you are looking at. |
|
|
|
根据建议,我做了以下几点:我还增加了100欧姆电阻和0.1UF旁路盖之间的调节器和PIC VDD的输出。还有很多,当PIC醒来时,LED变得非常模糊。在配置词中,我已经关闭了以下内容:另外,我还评论了:因为BLUNOUT重置是关闭的。当PIC在睡觉时,所有这些都使我下降到21.3UA。还有什么想法能让我刮掉更多的电流?现在看来它应该运行在最脆弱的骨头上。这是我现在的全部代码:
以上来自于百度翻译 以下为原文 As per the suggestions, I have done the following: BOREN = OFF; OSCCON = 0b00000000; LATAbits.RA2 = 0; // pull pin to GND I also added a 100 ohm resistor and 0.1uF bypass cap between output of regulator and PIC Vdd. Much more than that and the LED gets very dim when the PIC is awake. Also in the Config words I have turned off the following: #pragma config MCLRE = OFF #pragma config IESO = OFF // Internal/External Switchover (Internal/External Switchover mode is disabled #pragma config FCMEN = OFF // Fail-Safe Clock Monitor disabled #pragma config PLLEN = OFF // PLL disable #pragma config STVREN = OFF // Stack Overflow/Underflow Reset disabled Additionally I commented out: #pragma config BORV = LO // Brown-out Reset Voltage Selection since BrownOut reset is off. All of that gets me down to 21.3uA when the PIC is sleeping. Any more ideas where else I can shave off more current? It seems like it should be running on the barest of bones now. Here is my full code now: // PIC12F1822 Configuration Bit Settings // 'C' source line config statements // CONFIG1 #pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin) #pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled) #pragma config MCLRE = OFF // MCLR Pin Function Select (MCLR/VPP pin function is MCLR) #pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled) #pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled) #pragma config BOREN = OFF // Brown-out Reset Disable to Save Current (Brown-out Reset enabled) #pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin) #pragma config IESO = OFF // Internal/External Switchover (Internal/External Switchover mode is disabled) #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled) #pragma config WDTE = OFF // CONFIG2 #pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off) #pragma config PLLEN = OFF // PLL disable #pragma config STVREN = OFF // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset) //#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.) #pragma config LVP = OFF // Low-Voltage Programming Enable (Low-voltage programming enabled) // #pragma config statements should precede project file includes. // Use project enums instead of #define for ON and OFF. #include #include #define _XTAL_FREQ 31000 // 31kHz //long unsigned int lastDebounceTime = 0; //long debounceDelay = 50; volatile char strobeMode = 0; uint8_t ioc = 0; // used to read IOC interrupt into /******************************************** * Function Prototypes *********************************************/ void initPort(); void initInterrupts(); void main(void) { initPort(); initInterrupts(); while(1) { if (strobeMode > 3){ strobeMode = 0; } switch(strobeMode) { case 0: LATAbits.LATA5 = 0; // turn off LED SLEEP(); // save power until interrupt triggered NOP(); break; case 1: LATAbits.LATA5 = 1; // strobe pattern #1 __delay_ms(50); LATAbits.LATA5 = 0; __delay_ms(500); LATAbits.LATA5 = 1; __delay_ms(50); LATAbits.LATA5 = 0; __delay_ms(120); LATAbits.LATA5 = 1; __delay_ms(50); LATAbits.LATA5 = 0; __delay_ms(1000); break; case 2: { // strobe pattern #2 uint8_t i; for (i=0; i<=3; i++) { LATAbits.LATA5 = 1; __delay_ms(50); LATAbits.LATA5 = 0; __delay_ms(100); } LATAbits.LATA5 = 0; __delay_ms(400); break; } case 3: // strobe pattern #3 LATAbits.LATA5 = 1; __delay_ms(500); LATAbits.LATA5 = 0; __delay_ms(500); break; } } return; } void initPort() { // initialise PORTA OSCCON = 0b00000000; // 31kHz TRISA = 0x04; // PortA 000100 RA5 is LED, RA2 is Button Switch Interrupt // Pin 2 as INT input, pin 5 as LED output LATAbits.LATA0 = 0; // tie unused pins to GND LATAbits.LATA1 = 0; LATAbits.LATA2 = 0; // tie input to GND LATAbits.LATA4 = 0; LATAbits.LATA5 = 0; // turn off LED ANSELA = 0; // switch all PORTA pins to digital mode. } void initInterrupts() { INTCON = 0b10001000; // enable Global Interrupt Enable, // disable Peripheral Interrupt Enable. // disable Timer0 Overflow Interrupt Enable // disable INT External Interrupt Enable // enable Interrupt On Change Enable // set Timer0 Interrupt Flag to 0 // set External INT Interrupt to 0 // set IOC flag bit to 0 IOCAP = 0b00000100; // enable only RA2 as PinChange Int } void interrupt isr() { ioc = IOCAF & 0b00000100; // read bit 2 only IOCAF = 0x00; // reset IntOnChange register so other interrupts can be caught strobeMode++; } |
|
|
|
只有小组成员才能发言,加入小组>>
5183 浏览 9 评论
2005 浏览 8 评论
1932 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3178 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2229 浏览 5 评论
739浏览 1评论
626浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
510浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
637浏览 0评论
535浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-27 23:25 , Processed in 1.493982 second(s), Total 108, Slave 92 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号