0
\$\begingroup\$

I am trying to write a simple code to receive and resend data using USART but the code is not working.

Could someone give suggestions on possible flaws in the code?

I am using USART at baud rate 9600 with clock frequency 4MHz.

#include<pic.h>
void main()
{   
    TRISB=0X00;
    TXEN=1;
    SYNC=0;
    BRGH=1;
    SPEN=1;
    CREN=1;
   //RCEN=1;
    SPBRG=0X19;
    INTCON=0X80;
    TRISC7=1;
    TRISC6=0;
    RCIE=1;
    TXIE=1;
    while(1);
}

void interrupt rectrans()
{
    if(RCIF==1)
    {
      TXREG=RCREG;
      while(!TRMT);
      RCIF=0;
    }
    else if(TXIF==1)
    {
      TXIF=0;
    }
}

enter image description here

\$\endgroup\$

closed as unclear what you're asking by Leon Heller, Ricardo, nidhin, Daniel Grillo, Chetan Bhargava Jan 30 '15 at 6:49

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ What exactly is not working? And what compiler are you using? Are you sure the interrupt gets called? To check, you could temporarily use an unused pin as debug signal and attach an LED to it. Then, in the beginning of main you turn it off and in the interrupt you turn it on. Are you even sure that the PIC is working, executing the program, etc.? (same way to check) \$\endgroup\$ – user17592 Jan 28 '15 at 16:50
  • \$\begingroup\$ @CamilStaps , thanks for attending my question. I simulated the setup using isis but could not see any changes in the RCREG register(ISIS>>watch window).I am using XC8 compiler.Regarding the interrupts, I could not see any changes in the PIR1 register. I am a beginner when it comes to PIC and interrupts. so probably I have done some mistake at the concept level. \$\endgroup\$ – user2745976 Jan 28 '15 at 17:03
  • \$\begingroup\$ You haven't enabled Peripheral Interrupts in your INTCON register (bit 6). Try setting INTCON to 0xC0 instead of 0x80. \$\endgroup\$ – brhans Jan 28 '15 at 17:06
  • \$\begingroup\$ Thank you very much @CamilStaps. I understood my mistake.Its working perfectly now. \$\endgroup\$ – user2745976 Jan 28 '15 at 17:14
  • \$\begingroup\$ Thank you very much @brhans. I understood my mistake. The issue has been resolved. \$\endgroup\$ – user2745976 Jan 28 '15 at 17:29
1
\$\begingroup\$

You haven't enabled Peripheral Interrupts in your INTCON register (bit 6).
Try setting INTCON to 0xC0 instead of 0x80.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.