I have problems with serial communications microcontroler 8051. My hardware MCU with att7035au and module-1350M CJH NFC reader. I have tested the pc and mcu to receive and transmit with usb-ttl / rs232 goes well. for sending 1 character and length data successfully without error. But after I try communication between mcu with NFC module there is an error during the receive data from the NFC. I use an interrupt to receive the data.
example :
mcu send (hex data):
02 55 00 03 03 E0 B7
NFC receiving data mcu and send feedback:
02 55 00 02 FF AA
mcu receive the data from NFC should (02 55 00 02 FF AA) but received
02 55 00 02 FF 05
What is wrong?why MCU read receive data error.
my code config:
BWPR = 0xcf;
BWPR = 0xbc;
PLLCFG = 0x87;
CLKCFG |= 0x04;
EA = 1;
//Serial config 0
SCON0 = 0x50 ; //SCON: serail mode 1, 8-bit UART
PCON|=0x80; //PCON.7 (SMOD) Indicate Baud rate double
SMOD_1=1; //ADCON.7
S0RELL=0xfd; //115200bps
S0RELH=0x03;
TI=0;
RI=0;
ES0 = 1 ;
edit add code interrupt:
void UART_0(void) interrupt 4
{
if(SCON0&0x01){ //RI=1
SCON0=SCON0&0xfc; //RI=0
card_id[RECE_NUMB]=SBUF0;
if(RECE_NUMB==6){
if(card_id[1]==0x02&&card_id[2]==0x55&&card_id[5]==0xff){
f_ReceiveSucceed=1;
SCON0&=0xef; //disable receive
}
}
++RECE_NUMB;
}
SCON0=0x50;
}
for code main(void):
while(1)
{
//...
//lines of code to send NFC
//...
if(f_ReceiveSucceed)
{
f_ReceiveSucceed=0;
SerialCommunication();
}
}
if get receive so i send to serialcommunication() for monitor data serial with usb-ttl
void SerialCommunication(void){
unsigned char count;
ES0 = 0;
for(count=0;count<=50;count++)
{
SBUF0=card_id[count];
while(TI==0);
TI=0;
}
ES0 = 1;
clear();
SCON0=0x50; //open receive
}
void clear(void)
{
unsigned char r;
for(r=0;r<50;r++){
card_id[r]=0x00;
}
RECE_NUMB=0;
}
Related
Prelude: I am harmful in arduino.
My objective so far is to retrieve the RSSI (Received Signal Strength Indication) of an Xbee3.0 (denoted Xb1 thereafter and in API mode) connected to an Arduino Mega. The latter is connected to the computer by cable. Xb1 receives a signal from another Xbee3.0 (denoted Xb2 - also in API mode). Xb2 is connected directly to the computer and sends information via XCTU.
From what I see, Xb1 is receiving information from Xb2. The problem is that I can't display on Arduino the RSSI of the Xb1.
Xb1 is connected via PWM 10 of the arduino mega and via Tx and Rx (and is of course powered and grounded).
I tried several codes, including this one:
#include <XBee.h>
#include <SoftwareSerial.h>
// XBee's DOUT (TX) is connected to pin 8 (Arduino's Software RX)
// XBee's DIN (RX) is connected to pin 9 (Arduino's Software TX)
SoftwareSerial serial1(8, 9); // RX, TX
XBee xbee=XBee();
XBeeResponse response = XBeeResponse();
Rx16Response rx16 = Rx16Response();
Rx64Response rx64 = Rx64Response();
uint8_t option = 0;
uint8_t data = 0;
uint8_t rssi = 0;
void setup()
{
Serial.begin(9600);
serial1.begin(9600);
xbee.setSerial(serial1);
}
void loop()
{
xbee.readPacket(100);
if (xbee.getResponse().isAvailable())
{
Serial.println("available");
if(xbee.getResponse().getApiId() == RX_64_RESPONSE || xbee.getResponse().getApiId() == RX_16_RESPONSE)
{
Serial.println("16");
if (xbee.getResponse().getApiId() == RX_16_RESPONSE)
{
Serial.println("16");
xbee.getResponse().getRx16Response(rx16);
//option = rx16.getOption();
//data = rx16.getData(0);
int rssi = rx16.getRssi();
Serial.print("data: ");Serial.println(data);
Serial.print("option: ");Serial.println(option);
Serial.print("RSSI: ");
Serial.println(rssi);
}
else
{
Serial.println("64");
xbee.getResponse().getRx64Response(rx64);
//option = rx64.getOption();
//data = rx64.getData(0);
int rssi = rx64.getRssi();
Serial.print("RSSI: ");
Serial.println(rssi);
}
}
}
}
....which unfortunately did not give me satisfaction. The conditions would obviously not be verified.
Do you have any idea why this is not working?
Thanks in advance,
Cordially
I need to send orders from Arduino to ESP32.
I have one joystick button to test.
Arduino nano is sender
Esp32 is receiver
Esp32 receives the joystick button information from Arduino (each time I push the button).
I need the Esp32 to Serial.write according to the data, for example:
If I press the button in Arduino: Send the data to Esp32 and turn bluetooth on (or turn a led on).
These are my codes:
//Arduino NANO sender
byte j = 45;
#define boton_joystick A1
void setup() {
Serial.begin(9600);
pinMode(boton_joystick, INPUT_PULLUP);
}
int boton_joystick_state;
void loop() {
//Serial.println("100");
//Serial.write("BOTON EN GRANDE");
//delay(1500);
if(!digitalRead(boton_joystick)) {
boton_joystick_state = 1;
delay(170);
} else {
boton_joystick_state = 0;
}
if (boton_joystick_state) {
Serial.println(j);
Serial.print(" ");
Serial.write(j);
Serial.println();
}
//ESP-32 receiver
#define RXD2 16
#define TXD2 17
byte j = 45;
int comdata;
void setup() {
Serial.begin(115200);
Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
}
void loop() {
//Serial.print("LEYENDO ARDUINO");
Serial.println(Serial2.readString());
if (Serial2.available() >0) {
char comdata = char(Serial2.read());
if (comdata == 'j') {
Serial.println("joystick activado");
}
}
}
am not sure
but am using Nano 33 BLE with UART and Nano has also Serial1 no need to serial2 no need to Softwearserial. Sensd on serial 1 and recive in Serial 1 but also you have to connect it Via USB. so your serial is USB and your serial 1 is TX RX.
for me it work so you can try it.
I want to use an Arduino Nano to monitor SMS traffic of a GSM module. I'm able to read and send SMS but the notification system is not working: when I send an SMS (to the SIM card that is in the GSM module) no new data becomes available in the Serial port. Any idea why or how can I debug to find the problem?
The communication is done through pins 9 and 10 of Arduino and RX and TX for the GSM module, which is an Quectel EC25. The code I'm using:
#include <SoftwareSerial.h>
#define DEBUG Serial
SoftwareSerial EC25(10,9); // RX, TX - 9600 baud rate
// pin 8 of raspi -> pin 9 of arduino nano
// pin 10 of raspi -> pin 10 of arduino nano
#define AT_RESPONSE_LEN 100
#define TIMEOUT 1000
void setup() {
// put your setup code here, to run once:
EC25.begin(9600);
DEBUG.begin(9600);
// some AT commands just to see if the coms are ok
sendATComm("AT","OK\r\n");
sendATComm("AT+IPR?","OK\r\n");
sendATComm("AT+CGSN","OK\r\n");
sendATComm("AT+CNMI=2,1,0,0,0","OK\r\n");
DEBUG.println("listennig");
}
void loop() {
// put your main code here, to run repeatedly:
if (EC25.available()){
DEBUG.println("Notification received!");
}
}
// function for sending at command.
const char* sendATComm(const char *command, const char *desired_reponse)
{
uint32_t timer;
char response[AT_RESPONSE_LEN]; // module response for AT commands.
memset(response, 0 , AT_RESPONSE_LEN);
EC25.flush();
sendATCommOnce(command);
timer = millis();
while(true){
if(millis()-timer > TIMEOUT){
sendATCommOnce(command);
timer = millis();
}
char c;
int i = 0;
while(EC25.available()){
c = EC25.read();
DEBUG.write(c);
response[i++]=c;
delay(2);
}
if(strstr(response, desired_reponse)){
return response;
memset(response, 0 , strlen(response));
break;
}
}
}
// send at comamand to module
void sendATCommOnce(const char *comm)
{
EC25.print(comm);
EC25.print("\r");
delay(100);
}
So, it turns out that I had to define the output port of URC to use UART communication (not using it as default).
The default configuration was set to either usbat or usbmodem, meaning that the notification information I was waiting for was being sent to one of these serial ports. But I was listening to UART (through RX and TX pints) and therefore I was not getting any notification.
AT command QURCCFG can be used to set which port URC signals should be sent to. In this case I want them to be sent to UART:
AT+QURCCFG="urcport","uart1"
I'm trying to drive the LTC1664 DAC from an Arduino (Mega 2560). The SPI data and clock coming out of the Arduino is always synched (rise and falls) for all 4 modes when the DAC data sheet indicates phasing
I've tried all modes, the SS line on the chip is being brought low during the writes (as it should) and I've tried different speeds and shiftOut(), setClockDivider(), setDataMode(), beginTransaction() and endTransaction().
Is this a bug in Arduino SPI, something specific to the Mega 2560, should I try an Uno or Due? Help please! Code below o-scope traces.
BTW: The 16 bit word I'm trying to transmit is, 0x3600 (o-scope trace truncated).
/*
Test DAC Control
created 18 Mar 2020 (Covid-19, oppsies)
by Danny Holstein
*/
// inslude the SPI library:
#include <SPI.h>
void DAC(unsigned short value, unsigned char channel, int SS_Pin, int model);
enum models{LTC1664};
enum models model;
// set pin 10 as the slave select for the digital pot:
const int DAC_SS_Pin = 22;
void setup() {
// set the DAC_SS_Pin as an output:
// initialize SPI:
Serial.begin(115200);
SPI.begin();
Serial.println("SPI.begin");
pinMode(DAC_SS_Pin, OUTPUT);
}
void loop() {
// go through the six channels of the digital pot:
for (int channel = 1; channel < 6; channel++) {
delay(500);
DAC(128*channel, channel, DAC_SS_Pin, LTC1664);
}
delay(1000);
Serial.println("new loop");
}
/*
DAC Control
This function controls an LTC1664 Micropower Quad 10-Bit DAC.
The LTC1664 is SPI-controlled,and to command it, you send one 16 bit word,
A3 A2 A1 A0 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0 X1 X0
| ADDR | INPUT CODE | DON'T CARE
The circuit:
* CS - to digital pin 22 (SS pin)
* SDI - to digital pin 51 (MOSI pin)
* SDO - to digital pin 50 (MISO pin, not used in this function)
* CLK - to digital pin 52 (SCK pin)
created 18 Mar 2020 (Covid-19, oppsies)
by Danny Holstein
*/
void DAC(unsigned short value, unsigned char channel, int SS_Pin, int model) {
// take the SS pin low to select the chip:
digitalWrite(SS_Pin, LOW); delay(100);
unsigned short buf, b16;
unsigned char *c, b; c = (unsigned char *) &buf;
switch (model)
{
case LTC1664:
if (channel > 4) channel = 0xF;
buf = (channel << 12) | ((value & 0x3FF) << 2);
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
b16 = SPI.transfer16(buf);
Serial.print("0x" + String(buf, HEX)); Serial.println("\t0x" + String(b16, HEX) + "\t");
SPI.endTransaction();
break;
default:
break;
}
delay(100);
// take the SS pin high to de-select the chip:
digitalWrite(SS_Pin, HIGH);
// printf("value = 0x%04x", buf);
}
Turns out it wasn't related to the Arduino SPI function or phasing issues, the LTC1664 has a CLR pin that I had attached to a GPIO but had failed to command HIGH, it had been floating and inhibiting the chip, command HIGH and everything is good now.
This sketch works on Arduino Uno but does not to work on Arduino Mega 2560. The same connections. The same logic konwerter. To be sure I have tried all TX and RX pins on the board 0, 1, 14-21 and I did not find any solution.
So I think the sketch has issue I cant resolve.
Sketch upload is succesful. No errors.
#include <stdio.h>
#include <PMS.h>
#define N 23
char linia1[16], linia2[16];
String sumPM25, sumPM10;
unsigned char bufor [N];
int PM25 = 10, PM10 = 10;
int wartoscPM25(unsigned char *thebuf);
int wartoscPM10(unsigned char *thebuf);
char sprawdzLancuch(unsigned char *thebuf, char leng);
int a=0;
void setup(){
Serial.begin(9600);
}
void loop(){
if(Serial.find(0x42))
Serial.readBytes(bufor,N);
if(bufor[0] == 0x4d){
if(sprawdzLancuch(bufor,N)){
PM25=wartoscPM25(bufor);
PM10=wartoscPM10(bufor);
}
}
sprintf(linia1,"%d",PM25);
Serial.print(linia1);
sprintf(linia2,"%d",PM10);
Serial.println(linia2);
delay(1000);
}
int wartoscPM25(unsigned char *buf)
{
int PM25v;
PM25v=((buf[11]<<8) + buf[12]);
return PM25v;
}
int wartoscPM10(unsigned char *buf)
{
int PM10v;
PM10v=((buf[13]<<8) + buf[14]);
return PM10v;
}
bool sprawdzLancuch(unsigned char *buf, int dlugosc)
{
bool flaga=0;
int suma=0;
for(int i=0; i<(dlugosc-2); i++){
suma+=buf[i];
}
suma=suma + 0x42;
if(suma == ((buf[dlugosc-2]<<8)+buf[dlugosc-1]))
{
suma = 0;
flaga = 1;
}
return flaga;
}
I use logic converter on both Uno and Mega to connecte PMS3003.
The serials are on the following pins.
Serial: 0 (RX) and 1 (TX);
Serial1: 19 (RX) and 18 (TX);
Serial2: 17 (RX) and 16 (TX);
Serial3: 15 (RX) and 14 (TX).
Since Serial is also connected to the USB, you should probably connect the deice up to one of the other ports and then use the corresponding SerialX to communicate to it.
Also verify you have the RX and TX pins between the Arduino and the device (and the logic converters) hooked up correctly.
Sometimes datasheets are show the Rx to a device meaning Rx from the host (Rx into the device), and sometimes Rx to the host (ie TX from the device).