|
|
View previous topic :: View next topic |
Author |
Message |
saf84
Joined: 05 Nov 2007 Posts: 1
|
how to send data and display data using PIC18F6720and RS232 |
Posted: Mon Nov 05, 2007 3:07 am |
|
|
good day all!
i am doing a small project using PIC18F6720 in PIC C Compiler..i test the program in proteus design software.actually,i am still new with this things.
hopefully some one can share his/her knowledge.
below is my program:
#include "C:\Documents and Settings\PC2\Desktop\main system amr\main system amr\try\try2.h"
#int_RDA
void RDA_isr(void)
{
printf(" enter your data :\n ");
c = getc();
buffer[i++] = c;
if (i <= buffer_size){
putc(c);
fprintf(system,"%s",buffer);}
process = 1;
}
#int_RDA2
void RDA2_isr(void)
{
process = 2;
}
#int_TIMER1
void TIMER1_isr(void)
{
process = 3;
}
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_4(T4_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_oscillator(False);
enable_interrupts(int_rda);
enable_interrupts(global);
//enable_interrupts(int_rda2);
//enable_interrupts(int_TIMER1);
switch (process) {
case 0: printf("idle\n");
break;
case 1: //printf("process = 1");
process = 0;
break;
case 2: printf("process = 2");
process = 0;
break;
default: printf("process = 3");
process = 0;
break; }
}
actually it does not have any errors but the output is not as expected.
i want the output to be like this:
idle //until we press something on the keyboard
enter your data : h //say we press h
h
but the output i get is:
idle enter your data :
if i press h the output:
idle enter your data :
enter your data : hh
hopefully someone can help me on this problem...thanks you |
|
|
Ttelmah Guest
|
|
Posted: Mon Nov 05, 2007 6:52 am |
|
|
There are several problems.
First, change your basic thinking. Don't print in the interrupt handler. Do a bit of research here. The basic advice, is always that an interrupt handler, should be as _quick as possible_, and should only do the job of handling the interrupt event. What you have written, not only rather obviates the 'point' of the interrupt, but also leads to another problem. In the PIC, because there is no 'variable' stack, code must not be 're-entrant' (call itself). Because of this, if a routine is present inside an interrupt, and outside the interrupt, the compiler will disable interrupts for the routine when it is outside the interrupt (to prevent an interrupt occuring inside the routine). This ensures that interrupts are disabled for large periods in the external routine...
You should maintain your 'state' in the main code, and handle the prompting, and changing to different operations here, triggered by the arrival of characters in the interrupt. EX_SISR, shows how to receive characters, and detect tha they have been received, using the interrupt.
Now, you then have to realise that hardware will often do slightly different things from what you expect. Lgic level 'serial', as received by the PIC, idles 'high'. Chips won't generally do this when they are first switched on, so it is common for a 'spurious' character to be received on switch-on.
You start displaying your input prompt, after a character is received. The spurious character, sends you into this routine straight away. Then the prompt, and this character is displayed. When you add another character, the whole message is redrawn, with the extra character added (the huge time involved in this, potentially means that if the data was sent quickly, the input buffer would overflow, and hang the UART...).
Next comment. You must _never_ have a receive interrupt, which does not receive a character. If RDA2 triggers, the processor _will_ become permanently hung, as it interrupts, the event is not cleared, and it interrupts again... :(
Use the EX_SISR example to handle the receive interrupt. Then in your 'main', loop based upon your state, and if any characters are in the input buffer. When one arrives, change state, and start displaying your prompt.
Before enabling the interrupt, ensure that the serial input line has gone high, and retrieve any dummy characters, then clear the interrupt.
Best Wishes |
|
|
|
|
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
|