Table of Contents
Requirements:
Hardware:
- PIC24 Perf Board
- SIM800 Modem
- RS232 to USB converter Module
- Computer
- PICkit 3 Programmer
Software:
- MPLABX
- HyperTerminal/Serial Terminal
PIC24FJ256GA110 FAMILY
- On-Chip 2.5V Voltage Regulator
- 8 MHz Internal Oscillator
- 32-Bit by 16-Bit Hardware Divider
- Three 3-Wire/4-Wire SPI modules (support 4 Frame modes) with 8-Level FIFO Buffer
- Three I2C modules support Multi-Master/Slave modes and 7-Bit/10-Bit Addressing
- Four UART modules:
Pin diagram of PIC24
Fig. 1: PIC24FJ128GA106 Pinouts
Now we are going to see PIC24 Interrupt and GMS Data Send to Server Tutorial.
Both, Parallel and Serial modes of communication have certain advantages and disadvantages over one another. The serial communication is a preferred option due to its ability of long distance communication with error detection capability. The microcontrollers consist of an inbuilt hardware unit known as USART (Universal Synchronous Asynchronous Reception and Transmission) to facilitate serial transfer of data. For more details, refer to USART in AVR section.
Before starting USART, some general terms related to communication need to be understood. These terms are explained below.
Asynchronous Communication:
In this type of communication, both Transmitter (Tx) and Receiver (Rx) work on different clocks which means that they are not synchronized. Start and Stop bits are also sent with each Data byte to identify the data.
Synchronous Communication:
In this type of communication, both Tx and Rx are synchronized with the same clock and no Start or Stop bits are used.
Full-duplex Communication:
When either of the devices can send and receive data at the same instant, they are said to have full-duplex communication.
Fig. 2: Block Diagram Of Full-Duplex Communication in USART
Half-duplex Communication:
In this type of communication, a device can either behave as Transmitter or Receiver at an instant which means that a device can’t transmit data when it is receiving and vice versa.
Fig. 3: Block Diagram Of Half-Duplex Communication in USART
This PIC24 tutorial is a part of UART Communication using Pic Microcontroller. I recommend you to read this tutorial first. Because in that guide, we explain the following concepts of UART Module:
- PIC UART Module Registers
- Send data serially ( send string with UART)
- Receive data Serially (receive string with pic UART)
Why do we need to use PIC UART Interrupt?
But we used a polling method to receive string or data with pic UART module. But now we will see an interrupt method to receive data. Because polling is not an efficient method and it halts microcontroller execution at the same location waiting for the condition to meet. The polling method is kind of a round-robin method. The main disadvantage of this technique is the wastage of microcontroller time and resources.
Difference between Polling and Interrupt Method
This figure shows the comparison of the polling vs interrupt technique. In the first case, it keeps waiting until the specified condition is met. But in another case, the microcontroller will keep doing something else and as soon as interrupt response comes, it executes interrupt and return to the original program location where it left execution.
PIC24 UART Interrupt Registers
- Transmit Status and Control (UxTXREG)
- Receive Status and Control (UxRXREG.)
- Baud Rate Control (UxBRG)
Furthermore, to use pic UART interrupt, we need to configure some bits of these registers also
The primary features of the UART module are:
• Full-Duplex, 8 or 9-Bit Data Transmission through the UxTX and UxRX Pins
• Even, Odd or No Parity Options (for 8-bit data)
- One or Two Stop Bits
- Hardware Flow Control Option with UxCTS and UxRTS Pins
• Fully Integrated Baud Rate Generator with 16-Bit Prescaler
• Baud Rates Ranging from 1 Mbps to 15 bps at 16 MIPS
• 4-Deep, First-In First-Out (FIFO) Transmit Data Buffer
• 4-Deep FIFO Receive Data Buffer
• Parity, Framing and Buffer Overrun Error Detection
• Support for 9-Bit mode with Address Detect (9th bit = 1)
• Transmit and Receive Interrupts
• Loopback mode for Diagnostic Support
• Support for Sync and Break Characters
• Supports Automatic Baud Rate Detection
• IrDA Encoder and Decoder Logic
• 16x Baud Clock Output for IrDA Support
A simplified block diagram of the UART is shown in Figure 17-1. The UART module consists of these key
Important hardware elements:
• Baud Rate Generator
• Asynchronous Transmitter
• Asynchronous Receiver
UART Baud Rate Generator (BRG)
The UART module includes a dedicated 16-bit Baud Rate Generator. The UxBRG register controls the period of a free-running, 16-bit timer. Equation 17-1 shows the formula for computation of the baud rate with BRGH = 0.
Example 17-1 shows the calculation of the baud rate error for the following conditions:
• FCY = 4 MHz
• Desired Baud Rate = 9600
The maximum baud rate (BRGH = 0) possible is FCY/16 (for UxBRG = 0) and the minimum baud rate possible is FCY/(16 * 65536). Equation 17-2 shows the formula for computation of the baud rate with BRGH = 1.
The maximum baud rate (BRGH = 1) possible is FCY/4 (for UxBRG = 0) and the minimum baud rate possible is FCY/(4 * 65536). Writing a new value to the UxBRG register causes the BRG timer to be reset (cleared). This ensures the BRG does not wait for a timer overflow before generating the new baud rate.
Transmitting in 8-Bit Data Mode
1. Set up the UART:
a) Write appropriate values for data, parity and Stop bits.
b) Write appropriate baud rate value to the UxBRG register.
c) Set up transmit and receive interrupt enable and priority bits.
2. Enable the UART.
3. Set the UTXEN bit (causes a transmit interrupt two cycles after being set).
4. Write data byte to lower byte of UxTXREG word. The value will be immediately transferred to the Transmit Shift Register (TSR) and the serial bit stream will start shifting out with the next rising edge of the baud clock.
5. Alternately, the data byte may be transferred while UTXEN = 0, and then the user may set UTXEN. This will cause the serial bit stream to begin immediately because the baud clock will start from a cleared state.
6. A transmit interrupt will be generated as per interrupt control bit, UTXISELx.
Receiving in 8-Bit or 9-Bit Data
Mode
1. Set up the UART (as described in Section 17.2
“Transmitting in 8-Bit Data Mode”).
2. Enable the UART.
3. A receive interrupt will be generated when one or more data characters have been received as per interrupt control bit, URXISELx.
4. Read the OERR bit to determine if an overrun error has occurred. The OERR bit must be reset in software.
5. Read UxRXREG. The act of reading the UxRXREG character will move the next character to the top of the receive FIFO, including a new set of PERR and FERR values.
REGISTER : UxMODE: UARTx MODE REGISTER
bit 15 UARTEN: UARTx Enable bit(1)
1 = UARTx is enabled; all UARTx pins are controlled by UARTx as defined by UEN[1:0]
0 = UARTx is disabled; all UARTx pins are controlled by port latches, UARTx power consumption is minimal
bit 14 Unimplemented: Read as ‘0’
bit 13 USIDL: Stop in Idle Mode bit
1 = Discontinues module operation when the device enters Idle mode
0 = Continues module operation in Idle mode
bit 12 IREN: IrDA® Encoder and Decoder Enable bit(2)
1 = IrDA encoder and decoder are enabled
0 = IrDA encoder and decoder are disabled
bit 11 RTSMD: Mode Selection for UxRTS Pin bit
1 = UxRTS pin in Simplex mode
0 = UxRTS pin in Flow Control mode
bit 10 Unimplemented: Read as ‘0’
bit 9-8 UEN[1:0]: UARTx Enable bits
11 = UxTX, UxRX and BCLKx pins are enabled and used; UxCTS pin is controlled by port latches
10 = UxTX, UxRX, UxCTS and UxRTS pins are enabled and used
01 = UxTX, UxRX and UxRTS pins are enabled and used; UxCTS pin is controlled by port latches
00 = UxTX and UxRX pins are enabled and used; UxCTS and UxRTS/BCLKx pins are controlled by port
latches
bit 7 WAKE: Wake-up on Start Bit Detect During Sleep Mode Enable bit
1 = UARTx will continue to sample the UxRX pin; interrupt generated on falling edge, bit cleared in
hardware on following rising edge
0 = No wake-up enabled
bit 6 LPBACK: UARTx Loopback Mode Select bit
1 = Enables Loopback mode
0 = Loopback mode is disabled
bit 5 ABAUD: Auto-Baud Enable bit
1 = Enables baud rate measurement on the next character – requires reception of a Sync field (55h);
cleared in hardware upon completion
0 = Baud rate measurement is disabled or completed
Note 1: If UARTEN = 1, the peripheral inputs and outputs must be configured to an available RPn pin. See
Section 10.4 “Peripheral Pin Select (PPS)” for more information.
2: This feature is only available for the 16x BRG mode (BRGH = 0).
bit 4 RXINV: Receive Polarity Inversion bit
1 = UxRX Idle state is ‘0’
0 = UxRX Idle state is ‘1’
bit 3 BRGH: High Baud Rate Enable bit
1 = High-Speed mode (baud clock generated from FCY/4)
0 = Standard mode (baud clock generated from FCY/16)
bit 2-1 PDSEL[1:0]: Parity and Data Selection bits
11 = 9-bit data, no parity
10 = 8-bit data, odd parity
01 = 8-bit data, even parity
00 = 8-bit data, no parity
bit 0 STSEL: Stop Bit Selection bit
1 = Two Stop bits
0 = One Stop bit
REGISTER 17-1: UxMODE: UARTx MODE REGISTER (CONTINUED)
Note 1: If UARTEN = 1, the peripheral inputs and outputs must be configured to an available RPn pin. See
Section 10.4 “Peripheral Pin Select (PPS)” for more information.
REGISTER : UxSTA: UARTx STATUS AND CONTROL REGISTER
bit 15,13 UTXISEL[1:0]: Transmission Interrupt Mode Selection bits
11 = Reserved; do not use
10 = Interrupt when a character is transferred to the Transmit Shift Register (TSR), and as a result, the
transmit buffer becomes empty
01 = Interrupt when the last character is shifted out of the Transmit Shift Register; all transmit
operations are completed
00 = Interrupt when a character is transferred to the Transmit Shift Register (this implies there is at least
one character open in the transmit buffer)
bit 14 UTXINV: IrDA® Encoder Transmit Polarity Inversion bit(2)
IREN = 0:
1 = UxTX Idle ‘0’
0 = UxTX Idle ‘1’
IREN = 1:
1 = UxTX Idle ‘1’
0 = UxTX Idle ‘0’
bit 12 Unimplemented: Read as ‘0’
bit 11 UTXBRK: Transmit Break bit
1 = Sends Sync Break on next transmission – Start bit, followed by twelve ‘0’ bits, followed by Stop bit;
cleared by hardware upon completion
0 = Sync Break transmission is disabled or completed
bit 10 UTXEN: Transmit Enable bit(2)
1 = Transmit enabled; UxTX pin is controlled by UARTx
0 = Transmit disabled; any pending transmission is aborted and the buffer is reset, UxTX pin is controlled
by port
bit 9 UTXBF: Transmit Buffer Full Status bit (read-only)
1 = Transmit buffer is full
0 = Transmit buffer is not full; at least one more character can be written
bit 8 TRMT: Transmit Shift Register Empty bit (read-only)
1 = Transmit Shift Register is empty and transmit buffer is empty (the last transmission has completed)
0 = Transmit Shift Register is not empty, a transmission is in progress or queued
bit 7-6 URXISEL[1:0]: Receive Interrupt Mode Selection bits
11 = Interrupt is set on RSR transfer, making the receive buffer full (i.e., has four data characters)
10 = Interrupt is set on RSR transfer, making the receive buffer 3/4 full (i.e., has three data characters)
0x = Interrupt is set when any character is received and transferred from the RSR to the receive buffer;
receive buffer has one or more characters
Note 1: Value of bit only affects the transmit properties of the module when the IrDA® encoder is enabled (IREN = 1).
2: If UARTEN = 1, the peripheral inputs and outputs must be configured to an available RPn pin. See
Section 10.4 “Peripheral Pin Select (PPS)” for more information.
bit 5 ADDEN: Address Character Detect bit (bit 8 of received data = 1)
1 = Address Detect mode enabled; if 9-bit mode is not selected, this does not take effect
0 = Address Detect mode disabled
bit 4 RIDLE: Receiver Idle bit (read-only)
1 = Receiver is Idle
0 = Receiver is active
bit 3 PERR: Parity Error Status bit (read-only)
1 = Parity error has been detected for the current character (character at the top of the receive FIFO)
0 = Parity error has not been detected
bit 2 FERR: Framing Error Status bit (read-only)
1 = Framing error has been detected for the current character (character at the top of the receive FIFO)
0 = Framing error has not been detected
bit 1 OERR: Receive Buffer Overrun Error Status bit (clear/read-only)
1 = Receive buffer has overflowed
0 = Receive buffer has not overflowed (clearing a previously set OERR bit (1 0 transition) will reset
the receiver buffer and the RSR to the empty state)
bit 0 URXDA: Receive Buffer Data Available bit (read-only)
1 = Receive buffer has data; at least one more character can be read
What are AT commands?
AT commands are commands which are used to control the modems where AT stands for Attention. These commands were derived from Hayes commands which were used by the Hayes smart modems. Every wireless, as well as the dial up modems, require an AT command to interact with a computer machine. These AT commands along with other extended commands also require Hayes command set as a subset.
Requirements:
- STM32F303 Discovery board – 1
- GSM 800/900/M66/M60
- ST-Link Debugger – 1
- FTDI – 1
- Breadboard – 1
- Jumper wires
Usage
The AT commands can be used with GSM module and GPRS MODEMs or phone to access these services and information:
- SMS
- MMS
- Fax
- Voice link and other data over mobile network
- Information and configuration concerning the mobile devices or MODEM and SIM card.
Types of AT Commands:
There are four types of AT commands:
Fig. 1: Image Showing Classification of AT Commands
1) Test commands – used to check whether a command is supported or not by the MODEM.
SYNTAX: AT<command name>=?
For example: ATD=?
2) Read command – used to get mobile phone or MODEM settings for an operation.
SYNTAX: AT<command name>?
For example: AT+CBC?
3) Set commands – used to modify mobile phone or MODEM settings for an operation.
SYNTAX: AT<command name>=value1, value2, …, valueN
Some values in set commands can be optional.
For example: AT+CSCA=”+xxxxxxxxxx”, 120
4) Execution commands – used to carry out an operation.
SYNTAX: AT<command name>=parameter1, parameter2, …, parameterN
The read commands are not available to get value of last parameter assigned in execution commands because parameters of execution commands are not stored.
For example: AT+CMSS=1,”+ 9876543210”, 120
1) AT – This command is used to check communication between the module and the computer.
For example,
AT
OK
The command returns a result code OK if the computer (serial port) and module are connected properly. If any of module or SIM is not working, it would return a result code ERROR.
2) +CMGF – This command is used to set the SMS mode. Either text or PDU mode can be selected by assigning 1 or 0 in the command.
SYNTAX: AT+CMGF=<mode>
0: for PDU mode
1: for text mode
The text mode of SMS is easier to operate but it allows limited features of SMS. The PDU (protocol data unit) allows more access to SMS services but the operator requires bit level knowledge of TPDUs. The headers and body of SMS are accessed in hex format in PDU mode so it allows availing more features.
For example,
AT+CMGF=1
OK
3) +CMGW – This command is used to store message in the SIM.
SYNTAX: AT+CMGW=” Phone number”> Message to be stored Ctrl+z
As one types AT+CMGW and phone number, ‘>’ sign appears on next line where one can type the message. Multiple line messages can be typed in this case. This is why the message is terminated by providing a ‘Ctrl+z’ combination. As Ctrl+z is pressed, the following information response is displayed on the screen.
+CMGW: Number on which message has been stored
4) +CMGS – This command is used to send a SMS message to a phone number.
SYNTAX: AT+CMGS= serial number of message to be send.
As the command AT+CMGS and serial number of message are entered, SMS is sent to the particular SIM.
For example,
AT+CMGS=1
OK
5) ATD – This command is used to dial or call a number.
SYNTAX: ATD<Phone number>;(Enter)
For example,
ATD123456789;
6) ATA – This command is used to answer a call. An incoming call is indicated by a message ‘RING’ which is repeated for every ring of the call. When the call ends ‘NO CARRIER’ is displayed on the screen.
SYNTAX: ATA(Enter)
As ATA followed by enter key is pressed, incoming call is answered.
For example,
RING
RING
ATA
The AT commands for both, GSM module and the mobile phone, are listed below. Some of these commands may not be supported by all the GSM modules available. Also there might be some commands which won’t be supported by some mobile handsets.
List of AT commands:
Testing :
Command | Description |
AT | Checking communication between the module and computer. |
Call control :
Command | Description |
ATA | Answer command |
ATD | Dial command |
ATH | Hang up call |
ATL | Monitor speaker loudness |
ATM | Monitor speaker mode |
ATO | Go on-line |
ATP | Set pulse dial as default |
ATT | Set tone dial as default |
AT+CSTA | Select type of address |
AT+CRC | Cellular result codes |
Data card Control :
Command | Description |
ATI | Identification |
ATS | Select an S-register |
ATZ | Recall stored profile |
AT&F | Restore factory settings |
AT&V | View active configuration |
AT&W | Store parameters in given profile |
AT&Y | Select Set as power up option |
AT+CLCK | Facility lock command |
AT+COLP | Connected line identification presentation |
AT+GCAP | Request complete capabilities list |
AT+GMI | Request manufacturer identification |
AT+GMM | Request model identification |
AT+GMR | Request revision identification |
AT+GSN | Request product serial number identification (IMEI) |
Phone control :
Command | Description |
AT+CBC | Battery charge |
AT+CGMI | Request manufacturer identification |
AT+CGMM | Request model identification |
AT+CGMR | Request revision identification |
AT+CGSN | Request product serial number identification |
AT+CMEE | Report mobile equipment error |
AT+CPAS | Phone activity status |
AT+CPBF | Find phone book entries |
AT+CPBR | Read phone book entry |
AT+CPBS | Select phone book memory storage |
AT+CPBW | Write phone book entry |
AT+CSCS | Select TE character set |
AT+CSQ | Signal quality |
Computer data interface :
Command | Description |
ATE | Command Echo |
ATQ | Result code suppression |
ATV | Define response format |
ATX | Response range selection |
AT&C | Define DCD usage |
AT&D | Define DTR usage |
AT&K | Select flow control |
AT&Q | Define communications mode option |
AT&S | Define DSR option |
AT+ICF | DTE-DCE character framing |
AT+IFC | DTE-DCE Local flow control |
AT+IPR | Fixed DTE rate |
Service :
Command | Description |
AT+CLIP | Calling line identification presentation |
AT+CR | Service reporting control |
AT+DR | Data compression reporting |
AT+ILRR | DTE-DCE local rate reporting |
Network Communication parameter :
Command | Description |
ATB | Communications standard option |
AT+CBST | Select bearer service type |
AT+CEER | Extended error report |
AT+CRLP | Radio link protocol |
AT+DS | Data compression |
Miscellaneous :
Command | Description |
A/ | Re-execute command line |
AT? | Command help |
AT*C | Start SMS interpreter |
AT*T | Enter SMS block mode protocol |
AT*V | Activate V.25bis mode |
AT*NOKIATEST | Test command |
AT+CESP | Enter SMS block mode protocol |
SMS Text mode :
Command | Description |
AT+CSMS | Select message service |
AT+CPMS | Preferred message storage |
AT+CMGF | Message format |
AT+CSCA | Service centre address |
AT+CSMP | Set text mode parameters |
AT+CSDH | Show text mode parameters |
AT+CSCB | Select cell broadcast message types |
AT+CSAS | Save settings |
AT+CRES | Restore settings |
AT+CNMI | New message indications to TE |
AT+CMGL | List messages |
AT+CMGR | Read message |
AT+CMGS | Send message |
AT+CMSS | Send message from storage |
AT+CMGW | Write message to memory |
AT+CMGD | Delete message |
SMS PDU mode :
Command | Description |
AT+CMGL | List Messages |
AT+CMGR | Read message |
AT+CMGS | Send message |
AT+CMGW | Write message to memory |
List of commands used in this project and their description
TO CHECK THE MODEM WORKING OR NOT :
AT
Response
Step 1: Create Function For Power Key To Turn On The GSM Module
// Create Function For Power Key To Turn On The GSM Module
void gsm_power_key()
{
PORTBbits.RB15 = 0;
delay(300);
PORTBbits.RB15 = 1;
delay(1000);
}
Step 2: Set configuration bits
// CONFIG3
#pragma config WPFP = WPFP511 // Write Protection Flash Page Segment Boundary (Highest Page (same as page 85))
#pragma config WPDIS = WPDIS // Segment Write Protection Disable bit (Segmented code protection disabled)
#pragma config WPCFG = WPCFGDIS // Configuration Word Code Page Protection Select bit (Last page(at the top of program memory) and Flash configuration words are not protected)
#pragma config WPEND = WPENDMEM // Segment Write Protection End Page Select bit (Write Protect from WPFP to the last page of memory)
// CONFIG2
#pragma config POSCMOD = XT // Primary Oscillator Select (XT oscillator mode selected)
#pragma config IOL1WAY = ON // IOLOCK One-Way Set Enable bit (Write RP Registers Once)
#pragma config OSCIOFNC = OFF // Primary Oscillator Output Function (OSCO functions as CLKO (FOSC/2))
#pragma config FCKSM = CSDCMD // Clock Switching and Monitor (Both Clock Switching and Fail-safe Clock Monitor are disabled)
#pragma config FNOSC = PRI // Oscillator Select (Primary oscillator (XT, HS, EC))
#pragma config IESO = OFF // Internal External Switch Over Mode (IESO mode (Two-speed start-up) enabled)
// CONFIG1
#pragma config WDTPS = PS32768 // Watchdog Timer Postscaler (1:32,768)
#pragma config FWPSA = PR128 // WDT Prescaler (Prescaler ratio of 1:128)
#pragma config WINDIS = OFF // Watchdog Timer Window (Standard Watchdog Timer is enabled,(Windowed-mode is disabled))
#pragma config FWDTEN = OFF // Watchdog Timer Enable (Watchdog Timer is enabled)
#pragma config ICS = PGx3 // Comm Channel Select (Emulator functions are shared with PGEC3/PGED3)
#pragma config GWRP = OFF // General Code Segment Write Protect (Writes to program memory are allowed)
#pragma config GCP = OFF // General Code Segment Code Protect (Code protection is disabled)
#pragma config JTAGEN = OFF // JTAG Port Enable (JTAG port is disabled)
Step 3: Set USART Configuration( My GMS Modem is connected to USART1)
void UART1Init(void)
{
// U1BRG = 25; // 9600 @ 8MHZ
U1BRG = 32; // 9600 @ 10MHZ
U1MODEbits.UARTEN = 1; // UART2 is Enabled
U1MODEbits.USIDL = 0; // Continue operation at Idlestate
U1MODEbits.IREN = 0; // IrDA En/Decoder is disabled
U1MODEbits.RTSMD = 0; // flow control mode
U1MODEbits.UEN1 = 0b00; // UTX, RTX, are enabled U1CTS, U1RTS are disabled
U1MODEbits.UEN0 = 0b00; // UTX, RTX, are enabled U1CTS, U1RTS are disabled
U1MODEbits.WAKE = 1; // Wake-up on start bit is enabled
U1MODEbits.LPBACK = 0; // Loop-back is disabled
U1MODEbits.ABAUD = 0; // auto baud is disabled
U1MODEbits.RXINV = 0; // No RX inversion
U1MODEbits.BRGH = 0; // low boud rate
U1MODEbits.PDSEL = 0b00; // 8bit no parity
U1MODEbits.STSEL = 0; // one stop bit
U1STAbits.UTXISEL1 = 0b00;
U1STA &= 0xDFFF; // clear TXINV by bit masking
U1STAbits.UTXBRK = 0; // sync break tx is disabled
U1STAbits.UTXEN = 1; //transmit is enabled
U1STAbits.URXISEL = 0b00; // interrupt flag bit is set when RXBUF is filled whith 1 character
U1STAbits.ADDEN = 0; // address detect mode is disabled
U1STAbits.RIDLE = 0;
IPC2bits.U1RXIP = 7; // sET uart1 Priority to 7
IFS0bits.U1RXIF = 0; // clear interrupt flag of rx
IFS0bits.U1TXIF = 0; // clear interrupt flag of rx
IEC0bits.U1RXIE = 1; // enable rx recieved data interrupt
IEC0bits.U1TXIE = 0;
}
Set USART Configuration( I am using USART3 to monitoring serial data)
// Create Uart3 Init Function To Monitor Serial Output
void UART3Init(void)
{
// U3BRG = 65;// 4800 @ 10MHZ // 10; // 9600
U3BRG = 32; //9600
U3MODE = 0x8000;
U3STA = 0x8400;
IPC20bits.U3RXIP = 7; // sET uart1 Priority to 6
IFS5bits.U3RXIF = 0; // clear interrupt flag of rx
IFS5bits.U3TXIF = 0; // clear interrupt flag of rx
IEC5bits.U3RXIE = 1; // enable rx recieved data interrupt
IEC5bits.U3TXIE = 0;
}
Step 4: Create Modem Send Command To Send A AT Command And Response Monitor In Uart3
//Create Modem Send Command To Send A AT Command And Response Monitor In Uart3
int ModemSendATCmd(uint8_t *atcmd,uint16_t delay,int length)
{
memset(gsm_buffer,'\0',sizeof(gsm_buffer));
gsm_index = 0;
UART1TransmitString(atcmd);
uint16_t temp=0;
while(gsm_index<length)
{
if(temp>=delay)
{
UART3TransmitString(gsm_buffer);
return 0;
}
__delay_ms(1);
temp++;
}
UART3TransmitString(gsm_buffer);
}
Step 5: GSM init function
void gsm_init()
{
ModemSendATCmd("AT\r\n",100,8); // //AT command
ModemSendATCmd("ATE1\r\n",300,11); //Set Command Echo Mode
ModemSendATCmd("ATI\r\n",300,27); //Display Product Identification Information
ModemSendATCmd("AT+COPS?\r\n",300,37); // Operator Selection
ModemSendATCmd("AT+CMGF=1\r\n",300,17); // Select SMS Message Format
ModemSendATCmd("AT+CPIN?\r\n",300,31); // Check SIM PIN
ModemSendATCmd("AT+CLTS=1\r\n",300,31); // Get Local Timestamp
ModemSendATCmd("AT+CCLK?\r\n",300,50); // Get Local Timestamp
ModemSendATCmd("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"\r\n",2000,36); // Bearer Settings for Applications Based on IP
ModemSendATCmd("AT+SAPBR=3,1,\"APN\",\"Airteliot.com\"\r\n",2000,41); // Bearer Settings for Applications Based on IP
ModemSendATCmd("AT+SAPBR=1,1\r\n",2000,19); //TO OPEN A GPRS CONTEXT
ModemSendATCmd("AT+SAPBR=2,1\r\n",3000,51); // TO Quary the GPRS context
ModemSendATCmd("AT+HTTPINIT\r\n",2000,18); // init http service
ModemSendATCmd("AT+HTTPPARA=\"CID\",1\r\n",2000,86); // set parameters for http session
sprintf(url,"AT+HTTPPARA=\"URL\",\%s%d\r\n",http,b++); //
ModemSendATCmd(url,5000,128);
ModemSendATCmd("AT+HTTPACTION=0\r\n",6000,48); // get session start
ModemSendATCmd("AT+HTTPREAD\r\n",5000,200); // read the data of http server
ModemSendATCmd("AT+SAPBR=0,1\r\n",2000,19); // gprs context is released by network
ModemSendATCmd("AT+HTTPTERM\r\n",2000,20); // terminate http service
}
FINAL CODE
/*
* File: MAIN2.c
* Author: admin
#define FCY 10000000UL // Instructi
//Add Required Frequency
#include "stdio.h"
#include "string.h"
#include "libpic30.h"
#include <xc.h>
// CONFIG3
#pragma config WPFP = WPFP511 // Write Protection Flash Page Segment Boundary (Highest Page (same as page 85))
#pragma config WPDIS = WPDIS // Segment Write Protection Disable bit (Segmented code protection disabled)
#pragma config WPCFG = WPCFGDIS // Configuration Word Code Page Protection Select bit (Last page(at the top of program memory) and Flash configuration words are not protected)
#pragma config WPEND = WPENDMEM // Segment Write Protection End Page Select bit (Write Protect from WPFP to the last page of memory)
// CONFIG2
#pragma config POSCMOD = XT // Primary Oscillator Select (XT oscillator mode selected)
#pragma config IOL1WAY = ON // IOLOCK One-Way Set Enable bit (Write RP Registers Once)
#pragma config OSCIOFNC = OFF // Primary Oscillator Output Function (OSCO functions as CLKO (FOSC/2))
#pragma config FCKSM = CSDCMD // Clock Switching and Monitor (Both Clock Switching and Fail-safe Clock Monitor are disabled)
#pragma config FNOSC = PRI // Oscillator Select (Primary oscillator (XT, HS, EC))
#pragma config IESO = OFF // Internal External Switch Over Mode (IESO mode (Two-speed start-up) enabled)
// CONFIG1
#pragma config WDTPS = PS32768 // Watchdog Timer Postscaler (1:32,768)
#pragma config FWPSA = PR128 // WDT Prescaler (Prescaler ratio of 1:128)
#pragma config WINDIS = OFF // Watchdog Timer Window (Standard Watchdog Timer is enabled,(Windowed-mode is disabled))
#pragma config FWDTEN = OFF // Watchdog Timer Enable (Watchdog Timer is enabled)
#pragma config ICS = PGx3 // Comm Channel Select (Emulator functions are shared with PGEC3/PGED3)
#pragma config GWRP = OFF // General Code Segment Write Protect (Writes to program memory are allowed)
#pragma config GCP = OFF // General Code Segment Code Protect (Code protection is disabled)
#pragma config JTAGEN = OFF // JTAG Port Enable (JTAG port is disabled)
int gsm_index,uart3_index;
char gsm_buffer[100];
char uart3_buffer[100];
// Create Uart1 Init Function For GSM
char http[] = "http://"; // ADD YOUR URL
char url[150];
int b=1;
void UART1Init(void)
{
// U1BRG = 25; // 9600 @ 8MHZ
U1BRG = 32; // 9600 @ 10MHZ
U1MODEbits.UARTEN = 1; // UART2 is Enabled
U1MODEbits.USIDL = 0; // Continue operation at Idlestate
U1MODEbits.IREN = 0; // IrDA En/Decoder is disabled
U1MODEbits.RTSMD = 0; // flow control mode
U1MODEbits.UEN1 = 0b00; // UTX, RTX, are enabled U1CTS, U1RTS are disabled
U1MODEbits.UEN0 = 0b00; // UTX, RTX, are enabled U1CTS, U1RTS are disabled
U1MODEbits.WAKE = 1; // Wake-up on start bit is enabled
U1MODEbits.LPBACK = 0; // Loop-back is disabled
U1MODEbits.ABAUD = 0; // auto baud is disabled
U1MODEbits.RXINV = 0; // No RX inversion
U1MODEbits.BRGH = 0; // low boud rate
U1MODEbits.PDSEL = 0b00; // 8bit no parity
U1MODEbits.STSEL = 0; // one stop bit
U1STAbits.UTXISEL1 = 0b00;
U1STA &= 0xDFFF; // clear TXINV by bit masking
U1STAbits.UTXBRK = 0; // sync break tx is disabled
U1STAbits.UTXEN = 1; //transmit is enabled
U1STAbits.URXISEL = 0b00; // interrupt flag bit is set when RXBUF is filled whith 1 character
U1STAbits.ADDEN = 0; // address detect mode is disabled
U1STAbits.RIDLE = 0;
IPC2bits.U1RXIP = 7; // sET uart1 Priority to 7
IFS0bits.U1RXIF = 0; // clear interrupt flag of rx
IFS0bits.U1TXIF = 0; // clear interrupt flag of rx
IEC0bits.U1RXIE = 1; // enable rx recieved data interrupt
IEC0bits.U1TXIE = 0;
}
// Create Uart3 Init Function To Monitor Serial Output
void UART3Init(void)
{
// U3BRG = 65;// 4800 @ 10MHZ // 10; // 9600
U3BRG = 32; //9600
U3MODE = 0x8000;
U3STA = 0x8400;
IPC20bits.U3RXIP = 7; // sET uart1 Priority to 6
IFS5bits.U3RXIF = 0; // clear interrupt flag of rx
IFS5bits.U3TXIF = 0; // clear interrupt flag of rx
IEC5bits.U3RXIE = 1; // enable rx recieved data interrupt
IEC5bits.U3TXIE = 0;
}
void UART1TransmitString(uint8_t *tx_str)
{
uint8_t ch;
while(*tx_str != 0x00)
{
ch = *tx_str;
U1TXREG = ch;
while(!(U1STAbits.TRMT))
{
}
tx_str++;
}
}
void UART3TransmitString(uint8_t *tx_str)
{
_CNIE = 0;
uint8_t ch,i=0;;
while(*tx_str != 0x00)
{
ch = *tx_str;
U3TXREG = ch;
while(!(U3STAbits.TRMT))
{
}
tx_str++;
for(i=0;i<200;i++)
{
}
}
_CNIE = 1;
}
void __attribute__((interrupt,no_auto_psv)) _U1RXInterrupt()
{
uint8_t ch;
_U1RXIF = 0;
ch = U1RXREG;
gsm_buffer[gsm_index++]=ch;
if(gsm_index>300)gsm_index=0;
}
// uart 3 interrupt
void __attribute__((interrupt,no_auto_psv)) _U3RXInterrupt()
{
uint8_t ch3;
_U3RXIF = 0;
ch3 = U3RXREG;
uart3_buffer[uart3_index++]=ch3;
if(uart3_index>300)uart3_index=0;
}
// Create Function For Power Key To Turn On The GSM Module
void gsm_power_key()
{
PORTBbits.RB15 = 0;
delay(300);
PORTBbits.RB15 = 1;
delay(1000);
}
//Create Modem Send Command To Send A AT Command And Response Monitor In Uart3
int ModemSendATCmd(uint8_t *atcmd,uint16_t delay,int length)
{
memset(gsm_buffer,'\0',sizeof(gsm_buffer));
gsm_index = 0;
UART1TransmitString(atcmd);
uint16_t temp=0;
while(gsm_index<length)
{
if(temp>=delay)
{
UART3TransmitString(gsm_buffer);
return 0;
}
__delay_ms(1);
temp++;
}
UART3TransmitString(gsm_buffer);
}
void delay(int delay)
{
__delay_ms(delay/2);
}
void gsm_init()
{
ModemSendATCmd("AT\r\n",100,8); // //AT command
ModemSendATCmd("ATE1\r\n",300,11); //Set Command Echo Mode
ModemSendATCmd("ATI\r\n",300,27); //Display Product Identification Information
ModemSendATCmd("AT+COPS?\r\n",300,37); // Operator Selection
ModemSendATCmd("AT+CMGF=1\r\n",300,17); // Select SMS Message Format
ModemSendATCmd("AT+CPIN?\r\n",300,31); // Check SIM PIN
ModemSendATCmd("AT+CLTS=1\r\n",300,31); // Get Local Timestamp
ModemSendATCmd("AT+CCLK?\r\n",300,50); // Get Local Timestamp
ModemSendATCmd("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"\r\n",2000,36); // Bearer Settings for Applications Based on IP
ModemSendATCmd("AT+SAPBR=3,1,\"APN\",\"Airteliot.com\"\r\n",2000,41); // Bearer Settings for Applications Based on IP
ModemSendATCmd("AT+SAPBR=1,1\r\n",2000,19); //TO OPEN A GPRS CONTEXT
ModemSendATCmd("AT+SAPBR=2,1\r\n",3000,51); // TO Quary the GPRS context
ModemSendATCmd("AT+HTTPINIT\r\n",2000,18); // init http service
ModemSendATCmd("AT+HTTPPARA=\"CID\",1\r\n",2000,86); // set parameters for http session
sprintf(url,"AT+HTTPPARA=\"URL\",\%s%d\r\n",http,b++); //
ModemSendATCmd(url,5000,128);
ModemSendATCmd("AT+HTTPACTION=0\r\n",6000,48); // get session start
ModemSendATCmd("AT+HTTPREAD\r\n",5000,200); // read the data of http server
ModemSendATCmd("AT+SAPBR=0,1\r\n",2000,19); // gprs context is released by network
ModemSendATCmd("AT+HTTPTERM\r\n",2000,20); // terminate http service
}
void main(void) {
OSCCON = 0x0000;
OSCTUN = 0x0000;
CLKDIV = 0x0000;
//resetSource = RCON;
RCON = 0x00;
INTCON1bits.NSTDIS = 1;
AD1PCFGL = 0xFF; //dISABLE ANALOG I/P'S
// Initialize UART GPIO Pins
RPINR18bits.U1RXR = 6; // Assign U1RXR to RP6
_TRISB6 = 1; //CONFIGURE PIN TO I/P
RPOR3bits.RP7R = 3; //Assign U1TX To Pin RP7
_TRISB7 = 0; //CONFIGURE PIN TO O/P
//Set UART As Input (RX) And Output(TX)
RPINR17bits.U3RXR = 3; // Assign U3RXR to RP3
_TRISD10 = 1; //CONFIGURE PIN TO I/P
RPOR0bits.RP0R = 28; //Assign U3TX To Pin RP0
_TRISB0 = 0; //CONFIGURE PIN TO O/P
_TRISB15 = 0; // for modem reset
UART1Init();
UART3Init();
delay(1000);
gsm_power_key();
// UART1TransmitString("mevihub\r\n");
UART3TransmitString("MEVIHUB GSM_INIT \r\n");
delay(6000);
while(1)
{
gsm_init();
delay(30000);
}
return;
}
Hi im satvik and im currently working on a project related to interface between SIM800L and PIC24F32KA302 and i really want to appriciate how good and in a perfect sense you’ve written the code and the explanation of it. I just have one question is the above code same for the SIM800L module as well?