I am working with GSM (sim900a) Module interfaced with PIC18F4520 Microcontroller.
Here I am trying to send SMS through PIC controller serially to GSM Module but unable to receive any response from GSM and not receiving any Messages.
When I am trying to connect only GSM Module to Hyperterminal then I am able to send SMS. In the similar fashion when I am trying to send AT commands from PIC to HYPERTERMINAL then I am receiving Commands Serially.
Code Below
#include<p18f4520.h> /* Header File */
#include<string.h>
void delay_ms(int ms);
void Data(char Value);
void Cmd(char Value);
char temp;
void main()
{
char tocheck[] ="AT";/*Basic command to test GSM is working or not */
char sendingsmsmode[]="AT+CMGF=1";/* To change the GSM Module in SMS
sending Mode */
char newsms[]="AT+CMGS=918500923915";/*Set Text mode for SMS */
char msg[]="WelcometoGSM";/* The text which you want to send */
char terminator=0x1A;
int i=0;
TRISC = 0x80; /* RC6=0 (O/P) RC7=1(I/P) */
SPBRG = 0x33; /* Serial Port Baud rate Generator (9600) */
TXSTA = 0X24; /* Transmission Enabling(TXEN=1,SYNC=0,BRGH=1)
*/
RCSTA = 0X90; /* Rception Enabling (SPEN=1,CREN=1) */
TRISC=0X00; /* (RC1,RC0 ->O/P Setting by Zero)
*/
TRISD=0X00; /* PORTD (0 - 7)pins Config as Output
*/
while(tocheck[i]!='\0'){
while(TXSTAbits.TRMT==0);
TXREG=tocheck[i];
delay_ms(30);
i++;
}
i=0;
while(sendingsmsmode[i]!='\0'){
while(TXSTAbits.TRMT==0);
TXREG=sendingsmsmode[i];
delay_ms(30);
i++;
}
i=0;
while(newsms[i]!='\0'){
while(TXSTAbits.TRMT==0);
TXREG=newsms[i];
delay_ms(30);
i++;
}
i=0;
while(msg[i]!='\0'){
while(TXSTAbits.TRMT==0);
TXREG=msg[i];
delay_ms(30);
i++;
}
TXREG=terminator;
delay_ms(3000);
while(1);
}
void Cmd(char Value)
{
PORTD=Value;
PORTCbits.RC1=0; /* RC1=0(RS=0) [Command Registr
Selection]) */
PORTCbits.RC0=0; /* RC0=0(R/W=0) [Write Process])
*/
PORTCbits.RC2=1; /* RC2=1(Enable=1) [Enable Line ON]
*/
delay_ms(4); /* Minimun Delay For Hold On Data
*/
PORTCbits.RC2=0; /* RC2=0(Enable=0) [Enable Line OFF]
*/
}
void Data(char Value)
{
PORTD=Value;
PORTCbits.RC1=1; /* RC1=1(RS=1) [Data Registr
Selection]) */
PORTCbits.RC0=0; /* RC0=0(R/W=0) [Write Process])
*/
PORTCbits.RC2=1; /* RC2=1(Enable=1) [Enable Line ON]
*/
delay_ms(4); /* Minimun Delay For Hold On Data
*/
PORTCbits.RC2=0; /* RC2=0(Enable=0) [Enable Line OFF]
*/
}
void delay_ms(int ms)
{
int i,count;
for(i=1;i<=ms;i++)
{
count=498;
while(count!=1)
{
count--;
}
}
}
You need to send CR or CR+LF (characters number 13 and 10 respectively) after every string you send to the modem.
When using a terminal emulator like HyperTerminal, you press the Enter key, right? If you don't press that key, the modem does not know that the command is terminated and must be executed. The same must be done in your firmware.
There is more: you must allow some delay, and you should read back modem responses, but this could be a second step; first of all, to check the setup is working at the base level, you must send an "AT", followed by a CR, then see if the modem replies with OK (+ CR and LF...).
Modify
char tocheck[] ="AT";
in
char tocheck[] ="AT\r"; // the \r is the CR (carriage return)
and you will see that the modem will reply, if all is connected well.
Related
Hello Im trying to connect a ESP8266 to a STM32 via UART.
I want to use the STM32 to send AT commands to the ESP, so that the ESP can do HTTP GET requests.
The code down here works when I use my PC to send data via FTDI to the STM, then the interrupt is called everytime a byte is recieved.
But when i hook up the ESP8266 I don't get a response even thought im sending AT\r\n;
Also i just connected everything like so:
SMT32 Tx -- ESP Rx
ESP Tx -- FTDI Rx
And I sended the AT\r\n command from the SMT to the ESP and on my pc in putty I got the OK command back, so the ESP is responding. But the interrupt is not called.
#include "main.h"
#include "usb_device.h"
#include "usbd_cdc_if.h"
UART_HandleTypeDef huart1;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
char Rx_Byte[1]; // Creating a single byte buffer
uint8_t Rx_Buffer[256]; // Full buffer
uint8_t Rx_Buffer_Index = 0;
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){
HAL_UART_Receive_IT(&huart1, (uint8_t *)Rx_Byte, 1);
memcpy(Rx_Buffer + Rx_Buffer_Index, Rx_Byte, 1);
Rx_Buffer_Index++;
if (*Rx_Byte == '\r' || *Rx_Byte == '\n') {
// I could send data via putty and FTDI, and when enter was pressed it printed the data in the buffer back to you via uart
// HAL_UART_Transmit(&huart1, (uint8_t *) Rx_Buffer, Rx_Buffer_Index, 1000);
memset(Rx_Buffer, 0, sizeof(Rx_Buffer)); // Clear buffer
Rx_Buffer_Index = 0;
}
}
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_USB_DEVICE_Init();
char echo_off[] = "ATE0\r\n";
char data[] = "AT\r\n";
HAL_Delay(1000);
HAL_UART_Transmit(&huart1, (uint8_t*)echo_off, sizeof(echo_off), 10);
while (1)
{
HAL_GPIO_TogglePin(GPIOA, LED_1_Pin);
HAL_UART_Transmit(&huart1, (uint8_t*)data, sizeof(data), 1000);
HAL_Delay(500);
}
}
I have used LCD port of this RX111 and its working but I am not able to powerup the usb port of RX111.
please kindly suggest me a solution...I have put the main code of it
void main(void)
{
R_MAIN_UserInit();
/* Start user code. Do not edit comment generated here */
Clear_Display();
DisplayCenter(0, (uint8_t *)"USB Project");
/* Start toggling the LEDs */
R_CMT1_Start();
/* Configure a switch press callback function */
SetSwitchPressCallback(cb_switch_press);
while (1)
{
if( R_usb_cstd_Scheduler() == USB_FLGSET )
{
R_usb_hstd_HcdTask(); /* HCD Task */
R_usb_hstd_MgrTask(); /* MGR Task */
usb_hmsc_Task(); /* HMSC Task */
usb_hmsc_StrgDriveTask(); /* HSTRG Task */
usb_hmsc_SampleAplTask(); /* HMSC Sample Task */
}
}
} /* eof main() */
I'm beginer in STM32, i have a project and need to receive data from another device like arduino, and now I try transmit data from UART 3 and I receive data with UART 1. but I can't get any data. I connect TX uart 3 to RX uart 1 and TX uart 1 to RX uart 3.
/* USER CODE BEGIN PV */
int i = 0;
char bufferReceive[6], bufferTransmit[10];
/* USER CODE END PV */
/* USER CODE BEGIN 0 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart->Instance == USART1) //current UART
{
HAL_UART_Receive_IT(&huart1, (uint8_t*)bufferReceive, 1); //activate UART receive interrupt every time
}
}
/* USER CODE END 0 */
int main(void)
{
HAL_UART_Receive_IT(&huart1, (uint8_t*)bufferReceive, 1);
while (1)
{
/* USER CODE END WHILE */`enter code here`
sprintf(bufferTransmit,"%d\n",i);
HAL_UART_Transmit(&huart3, (uint8_t*)bufferTransmit, sizeof(bufferTransmit), 1000);
HAL_Delay(500);
i++;
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
First thing your code is messy and hard to read.
Second, you are missing your entire initialization region before while(1) so your system shouldn't be doing anything.
/* USER CODE BEGIN PV */
int i = 0;
char bufferReceive[6], bufferTransmit[10];
/* USER CODE END PV */
/* USER CODE BEGIN 0 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart->Instance == USART1)
{
HAL_UART_Receive_IT(&huart1, (uint8_t*)bufferReceive, 1);
}
}
/* USER CODE END 0 */
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART3_UART_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&huart1, (uint8_t*)bufferReceive, 1);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
sprintf(bufferTransmit,"%d\n",i);
HAL_UART_Transmit(&huart3, (uint8_t*)bufferTransmit, sizeof(bufferTransmit), 1000);
HAL_Delay(500);
i++;
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
You are receiving one byte at HAL_UART_Receive_IT(&huart1, (uint8_t*)bufferReceive, 1);.
Which does not wait it simply moves onward.
Next, you transmit bufferTransmit which then causes UART1 to receive one byte and continues to receive all bytes one by one in the same memory location.
It seems you are not using i properly as you probably want something like HAL_UART_Receive_IT(&huart1, (uint8_t*)&bufferReceive[i], 1);
You do not initialize anything. Peripherals, pins, clocks etc have to be set up before they can work.
I am working on Zynq 702 Evaluation Board where I am trying to send and receive data using UART interface. My code works fine when I generate the bitstream from Vivado after selecting the Zynq 702 board in project settings but the same code doesn't work when I specify the corresponding part number(xc7z020clg484-1) in Vivado and then generate the bitstream. (Need to use part number as I will be using custom boards in the future)
Following is the code snippet for sending data:
for (Index = 0; Index < TEST_BUFFER_SIZE; Index++) {
/* Wait until there is space in TX FIFO */
while (XUartPs_IsTransmitFull(UartBaseAddress));
/* Write the byte into the TX FIFO */
XUartPs_WriteReg(UartBaseAddress, XUARTPS_FIFO_OFFSET,
SendBuffer[Index]);
}
The above code works perfectly fine for both with Zynq Board and when using part number.
However I am unable to receive data from UART using following code. This works perfectly when specifying Zynq Board instead of part number in Vivado:
Running = TRUE;
while (Running) {
/* Wait until there is data */
while (!XUartPs_IsReceiveData(UartBaseAddress));
while(recvCount<4)
{
input.bytes_array[recvCount]=XUartPs_RecvByte(UartBaseAddress);
recvCount++;
}
}
The above code is unable to hit the while(recvCount<4) code (on debugging) after sending data through UART terminal(when using part number in Vivado). What could be the problem here? Everything in Vivado block design is the same except the specification of board in one case and part number in the other.
Edit: adding main function and corresponding code:
#include "xparameters.h"
#include "xstatus.h"
#include "xil_types.h"
#include "xil_assert.h"
#include "xuartps_hw.h"
#include "xil_printf.h"
#include "xtime_l.h"
#include <stdio.h>
#include <math.h>
#include "xuartps.h"
#define UART_BASEADDR XPAR_XUARTPS_0_BASEADDR
#define UART_CLOCK_HZ XPAR_XUARTPS_0_CLOCK_HZ
#define UART_DEVICE_ID XPAR_XUARTPS_0_DEVICE_ID
#define TEST_BUFFER_SIZE 16
int UartPsEchoExample(u32 UartBaseAddress,u16 DeviceId);
/************************** Variable Definitions ***************************/
XUartPs Uart_Ps; /* The instance of the UART Driver */
u8 SendBuffer[TEST_BUFFER_SIZE]; /* Buffer for Transmitting Data */
int main(void)
{
int Status;
/*
* Run the Uart Echo example , specify the Base Address that is
* generated in xparameters.h
*/
Status = UartPsEchoExample(UART_BASEADDR, UART_DEVICE_ID);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
return XST_SUCCESS;
}
/**************************************************************************/
/**
*
* This function does a minimal test on the UART device using the hardware
* interface.
*
* #param UartBaseAddress is the base address of the device
*
* #return XST_SUCCESS if successful, XST_FAILURE if unsuccessful
*
* #note None.
*
**************************************************************************/
int UartPsEchoExample(u32 UartBaseAddress,u16 DeviceId)
{
int Index;
u32 Running;
u32 CntrlRegister;
int Status;
union {
float float_variable;
u8 bytes_array[4];
} input,output;
// Overwrite bytes of union with float variable
int recvCount=0;
XUartPs_Config *Config;
/*
* Initialize the UART driver so that it's ready to use
* Look up the configuration in the config table and then initialize it.
*/
Config = XUartPs_LookupConfig(DeviceId);
if (NULL == Config) {
return XST_FAILURE;
}
Status = XUartPs_CfgInitialize(&Uart_Ps, Config, Config->BaseAddress);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
XUartPs_SetBaudRate(&Uart_Ps, 115200);
CntrlRegister = XUartPs_ReadReg(UartBaseAddress, XUARTPS_CR_OFFSET);
/* Enable TX and RX for the device */
XUartPs_WriteReg(UartBaseAddress, XUARTPS_CR_OFFSET,
((CntrlRegister & ~XUARTPS_CR_EN_DIS_MASK) |
XUARTPS_CR_TX_EN | XUARTPS_CR_RX_EN));
/*
* Initialize the send buffer bytes with a pattern to send and the
* the receive buffer bytes to zero
*/
for (Index = 0; Index < TEST_BUFFER_SIZE; Index++) {
SendBuffer[Index] = Index + '0';
}
/* Send the entire transmit buffer. */
for (Index = 0; Index < TEST_BUFFER_SIZE; Index++) {
/* Wait until there is space in TX FIFO */
while (XUartPs_IsTransmitFull(UartBaseAddress));
/* Write the byte into the TX FIFO */
XUartPs_WriteReg(UartBaseAddress, XUARTPS_FIFO_OFFSET,
SendBuffer[Index]);
}
Running = TRUE;
while (Running) {
/* Wait until there is data */
while (!XUartPs_IsReceiveData(UartBaseAddress));
while(recvCount<4)
{
input.bytes_array[recvCount]=XUartPs_RecvByte(UartBaseAddress);
//XUartPs_Recv(&Uart_Ps, &u.temp_array[recvCount], 1);
recvCount++;
}
recvCount=0;
}
return XST_SUCCESS;
}
I m using dspic33f series micro controller to receive SMS from modem SIM800A. I m using interrupt method to receive response from the modem using serial communication . If I send more than one AT command to modem via UART sequentially I'm getting response into the recieve buffer only for 1st command and no response in the receive buffer for the remaining commands though the modem was responding.
After debugging I found the reason for this peculiar behavior. The reason was I was clearing the receive buffer after receiving the response into the buffer and printing on the console i.e. before sending next command to modem via UART using memset function in c.
But on commenting this memset function i was able receive the response into the receive buffer for the all the AT commands that was sent sequentially, if the memset function is not commented then no response is filled in the receive buffer though the modem was responding so please help out in receiving the response into the buffer.
Code which i have written
#include "p33FJ64GS606.h"
#include <stdio.h>
#define FCY 40000000UL
#include <libpic30.h>
#include <string.h>
_FOSC(FCKSM_CSECMD & OSCIOFNC_OFF)
_FWDT(FWDTEN_OFF)
_FPOR(FPWRT_PWR128 )
_FICD(ICS_PGD1 & JTAGEN_OFF)
char StringLoop[161];
void Init_Uart1(int uart_no)
{
U1MODEbits.STSEL = 0; // 1-Stop bit
U1MODEbits.PDSEL = 0; // No Parity, 8-Data bits
U1MODEbits.ABAUD = 0; // Auto-Baud disabled
U1MODEbits.BRGH = 0; // Standard-Speed mode
U1BRG = UBRG1_VALUE; // Baud Rate setting for 115200
U1STAbits.UTXISEL0 = 0; // Interrupt after one TX character is transmitted
U1STAbits.UTXISEL1 = 0;
U1STAbits.URXISEL0 = 0;
U1STAbits.URXISEL1 = 0;//jkv
IEC0bits.U1RXIE = 1;
IEC0bits.U1TXIE = 0; // Enable UART TX interrupt
U1MODEbits.UARTEN = 1; // Enable UART
U1STAbits.UTXEN = 1; // Enable UART TX
U1MODEbits.USIDL=0;
}
void UART1_puts(unsigned char data)
{
while (U1STAbits.TRMT==0);
U1TXREG = data;
}
void UART1_send(unsigned char *s)
{
memset(StringLoop,'\0',sizeof(StringLoop)); /* if I comment this line then i can receive the response into the buffer StringLoop continuously else i receive the response only for the 1st AT command and I don't get the response for other AT commands into the StringLoop though the modem is responding*/
while(*s)
{
UART1_puts(*s);
s++;
}
}
void __attribute__((interrupt, no_auto_psv)) _U1RXInterrupt(void)
{
if(IFS0bits.U1RXIF)
{
StringLoop[rcindex++] = U1RXREG;
if (rcindex >= (sizeof(StringLoop) - 1))
rcindex = 0;
}
IFS0bits.U1RXIF=0;
}
}
void main()
{
int i=0,j=0;
Init_Clocks();
Init_Uart2(1);
Init_Uart1(1);
TRISFbits.TRISF1=0;
LATFbits.LATF1=1;
UART1_send("AT+CMGR=1\r\n");
__delay_ms(2000);
printf("stringloop is %s\n",StringLoop);
UART1_send("AT+CPMS=?\r\n");
__delay_ms(2000);
printf("stringloop is %s\n",StringLoop);
UART1_send("AT+CPMS?\r\n");
__delay_ms(2000);
printf("stringloop is %s\n",StringLoop);
}