|
|
View previous topic :: View next topic |
Author |
Message |
fabiofacir
Joined: 01 Feb 2018 Posts: 2
|
Help, how can I implement this? [PIC16F1703] |
Posted: Thu Feb 01, 2018 11:02 am |
|
|
Hello, I'm working with a 16F1703 PIC mcu, and I want to begin a 7segment lcd cycle (0-9) loop at a touch of a button (A1), after that if I touch the button (A1) twice, I want the Pic to enter in sleep mode.
To do that, I implemented this:
Code: |
#include <test_interrupt.h>
byte const DataExit[10]={0b01000100,
0b01011111,
0b01100010,
0b01001010,
0b01011001,
0b11001000,
0b11000000,
0b01011110,
0b01000000,
0b01001000};
byte const bitMask[8]={1,2,4,8,16,32,64,128};
//show seven numbers
void segmentCycle(void){
int i, j;
for(i=0;i<10;i++){
for (j=0; j<8;j++){
output_low(CLK);
output_bit(DATA,DataExit[i] & bitMask[j]);
output_high(CLK);
}
delay_ms(7000);
output_low(CLR);
delay_ms(6000);
output_high(CLR);
}
}
#INT_IOC
void IOC_isr(void)
{
segmentCycle();
sleep();
}
void main()
{
port_a_pullups(0x02);
enable_interrupts(INT_IOC_A1);
enable_interrupts(INT_IOC_A1_H2L);
enable_interrupts(GLOBAL);
while(TRUE);
}
|
For now, if I touch the button, sometimes it starts, otherwise it don't.
What do you suggest?
I'm using ccs compiler. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Thu Feb 01, 2018 11:20 am |
|
|
What is the hardware ?
All mechanical switches (buttons) have a LOT of 'bounce'. This translates into 'noise'. That being multiple 1-0-1 transistions, easily seen on an Oscilloscope. Normally you use a 'strong' pullup and a small cap on the input pin to reduce this 'bounce'.
Honestly if you're going to 'play with PICs', invest in a 'scope. Even a 20MHz, 2 chnl, tube type for $50 is money very well spent. The ability to actually SEE the signals will make every project you do enjoyable as you learn how stuff works. A strong pullup is a resisitor of say 1K to 2K2. For the cap I use .68mfd as I have 1,000s of them here.
I'd start by getting the PIC to toggle an LED everytime you press the switch. Be sure to read the datasheet able the IOC feature. With some interrupts you NEED to read a register,port,etc. to clear the interrupt. Since I don't use that PIC ,you'll have to do the reading. Also check the Compiler manual ! It has FAQ and howto sections as well as HOW to use ISRs.
For small programs like this it's best to have ALL the code in one file. We don't know what's in 'test_interrupt.h'. There could be code there that is preventing the PIC from doing what you want. It also prevents us from copy/compile/test for you in our labs.
Jay |
|
|
fabiofacir
Joined: 01 Feb 2018 Posts: 2
|
|
Posted: Thu Feb 01, 2018 11:32 am |
|
|
I'm using a simple button.
Because of the noise I've tried to set up a:
Code: | port_a_pullups(0x02); |
I don't know If I cant use this way.
Inside the 'test_interrupt.h'. it is just the ports declaration and the pic.h:
Code: | #include <16F1703.h>
#device ADC=10
#use delay(internal=31kHz)
#define CLK PIN_A5
#define CLR PIN_A4
#define DATA PIN_C5
#define DELAY 1000 |
I know that the segmentCycle function works nice, because if I test it out of the interrupt it works fine.
I know this pic allows IOC feature, and the pic.h have it defined.
Any thoughts, how can I do this? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Thu Feb 01, 2018 12:08 pm |
|
|
A pullup of some form is essential or otherwise what is going to actually take the signal high?.
However the pullup doesn't help bounce. The point about bounce is that switches don't just go nicely on/off (actually there are some very specialist types like mercury wetted types that can do this, but they are very unusual). When a switch goes on or off, you get a sequence of on/off/on/off/on/off often lasting for several mSec. You can improve this with a capacitor.
You need to step back from trying to trigger sleep, and get reliable switch detection working first.
As a comment, the sleep should always be followed by a nop (delay_cycles(1);). The instruction after the sleep is preloaded, and should always be a nop.
Then there is a problem. You can't go to sleep if an interrupt is set. The IOC is set in the routine. The IOC can't generally be cleared until the pin that triggers it is read, and isn't cleared till the interrupt handler is exited. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|