![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
JimB
Joined: 25 Aug 2005 Posts: 65 Location: Huntington Beach, CA
|
Help with External Interrupt |
Posted: Mon Oct 31, 2005 2:36 pm |
|
|
I am driving some external electronics with a square wave produced by the PWM output. I want to read the peak with the A/D and have attempted to use an external interrupt that detects a H_to_L transition. I have fed back the PWM into B0 to trigger the interrupt. When I de-comment the three lines in the code below it ceases to run, even the RS232 has no output. Also is there a way to use an internal signal to trip the interrupt or use the CC1 interrupt?
Any help appreciated.
Code: |
#include <16F877A.h>
#device ICD=TRUE
#fuses HS,NOWDT,NOPROTECT,NOLVP
#device ADC=10
#use delay(clock=20000000)
#define output_high(PIN_A5,PIN_B0)
#define PUSH_BUTTON PIN_A5
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <lcd_01.c>
#int_ext
main()
{
int i;
long value, total, Ave;
float volts;
lcd_init();
{
while(TRUE)
if (input(PUSH_BUTTON))// Put ! before "input" to enable PB
{
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_1, 250, 1);
set_pwm1_duty(125);
setup_adc_ports(AN0_AN1_VSS_VREF);//Analog in A0, A1 and ext ref A3
setup_adc( ADC_CLOCK_INTERNAL );
set_adc_channel( 0 );
for(i=0; i<=5; ++i)
{
//enable_interrupts(int_ext);
//enable_interrupts(global);
//EXT_INT_EDGE(l_to_h);
delay_us(5);
value = Read_ADC();
total = total + value;
}
Ave=total/6;
volts=1.238*Ave/1024;
printf("\n\r Voltage = %f \n\r",volts);
printf(lcd_putc,"\fVoltage=%4.3f", volts);
total=0;
}
else
setup_ccp1(CCP_OFF);
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Oct 31, 2005 2:46 pm |
|
|
Quote: | When I de-comment the three lines in the code below it ceases to run |
You have no interrupt service routine. You need to put one
under your #int_ext statement. There are a few other things
wrong with your code, but at a minimum, you need to do this.
Example:
Code: | #int_ext
void int_ext_isr(void)
{
// Put code here to service the interrupt.
} |
|
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
Additional documentation? |
Posted: Mon Oct 31, 2005 8:40 pm |
|
|
I would like to know if there are any books or other documentation that get into the details of using the micro and the C compiler? I find that the only way to get information that is not contained in the existing compiler manual, the CCS ACE board exercises and the microcontroller data sheet, is to ask here. Much of the terminology is foreign to me, even though I have one C course under my belt and have been designing electronics for 40 years. I do well when there is documentation to study.
This question asked here being typical. Reading the data sheets is helpful but terminology is unclear to me at this level in the learning curve. The example files that come with it are also very helpful but not complete for sure.
I have seen several that address programming in assembly, but that would stop me in my tracks at this point.
Is there a text that takes one through the various capabilities of the compiler and processor? I have seen one book on Amazon, but the reviews were not so good. |
|
![](templates/subSilver/images/spacer.gif) |
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Tue Nov 01, 2005 4:24 am |
|
|
I must admit, that seeing this code, I found myself wondering what the compiler really did generate for it?. The #int declarations signal to the compiler, that the following routine, is to be used to 'handle' the interrupt concerned. Main, is itself a 'reserved' word, signalling the 'main' routine to be called when the machine starts. Now 'main', because it then comprises the 'core' ofthe code, with nowhere to return to, should loop for ever (as it does in this example). The interrupt flag for a particular event, will be cleared when it's 'handler' exits. Interrupts are prevented from re-entering themselves, by the hardware clearing the GIE flag when the interrupt handler is called (actually the global handler), and re-enabling an interrupt inside the handler will cause a permanent loop.
So what I'd 'expect' to have happened in this case, is as soon as an interrupt occurs, the code would loop back to the start of the main, get as far as the 'enable_interrupts(global)' command, and then trigger again, and loop back once more, and keep going with the stack overflowing after a few loops, but on the 877, there is no stack overflow protection, so the loop will keep running.
On the question of guidance about where to understand such things, it is perhaps hard to answer in one 'go'.
There are really a whole series of 'parts' to what is going on. The first is understanding 'C', with most books now organised towards C++, which differs in many ways. The 'best' guide here is the original 'C programming language' book, by Kerninghan and Ritchie. The compiler is very close to supporting exactly what they originally describe, with the exception, that you are dealing with a processor with no 'file system' (so no 'file I/O', and with features like the interrupts that need to be handled.
Then though it may seem overly complex, the chip's data sheet is vital. First it will tell you what the hardware features 'are', how they interract, and give an idea of what is actually happening inside some of the routines.
The final part is to use some tutorials and books,such as the ones already mentioned in the thread referred to by the previous poster.
I would 'start simple' though. The phases to get going, would be something like:
1) Flash an LED.
2) Change the flash when a button is pushed.
3) Print a message to RS232.
4) Read a message from RS232.
Once these are 'handled', then consider trying the other peripherals, and possibly starting with an interrupt.
The old adage, about 'walk', before trying to 'run', does apply. Interrupts are an often misused/abused ability, and using them well, demands a good understanding of the chip first.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|