raja_rajan
Newbie level 6
Hi,
I am using dsPIC33FJ64GS606. UART receive interrupt not working prorperly. while sending data from real term terminal two or three times.
i have get a response message or the specified operation are working after sending the data two or three times.
Here is the code for uart receive.
I am using dsPIC33FJ64GS606. UART receive interrupt not working prorperly. while sending data from real term terminal two or three times.
i have get a response message or the specified operation are working after sending the data two or three times.
Here is the code for uart receive.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 void __attribute__((interrupt, no_auto_psv)) _U1RXInterrupt( void ) { /* Check for receive errors */ if(U1STAbits.FERR == 1) { buf=U1RXREG;; buf=U1STAbits.FERR; } /* Must clear the overrun error to keep UART receiving */ if(U1STAbits.OERR == 1) { U1STAbits.OERR = 0; } /* get the data */ // if(U1STAbits.URXDA ==1) { rx[count++] = U1RXREG; } R_flag=1; IFS0bits.U1RXIF = 0; // clear TX interrupt flag } Here is the check receive function. void check_receive(void) { /* if(U1STAbits.TRMT==1); U1TXREG = 'A'; // Transmit one character __delay_ms(30); */ if((rx[0]=='1')&&(rx[1]=='p')&&(rx[2]=='1')) { uart1_send('1'); uart1_send('p'); } if((rx[0]=='1')&&(rx[1]=='s')) //start pwm { PTCONbits.PTEN = 1; /* Enable the PWM Module */ if(rx[2]=='f') { forward=1; reverse=0; // while(rx[2]=='f') // motor_forward(); } else if(rx[2]=='r') { reverse=1; forward=0; //while(rx[2]=='r') // motor_reverse(); } } if((rx[0]=='1')&&(rx[1]=='d')) //stop pwm { PTCONbits.PTEN = 0; /* Disable the PWM Module */ } if((rx[0]=='s')&&(rx[1]=='p')) // speed count { if(rx[2]=='4') speed=0.004; if(rx[2]=='5') speed=0.005; if(rx[2]=='6') speed=0.006; if(rx[2]=='7') speed=0.007; if(rx[2]=='8') speed=0.008; if(rx[2]=='9') speed=0.009; if((rx[2]=='1')&&(rx[3]=='0')) speed=0.010; } count=0; rx[0]=0; rx[1]=0; rx[2]=0; R_flag=0; // } } Here is the main function int main() { /* Configure Oscillator to operate the device at 40Mhz Fosc= Fin*M/(N1*N2), Fcy=Fosc/2 Fosc= 7.37*(43)/(2*2)=80Mhz for Fosc, Fcy = 40Mhz */ /* Configure PLL prescaler, PLL postscaler, PLL divisor */ PLLFBD=41; /* M = PLLFBD + 2 */ CLKDIVbits.PLLPOST=0; /* N1 = 2 */ CLKDIVbits.PLLPRE=0; /* N2 = 2 */ __builtin_write_OSCCONH(0x01); /* New Oscillator FRC w/ PLL */ __builtin_write_OSCCONL(0x01); /* Enable Switch */ while(OSCCONbits.COSC != 0b001); /* Wait for new Oscillator to become FRC w/ PLL */ while(OSCCONbits.LOCK != 1); /* Wait for Pll to Lock */ //TRISB=0x0000; TRISFbits.TRISF2=1; TRISFbits.TRISF3=0; TRISBbits.TRISB9=1; TRISBbits.TRISB10=1; // TRISD=0x00ff; // PORTB=0x00ff; uart1_init(); __delay_ms(1000) adc_init(); init_PWM(); External_interrupt_init(); // INTCON1bits.NSTDIS=1; uart1_send('p'); uart1_send('w'); uart1_send('m'); while(1) /* Infinite Loop */ { __delay_us(300) if(R_flag==1) { check_receive(); } if(forward==1) { if(C_pwm3<18000) motor_forward(); else{ PTCONbits.PTEN= 0; C_pwm3=0; } } if(reverse==1) { if(C_pwm3<18000) motor_reverse(); else{ PTCONbits.PTEN= 0; C_pwm3=0; } } /* if((flag3==1)||(flag4==1)) { if(flag3==1) uart1_send(C_pwm3); else uart1_send(C_pwm1); flag3=flag4=0; } */ } }
Last edited by a moderator: