cancel
Showing results for 
Search instead for 
Did you mean: 

PSoC 4 MCU

New Contributor II

 Hi Everyone. I have a poject use Uart and Control.

   

My Uart have 3 input RX,  RX - GPS, RX- LCD,  RX- GSM; and 3 output for TX - LCD, TX-GSM,TX-GPS.

   

I use control reg for choise only  input RX, and output TX.

   

Please tell me some advices that:

   

Fisrtly tell me: I should use while(1) for getchar() data buffer in UART? (or)  I should use interupt UART for getchar() data buffer? Because?

   

this is image:

0 Likes
Reply
18 Replies
Esteemed Contributor II

Using interrupt or polling method mostly changes CPU performance. When there are not many jobs the CPU has to perform in the main-loop polling might be o.k.

   

When there are other time-critical jobs interrupt driven might be the better solution.

   

So the answer as usual is: It depends...

   

 

   

Bob

0 Likes
Reply
New Contributor II

 hi bob!

   

Please see image of me, I use 3 input RX for UART . if I use interrupt for get data, I have to  how do it?

   

I will write 3 function interrupt ? and use control to choise them?

   

this image:

0 Likes
Reply
Esteemed Contributor II

A single interrupt handler will be enough, since you switvh the input you will know where the data come from.

   

Look into the datasheet of UART to clear the interrupt in the handler. To write a handler, look into the System reference guide (undr the Help-menu of Creator) and use CY_ISR_PROTO() and CY_ISR() macros and use isr_StartEx() so you do not need to make changes in a generated file.

   

 

   

Bob

0 Likes
Reply
New Contributor II

 hi Bob!

   

I was very confused when I use Control Reg ( switch) in case of RX uart interrupt.  I do not know where to put it (it = Control Reg) .

   

I will put it in function CY_RS { } and then it will turn each job? 

   

Please tell me how to run of interrupt  when I change the selected input uart.

   

Thank so much!

0 Likes
Reply
Esteemed Contributor II

Best would be to post your current project here.  To do so, use
Creator->File->Create Workspace Bundle (minimal)
and attach the resulting file.



Bob
 

0 Likes
Reply
New Contributor II

 Hi Bob! This is my project . In function CY_ISR (Interuptrx) , I do not know how to do the job for three interrupt input.

   

Please tell me about them. do you talk about how to run the interrupt when using control reg?

   

thank so much!

0 Likes
Reply
Esteemed Contributor II

I just saw that you are using an older version of Creator, actual is 3.1. I suggest you to update, there are improvements in the components.

   

See attached project on how to use your interrupt handler, just a suggestion.

   

 

   

Bob

0 Likes
Reply
Esteemed Contributor II

Attachment second try..

   

 

   

Bob

0 Likes
Reply
New Contributor II

 Thank so much Bob!

   

I will devolop my project follow your suggestion. I will relay for you in next time. 

   

Thank a gain!

0 Likes
Reply
Esteemed Contributor II

You are always welcome!

   

 

   

Bob

0 Likes
Reply
New Contributor II

 Hi Bob!

   

Please give me some suggests.

   

in your project , I do not understand how to run the interrupt  and how to run and how to run the program when receiving data from the UART.

   

can you give me the flowchart drawing algorithm?

   

thank you!

   

this is main.c of you

   

 

   

/* ========================================

   

 *

   

 * Copyright YOUR COMPANY, THE YEAR

   

 * All Rights Reserved

   

 * UNPUBLISHED, LICENSED SOFTWARE.

   

 *

   

 * CONFIDENTIAL AND PROPRIETARY INFORMATION

   

 * WHICH IS THE PROPERTY OF your company.

   

 *

   

 * ========================================

   

*/

   

#include <project.h>

   

#include <stdio.h>

   

#include <Control_Rx.h>

   

 

   

#define OPEN_RX_LCD  Control_RX_Write(0x00)

   

#define OPEN_RX_GPS  Control_RX_Write(0x01)

   

#define OPEN_RX_GPRS  Control_RX_Write(0x02)

   

#define BUFFSIZE 20

   

 

   

uint8 Flag = 0;

   

char Buffer_LCD[BUFFSIZE];

   

uint8 LCD_Index = 0;

   

char Buffer_GPS[BUFFSIZE];

   

uint8 GPS_Index = 0;

   

char Buffer_GPRS[BUFFSIZE];

   

uint8 GPRS_Index = 0;

   

uint8 SelectedInput = 0;

   

 

   

CY_ISR (Interuptrx)

   

{

   

char ChrReceived = UART_RX_GetByte(); // Interrupt fires only when there is a char, so no check for char ready

   

switch (SelectedInput)

   

{

   

case 0: Buffer_LCD[LCD_Index++] = ChrReceived;

   

break;

   

case 1: Buffer_GPS[GPS_Index++] = ChrReceived;

   

break;

   

case 2: Buffer_GPRS[GPRS_Index++] = ChrReceived;

   

break;

   

default:SelectedInput = 3;

   

}

   

}

   

void InitializeHardware(void)

   

{

   

    UART_RX_Start();

   

    RX_INTERUPT_StartEx(Interuptrx);

   

    CyGlobalIntEnable;

   

}

   

 

   

void main()

   

{

   

   

InitializeHardware();

   

   

    for(;;)

   

    {

   

switch (SelectedInput)

   

{

   

case 0: OPEN_RX_LCD;

   

break;

   

case 1: OPEN_RX_LCD;

   

break;

   

case 2: OPEN_RX_LCD;

   

break;

   

default:SelectedInput = 3;

   

}

   

SelectedInput++;

   

if(SelectedInput > 2) SelectedInput = 0;

   

CyDelay(1000); // Listen for one second

   

   

// if one of the indexes > 0, there are characters in the corresponding buffer

   

// read them off and reset the index back to 0

   

   

}

   

}

   

 

   

/* [] END OF FILE */

0 Likes
Reply
Esteemed Contributor II

It is always a bit annoying to check a program without the help of the IDE. Can you again please attach your complete project? Additionally be a bit more precise what runs and what exactly does not run.

   

 

   

Bob

0 Likes
Reply
New Contributor II

 Hi Bob, I am so sorry hihi.

   

This is my project.

   

I am using touch LCD for controls the mode gps and mode gsm. when I touch an icon on the screen of LCD ,  Immediately LCD will send for MCU a buffer 7byte, but to do this I have OPEN_LCD (in main.c). I want to finish this project, I can use it as a simple phone.

   

Please help me hearted .

   

could you tell me some more specific suggestions?

   

Thank you!

0 Likes
Reply
Esteemed Contributor II

Your interrupt handler will be started as soon as a byte is received, that's hardware. I assume you have got a Pioneer Kit to run and debug your program. I commented a very bit in your code.

   

Beware of the UART levels! Check the datasheet for your GPRS so that you do not exceed the max voltage of the PSoC which is the supply voltage 3.3V or 5V (selectable by a jumper). Some external devices deliver up to +-12V which will blow your PSoC at once.

   

 

   

Bob

0 Likes
Reply
New Contributor II

Thank bob

   

Please tell me specifically about the use of an interrupt for multiple input data. I have no idea how to run its (interupt).

   

if you can, please paint  draw algorithm flowchart?

0 Likes
Reply
Esteemed Contributor II

Interrupt handling is something very basic in computer programming and is not related to PSoCs only, so it is advisable to study different sources. a good starting point would be wikipedia.

   

 

   

Excerpt of a manuscript of mine

   

 

   

Using Interrupts

   

The difference between "wired" and "weird" is only minimal

   

 

   

A cooking-recipe has many similarities with a program: It tells what to do with which resources in which order. Instructions like "Whip the egg-white until stiff" does not differ much from usual pseudo-code. Now imagine you are stirring a cake dough and suddenly the door-bell rings. You put aside your equipment and open the door. The postman delivers a parcel which you sign for, close the door leaving the parcel on the mantelpiece and continue with your cake. When now you have got a balll-pen in your hand instead of your wooden spoon something with your interrupt has gone wrong.

   

One major task an embedded designer has to care for are the interrupts, sometimes called "Exceptions". A designer not only cares for interrupts, he will use and handle them because they make project realization easier or even possible. Interrupts -depending on the IDE you use- are often encapsulated in functions or objects helping to maintain their properties.

   

An interrupt halts the actual program flow and executes an associated piece of code named "interrupt handler". When the handler finishes the original environment of the interrupted program is restored and the execution resumes at the point where the program has been halted. While the handler executes, only interrupts with a higher priority will be served from now on (if any) until the handler finishes.

   

The interrupt in an embedded micro is normally generated by a programmable pice of hardware named "Interrupt Controller Unit" (ICU) which usually allows for

   
        
  • Setting an interrupt priority1

  •     
  • Masking interrupts

  •     
  • Defining the signal edge or a level generating the interrupt

  •     
  • Firing an interrupt by software command

  •    
   

Some ICUs require to clear the interrupt explicitly, some ICUs do that automatically and some clearing can be done within the encapsulated interrupt-code, get acquainted with your embedded handbook. But always the source of the interrupt has to be cleared before the handler finishes, otherwise when the handler finishes and interrupts are allowed again the non-cleared source would instantly fire the same interrupt again and again.

   

Since interrupts may fire at any time within the normal program flow without caring for C-statements or lines of code some pitfalls are associated with them, I will later explain how to handle those.

   

Keep handlers short! That minimizes jitter in the timing and helps to reduce side effects due to a loss of responsiveness while the handler is executed.
Avoid loops in handlers. An absolute no-go is to wait for an event or to use delays in a handler. Check especially system-functions you call from within a handler for delay-loops, as for instance writing to LCDs or peripherals may induce them. A very common practice is just to set a flag within the handler and exit. In the main-loop the flag is tested, acted upon the result and reset back again. This of course only works when the expected frequency of the interrupts is less than the frequency of the main-loop.

   
    

1Trough historical reasons are lower priority numbers used to define higher priorities

   
0 Likes
Reply
Esteemed Contributor

A useful reference -

   

 

   

    

   

          https://www.youtube.com/watch?v=tn45bY-EA8Y

   

 

   

Regards, Dana.

0 Likes
Reply
New Contributor II

 thank so much to bob and daknigh!

0 Likes
Reply