View previous topic :: View next topic |
Author |
Message |
gahaas
Joined: 05 Jan 2020 Posts: 2
|
programming questions in ccs |
Posted: Sun Jan 05, 2020 2:40 pm |
|
|
Good afternoon folks. I'm new to the ccs world. I am posting my pic code here in the forum.
https://drive.google.com/file/d/12GWEDyNdQ6BEimHyXzYTLoF8qlU4VvQW/view?usp=sharing
He is reporting an error in the void sensors (void) line. I do not know what it is. Can someone help me?
Code: |
#device PIC16F628A
#fuses mclr,hs,nowdt
#use delay (clock=10000000)
#define ra0 40
#define global 0x0BC0
#define INT_EXT 0x60000B10
#define H_TO_L 0
#INT_EXT
void sensores (void)
{
DISABLE_INTERRUPTS(global);
disable_interrupts(INT_EXT);
output_low(ra0);
delay_ms(5000);
DISABLE_INTERRUPTS(global);
disable_interrupts(INT_EXT);
return;
}
void main()
{
setup_comparator(0x0ff07);
enable_interrupts(global);
enable_interrupts(INT_EXT);
EXT_INT_EDGE(H_TO_L);
PORT_B_PULLUPS(1);
set_tris_a(0b0);
set_tris_b(0b1);
output_low(ra0);
while(1)
{
output_high(ra0);
delay_ms(1000);
output_low(ra0);
delay_ms(1000);
}
} |
Last edited by gahaas on Sun Jan 05, 2020 7:01 pm; edited 1 time in total |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Jan 05, 2020 3:03 pm |
|
|
I fixed your program so it should now compile without errors or warnings:
Code: |
#include <16F628A.h>
#fuses HS,NOWDT, BROWNOUT, PUT, NOLVP
#use delay (clock=10M)
#INT_EXT
void sensores (void)
{
output_low(PIN_A0);
}
//===========================
void main()
{
setup_comparator(NC_NC_NC_NC);
ext_int_edge(H_TO_L);
enable_interrupts(INT_EXT);
clear_interrupt(INT_EXT);
enable_interrupts(GLOBAL);
port_b_pullups(TRUE);
output_low(PIN_A0);
while(TRUE)
{
output_high(PIN_A0);
delay_ms(1000);
output_low(PIN_A0);
delay_ms(1000);
}
}
|
|
|
|
gahaas
Joined: 05 Jan 2020 Posts: 2
|
|
Posted: Sun Jan 05, 2020 6:50 pm |
|
|
Thank you so much PCM programmer. I could also compile on mplab. I did some research on the internet about what I wanted to do. I wrote this code below. LED flashes normal on PIN_A0. But when I press a button on port b, the PIN_A1 led does not light up and does not go out. Is something wrong with the interruption I made? I simulated in the proteus program.
Code: |
#include <16F628A.h>
#fuses intrc_io,HS,NOWDT, BROWNOUT, PUT, NOLVP
#use delay (clock=10M)
#use fast_io(a)
#use fast_io(b)
int x;
#INT_RB
void sensores (void)
{
if(x==0)
{
x=1;
output_high(PIN_A1);
delay_ms(5000);
return;
}
else
{
x=0;
output_low(PIN_A1);
delay_ms(1000);
return;
}
}
//===========================
void main()
{
setup_comparator(NC_NC_NC_NC);
set_tris_a(0x00);
set_tris_b(0Xff);
ext_int_edge(H_TO_L);
enable_interrupts(GLOBAL);
enable_interrupts(INT_RB);
clear_interrupt(INT_RB);
port_b_pullups(TRUE);
output_low(PIN_A0);
x=1;
while(TRUE)
{
output_high(PIN_A0);
delay_ms(1000);
output_low(PIN_A0);
delay_ms(1000);
}
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19518
|
|
Posted: Mon Jan 06, 2020 12:08 am |
|
|
As a 'comment', you must never (ever) use 'enable_interrupts(GLOBAL)'
inside an interrupt handler. Doing so is a 'no no' on a PIC.
Doing so, can result in interrupts occuring inside the interrupt handler
which is a way to crash the PIC. As you see, PCM_Programmer removed
these.
Then further, consider doing your delay, by triggering a timer
and using a timer interrupt. As you currently have, with delays inside an
interrupt, your system will stop in these delays and cannot do anything
else while they are occurring. Not good.... Generally consider that
anything beyond very short delays should never be done inside an
interrupt handler. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jan 06, 2020 1:00 am |
|
|
gahaas wrote: |
LED flashes normal on PIN_A0. But when I press a button on port b,
the PIN_A1 led does not light up and does not go out.
|
This one works.
Code: | #include <16F628A.h>
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=4M)
// This function reads Port B without changing the TRIS.
int8 input_state_b(void)
{
#byte PortB = getenv("SFR:PORTB")
return(PortB);
}
//--------------------------------
#INT_RB
void sensores (void)
{
int8 current;
static int8 last=0;
current = input_state_b();
if((!bit_test(current,4)) && (bit_test(last,4)) ) // Press switch
//if((!bit_test(last,4)) && (bit_test(current,4)) ) // Release switch
{
output_toggle(PIN_A1);
}
last = current;
delay_ms(1); // Debounce delay for switch
}
//===========================
// Create a 2nd instance of CCS delay library
// to avoid compiler warning about re-entrancy.
#use delay(clock=4M)
void main()
{
int8 temp;
port_b_pullups(TRUE);
delay_us(10); // Allow time for weak pull-ups to pull up
// Read PortB to clear any pre-existing change
// condition for PortB.
temp = input_state_b();
enable_interrupts(INT_RB);
clear_interrupt(INT_RB);
enable_interrupts(GLOBAL);
output_low(PIN_A1);
while(TRUE)
{
output_high(PIN_A0);
delay_ms(500);
output_low(PIN_A0);
delay_ms(500);
}
} |
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9229 Location: Greensville,Ontario
|
|
Posted: Mon Jan 06, 2020 8:21 am |
|
|
as a general comment
never,ever put delays in an ISR !!
Code within an ISR needs to be short and fast.
So...
No delays
No printing
No math(esp. floating point)
Jay |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jan 06, 2020 9:55 am |
|
|
I realize that, but a debounce was needed for this example, and I didn't
have time to think up, code, and test in hardware, an example. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19518
|
|
Posted: Mon Jan 06, 2020 1:00 pm |
|
|
Yes.
A short delay is usually acceptable. I saw yours and decided it was short
enough to probably not cause problems. Something like 10 or 20uSec
should be enough to debounce most switches and might be a better
value to use. |
|
|
|