Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

need help with interrupt

Status
Not open for further replies.

blisz

New Member
i write a program code that will run when a choice is made ;
Compiler: mikroC

please choose:
1.) start
2.) stop

when '1' is chosen, it will display 'hello' continuously. If any other button is press, the program will go back to the menu selection untill '1' is pressed to start..
The 'hello' must run continuously.. the problem is once '1' is pressed, i cant press '2' to stop it... so now, i want to interrupt it once its started so that i can stop the program.. i've tried using the interrupt but to no avail..

i need help !! any1 check my codes pleasee!! (sorry, i'm new to C language and PIC. this is my 1st attempt)
thank you in advance~

here is the codes without interrupt

Code:
unsigned int i; 
void usart_str_write(char* string) 
{ 
     while ((*string) != '\0'){ 
     //while (strlen(string)){ 
       Usart_Write((char) (*string)); 
       ++string; 
     } 
     Usart_Write('\r');               // add a new line to every string 
} 

unsigned int read() 
{ 
      while(1) 
      { 
              usart_str_write("hello"); 
      } 
return (0); 
} 

void main() 
{ 
ADCON1 = 0x00;                      // to configure analog inputs and Vref=vdd 
TRISA  = 0xFF;                     // PORTA is input ( to read adc input) 
USART_Init(9600); 

usart_str_write("please choose:"); 
usart_str_write("1.) start"); 
usart_str_write("2.) stop"); 


do{   if (Usart_Data_Ready())     // If data is received 
    { 

      i = Usart_Read(); 
      if ( i == '1') 
              { 
              usart_str_write("->execute"); 
              USART_Write(0x0D); 
              read(); 

              } 

      else 
              { 
               usart_str_write("please choose:"); 
               usart_str_write("1.) start"); 
               usart_str_write("2.) stop"); 
              } 
     } 

  } 
  while(1); 
}



codes with the interrupt

Code:
unsigned int i; 
void usart_str_write(char* string) 
{ 
     while ((*string) != '\0'){ 
     //while (strlen(string)){ 
       Usart_Write((char) (*string)); 
       ++string; 
     } 
     Usart_Write('\r');               // add a new line to every string 
} 


unsigned int read() 
{ 
      while(1) 
      { 
              usart_str_write("hello"); 
      } 
return (0); 
} 


void interrupt() 
{ 

  if (PIR1.RCIF=1) 

    { 
     if (Usart_Data_Ready()) 
         { 
         i = Usart_Read(); 
         } 
    } 

 PIR1.RCIF=0; 
} 


void main() 
{ 
ADCON1 = 0x00;                    // to configure analog inputs and Vref=vdd 
TRISA  = 0xFF;                      // PORTA is input ( to read adc input) 
USART_Init(9600); 

INTCON.GIE=1; 
INTCON.PEIE=1; 
PIE1.RCIE=1; 

usart_str_write("please choose:"); 
usart_str_write("1.) start"); 
usart_str_write("2.) stop"); 


do{    

           if ( i == '1') 
              { 
              usart_str_write("->execute"); 
              USART_Write(0x0D); 
              read(); 

              } 

      else 
              { 
               usart_str_write("please choose:"); 
               usart_str_write("1.) start"); 
               usart_str_write("2.) stop"); 
              } 
     } 
  while(1); 
}


i run the codes( with interrupt)..
but why does the programme displays the menu selection continuously, instead of waiting for my input ( i.e. : usart_read)

i wanted the program to wait for my input, then go through the main program loop ( attention: the 'read()' must run continuosly !! )
and the program has to stop when i press '2'..

please help!!
 
Why not just make the read function return when Usart_Data_Ready is true.

Code:
unsigned int read() 
{ 
      while(Usart_Data_Ready()==0) 
      { 
              usart_str_write("hello"); 
      } 
return (0); 
}

Mike.
 
Why not just make the read function return when Usart_Data_Ready is true.

Code:
unsigned int read() 
{ 
      while(Usart_Data_Ready()==0) 
      { 
              usart_str_write("hello"); 
      } 
return (0); 
}

Mike.



yeah it works... thanks for your help..!
 
Why not just make the read function return when Usart_Data_Ready is true.

Code:
unsigned int read() 
{ 
      while(Usart_Data_Ready()==0) 
      { 
              usart_str_write("hello"); 
      } 
return (0); 
}

Mike.


hi mike..

the code works fine..but can u explain the codes to me..?
why do u use
''Usart_Data_Ready()==0"
 
Testing for equal to zero is the same as testing for false and so all the code is doing is sending "hello" until any character is received. You will find that any key will stop the hello, except "1".

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top