I have been able to get outputs working on my PIC and can make a bunch of LEDs dance on and off as I set various output latches high/low. However, I'm having a lot o difficulty reading in the state of a pin.
See the code below. I set up my config, and define the TRISC as input and TRISB as output. In an infinite loop, I check to see whether RC6 is high or low, and set the entire B latch high or low depending on the result.
#include <htc.h>
__CONFIG(1, FOSC_IRC & FCMEN_OFF & IESO_OFF);
__CONFIG(2, PWRTEN_OFF & BOREN_OFF & WDTEN_OFF);
__CONFIG(3, MCLRE_OFF);
__CONFIG(4, STVREN_ON & LVP_OFF & DEBUG_OFF);
__CONFIG(5, 0xFFFF);
__CONFIG(6, 0xFFFF);
__CONFIG(7, 0xFFFF);
void main(void)
{
TRISC = 0xFF; // input
TRISB = 0x00; // output
while (1)
{
if (PORTCbits.RC6 == 0)
LATB = 0b00000000;
else
LATB = 0b11111111;
}
return;
}
The code compiles fine with no warnings or errors. If my code logic is simply setting latches high and low then that works fine, but I haven't been able to successfully read the state of a pin.
Any ideas?
I'm using a PIC18F14K50 and MPLAB v8.43 and the HiTech ANSI C Compiler.
My connections are +5V to the VDD (pin 1), VSS (pin 20) to ground. I have +5V to C6 (pin 8), and a 100K resistor and LED from B7 (pin 10) to ground. To toggle RC6, I connect/disconncet the wire to C6.
┌────────┐
+5v -│ 01 20 │- gnd
-│ 02 19 │-
-│ 03 18 │-
-│ 04 17 │-
-│ 05 16 │-
-│ 06 15 │-
-│ 07 14 │-
+5v -│ 08 13 │-
-│ 09 12 │-
led -│ 10 11 │-
└────────┘
The ADC pins on the PICs unfortunately are configured as analog inputs on powerup. You need to disable the ADC functionality on that pin to use it as a digital input.
Clearing bit 0 of ANSELH will set RC6 to digital input. Then your code will work.
This is documented in the datasheet in section 9.4 : Port Analog Control
PIC18F/LF1XK50 Datasheet
Setting...
TRISC = 1; // input
...should set only pin RC0 of PORTC as input pin, all other pins are defined as output.
So PORTCbits.RC6 == 0 should not return correct input state of in RC6 pin.
TRISC = 255 should set all pins of PORTC as input.
It is worth mentioning that some other PIC18 variants are set up differently:
On PIC18F4520 and PIC18F4680 you need to configure pins as digital it trough a ADCON1 register instead of ANSELH.
Setting a bit to 1 will make a pin digital.
Besides this you need to make them input pins by setting a bit to 1 in the corresponding TRIS register.
Related
I trying to use custom pins for SPI on the pico-mini-02 board. The board has esp32 (pico-v3-02). I'm using Arduino IDE (with arduino-esp32) and windows 10.
When I try to use the SPI example (multiple buses example), I'm getting Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled. error. I have decoded the exception backtrace
0x40080f31: __pinMode at C:\Users\newto\OneDrive\Documents\ArduinoData\packages\esp32\hardware\esp32\1.0.5\cores\esp32\esp32-hal-gpio.c line 115
0x400d0f3a: spiAttachSCK at C:\Users\newto\OneDrive\Documents\ArduinoData\packages\esp32\hardware\esp32\1.0.5\cores\esp32\esp32-hal-spi.c line 87
0x400d0db2: SPIClass::begin(signed char, signed char, signed char, signed char) at C:\Users\newto\OneDrive\Documents\ArduinoData\packages\esp32\hardware\esp32\1.0.5\libraries\SPI\src\SPI.cpp line 57
0x400d0c2b: setup() at C:\Users\newto\OneDrive\Documents\ArduinoData\packages\esp32\hardware\esp32\1.0.5\libraries\SPI\examples\SPI_Multiple_Buses/SPI_Multiple_Buses.ino line 67
0x400d190a: loopTask(void*) at C:\Users\newto\OneDrive\Documents\ArduinoData\packages\esp32\hardware\esp32\1.0.5\cores\esp32\main.cpp line 32
0x40085fa5: vPortTaskWrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c line 143
The full error message:
rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 271414342, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1044
load:0x40078000,len:10124
load:0x40080400,len:5856
entry 0x400806a8
Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
PC : 0x40080f31 PS : 0x00060730 A0 : 0x800d0f3d A1 : 0x3ffb1f30
A2 : 0x0000000e A3 : 0x00000002 A4 : 0x3ffb8364 A5 : 0x00000001
A6 : 0x00000000 A7 : 0x00000004 A8 : 0x3f400674 A9 : 0xaaaaaaaa
A10 : 0xaaaaaaaa A11 : 0x00000030 A12 : 0x3ffb82cc A13 : 0x00000000
A14 : 0x00000000 A15 : 0x00000000 SAR : 0x00000009 EXCCAUSE: 0x0000001c
EXCVADDR: 0xaaaaaaaa LBEG : 0x400d1258 LEND : 0x400d1265 LCOUNT : 0x00000000
ELF file SHA256: 0000000000000000
Backtrace: 0x40080f31:0x3ffb1f30 0x400d0f3a:0x3ffb1f50 0x400d0db2:0x3ffb1f70 0x400d0c2b:0x3ffb1f90 0x400d190a:0x3ffb1fb0 0x40085fa5:0x3ffb1fd0
Rebooting...
ets Jul 29 2019 12:21:46
Any help would be appreciated.
The sketch:
/* The ESP32 has four SPi buses, however as of right now only two of
* them are available to use, HSPI and VSPI. Simply using the SPI API
* as illustrated in Arduino examples will use VSPI, leaving HSPI unused.
*
* However if we simply intialise two instance of the SPI class for both
* of these buses both can be used. However when just using these the Arduino
* way only will actually be outputting at a time.
*
* Logic analyser capture is in the same folder as this example as
* "multiple_bus_output.png"
*
* created 30/04/2018 by Alistair Symonds
*/
#include <SPI.h>
// Define ALTERNATE_PINS to use non-standard GPIO pins for SPI bus
#ifdef ALTERNATE_PINS
#define VSPI_MISO 2
#define VSPI_MOSI 4
#define VSPI_SCLK 0
#define VSPI_SS 33
#define HSPI_MISO 26
#define HSPI_MOSI 27
#define HSPI_SCLK 25
#define HSPI_SS 32
#else
#define VSPI_MISO MISO
#define VSPI_MOSI MOSI
#define VSPI_SCLK SCK
#define VSPI_SS SS
#define HSPI_MISO 12
#define HSPI_MOSI 13
#define HSPI_SCLK 14
#define HSPI_SS 15
#endif
static const int spiClk = 1000000; // 1 MHz
//uninitalised pointers to SPI objects
SPIClass * vspi = NULL;
SPIClass * hspi = NULL;
void setup() {
//initialise two instances of the SPIClass attached to VSPI and HSPI respectively
vspi = new SPIClass(VSPI);
hspi = new SPIClass(HSPI);
//clock miso mosi ss
#ifndef ALTERNATE_PINS
//initialise vspi with default pins
//SCLK = 18, MISO = 19, MOSI = 23, SS = 5
vspi->begin();
#else
//alternatively route through GPIO pins of your choice
vspi->begin(VSPI_SCLK, VSPI_MISO, VSPI_MOSI, VSPI_SS); //SCLK, MISO, MOSI, SS
#endif
#ifndef ALTERNATE_PINS
//initialise hspi with default pins
//SCLK = 14, MISO = 12, MOSI = 13, SS = 15
hspi->begin();
#else
//alternatively route through GPIO pins
hspi->begin(HSPI_SCLK, HSPI_MISO, HSPI_MOSI, HSPI_SS); //SCLK, MISO, MOSI, SS
#endif
//set up slave select pins as outputs as the Arduino API
//doesn't handle automatically pulling SS low
pinMode(VSPI_SS, OUTPUT); //VSPI SS
pinMode(HSPI_SS, OUTPUT); //HSPI SS
}
// the loop function runs over and over again until power down or reset
void loop() {
//use the SPI buses
vspiCommand();
hspiCommand();
delay(100);
}
void vspiCommand() {
byte data = 0b01010101; // junk data to illustrate usage
//use it as you would the regular arduino SPI API
vspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
digitalWrite(VSPI_SS, LOW); //pull SS slow to prep other end for transfer
vspi->transfer(data);
digitalWrite(VSPI_SS, HIGH); //pull ss high to signify end of data transfer
vspi->endTransaction();
}
void hspiCommand() {
byte stuff = 0b11001100;
hspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
digitalWrite(HSPI_SS, LOW);
hspi->transfer(stuff);
digitalWrite(HSPI_SS, HIGH);
hspi->endTransaction();
}
Hi Mate I tried the code and it works. I have selected AI Thinker ESP32 that matches my board,and I have selected Flash Mode QIO and also DIO: both working fine. If you may still have the issue I would suggest to select DOUT or DIO.
Basically please check if your board matches the one selected on Tools->Board and check also the selection of 'Flash mode'.
Not all chips support all of these modes. The datasheet is the best source to know what supports what.
DIO - SPI host uses the "Dual I/O Fast Read" command (BBh). Two SPI pins are used to write the flash address part of the command, and to read flash data out. Therefore these phases need half the clock cycles compared to standard SPI.
DOUT - SPI host uses the "Dual Output Fast Read" command (3Bh). Two SPI pins are used to read flash data out. Slightly slower than DIO, because the address is written via the single MOSI data pin.
QIO - SPI host uses the "Quad I/O Fast Read" command (EBh). Four SPI pins are used to write the flash address part of the command, and to read flash data out. Therefore these phases need a quarter the clock cycles compared to standard SPI.
QOUT - SPI host uses the "Quad Output Fast Read" command (6Bh). Four SPI pins are used to read the flash data out. Slightly slower than QIO, because the address is written via the single MOSI data pin.
ESP-IDF defaults to DIO because some flash chips use a mode bit to enable QIO & QOUT support, and this can vary between manufacturers.
If using DIO/DOUT mode, GPIOs 9 & 10 are available for other uses. However note that if they are wired to the SPI flash chip (as they are in WROOM, for example) then the SPI flash chip may use them for other pin functions. Consult the SPI flash chip data sheet to be sure.
Very interesting discussion on this subject here: https://www.esp32.com/viewtopic.php?t=1250
Hope this helps. All the best.
I am trying to use a PN532 (v3) board with an Arduino UNO and libnfc for my university project. One problem I have stumbled upon is that libnfc-1.7.1 gives me the following error when I invoke nfc-list:
debug libnfc.general log_level is set to 3
debug libnfc.general allow_autoscan is set to false
debug libnfc.general allow_intrusive_scan is set to false
debug libnfc.general 1 device(s) defined by user
debug libnfc.general #0 name: "PN532 NFC Board on Arduino", connstring: "pn532_uart:/dev/ttyUSB0:115200"
nfc-list uses libnfc 1.7.1
debug libnfc.driver.pn532_uart Attempt to open: /dev/ttyUSB0 at 115200 bauds.
debug libnfc.bus.uart Serial port speed requested to be set to 115200 bauds.
debug libnfc.chip.pn53x Diagnose
debug libnfc.chip.pn53x Timeout value: 500
debug libnfc.bus.uart TX: 55 55 00 00 00 00 00 00 00 00 00 00 00 00 00 00
debug libnfc.chip.pn53x SAMConfiguration
debug libnfc.chip.pn53x Timeout value: 1000
debug libnfc.bus.uart TX: 00 00 ff 03 fd d4 14 01 17 00
debug libnfc.bus.uart Timeout!
debug libnfc.driver.pn532_uart Unable to read ACK
error libnfc.driver.pn532_uart pn53x_check_communication error
debug libnfc.chip.pn53x InRelease
debug libnfc.bus.uart TX: 00 00 ff 03 fd d4 52 00 da 00
debug libnfc.bus.uart Timeout!
debug libnfc.driver.pn532_uart Unable to read ACK
debug libnfc.general Unable to open "pn532_uart:/dev/ttyUSB0:115200".
nfc-list: ERROR: Unable to open NFC device: pn532_uart:/dev/ttyUSB0:115200
I have no issues using the reader with Arduino, I can upload sketches and the reader interacts with tags. The reader is in SPI mode, and when I invoke nfc-list the LEDs blink, therefore the only thing I can think of is some sort of issue with libnfc.
I hope someone can help,any suggestions would be amazing! :)
Thank you!
Connections
PN532 V3
Arduino.1
Arduino.2
File Configurations
/etc/nfc/libnfc.conf
# Allow device auto-detection (default: true)
# Note: if this auto-detection is disabled, user has to set manually a device
# configuration using file or environment variable
allow_autoscan = false
# Allow intrusive auto-detection (default: false)
# Warning: intrusive auto-detection can seriously disturb other devices
# This option is not recommended, user should prefer to add manually his device.
allow_intrusive_scan = false
# Set log level (default: error)
# Valid log levels are (in order of verbosity): 0 (none), 1 (error), 2 (info), 3 (debug)
# Note: if you compiled with --enable-debug option, the default log level is "debug"
log_level = 3
# Manually set default device (no default)
# To set a default device, you must set both name and connstring for your device
# Note: if autoscan is enabled, default device will be the first device available in device
list.
device.name = "PN532 NFC Board on Arduino"
device.connstring = "pn532_uart:/dev/ttyUSB0:115200"
uartnfc.info
#include <PN532.h>
#define SCK 13
#define MOSI 11
#define SS 10
#define MISO 12
PN532 nfc(SCK, MISO, MOSI, SS);
uint8_t buffer[32];
void setup(void) {
Serial.begin(115200); //460800, 115200
Serial.flush();
nfc.begin();
}
void loop(void) {
int b = Serial.available();
if (b >= 5){
Serial.readBytes((char*)buffer, 5);
if(buffer[0] == 0x55){
//handle wake up case
while(Serial.available() < 5); //wait the command
b = Serial.readBytes((char*)buffer+5, 5);
//send raw command to pn532
//get length of package :
// (PREAMBLE + START + LEN + LCS) + (TFI + DATA[0...n]) + (DCS + POSTAMBLE)
uint8_t l = buffer[8] + 2;
while(Serial.available() < l); //wait the command
//read command from uart
Serial.readBytes((char*)buffer+10, l);
//send raw command to pn532
nfc.sendRawCommandCheckAck(buffer, l+10);
//read pn532 answer
nfc.readRawCommandAnswer(buffer, l+10);
}else{
//normal case
//get length of package :
// (PREAMBLE + START + LEN + LCS) + (TFI + DATA[0...n]) + (DCS + POSTAMBLE)
uint8_t l = buffer[3] + 2;
//read command from uart
//while(Serial.available() < l); //wait the command
//Serial.readBytes((char*)buffer+5, l);
uint8_t br = l;
uint8_t* bufptr = buffer + 5;
while(br){
if(Serial.available()){
*bufptr++ = Serial.read();
--br;
}
}
//send raw command to pn532
nfc.sendRawCommandCheckAck(buffer, l+5);
//read pn532 answer
nfc.readRawCommandAnswer(buffer, l+5);
}
}
}
Used Resources
http://nfc-tools.org/index.php/Libnfc:Arduino - Used to install libnfc
https://github.com/gunmetal313/mfocuino - Uploaded the sketch uartnfc.ino and imported the libraries (mfocuino/mfocuino-read-only/nfcreader/arduino/libraries/PN532/) into the Arduino IDE.
Things I have already tried to fix the problem
Why "No NFC device found" with libnfc and PN532 SHIELD
PN532 Unable to open NFC device
https://github.com/nfc-tools/libnfc/issues/507
https://forums.adafruit.com/viewtopic.php?f=19&t=58188
https://superuser.com/questions/1409108/nfc-unable-to-open-nfc-device
I almost gave up having the same problem. For me it finally worked when using 9600 baud in the Arduino Sketch and libnfc config.
I am a beginner in Arduino. I would like to control 64 LEDs using 2 Arduino Mega boards.
The logic is:
Arduino 1 blinks the LED in serial ===>
Arduino 1 finished blinking and sends the signal (HIGH) to Arduino 2 ===>
Arduino 2 blinks the LED in serial ===>
Arduino 2 finishes blinking and sends a signal (HIGH) to Arduino 1, and both Arduinos reset using asm volatile("jmp 0").
I am using pin 52 as TX and pin 53 as RX for both.
And now the problem is that after Arduino 1 finishes blinking and sends out the signal (HIGH) to Arduino 2, it doesn't wait for the signal from Arduino 2 but resets itself.
Can anyone have a look of my code to see whether it is a logical mistake or a coding error?
digitalWrite(TX, HIGH);
delay(1000);
if(digitalRead(RX)==HIGH) {
asm volatile("jmp 0");
}
digitalWrite(TX, HIGH);
delay(1000);
if(digitalRead(RX)==HIGH) {
asm volatile("jmp 0");
}
When you do this, you have to make sure that Arduino 2 sets its TX pin to LOW first, before playing with the LEDs. Only when it is finished, will should it set its TX pin to HIGH.
You need to interlock the shutdown sequence:
// Arduino 1:
digitalWrite(TX, HIGH); // set high for 1 second
delay(1000);
while (digitalRead(RX)) // wait for a low pulse from # 2
;
digitalWrite(TX, LOW); // #2 is latched.
while (!digitalRead(RX))
;
// RX is high again, #2 is ready to reset as well...
asm volatile("jmp 0");
// arduino # 2, assuming you have already detected a pulse longer than 500ms on RX,
// sequence:
digitalWrite(TX, LOW); // indicate we're latched
while(digitalRead(RX)) // wait for end of pulse from #1
;
digitalWrite(TX, HIGH); // indicate we're ready
delay(2); // make sure #1 gets it.
asm volatile("jmp 0"); // reset at approx same time as #1
Arduino #2 should keep its TX line HIGH while it's busy and wants to delay the reset.
I am using an A-Star 32U4 Micro Arduino and I'm trying to connect the RDM6300 - 125KHz Cardreader Mini-Module.
I'm using this sketch at the moment:
#include <SoftwareSerial.h>
// RFID | Nano
// Pin 1 | D2
// Pin 2 | D3
SoftwareSerial Rfid = SoftwareSerial(2,3);
void setup() {
// Serial Monitor to see results on the computer
Serial.begin(9600);
// Communication to the RFID reader
Rfid.begin(9600);
}
void loop() {
// check, if any data is available
if(Rfid.available() > 0 ){
// as long as there is data available...
while(Rfid.available() > 0 ){
// read a byte
int r = Rfid.read();
// print it to the serial monitor
Serial.print(r, DEC);
Serial.print(" ");
}
// linebreak
Serial.println();
}
}
With this circuit:
module TX --- Arduino pin 2
module VCC ----- 5v
module ground ---- ground
antenna pins ---- antenna
When I put the card in the sensor nothing shows up on serial port. I tried this setup and the exact same sensors on an Arduino Uno (same sketch) and it worked perfectly, but I cant get this working on the Micro.
Arduino UNO and Micro uses different processors, though they work fairly similarly, they are not totaly identical.
It seams that
not all pins on the Leonardo and Micro support change interrupts, so only the following can be used for RX: 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).
From the SoftwareSerial Library description ( https://www.arduino.cc/en/Reference/softwareSerial )
Change the module TX from pin 2 to pin 8. and you should be good. :-)
I have bought a GT-511C1R fingerprint scanner, to work in combination with my Arduino Mega. I used this example: http://www.homautomation.org/2014/10/11/playing-with-finger-print-scanner-fps-on-arduino/
But when i try to blink the led using the default code which came with the library
#include "FPS_GT511C3.h"
#include "SoftwareSerial.h"
// Hardware setup - FPS connected to:
// digital pin 4(arduino rx, fps tx)
// digital pin 5(arduino tx - 560ohm resistor fps tx - 1000ohm resistor - ground)
// this brings the 5v tx line down to about 3.2v so we dont fry our fps
FPS_GT511C3 fps(4, 5);
void setup()
{
Serial.begin(9600);
fps.UseSerialDebug = true; // so you can see the messages in the serial debug screen
fps.Open();
}
void loop()
{
// FPS Blink LED Test
fps.SetLED(true); // turn on the LED inside the fps
delay(1000);
fps.SetLED(false);// turn off the LED inside the fps
delay(1000);
}
it doesn't do anything. On my serial monitor i get:
FPS - Open
FPS - SEND: "55 AA 01 00 00 00 00 00 01 00 01 01"
but the LED of the finger scanner stays off. In combination with my Arduino Uno it does work. What can be the problem?
EDIT: After the answer of changing the pins, it worked just 1 time via pin 10 and 11. After removing the power, it didn't work anymore. Also not on other pins.
Not all pins are supported for SoftwareSerial on the Mega; the RX pin must be an interrupt-on-change pin. The list of such pins:
10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69
Basically, change the pins in your object to, say, 10 and 11. I would suggest using the hardware serial ports, which the Mega has in abundance, but from my brief perusal, your library seems hardwired to use SoftwareSerial and isn't so easily adapted.