|
|
View previous topic :: View next topic |
Author |
Message |
doguhanpala
Joined: 05 Oct 2016 Posts: 120
|
18f2550 led blink with interrupt SOLVED |
Posted: Mon Oct 10, 2016 4:42 am |
|
|
hello
I am new to pic and try to understand concept of interrupts. I want to blink a led with a button switch using analog read. I read some tutorials and watched some videos but there are still things that i don't understand.
Which code determines when the interrupt does its job? I thought the 2 lines in main do the the work but i am not sure.
I am trying to drive 2 leds to see my interrupt code is working or not. one led blinks with delay and the other one blinks only when switch is pressed.
So i can see 18f2550 can do 2 things same time.
I wrote this code but it doesnt work. Since i am new to pics and "newer" to interrupts i am sure i am doing something wrong. Any ideas?
Note: I know just asking what is wrong about code is not a good way of learning but i could not find a "really simple" tutorial. If you can send tutorial links that could be very helpful too. thanks!
Code: |
#include <18F2550.h>
#fuses INTRC_IO,HS, NOWDT, PUT, NOLVP,NOMCLR,NOPROTECT,NOLVP,NODEBUG,NOBROWNOUT,USBDIV,PLL5,CPUDIV1,VREGEN,NOPUT
#device ADC=10
#int_RTCC // i dont know what that line means?
#use delay(clock=4000000)
set_adc_channel(A0);
void isr(void) {
unsigned int16 encoder_val=0;
delay_us(10);
encoder_val = read_adc(); //read analog
if(encoder_val>=100) // if switch pressed, drive led1 to see interrupt is working.
{
output_high(PIN_A2); //led 1
}
else
{
output_low(PIN_A2);
}
}
#use delay(clock=20000000)
void main(void) {
enable_interrupts(int_RTCC); // what is rtcc and can i change this line?
enable_interrupts(GLOBAL);
set_tris_a(0x00000001); //analogread setup
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_INTERNAL);
setup_oscillator(OSC_4MHZ); // analog read setup
while(TRUE)
{
//led 2
output_high(PIN_A1); // blink led code
delay_ms(1000); // i blink 2 leds. one for switch and the other one for too see multifunctionalty works
output_low(PIN_A1);
delay_ms(1000);
}
} |
edit: hello guys, i watched some videos and found this code. it worked well for me. i hope it will be useful for you.
note: the code blinks one led, and opens another led when you press the button.
i connected switch to B0
led 1 (blinking led) to B1
and led 2 (interrupt led) to B2
Code: |
#include <18F2550.h>
#fuses INTRC_IO,HS, NOWDT, PUT, NOLVP, HSPLL,NOMCLR,NOPROTECT,NOLVP,NODEBUG,NOBROWNOUT,USBDIV,PLL5,CPUDIV1,VREGEN,NOPUT
#device ADC=10
#use delay(clock=4000000)
#int_EXT
void external_interrupt_isr()
{
output_high(PIN_B2); //open interrupt led
}
void main()
{
//setup lines
set_tris_b(0b1); //b0 is input, the voltage from the switch goes there
ext_int_edge(L_TO_H);
setup_oscillator(OSC_4MHZ);
ENABLE_INTERRUPTS(INT_EXT);
ENABLE_INTERRUPTS(GLOBAL);
while(TRUE)
{
output_low(PIN_B2); //close the interrupt led
output_high(PIN_B1); //blink led1
delay_ms(500);
output_low(PIN_B1);
delay_ms(500);
}
} |
just a tiny problem, i changed the ext_int_edge(L_TO_H); line to ext_int_edge(H_TO_L); and it still works. any ideas?
Last edited by doguhanpala on Tue Oct 11, 2016 2:03 am; edited 1 time in total |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9247 Location: Greensville,Ontario
|
|
Posted: Mon Oct 10, 2016 5:14 am |
|
|
First you've got to go back to 'basics' and do a LOT of reading. CCS supplies lots a examples, in the 'examples' folder, which is a great place to start.
When 'playing with PICs', you need to get a simple 1Hz LED program running. This will get you 'organized' as to what order the C code has to be as well as confirm the hardware is good.
Once the LED flashes properly THEN cut a 'Hello PC' program to see how the to send data to a PC ( aka RS-232 ), you'll either need a 'MAX232' chip or a TTL-USB module to get to the PC. I use the module, for $2 it's simple, cheap,easy to wire and works.
As for interrupts, CCS does have examples and you need to know that code inside the ISR must be SMALL! NO delays, NO printing, NO FP math ! Set 'flags', simple variable math (like x=100 or x++). ISRs by their nature are supposed to be FAST.
Others can help will the code you've presented , I'm not on my PICeng PC .
Jay |
|
|
doguhanpala
Joined: 05 Oct 2016 Posts: 120
|
|
Posted: Mon Oct 10, 2016 5:27 am |
|
|
temtronic wrote: | First you've got to go back to 'basics' and do a LOT of reading. CCS supplies lots a examples, in the 'examples' folder, which is a great place to start.
When 'playing with PICs', you need to get a simple 1Hz LED program running. This will get you 'organized' as to what order the C code has to be as well as confirm the hardware is good.
Once the LED flashes properly THEN cut a 'Hello PC' program to see how the to send data to a PC ( aka RS-232 ), you'll either need a 'MAX232' chip or a TTL-USB module to get to the PC. I use the module, for $2 it's simple, cheap,easy to wire and works.
As for interrupts, CCS does have examples and you need to know that code inside the ISR must be SMALL! NO delays, NO printing, NO FP math ! Set 'flags', simple variable math (like x=100 or x++). ISRs by their nature are supposed to be FAST.
Others can help will the code you've presented , I'm not on my PICeng PC .
Jay |
I did 1 hz led program and i did analog read with switch before jumping into interrupts. Now i am going to play around example codes. Thank you so much for the advice and info about ISR! |
|
|
|
|
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
|