M8 MCU serial port communication monitoring (using PROTEUS serial port to simulate GSM module AT command communication)

Publisher:云淡雅致Latest update time:2019-11-06 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Implement monitoring (using PROTEUS serial port example) V1.0
M8 MCU simulation schematic diagram is as follows

The microcontroller source program is as follows:

////////////////////////////////////////////////////////////////////////////////

// ICC-AVR application builder : 6-25 06:02:39 PM

// Target : M8

// Crystal: 3.6864Mhz

// Author:  Anwarye

// Title:   Detcetaphone

////////////////////////////////////////////////////////////////////////////////


#include

#include

#include "var.h"


#define XTAL == 3.6864M


////////////////////////////////////////////////////////////////////////////////

#pragma interrupt_handler uart0_rx_isr:12

void uart0_rx_isr(void)

{

    unsigned char i,j;

    if(UDR == 0x0A)

    { 

       return; // Receive 0x0A and discard it directly

    }

        

        i = RxIndex;

        j = RxIndex-1;

    if((RxBuf[0] == 0x30) && (RxBuf[1] == 0x0D))

    {

        RxIndexTail = RxIndex; // Save the receive buffer pointer tail

        RxIndex = 0; // Receive buffer pointer reset to zero

            RxStatus = 1; // Set the reception completion flag

                OkFlag = 1;

                error = 0;

                return; 

        }

    if((RxBuf[0] == 0x32) && (RxBuf[1] == 0x0D)) // Received incoming call signal, set the incoming call flag

    {

        CallInFlag = 1; // Set incoming call flag

            RxIndex = 0;

                RxStatus = 1;

                OkFlag = 1;

                return;  

    }

    else if((RxBuf[0] == 0x33) && (RxBuf[1] == 0x0D)) // The called number is busy or the caller is on hang-up

    {

        BusyFlag = 1; //Set incoming call flag

            RxIndex = 0;

                RxStatus = 1;

                OkFlag = 1;

                return;  

    }      

    else if((RxBuf[0] == 0x37) && (RxBuf[1] == 0x0D)) // Received incoming call signal, set the incoming call flag

    {

        NoCarrierFlag = 1; // Set the incoming call flag NO CARRIER

            RxIndex = 0;

                RxStatus = 1;

                OkFlag = 1;

                return;

    }

    else if((RxBuf[0] == 0x34) && (RxBuf[1] == 0x0D)) // Received incoming call signal, set the incoming call flag

    {

            RxIndex = 0;

                RxStatus = 1;

                OkFlag = 1;

                error = 1;

                return;        

        }

        else 

    {

    

    RxBuf[RxIndex++] = UDR; // Continue receiving data and put it into the receive buffer

        RxStatus = 0; // Serial port receiving

// return; // End receiving and exit directly

        }

}


////////////////////////////////////////////////////////////////////////////////

#pragma interrupt_handler uart0_tx_isr:14

void uart0_tx_isr(void)

{

    

    if(!TxStatus) // This frame of data has not been sent out, so continue to send  

        {

            //while ( !(UCSRA & (1<            UDR = TxBuf[TxIndex];                

        }

        if((TxBuf[TxIndex] == 0x0D) || (TxBuf[TxIndex] == 0x1A)) // End of sending this frame of data

        {

                TxStatus = 1; // Set the sending completion status flag

                TxIndex = 0; // Send buffer pointer reset to zero

                UCSRB &= ~((1<                return;

        }

        TxIndex++;                              

}

unsigned char CheckRx(void)

{

    unsigned char i,j;

        i = RxIndex;

        j = RxIndex - 1;

    if(( RxBuf[j] == 0x30) && (RxBuf[i] == 0x0D))

        {

            RxIndex = 0;

                return (1);

        }

        else

        {

            return (0);

        }

}

/*-----------------------------------------------------------------------*/

///////////////////////////// Clear receive buffer //////////////////////////////////

/*-----------------------------------------------------------------------*/

void ClearRxBuf(void)

{

    unsigned char i;

        for(i=0;i<128;i++)

        {

            RxBuf[i] = 0;

        }

}

/*-----------------------------------------------------------------------*/

///////////////////////////// Enable serial port sending /////////////////////////////////

/*-----------------------------------------------------------------------*/

void TxEnable(void)

{

    //RxIndex = 0;

    UDR = TxBuf[0]; // Send the buffer header to the serial port send register, activate and start sending

        UCSRB |= ((1<}

/*-----------------------------------------------------------------------*/

////////////////////////////// Send AT command////////////////////////////////

/*-----------------------------------------------------------------------*/

void Put_AT_command(const unsigned char *atc, unsigned char atlen)

{

    unsigned char count;

        

        for(count = 0;count < atlen;count++) // AT command is moved into the send buffer

        {

            TxBuf[count] = atc[count];

        }

        

        TxBuf[atlen] = 0x0D; // Add "Enter" to the end of the send buffer

        TxBuf[atlen + 1] = 0x00; // Send buffer end symbol


        OkFlag = 0;

    TxStatus = 0;

        TxIndex = 1; // Send pointer offset 1

        TxEnable(); // Activate sending

    while(!TxStatus); // Wait for the transmission to end, and then send after the transmission buffer pointer is zero

        DelayMs(20);

        while(!OkFlag); // Receive the module return OK, the command ends

        OkFlag = 0;


}


/*-----------------------------------------------------------------------*/

/////////////////////////// Send AT command string from memory //////////////////////////////

/*-----------------------------------------------------------------------*/

void Put_AT_String(unsigned char *atc, unsigned char atlen)

{

    unsigned char count;

                

        for(count = 0;count < atlen;count++)

        {

            TxBuf[count] = *(atc+count);

        }

        

        TxBuf[atlen] = 0x0D; // Add "Enter" to the end of the send buffer

        TxBuf[atlen + 1] = 0x00; // Send buffer end symbol


    OkFlag = 0;

    TxStatus = 0;

        TxIndex = 1; // Send pointer offset 1

        TxEnable(); // Activate sending

    while(!TxStatus); // Wait for the transmission to end, and then send after the transmission buffer pointer is zero

        DelayMs(20);

        while(!OkFlag); // Receive the module return OK, the command ends

        OkFlag = 0;

}

/*-----------------------------------------------------------------------*/

////////////////////////// Send a string of data to the serial port /////////////////////////////

/*-----------------------------------------------------------------------*/

void PutString(unsigned char *str, unsigned char length,unsigned char retflag)

{

    unsigned char count;


        for(count = 0;count < length;count++)

        {

            TxBuf[count] = *(str+count);

        }

        

        TxBuf[length] = 0x0D; // Add "Enter" to the end of the send buffer

        TxBuf[length+ 1] = 0x00; // Send buffer end symbol

        

    TxStatus = 0;

        TxIndex = 1; // Send pointer offset 1

        OkFlag = 0;

        TxEnable();

                                                  // Activate sending

    while(!TxStatus); // Wait for the transmission to end, and then send after the transmission buffer pointer is zero

        DelayMs(20);

    if(retflag)

    {        

        while(!OkFlag); // Receive the module return OK, the command ends

                OkFlag = 0;

        }

        else

        {

        DelayMs(200);

                OkFlag = 0;

        }

}

////////////////////////////////////////////////////////////////////////////////


/*-----------------------------------------------------------------------*/

//////////////////////////// Dial ////////////////////////////

/*-----------------------------------------------------------------------*/

void DialNum(void)

{

    unsigned char i,count=0,length=0;


        while ((PhoneNum[count++] != 0)) // Calculate the length of the phone number

        {

            length++;

        }

        

        TxBuf[0] = 'A';

        TxBuf[1] = 'T';

        TxBuf[2] = 'D';

        

        i = 3;

    for(count=0;count        {

            TxBuf[i++] = PhoneNum[count];

        }

    

        TxBuf[length+3] = ';';

        TxBuf[length+4] = 0x0D;

        

    OkFlag = 0;

    TxStatus = 0;

        TxIndex = 1; // Send pointer offset 1

        TxEnable(); // Activate sending

    while(!TxStatus); // Wait for the transmission to end, and then send after the transmission buffer pointer is zero

        DelayMs(20);

        //while(!OkFlag); // Receive the module return OK, the command ends        

    OkFlag = 0;


/*-----------------------------------------------------------------------*/

////////////////////////////// Compare two strings/////////////////////////////////

/*-----------------------------------------------------------------------*/

unsigned char StringCompare(volatile unsigned char *str1,const unsigned char *str2,unsigned char strlen)

{

    while(strlen--)

        {

            if(*str1++ != *str2++) return(FALSE); // If the two strings are not equal, return false

        }

        return(TRUE);

}


/*-----------------------------------------------------------------------*/

//////////////////////////////// Delete text message ////////////////////////////////

/*-----------------------------------------------------------------------*/

void Delete_SMS(unsigned char num) // Delete SMS, =0 deletes all SMS

{

    unsigned char count;


        for(count = 0;count < AtcmgdLen;count++) // AT command is transferred into the send buffer

        {

            TmpBuf[count] = Atcmgd[count];

        }

                

        if(num == ALL)

        {

           for(count = 1; count < 10; count++)

           {

               TmpBuf[AtcmgdLen] = count + 0x30;

               Put_AT_String((unsigned char*)TmpBuf,(AtcmgdLen+1));


                }

           TmpBuf[AtcmgdLen] = 0x31;

       for(count = 0; count < 10; count++)

           {

               TmpBuf[AtcmgdLen+1] = count + 0x30;

               Put_AT_String((unsigned char*)TmpBuf,(AtcmgdLen+2));


            }


        }

        else

        {

[1] [2] [3]
Reference address:M8 MCU serial port communication monitoring (using PROTEUS serial port to simulate GSM module AT command communication)

Previous article:Dynamixel digital servo driver
Next article:M16 MCU l infrared decoding program + led digital tube display

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号