USART interrupt example

General discussion on mikroC.
Author
Message
Wessam
Posts: 21
Joined: 24 Jan 2014 00:49

Re: USART interrupt example

#16 Post by Wessam » 07 Jun 2014 00:38

I know this topic is old now. Thanks for your posts, after adding interrupt to my code, what i wanna do is to be able to read using UART more then one time but i only got it to make it stop not to read again!
am using PIC16F877A and this is my code:

Code: Select all

void interrupt() 
  {
       INTCON.GIE = 0;

       if (PIR1.RCIF)      // test the interrupt for uart rx
       {
           dat[i] = UART1_Read();
            i++;
            if (i == 11)
            {flag = 1;}
           PIR1.RCIF = 0;       // clear RCIF

       }
  }
here is the process part all my variables are global variables, what am i doing wrong that it wont re-read the data again?

Code: Select all

void main()
{


    TrisB.f0 = 0;     
    TrisB.f1 = 0;    
    TrisB.f2 = 0;      
    PortB = 0;
    PIE1.RCIE = 1;     //enable USART receiver interrupt bit
    INTCON.PEIE = 1;   //enable peripheral interrupt bit
    INTCON.GIE = 1;    //enable global interrupt bit

    PortD = 0x00;
    TrisD = 0x00;


    adcon1 = 0x06; // Configure AN pins as digital
    adcon0 = 0x00;
    cmcon  = 0x07;

    UART1_Init(9600);
    Delay_ms(100);

  while (1)
    {
        if(flag ==1){  
label1: while (1)
        {

            for (j=0; j < GRE; j++)
            {
               if(PIR1.RCIF)
                {GRE=0, RED=0, RED21=0, RED22=0, RED31=0, RED32=0, YLL, TOT=0;goto reset;}
            else{
                //process
                }
            }
            for (j=0; j < 1; j++)
            {
                if(PIR1.RCIF)
               {GRE=0, RED=0, RED21=0, RED22=0, RED31=0, RED32=0, YLL, TOT=0;goto reset;}
            else{
                   //process
                }
             }
             for (j=0; j < RED; j++)
            {  
                if(PIR1.RCIF)
                {GRE=0, RED=0, RED21=0, RED22=0, RED31=0, RED32=0, YLL, TOT=0;goto reset;}
            else{
                  //process 
                }
            }
            for (j=0; j < 1; j++)
            {    
                 if(PIR1.RCIF)
               {GRE=0, RED=0, RED21=0, RED22=0, RED31=0, RED32=0, YLL, TOT=0;goto reset;}
            else{
                   //process
                }

            }

      }
     flag = 0; i=0;
      }}

aviraje8@gmail.com
Posts: 20
Joined: 26 Jun 2014 07:03

Re: USART interrupt example

#17 Post by aviraje8@gmail.com » 25 Jul 2014 07:05

hi these is my code.I am trying to send sms ON then LED blinks ON & when send sms OFF then LED blinks OFF but this happens only one time & i want it forever plz plz help me


#include<C:\Documents and Settings\A TO Z\Desktop\NIRMITI FINAL\gsm.h>
#include<C:\Documents and Settings\A TO Z\Desktop\NIRMITI FINAL\send_msg.h>
char count=0,j,i=0,a[10],b[30];
char rxchar,flag=0,x;
char rxarray[4];
char buff[]="ON";
char stuff[]="OFF";

const code char nirmitifinal2[1024];

char GLCD_DataPort at PORTB;
sbit GLCD_CS1 at RC0_bit;
sbit GLCD_CS2 at RC1_bit;
sbit GLCD_RS at RC2_bit;
sbit GLCD_RW at RC3_bit;
sbit GLCD_EN at RC4_bit;
sbit GLCD_RST at RA3_bit;

sbit GLCD_CS1_Direction at TRISC0_bit;
sbit GLCD_CS2_Direction at TRISC1_bit;
sbit GLCD_RS_Direction at TRISC2_bit;
sbit GLCD_RW_Direction at TRISC3_bit;
sbit GLCD_EN_Direction at TRISC4_bit;
sbit GLCD_RST_Direction at TRISA3_bit;

void interrupt()
{
if (PIR1.RCIF)
{
if (UART1_Data_Ready())
{
rxchar = UART1_Read();
rxarray = rxchar;
Delay_ms(500);
i++;
if(rxchar==4)
{
flag=1;
}

}
}
}

void send_num()
{
UART1_Write_Text("AT+CMGS="); // AT+CMGS
UART1_Write(0x22); //"
for(count=0;count<10;count++)
{
IntToStr(b[count],a);
UART1_Write_Text(a); //"
Delay_ms(200);
}
UART1_Write(0x22); //"
UART1_Write(0x0D); //Enter
}

void gsm_receive()
{
gsm_Init();
UART1_Write_Text("AT+CMGR=1"); // AT+CMGS
UART1_Write(0x0A); //Enter
UART1_Write(0x0D); //Enter
}

void main()
{
INTCON.GIE = 1;
INTCON.PEIE = 1;
PIE1.RCIE=1;
ADCON1=0X06;
TRISA=0XFF;
TRISC5_bit = 0;

Glcd_Init();
Glcd_Image(nirmitifinal2); // Draw image
Delay_ms(1000);
Glcd_Fill(0xFF);
Delay_ms(1000);
Glcd_Fill(0xFF);

while(1)
{
if(RA0_bit==0)
{
count++;
if(count==10)
{
count=0;
}
EEPROM_Write(0x00+i, count);
Delay_ms(500);
j = EEPROM_Read(0x00+i);
b=j;
IntToStr(b,a);
Glcd_Write_Text(a, 0, 0, 0);
}

if(RA1_bit==0)
{
Delay_ms(500);
i++;
count=0;
}

if(RA2_bit==1)
{
if(RA4_bit==1)
{
if(RA5_bit==0)
{
Glcd_Write_Text("on", 2, 2, 1);
RC5_bit=1;
Delay_ms(1000);
RC5_bit=0;
gsm_Init();
send_num();
send_msg();
}
}
}

if(RA2_bit== 1)
{
if(RA4_bit==0)
{
if(RA5_bit==0)
{
Glcd_Write_Text("remains on", 2, 2, 1);
RC5_bit=1;
Delay_ms(500);
RC5_bit=0;
}
}
}

if(RA2_bit== 0)
{
if(RA4_bit==0)
{
if(RA5_bit==0)
{
Glcd_Write_Text("lcd off", 2, 2, 1);
gsm_Init();
send_num();
send_msg();
}
}
}

if(RA2_bit== 0)
{
if(RA4_bit==1)
{
if(RA5_bit==0)
{
Glcd_Write_Text("worst condition", 2, 2, 1);
}
}
}

gsm_receive();
Delay_ms(200);

if(flag=1)
{
if(strcmp(buff,rxarray)==0)
{
RC5_bit=1;
Delay_ms(500);
UART1_Write(13);
UART1_Write(0x0A); //Enter
}

else if(strcmp(buff,rxarray)!=0)
{
RC5_bit=0;
Delay_ms(500);
UART1_Write(13);
UART1_Write(0x0A); //Enter
}
flag=0;
}
}
}

User avatar
marina.petrovic
Posts: 2986
Joined: 18 Apr 2013 08:11

Re: USART interrupt example

#18 Post by marina.petrovic » 28 Jul 2014 13:21

Hi,

From your explanation and code that you posted I can't know for sure where the problem occurs.
You can take a look at the examples which we provided with, for example, GSM click board:
http://www.libstock.com/projects/view/4 ... ck-example

We provided simple code which demonstrate controlling the EasyPIC v7 board with SMS.
You can try to adjust the example for hardware that you use and to suit your needs.

Brest regards,
Marina

Imbalanc3
Posts: 7
Joined: 27 Dec 2014 09:28

Re: USART interrupt example

#19 Post by Imbalanc3 » 07 Jan 2015 12:25

I noticed that the this example is using USART but i never used USART in and of my programs but i already used UART in one of my projects... can i use this interupt in UART? BTW why is there no library in USART for MikroC PRO?

User avatar
marina.petrovic
Posts: 2986
Joined: 18 Apr 2013 08:11

Re: USART interrupt example

#20 Post by marina.petrovic » 08 Jan 2015 14:05

Hi,

I am not sure that I understand your question properly.
In GSM click examples we use UART interrupt (more detailed explanation about very UART interrupt you can find in datasheet
of the MCU which you use).
If you want to use standard UART communication with some other interrupt (for example Timer interrupt),
it's also possible, but you need to be very careful when you write your project to avoid possible problems in UART communication.

In our compilers (like mikroC PRO for PIC), we provided standard UART library.
More about this library you can find in compiler Help: mikroC PRO for PIC Libraries -> Hardware Libraries -> UART Library.

Best regards,
Marina

Imbalanc3
Posts: 7
Joined: 27 Dec 2014 09:28

Re: USART interrupt example

#21 Post by Imbalanc3 » 09 Jan 2015 13:20

Sir thanks for the reply... i just want to know if the example of drdoug works for uart. since the code of drdoug is for USART.
Since im using mikroc pro, is it possible to use the uart library for this interrupt?
My only experience in interrupts are with timer interrupt and i think it is hardware interrupt.....

ayoubakil
Posts: 1
Joined: 09 Jan 2015 18:19

Re: USART interrupt example

#22 Post by ayoubakil » 09 Jan 2015 18:28

Hi
I tried to use the first example
in this code i will control 4 servomotors with two PICs microcontrollers via uart
that's doesn't work any help please :(

Transmitter:

Code: Select all

unsigned char adcData,adcData1,adcData2,adcData3;
void main()
{
  ADCON1 = 0x80;
  TRISA = 0b00001111;  // Analog input
  UART1_Init(9600);    // Initialize UART module at 9600bps
  Delay_ms(100);       // Wait for UART module to stabilize
  ADC_Init();
  
while(1){              // repeat operation for 4 adc chanels
// 1er channel
adcData = ADC_Read(0)*0.25;
Uart1_Write(adcData);        // send 2 most significant
Delay_ms(100);
//2eme channel
adcData1 = ADC_Read(1)*0.25;
Uart1_Write(adcData1);
Delay_ms(100);
//3eme channel
adcData2 = ADC_Read(2)*0.25;
Delay_ms(100);
Uart1_Write(adcData2);
//4eme channel
adcData3 = ADC_Read(3)*0.25;
Uart1_Write(adcData3);
Delay_ms(100);
}                     // lets uart bus free for other chanel to send data
}
And receiver

Code: Select all

unsigned char result,result1,result2,result3;
const short n = 4;
char rxchar, i = 0, flag = 0; // Variable for storing the data from UART and array counter
unsigned char rxarray[n];   // array to store the received charaters

void interrupt () {
  if (PIR1.RCIF) {          // test the interrupt for uart rx
    rxchar = Uart1_Read();  //
    rxarray[i] = rxchar;
    i++;
    if(i==4)i=0;
  // Only select one of the following statements
    if (rxchar == 36) {  // select this if looking for a terminating character
  //if (i == n) (         // select this if looking for a number of characters
      flag = 1;
      } // end if (rxchar == "$")
    } // end  if (PIR1.RCIF)
  } // end interrupt


void main () {
unsigned short j;

 INTCON.GIE = 1;
 INTCON.PEIE = 1;
 PIE1.RCIE = 1; //enable interrupt.
 Uart1_Init(9600);
 TRISB = 0b00000000;
 while(1) {   // Begin endless loop
  // This section is where you tell the program what to do with the data once it
  // has all arrived
  if (flag ==1) {
    j = 0;
    while(j < i) {
     Uart1_Write(rxarray[j]);
     j++;
     } // end while(rxarray[i])
    i = 0;
    flag = 0;
  } // end if (flag)
result = rxarray[0];      // or
portB.B0 = 1;                  // Rising edge of pulse
Delay_us(1244);
for(i=0; i<result; i++)
{
Delay_us(1);
}
portB.B0 = 0;                 // Falling edge of pulse
//2 ème channel
result1 = rxarray[1];      // or
portB.B1 = 1;                  // Rising edge of pulse
Delay_us(1244);
for(i=0; i<result1; i++)
{
Delay_us(1);
}
portB.B1 = 0;                 // Falling edge of pulse
//3ème channel
result2 = rxarray[2];      // or
portB.B2 = 1;                  // Rising edge of pulse
Delay_us(1244);
for(i=0; i<result2; i++)
{
Delay_us(1);
}
portB.B2 = 0;                 // Falling edge of pulse
//4ème channel
result3 = rxarray[3];      // or
portB.B3 = 1;                  // Rising edge of pulse
Delay_us(1244);
for(i=0; i<result3; i++)
{
Delay_us(1);
}
portB.B3 = 0;                 // Falling edge of pulse

 } // end while
} // end main
Attachments
Sans titre.png
Sans titre.png (27.55 KiB) Viewed 9163 times

abdo2007451aa
Posts: 2
Joined: 11 Mar 2016 14:26

Re: USART interrupt example

#23 Post by abdo2007451aa » 31 Dec 2016 18:08

you must clear rxarray when you make flag=0 or make message of "ON" with 3 letters like "ONN", the program will be okay .

yogesh
Posts: 22
Joined: 27 Aug 2017 19:28

Re: USART interrupt example

#24 Post by yogesh » 08 Sep 2017 11:43

Code: Select all

///////////////////////////////////////////
 //LCD decleration//
#define LCD_DIR   TRISD
#define CTRL_DIR1   TRISB.F0
#define CTRL_DIR2 TRISB.F1
#define LCD_DIR TRISD

#define LCD       PORTD
#define RS        PORTB.F0
#define E         PORTB.F1
#include"lcd.h"
//////////////////////////////////////////////////////
 #define STRLEN 55
volatile unsigned char flag=0;
unsigned char flag1=0;
unsigned char t;
volatile unsigned char rcindex;
volatile unsigned char rcbuf[STRLEN];
unsigned char num[]="918208996450";
unsigned char i,j;
unsigned char rcvd[13];
unsigned char Msg[]="Relay On";


 void UART_Init(void)
 {
 TXSTA.TXEN=1;     // enable transmitter
 TXSTA.BRGH=1;     // enable high baud rate
 RCSTA.CREN=1;     // continuous data receiption

 //configure input output
 TRISC.TRISC6=1;   // rRX pin as a input
 TRISC.TRISC7=1;   // TX pin as a input
 TRISC.TRISC0=1;
 SPBRG=103;         // set buad rate 9600

 PIE1.RCIE=1;      //enable USART receive interrupt
 RCSTA.SPEN=1;     //enable USART

 }
 ///////////Initialise Message///////////////

 void Init_Msg()
 {
      UART1_Write_Text("AT\r\n");           // turn disable the echo
      delay_ms(500);
      UART1_Write_Text("AT+CMGF = 1\r");        // set text mode
      Delay_ms(100);
      UART1_Write_Text("AT+CMGS=\"+918446323311\"\r");
      delay_ms(500);
      UART1_Write_text("Relay Opearted \r");
      UART1_Write(26);
      UART1_Write(0x1A);
 }



void main() {

      INTCON.PEIE=1;                          //enable interrupt
     INTCON.GIE=1;                             // enable globle interrupt
     CTRL_DIR1=CTRL_DIR2=0X00;
     LCD_DIR=0X00;
     TRISB.F6=0x00;
     TRISB.F7=0xFF;
     init_lcd();
     delay_ms(500);
     
      //Init_gsm();
      UART_Init();
      delay_ms(500);
      UART1_Write_Text("ATE0\r\n");           // turn disable the echo
      delay_ms(500);
      delay_ms(500);
      UART1_Write_Text("AT+CMGF = 1\r");        // set text mode
      Delay_ms(1000);
      UART1_Write_Text("AT+CNMI=2,2,0,0,0\r");
      Delay_ms(1000);

     cmd_lcd(0x80);
     string_lcd("Init Complete");
   while(1)
   {
   if(flag)                                //if flag is set execute the below command
           {
            // check();
             //cmd_lcd(0x80);
             //string_lcd(rcbuf);


             for(i=8;i<20;i++)
             {rcvd[i-8]=rcbuf[i];}      //get the number

                 if(rcvd[0]=='9')       //compare with predefined number
                 {
                 if(rcvd[1]=='1')
                 {
                  if(rcvd[2]=='8')
                  {
                   if(rcvd[3]=='2')
                   {
                    if(rcvd[4]=='0')
                    {
                     if(rcvd[5]=='8')
                     {
                      if(rcvd[6]=='9')
                      {
                       if(rcvd[7]=='9')
                       {
                        if(rcvd[8]=='6')
                        {
                         if(rcvd[9]=='4')
                         {
                           cmd_lcd(0x80);
                            string_lcd(rcbuf);
                           PORTB.F6=1;
                           delay_ms(5000); 
                           Init_Msg();           //if yes relay opearateed
                           // INTCON.PEIE=0;                          //enable interrupt
                           // INTCON.GIE=0;
                            /*delay_ms(1000);
                            Init_Msg();
                            delay_ms(2000);*/
                            /*INTCON.PEIE=1;                          //enable interrupt
                            INTCON.GIE=1;*/
                            //flag1=1;
                          }}}}}}}}}}
              flag=0;                          //set the falg to zero
        }
        if(PORTB.F7==0)
        {PORTB.F6=0;}

   }
}

void interrupt(void)
{
 if(PIR1.RCIF)           //check the receive interrupt flag has fired
 {
 t=RCREG;
 if((t!='\n')&&(rcindex<STRLEN))    //check the new line ,if not found  save the string to rcbuf
 {
    rcbuf[rcindex]=t;
     if(rcbuf[0]=='+')              //check the '+' char ,if yes increment rcindex
     {
      rcindex++;
     }
     else                          //if not ,reset the rcindex ,flag to zero
     {
      rcindex=0;                    //reset string index
      flag=0;
     }
 }
 else                       //if new line found reset the rcindex and set he flag
 {
 flag=1;
 rcindex=0;}
}
//PIR1.RCIF=0;              //it is read only
}
hi ,
my task is whenever there is msg received form particular number , relay will be operated and there should be acknowledgement Msg to sender that " relay operated"
but problem without the msg function i can operate relay , as i inserted msg function neither relay operate nor code work properly.
what should be the problem i could not find out.please help me . itroed many ways but still couldn't.
thanks in advance

Post Reply

Return to “mikroC General”