PIC Interrupt Usart not looping

Discussion in 'Embedded Systems and Microcontrollers' started by adem2, Sep 13, 2008.

  1. adem2

    Thread Starter New Member

    Sep 13, 2008
    1
    0
    Hi All
    I have been working on the following program for some time but to no avail .The program works okay but it stops after 3 iterations thus terminating the loop .Any ideas will be greatly appreciated .Thanks in advance .
    Adam

    //Program int_usart.c
    //Picf18f4620 @20Mhz
    //MikroC v8 demo .
    //MCU Usart reads 5 ascii char from a pc
    //ASCII chars stored in MCU memory .Then sent back to PC via usart
    //Program works but does not iterate
    //Eg. Repeats 3 times then halts.
    char i,j,m;
    char string[8];
    /**********************************************/
    // RX /TX Interrupt Handler
    void interrupt() {
    i=0;
    do{
    if (PIR1.RCIF == 1){ //if RX interrupt flag is set
    m=RCREG ; //Read usart RX reg
    PIR1.RCIF=0 ; //Clear flag
    string=m ; //Store it in string array
    i++ ;
    }
    }while(i<5); //Get 5 ASCII chars
    string='\0' ; //Null terminate the string array
    PIE1.RCIE = 0 ; //stop RX ints
    //Now read back the string
    i=0 ;
    PIE1.TXIE=1; //Enable TX ints
    do{
    if(TXSTA.TRMT==1) {
    m=string; //Read the stored array element
    TXREG=m; //put it in TX reg
    i ++;
    }
    }while(string!='\0');//continue until null found
    PIE1.RCIE = 1; //Re-enable RX ints
    PIE1.TXIE=0; //Disable TX ints
    } //Return from interrupts
    /*************************************/
    function_init() {
    Usart_Init(4800);
    // Set comms and Interrupt Enable bits
    RCSTA.SPEN=1; //CONFIGURE SER PORT rx/tx pins
    PIE1.RCIE = 1; // bit 5 USART Receive Interrupt Enable
    TXSTA.SYNC=0;
    RCSTA.CREN=1; //Enable ser coms
    TXSTA.TXEN=1; //Enable usart transmission
    //Set global Interrupt Enable bits
    INTCON.GIE = 1; // global interrput enable
    INTCON.PEIE = 1; // Peripheral Interrupt Enable bit...1 = Enables all unmasked peripheral interrupts
    PIE1.TXIE=0; //enable transmit interrupt
    Delay_ms(100);
    }
    void main()
    {
    function_init();
    //loop the program
    do{
    }while(1);
    } //end main
     
  2. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    Code ( (Unknown Language)):
    1.  
    2. //Program int_usart.c
    3. //Picf18f4620 @20Mhz
    4. //MikroC v8 demo .
    5. //MCU Usart reads 5 ascii char from a pc
    6. //ASCII chars stored in MCU memory .Then sent back to PC via usart
    7. //Program works but does not iterate
    8. //Eg. Repeats 3 times then halts.
    9.  
    10. char i,j,m;
    11. char string[8];
    12. /**********************************************/
    13. // RX /TX Interrupt Handler
    14. void interrupt()
    15. {
    16.     i=0;
    17.     do
    18.    {
    19.         if (PIR1.RCIF == 1)
    20.         { //if RX interrupt flag is set
    21.             m=RCREG ; //Read usart RX reg
    22.             PIR1.RCIF=0 ; //Clear flag
    23.             string[i]=m ; //Store it in string array
    24.             i++ ;
    25.         }
    26.     }while(i<5);  //Get 5 ASCII chars
    27.     string[i]='\0' ; //Null terminate the string array
    28.     PIE1.RCIE = 0 ; //stop RX ints
    29.     //Now read back the string
    30.     i=0 ;
    31.     PIE1.TXIE=1;  //Enable TX ints
    32.     do
    33.     {
    34.         if(TXSTA.TRMT==1)
    35.         {
    36.             m=string[i]; //Read the stored array element
    37.             TXREG=m;   //put it in TX reg
    38.             i ++;
    39.         }
    40.     }while(string[i]!='\0') ;   //continue until null found
    41.     PIE1.RCIE = 1;  //Re-enable RX ints
    42.     PIE1.TXIE=0;   //Disable TX ints
    43. }   //Return from interrupts
    44.  
    45. /*************************************/
    46. function_init()
    47. {
    48.     Usart_Init(4800);
    49.     // Set comms and Interrupt Enable bits
    50.     RCSTA.SPEN=1; //CONFIGURE SER PORT rx/tx pins
    51.     PIE1.RCIE = 1; // bit 5 USART Receive Interrupt Enable
    52.     TXSTA.SYNC=0;
    53.     RCSTA.CREN=1; //Enable ser coms
    54.     TXSTA.TXEN=1; //Enable usart transmission
    55.     //Set global Interrupt Enable bits
    56.     INTCON.GIE = 1; // global interrput enable
    57.     INTCON.PEIE = 1; // Peripheral Interrupt Enable bit...1 = Enables all unmasked peripheral interrupts
    58.     PIE1.TXIE=0; //enable transmit interrupt
    59.     Delay_ms(100);
    60. }
    61.  
    62. void main()
    63. {
    64.     function_init();
    65.     //loop the program
    66.      do
    67.     {
    68.     }while(1);
    69. } //end main[/i][/i][/i][/i]



    Applied CODE tags for clarity....

    hgmjr
     
Loading...