• AVR Freaks

AnsweredHot!Uart Interrupt

Page: 123 > Showing page 1 of 3
Author
Dimitris
Super Member
  • Total Posts : 307
  • Reward points : 0
  • Joined: 2017/11/03 10:29:26
  • Location: 0
  • Status: offline
2018/02/25 23:28:39 (permalink)
0

Uart Interrupt

Is there any easy tutorial how can i use uart interrupt with a method like void?PIC18F45K22 XC8
I'm using default mplab libraries
Example In timer interrupt i can use a method inside like
 
    
void TMR1_SetInterruptHandler(MyMethod)//Here is my routine


Is there any corresponding for uart?
My default source include:
 
    
EUSART1_Read
    EUSART1_Write
    EUSART1_Transmit_ISR
    EUSART1_Receive_ISR

 
 
My desire result is that every time it will receive data my uart, to make interrupt and running a void.Thank you
#1
qɥb
Monolothic Member
  • Total Posts : 3332
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: Uart Interrupt 2018/02/26 00:52:40 (permalink)
+3 (3)
dimis1989
Is there any easy tutorial how can i use uart interrupt with a method like void?

Nonsense question, what do you mean?
 

I'm using default mplab libraries

There is no such thing.
Do you mean the old PLIB libraries, that used ot come with XC8, not MPLAB,
or do you mean the new MCC plugin?
 

Example In timer interrupt i can use a method inside like
 
    
void TMR1_SetInterruptHandler(MyMethod)//Here is my routine


I'm guessing that might be MCC. Are you using MCC (Microchip Code Configurator)?
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#2
Dimitris
Super Member
  • Total Posts : 307
  • Reward points : 0
  • Joined: 2017/11/03 10:29:26
  • Location: 0
  • Status: offline
Re: Uart Interrupt 2018/02/26 01:27:03 (permalink)
-1 (1)
Yes exactly i mean code configuration
Tools->Embedded->Code Configuration v3
post edited by dimis1989 - 2018/02/26 03:44:18
#3
rodims
Super Member
  • Total Posts : 1557
  • Reward points : 0
  • Joined: 2009/02/10 11:08:59
  • Location: 51.9627, 7.6262
  • Status: offline
Re: Uart Interrupt 2018/02/26 03:40:38 (permalink)
+1 (3)
Did you read (and understand) qhb's post ??
#4
rodims
Super Member
  • Total Posts : 1557
  • Reward points : 0
  • Joined: 2009/02/10 11:08:59
  • Location: 51.9627, 7.6262
  • Status: offline
Re: Uart Interrupt 2018/02/26 04:24:16 (permalink)
0
That's your way, of not answering questions Smile: Smile
#5
Dimitris
Super Member
  • Total Posts : 307
  • Reward points : 0
  • Joined: 2017/11/03 10:29:26
  • Location: 0
  • Status: offline
Re: Uart Interrupt 2018/02/26 04:45:25 (permalink)
-1 (1)
But i answered the question...Smile: Smile
#6
rodims
Super Member
  • Total Posts : 1557
  • Reward points : 0
  • Joined: 2009/02/10 11:08:59
  • Location: 51.9627, 7.6262
  • Status: offline
Re: Uart Interrupt 2018/02/26 04:50:59 (permalink)
+1 (1)
Yes, you are perfectly right.
It answers my question that you did not understand mine nor qhb's post
 
#7
katela
Super Member
  • Total Posts : 1815
  • Reward points : 0
  • Joined: 2013/06/11 05:25:18
  • Location: South Africa
  • Status: online
Re: Uart Interrupt 2018/02/26 05:11:30 (permalink)
+1 (1)
This is the question: Is there any easy tutorial how can i use uart interrupt with a method like void?
What do you mean by void method?
Can you explain what you want exactly to do when there is an interrupt?
When an interrupt occurs it should run the ISR code.

Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
YouTube Tutorials: https://www.youtube.com/StudentCompanionSA
#8
Dimitris
Super Member
  • Total Posts : 307
  • Reward points : 0
  • Joined: 2017/11/03 10:29:26
  • Location: 0
  • Status: offline
Re: Uart Interrupt 2018/02/26 06:33:31 (permalink)
-2 (2)
And how can i Create a routine inside isr code?
#9
Dimitris
Super Member
  • Total Posts : 307
  • Reward points : 0
  • Joined: 2017/11/03 10:29:26
  • Location: 0
  • Status: offline
Re: Uart Interrupt 2018/02/26 06:40:33 (permalink)
0
How to use custom code inside this?
 
    
void EUSART1_Receive_ISR(void)
{

if(1 == RCSTA1bits.OERR)
{
// EUSART1 error - restart
RCSTA1bits.CREN = 0;
RCSTA1bits.CREN = 1;
}
// buffer overruns are ignored
eusart1RxBuffer[eusart1RxHead++] = RCREG1;
if(sizeof(eusart1RxBuffer) <= eusart1RxHead)
{
eusart1RxHead = 0;
}
eusart1RxCount++;
}

 
#10
katela
Super Member
  • Total Posts : 1815
  • Reward points : 0
  • Joined: 2013/06/11 05:25:18
  • Location: South Africa
  • Status: online
Re: Uart Interrupt 2018/02/26 07:32:38 (permalink)
+1 (1)
dimis1989
And how can i Create a routine inside isr code?

Create a function that you want to be called when there is interrupt.
Check if RC1IE = 1; 
IF yes, call your function. 

Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
YouTube Tutorials: https://www.youtube.com/StudentCompanionSA
#11
Dimitris
Super Member
  • Total Posts : 307
  • Reward points : 0
  • Joined: 2017/11/03 10:29:26
  • Location: 0
  • Status: offline
Re: Uart Interrupt 2018/02/26 23:15:50 (permalink)
0
RC1IE can be every where in my source files? Or it should be placed in my main loop?
Like
 
void main(void)

INTERRUPT_GlobalInterruptEnable();
  INTERRUPT_PeripheralInterruptEnable();
if(PIR1bits.RC1IF){
ReadUart(); //Here is my Routine
}

 
 
post edited by dimis1989 - 2018/02/26 23:26:05
#12
Dimitris
Super Member
  • Total Posts : 307
  • Reward points : 0
  • Joined: 2017/11/03 10:29:26
  • Location: 0
  • Status: offline
Re: Uart Interrupt 2018/02/27 00:26:02 (permalink)
0
Also how can i avoid conflicting when uart buffer is reading or writing?
The code below stucks when every time which trying to read data for first time. In line 'getch'
 
void WriteUart()
{
 
UART_IO=HIGH;//This is pin on 485. If its high means to write if its low means to Read
__delay_ms(2);
sprintf(txt,"%d,%d\n",2,2);
printf(txt);
__delay_ms(2);
UART_IO=LOW;
 

ReadUart(); //When writing - sending is finished i'm calling read routine for reading the answer from other side
}

 
void ReadUart(){
 
 
 
if(PIR1bits.RC1IF){


for( Counter.CountArrayValues=0;Counter.CountArrayValues<ArrayCharNum;Counter.CountArrayValues++) {
line[Counter.CountArrayValues] = getch();//In this line my code stucks every first -third- fifth time i send something

if (line[Counter.CountArrayValues] == '\n') break;
}

line[Counter.CountArrayValues] = 0;
Counter.CountArrayValues=0;

p = strtok(line, ",");

for( Counter.CountArrayWords=0;Counter.CountArrayWords<NumberOfRegisters;Counter.CountArrayWords++){

ReadRegister[Counter.CountArrayWords].Position=atoi(p);
p = strtok(NULL, ",");

if (p == NULL) break;
}
Counter.CountArrayWords=0;

SendValuesToRegisters();

}
}

 
How can i avoid conflict of this two methods?
post edited by dimis1989 - 2018/02/27 00:51:38
#13
Dimitris
Super Member
  • Total Posts : 307
  • Reward points : 0
  • Joined: 2017/11/03 10:29:26
  • Location: 0
  • Status: offline
Re: Uart Interrupt 2018/02/27 02:50:36 (permalink)
-1 (1)
Any suggestions?
#14
qɥb
Monolothic Member
  • Total Posts : 3332
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: Uart Interrupt 2018/02/27 02:56:12 (permalink)
+1 (1)
dimis1989
Also how can i avoid conflicting when uart buffer is reading or writing?
The code below stucks when every time which trying to read data for first time. In line 'getch'
 

Just a guess, but you might be getting some corrupt character sent as you're initialising everything, corrupting the first command sent., so you don't get the expected response.
Maybe implement a timeout so you try again if you don't get the expected response within a certain time.
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#15
Aussie Susan
Super Member
  • Total Posts : 3726
  • Reward points : 0
  • Joined: 2008/08/18 22:20:40
  • Location: Melbourne, Australia
  • Status: offline
Re: Uart Interrupt 2018/02/27 18:36:42 (permalink) ☄ Helpfulby dimis1989 2018/02/28 01:47:23
+1 (1)
I get the feeling that there are a whole lot of concepts that are getting mixed up here, especially when I see the mainline testing for the RCIF bit right after enabling the interrupts.
I assume that the OP can send and receive characters through the EUSART without using interrupts. If not then let's stop here and get that sorted out.
 
The EUSART has two separate 'interrupt' sources, one for the Tx side and one for the Rx side. These are controlled by the TXnIE and RXnIE bits respectively. If these bits are not set then no interrupts will be triggered at the completion of a character send or a character receive by the EUSART.
When the EUSART finishes ending a character, it will set the appropriate TXnIF bit. If the corresponding TXnIE bit is set then an interrupt will be triggered.
On these devices there are two interrupt vectors - high and low priority. When the interrupt triggers, it will pass control to the high or low priority ISR. Note that ALL interrupts will call the same ISR (depending on the priority) and so the first thing you need to do inside the ISR is work out which peripheral needs to be serviced. This is done by testing the various xxIF (and for safety sake the xxIE bit as well) and, if set, then do whatever is required.
From this, it should be apparent that there is no conflict between the Tx and Rx interrupts - you need to test for each within the ISR and handle it accordingly.
Part of that handling should be to clear the xxIF bit. How that is done depends on the peripheral - some you clear the bit in your code and others, such as the EUSART, occur when you perform some operation on the buffer (e.g. the RXnIF bit is cleared when you read the RXREGn register).
 
Now for the 'other' confusion that I think is occurring. There are two ways to interact with a peripheral such as the EUSART: interrupt and blocking. Interrupt processing means that you use the ISR code to handle the completed operation and do whatever is required after that. For example, if you have just received a character, then you could put it into a ring buffer and test for a terminating character to set as flag for the main line to say a complete line has been read in. The ISR does all of this and the main code can do whatever else it wants to without interacting with the peripheral itself.
Blocking processing means that you do not use interrupts and there is no code in the ISR related to the peripheral; instead  you do tests yourself on the xxIF bit. The term 'blocking' comes about as doing your own tests is typically done in a 'while' loop that only exits when the bit is set. This will "block" the processor in that loop and not let it do anything else (except process interrupts form other sources). When the bit is set, then you can continue interacting with the peripheral in whatever way is required.
You must chose on or the other approach when handling a peripheral (or part of the peripheral in the case of the EUSART which has a Tx and an Rx part). If you mix them up then you will get into a mess and your program will not operate as expected.
 
Once the OP understands this then we can have a look at the code that uses one approach or the other and for the Rx and Tx sides so there is no 'conflict'.
Susan
#16
Dimitris
Super Member
  • Total Posts : 307
  • Reward points : 0
  • Joined: 2017/11/03 10:29:26
  • Location: 0
  • Status: offline
Re: Uart Interrupt 2018/02/28 01:55:09 (permalink)
0
Thank you post is very helpful. In my example i didnt used interrupt. So maybe thats why my comminication stucks. In my case is it better to use only rx interrupt? Or both of them with high and low priority?
#17
Aussie Susan
Super Member
  • Total Posts : 3726
  • Reward points : 0
  • Joined: 2008/08/18 22:20:40
  • Location: Melbourne, Australia
  • Status: offline
Re: Uart Interrupt 2018/02/28 18:24:42 (permalink)
+1 (1)
I must admit that I don't know which 'example' you are referring to but based on the title of this thread and the code I've seen throughout, it looks like some interrupts are being used somewhere. Anyway....
Whether to use interrupts an, i so, at which priority is a design decision that you need to make. I suspect that you are jumping in to a solution without thinking clearly about what you are are actually trying to do and then thinking about what is the best way to achieve that. I know it sounds trite but a few hours of thinking and planning can save days of coding and weeks of debugging.
Susan
#18
Dimitris
Super Member
  • Total Posts : 307
  • Reward points : 0
  • Joined: 2017/11/03 10:29:26
  • Location: 0
  • Status: offline
Re: Uart Interrupt 2018/03/06 02:17:15 (permalink)
0
I'm trying to find why i have conflict between read and write from uart.
 
First i need to write something in uart and after that immidiatelly to read a response.
 
void WriteUart()
{
while(PIR1bits.TX1IF == 1)
{
printf("TEST");
}
PIR1bits.TX1IF =0;
__delay_ms(10);
 
ReadUart();
}

 
 
void ReadUart()
{

  while(PIR1bits.RC1IF==1)
{
//My method
}
PIR1bits.RC1IF=0;
}

 
The problem is that when it tries to read for some reason it stucks and nothing is responsed
 
#19
qɥb
Monolothic Member
  • Total Posts : 3332
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: Uart Interrupt 2018/03/06 02:34:56 (permalink)
+1 (1)
Both of those routines are rubbish.
Stop trying to clear TX1IF and RC1IF.
Those bits are read-only,
the only way to clear TX1IF is to write a character to the USART
the only way to clear RC1IF is to read a character from the USART.
 
I don't know how printf() is getting to the USART, but assuming that it is, then it will be checking TXIF itself, you shouldn't have any test of TXIF. I don't understand why you are looping, and re-sending "TEST" as long as you see TXIF set.
 
Is your intention to send a single character, or that whole "TEST" string?
 
Is ReadUart() meant to be reading a character, or a string?
Each time it is set it tells you there's at least one character ready.

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#20
Page: 123 > Showing page 1 of 3
Jump to:
© 2020 APG vNext Commercial Version 4.5