Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Eusart using pic18 interrupt flag is not set

Status
Not open for further replies.

abinesh raja

Newbie level 6
Newbie level 6
Joined
Mar 3, 2013
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
INDIA
Visit site
Activity points
1,374
I CHECKED EUART PROGRAM OF PIC18F45K22 BY ECHOING THE CHARACTER BUT I HAVE NO RESULT.I CHECKED TRANSMITTING IT WORKED FINE.

HERE IS MY PROGRAM
Code:
#include<htc.h>
#include<pic18.h>
unsigned char e;
void tx(unsigned char c)
{
  while(!TRMT1);
  TXREG1='c';
}
void main()
{

  TRISC6=0;
  TRISC7=1;
  BAUDCON1=0x40;
  SPBRG1=25;
  TXSTA1=0x06;
  RCSTA1=0x80;
  ANSELC=0;
  GIE=1;
PEIE=1;
 RC1IF=1;
  TXEN1=1;
  CREN1=1;
while(1)
{
while(!RC1IF)
{
e=RCREG1;
DelayMs(100);
tx(e);
}
}
}
I THINK THAT THE RECEIVER INTERRUPT FLAG IS NOT SETTING.

THANKS IN ADVANCE
 
Last edited by a moderator:

sorry I am new to pic ,this is my first project in uart. pls can you elaborate a little.

- - - Updated - - -

I enabled all the interrupts mentioned in the datasheet do i have to follow any order in enabling the registers.

thanks in adv.
 

Is there any wrong with my program
Code:
#include<htc.h>
#include<pic18.h>
unsigned char e;
void tx(unsigned char c)
{
  while(!TRMT1);
  TXREG1='c';
}
void main()
{

  TRISC6=0;
  TRISC7=1;
  BAUDCON1=0x40;
  SPBRG1=25;
  TXSTA1=0x06;
  RCSTA1=0x80;
  ANSELC=0;
  GIE=1;
  PEIE=1;
 RC1IF=1;
  TXEN1=1;
  CREN1=1;
while(1)
{
while(!RC1IF);
e=RCREG1;
DelayMs(100);
tx(e);

}
}
i am doing project to receive character from gps and to send to mobile through gsm.As i need two uart i chose this pic(18f45k22).
 

You must not enable interrupts. There's no interrupt function in the program and interrupts aren't presently needed at all.
 

HI.
Then how to receive character.I am checking uart tx-rx by echo back but i cant receive the character i send.

pls help...
 

After while(!TRMT1); you have to clear TRMT1 using TRMT1 = 0; otherwise it transmits only one character. Where is your serial interrupt routine? You should also clear RC1IF inside the while(!RC1IF) loop. In general when you use any interrupts, you have to clear the interrupt flag bit after it is set so that it can happen again.
 

After while(!TRMT1); you have to clear TRMT1 using TRMT1 = 0; otherwise it transmits only one character.
TRMT is a read only status bit. It can't be cleared.
You should also clear RC1IF inside the while(!RC1IF) loop.
RCIF is autocleared when reading RCREG.

A possible problem of the code, beside the discussed interrupt problem is that it won't recover from a receive overflow.
 
can any one help with this one i have to submit my project soon.how to receive char using uart.

I checked all the sample coding but didn't turn well.

Actually both the tx and rx were read only.do i have to use isr similar to timer program.

If so can any one post me a sample program.

Thanks in advance.
 

Try this code and Compile it for 16 MHz Clock.


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
#include <htc.h> 
 
//__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON & LVP_OFF & CPD_OFF & WRT_ON & DEBUG_OFF);
 
#define _XTAL_FREQ 16000000   //MHz
 
 
//Function Prototyper
void InitUART(void);
void SendByte(unsigned char);
unsigned char ReceiveByte(void);
void SendString(const unsigned char*);
 
void interrupt ISR(void)
{
    if(RC1IF)  //If UART Rx Interrupt
    {
        if(OERR) //if over run error, then reset the receiver
        {
            CREN = 0;
            CREN = 1;
        }
 
        SendByte(RCREG1);
    }
}
 
void InitUART(void)
{
    
    TRISC6 = 0;   //TX Pin
    TRISC7 = 1;   //RX Pin
    TRISD6 = 0;
    TRISD7 = 1;
    
    TXSTA1 = 0b00100110;
    RCSTA1 = 0b10010000;
    BAUDCON1bits.BRG16 = 0;
    SPBRG1 = 0x67;
 
    TXSTA2 = 0b01100100;
    RCSTA2 = 0b00001001;
    BAUDCON2bits.BRG16 = 0;
    SPBRG2 = 0x67;
    
    INTCONbits.PEIE = 1;
    INTCONbits.GIEL = 1;
    INTCONbits.GIE = 1;
    INTCONbits.GIEH = 1;
    PIE1bits.RC1IE = 1;
    PIE1bits.TX1IE = 1;
    PIE3bits.RC2IE = 1;
    PIE3bits.TX2IE = 1;
}
 
 
void SendByte(unsigned char Byte)  //Writes a character to the serial port
{
    while(!TX1IF);  //wait for previous transmission to finish
    TXREG1 = Byte;
}
 
unsigned char ReceiveByte(void)   //Gets a character from the serial port
{
    if(OERR) //if over run error, then reset the receiver
    {
        CREN = 0;
        CREN = 1;
    }
    
    while(!RC1IF);  //wait for transmission to receive
    
    return RCREG1;
}
 
void SendString(const unsigned char* st)
{
    while(*st)
        SendByte(*st++);
}
 
void main(void)
{
    InitUART();
    GIE  = 1;   //Enable global interrupts
    PEIE = 1;   //Enable Peripheral Interrupts
    SendString("Hello World!");
    
    while(1)
    {
        ;   //Do nothing, as Received character is echoed back in the ISR
            //SendByte(ReceiveByte());  //Echo Back
    }   
}




Mods. This is not mu fault. When I posted the previous post this post got created automatically.
 
Last edited:
i have tried this program but same result if i check rc1if in loop the loop is not executing but if i remove then the loop is transmitting character

Code:
void gps()
{
while(!RC1IF)
tx(RCREG1);
}

- - - Updated - - -

i have tried this program but same result if i check rc1if in loop the loop is not executing but if i remove then the loop is transmitting character

Code:
void gps()
{
while(!RC1IF)
tx(RCREG1);
}

- - - Updated - - -

I enabled GIE,PEIE AND RC1IE IS THAT ENOUGH I ENABLED CREN1 AT LAST
 

Code:
void tx(unsigned char c)
{
  while(!TRMT1);
  TXREG1='c';
}

you are transmitting the ASCII character 'c' instead of the variable c :lol:

Also, why are you polling the TRMT bit? it should be the TXIF bit...
 

The problem i am facing is only receiving not transmitting.tx worked fine but i have problem in receiver interrupt.i think rcif flag is not setting.
 

HERE IS MY CODE
Code:
#include<htc.h>
#include<pic18.h>
void main()
{
TRISC6=1;
  TRISC7=1;
  BAUDCON1=0x40;
  SPBRG1=25;
  ANSELC=0;
  TXSTA1=0x06;
  RCSTA1=0x80;
CREN1=1;
  TXEN1=1;

while(1)
{
while(!RC1IF);
tx(RCREG1);
}
}
void tx(unsigned char c)
{
  while(!TRMT1);
  TXREG1=c;
}

thanks

- - - Updated - - -

is there any other flag like TRMT-TXIF and RCIF and any thing like that

- - - Updated - - -

Actually the hardware was build by me i checked max 232 by shorting rx and tx pin at cmos level and it echoed back the character
 

your coding is shaky.. granted your just beginning with UART, but still..

You are obviously using RS232 but you failed to clear the SYNC bit during initialization to indicate that you are using ASYNCHRONOUS mode.. fortunately, it is cleared at reset.. but this is sign you have not read the datasheet thoroughly

The value you wrote to BAUDCON1 is questionable, you set the RCIDL bit when it is a read-only bit.

Writing 0x06 to TXSTA1 means you just set the TRMT bit which is also read-only.. It achieves nothing..

As for the baud rate, you havent indicated what baudrate you are using as well as the clock speed you are running your pic18.. But based on the value you used for SPBRG1 and that you set the BRGH bit, i assume you are using 4Mhz crystal

Another member above posted a code for 16Mhz, so confusion increases! :grin:

Also, what software are you using to send your test data? did you use parity bit? are they even the same baudrate?

One last thing, as what FvM mentioned above, you need to add code during receive to check for overrun error.

If there is an overrun error, you need to clear it, else the receiver stops functioning..
 

Thanks everyone, the tutorial helped me a lot.finally i got receiver working and i interfaced gsm and gps and it is working.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top