MikroC Pro for ARM Multiple byte store using UART Interrupt problem

Fully featured ARM compilers available on Windows, Linux, and macOS.
Post Reply
Author
Message
himueeepust
Posts: 2
Joined: 06 Oct 2015 15:37

MikroC Pro for ARM Multiple byte store using UART Interrupt problem

#1 Post by himueeepust » 15 Jul 2021 06:38

I am new in MikroC pro for ARM. I am trying to use UART interrupt to store multiple myte in a buffer and later compare it with my desire value. First byte is a header which is '*' (Star) . If star is received first then start to store 8 byte more. But when i send "*12345678" it receive some garbage value. When i send then value one by one then it is received well but when i send these value at a time from a port monitor software it not working. Please give me some guide line if any thing found in my code given bellow:
//*******************************************************************************
// MCU : STM32F030C6
// Crystal : Internal 8MHz

Code: Select all

// LCD module connections
sbit LCD_RS at GPIOB_ODR.B3;
sbit LCD_EN at GPIOB_ODR.B4;
sbit LCD_D4 at GPIOB_ODR.B5;
sbit LCD_D5 at GPIOB_ODR.B6;
sbit LCD_D6 at GPIOB_ODR.B7;
sbit LCD_D7 at GPIOB_ODR.B8;
// End LCD module connections


char byte[16],i;
char rxchar='$';
unsigned short ready,header=0,flag=0;
unsigned int check_sum_total=0,check_sum=1234;
char *Fan_RPM=" 000";

void interrupt() iv IVT_INT_USART1 ics ICS_AUTO
                      {
                              if(RXNEIE_USART1_CR1_bit == 1)     // are Rx data ready ints enabled ?
                                 {

                                    if(RXNE_USART1_SR_bit == 1)       // yes, is interupt pending ?
                                                {

                                             if(header==1)     // If Header is OK read next n bytes
                                                                 {
                                                                    rxchar = UART1_Read();  //
                                                                    byte[i] = rxchar;
                                                                    i++;

                                                               if(i==8)    //    Read byte till 38 byte receive
                                                                      {
                                                                      for(i=1;i<8;i++)
                                                                            {
                                                                            check_sum_total=check_sum_total+byte[i];
                                                                            }
                                                                        check_sum=check_sum_total&255;


                                                                            {

                                                                            GPIOB_ODR.B15=~GPIOB_ODR.B15;    // Just for indication
                                                                            header=0;// RESET HEADER
                                                                      }


                                                                        }

                                                                }     // End (header==1)
                                                                
                                 if(header==0) // Check the header
                                                                 {

                                                                 rxchar = UART1_Read();  //
                                                                 if(rxchar==42)    // HEADER if '*' or not
                                                                                {
                                                                                 GPIOB_ODR.B14=~GPIOB_ODR.B14;      // Just for indication
                                                                                 header=1;    // Header OK, allow next byte to store in buffer
                                                                                 check_sum_total=0; // Reset Check SUM
                                                                                 i=0;     // Reset byte count
                                                                                 flag=1;
                                                                                }


                                                                 }  // END Header check
                                    RXNE_USART1_SR_bit = 0 ;    // Clear Interrupt Flag
                                                                
                                 }
                               }           //  END Interrupt Flag
                       }      // End UART ISR

 void display()
               {
               if(flag==1)
               {
               if(byte[2]==51)      // if 3rd byte is '3'
                                {
                                Lcd_Out(3,10,"OK");
                                Lcd_Out(2,1,byte);
                                }
                                else
                                    Lcd_Out(3,10,"NO");
               flag=0;
               }
               
               
               }

 void main()
           {

             GPIO_Digital_Output(&GPIOB_ODR, _GPIO_PINMASK_14);       // Set PORTC9 as digital output
             GPIO_Digital_Output(&GPIOB_ODR, _GPIO_PINMASK_15);       // Set PORTC9 as digital output
             GPIOB_ODR.B14=0;
             GPIOB_ODR.B15=1;

             Lcd_Init();                        // Initialize LCD
             Lcd_Cmd(_LCD_CLEAR);               // Clear display
             Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
             Lcd_Out(1,1,"STM Blue Pill");                 // Write text in second row
             Delay_ms(1000);
             Lcd_Cmd(_LCD_CLEAR);               // Clear display

             UART1_Init_Advanced(2400, _UART_8_BIT_DATA, _UART_NOPARITY, _UART_ONE_STOPBIT, &_GPIO_MODULE_USART1_PA9_10);

             RCC_APB2ENR.USART1EN = 1 ;          // Turn on USART1 module clocks
             USART1_CR1bits.UE = 1;                 //Set USART1
             USART1_CR1bits.RE = 1;                 //Set USART1 RX to receive incoming data
             USART1_CR1bits.TE = 1;                 //Set USART1 TX to send data
             USART1_CR1bits.RXNEIE = 1;             //Enable RX interrupt


             UART1_Write_Text("INTERRUPT TEST\n\n");
             UART_Write(10);
             UART_Write(13);
             NVIC_IntEnable(IVT_INT_USART1);
             Enableinterrupts();



         while(1)
                 {

                 display();
                                    
                 if(header==1) // Just for checking in header or not
                              Lcd_Out(3,1,"1");
                              else
                                  Lcd_Out(3,1,"0");

                 Fan_RPM[0]=(check_sum/1000)+48;
                 Fan_RPM[1]=((check_sum/100)%10)+48;
                 Fan_RPM[2]=((check_sum/10)%10)+48;
                 Fan_RPM[3]=((check_sum)%10)+48;
                 Lcd_Out(4,1,Fan_RPM);

                 Fan_RPM[0]=(i/1000)+48;
                 Fan_RPM[1]=((i/100)%10)+48;
                 Fan_RPM[2]=((i/10)%10)+48;
                 Fan_RPM[3]=((i)%10)+48;
                 Lcd_Out(4,10,Fan_RPM);


                 }         // end while


             }           // End main
             
             

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: MikroC Pro for ARM Multiple byte store using UART Interrupt problem

#2 Post by filip » 15 Jul 2021 07:54

Hi,

It is possible that you go through the code with a debugger ?
On the other side, I see that the counter variable, i, is not initialized. This can sometimes cause issues.

Regards,
Filip.

himueeepust
Posts: 2
Joined: 06 Oct 2015 15:37

Re: MikroC Pro for ARM Multiple byte store using UART Interrupt problem

#3 Post by himueeepust » 15 Jul 2021 09:01

filip wrote:
15 Jul 2021 07:54
Hi,

It is possible that you go through the code with a debugger ?
On the other side, I see that the counter variable, i, is not initialized. This can sometimes cause issues.

Regards,
Filip.
I have initialized the variable i as character ; see char i; Almost same code i have done in MikroC pro for PIC and working well. Just here i have change the ISR name and its syntax. Do you have any example code to store multiple byte in buffer by UART using MikroC pro for ARM.

Post Reply

Return to “ARM AI Compilers”