Arduino Mega 2560 Interrupt w/ Rotary Encoder - button

I started out using the Uno and I was able to get an interrupt working from a rotary library I found online but when I moved the project to the Mega and tried changing it for the different pins it stops. I spent a few hours trying to figure out the interrupt pin on the mega from online sources and just can't find any good resource to explain the mega interrupt pins sufficiently.
I am trying to use interrupts like so.
Rotary r = Rotary(10,11);
void setup(){
PCICR |= (1 << PCIE0);
PCMSK0 |= (1 << PCINT4) | (1 << PCINT5);
sei();
}
ISR(PCINT0_vect){
//stuff
}
It doesnt really matter what pin I am using for the interrupt if someone has a preferred method. I just need it to work.

Arduino interrupts are described here. It is easier to use than the example code you provide.
//Mega2560
// external interrupt int.0 int.1 int.2 int.3 int.4 int.5
// pin 2 3 21 20 19 18
void setup()
{
// interrupt # 0, pin 2
attachInterrupt(0, myISR, CHANGE); // Also LOW, RISING, FALLING
}
void loop()
{
}
void myISR() // must return void and take no arguments
{
// stuff
}
You don't need to enable interrupts with sei();, because attachInterrupt() does it for you. But you can disable interrupts with cli(); and re-enable them with sei();

Related

How can i use SPI on Arduino Due to communicate with AD7124-8 correctly

I want to read data from AD7124-8 with arduino Due via SPI. I found several libraries, but i dont get anything back from my ADC modul. By using an Oscilloscope i made sure my Arduino sends data via MOSI, SCK and CS work aswell. Just the MISO dataline doesnt get me anything back.
I first used this library (https://github.com/NHBSystems/NHB_AD7124), but decided to use a much easier Code to just make sure everything is working fine. I tried to to talk to the communication register to get the ID of my device from the ID register. You can find the registers on page 39 of the datasheet :https://www.analog.com/en/products/ad7124-8.html .
Im sending 0x00 and 0x05 to get back the 0x14 which should be the correct ID. Just zeros arriving (shown by Osci).
I found a solution in a Forum, but im not sure about why it differs with the data sheet:
https://ez.analog.com/data_converters/precision_adcs/f/q-a/24046/ad7124-8-for-arduino-due
when i use it the code stops running at the Line: value[0] = SPI.transfer(0x00);
They send 0x40 at the beginning, too.
Here my simple Code:
#include <SPI.h>
// Physical Pins
const int csPin = 10;
int value[7] {0};
void setup() {
Serial.begin (9600);
pinMode(10,OUTPUT);
SPI.begin(10);
SPI.setClockDivider(10, 128);
SPI.setDataMode(SPI_MODE3);
}
void loop() {
digitalWrite(csPin, LOW);
//SPI.transfer(csPin, 0x00);
SPI.transfer(csPin,0x00,SPI_CONTINUE); //Tell Communication Register you are going to read ID register
SPI.transfer(csPin,0x05);
//SPI.transfer(csPin,0x00); //Get data from Communication Register
delay(1);
digitalWrite(csPin, HIGH);
delay(1);
Serial.print(value[0],HEX);
}
I hope someone can help me out.
Greetings
First of all, this may not related to your question, but you are using old SPI methods setClockDivider(), setDataMode(), and setBitOrder() that has been deprecated since 2014. It is recommend to use use transactional SPI APIs which I have some explanation here.
Secondly, according to datasheet page 85, to access the ID register, you send one-byte to specific which register that you need to communicate with. Your code send two bytes, 0x00 and 0x05, which is incorrect.
Furthermore, based on page 78 of the datasheet, in order to read a register, bit 6 need to be set to 1 for a read operation, and 0 for a write operation.
Try the following code:
#include <SPI.h>
#define READ_REGISTER 0B01000000 // bit 6 set to 1 for read operation
#define ID_REGISTER 0x05
const int csPin = 10;
void setup() {
Serial.begin (9600);
digitalWrite(csPin, HIGH); // set csPin to HIGH to prevent false trigger
pinMode(csPin,OUTPUT);
SPI.begin();
}
void loop() {
SPI.beginTransaction(SPISettings(84000000/128, MSBFIRST, SPI_MODE3));
digitalWrite(csPin, LOW);
SPI.transfer(READ_REGISTER | ID_REGISTER); // read register 5
uint8_t id = SPI.transfer(0x00); // get the id
digitalWrite(csPin, HIGH);
SPI.endTransaction();
Serial.print(id,HEX);
}

radio control with arduino

Hi I am attempting to read from an rc transmitter using an Arduino Uno board, I have a signal pin connected from the receiver to pin 9 on the Arduino. Here is the code I would really appreciate some help all I am trying to achieve is the read the pmw from the receiver. I am able to plug a servo into the receiver and that works fine along with a motor I am just struggling when I try and use the Arduino with the receiver. When I run my program all I get in the Serial monitor are values such as 9991,9972,10030,10050 that are completely unrelated.
I want to have a pmw value that I can map to 0-255 in order to control a motor
My Circuit:
battery -> ESC(for BEC to regulate five volts back to receiver)-> receiver -> ch3 signal pin -> Arduino uno (pin9)
void setup() {
Serial.begin(9600);
}
void loop() {
int pwm = pulseIn(9, HIGH, 25000);
Serial.println(pwm);
delay (5);
}
You are using pulseIn which returns a time (in ms). The time being how long it waited for a HIGH signal. If you want the actual value, use analogRead. You can still use pulseIn, just don't use the return value
void setup() {
Serial.begin(9600);
}
void loop() {
byte pwm = analogRead(A5) / 4;
Serial.println(pwm);
delay(5);
}

Arduino timer4 custom PWM issue

I made a nice code which generates fast PWM with 50% duty cycle and I can change the frequency with a potentiometer. It outputs straight and inverted channels with some dead time. I am using Arduino Micro aka ATmega32U4. The code is actually "Atmel" code. Code is working fine until I power Arduino Micro off and then on again.
I have programmed the code and registers so that the frequency is changeable from 10kHz to 100kHz. But after power on/off the frequency changes from 5kHz to 50kHz. After this has happened I have to program the board again using Arduino IDE, to make it work correctly. Again after power on/off it has changed. I am quite sure that one of the registers is overwritten by the "Arduino hardware abstraction layer" or however we should name it. I have not yet read out all the registers so I do not know which one is overwritten. I guess it's the prescaler.
How do I prevent this from happening? Should I write the register contents somewhere else? Or should I write it few times to be sure?
Why or how this is happening anyway?
Here's the code:
#define OSC1 5
#define OSC2 13
uint8_t read_reg1;
uint8_t read_reg2;
int pot, freq;
void setup() {
pinMode(OSC1, OUTPUT);
pinMode(OSC2, OUTPUT);
Serial.begin(9600);
cli(); // disable global interrupts
TCCR4A=0; // clear register
TCCR4B=0x06; // configure prescaler to 64 (CK = CLK / 64 = 1.5 MHz)
TCCR4C=0;
TCCR4D=0; // select Fast PWM operation (0 << WGM41)|(0 << WGM40)
PLLFRQ=(PLLFRQ&0xCF)|0x30; // select clock source and frequency
OCR4C=150; // select PWM frequency
OCR4A=150/2; // set duty cycle
DT4 = 0x55; // set dead times. DT = (1 / 48Mhz) * 0...15
// enable interrupt on timer4 overflow
TIMSK4|=(1 << TOIE4);
// This register write has to be after others. Otherwise the PWM generation will not work. I do not know why.
TCCR4A=0x42; // COM4A1..0 = 01, OC4A and !OC4A connected. PWM4A = 1 (activate channel A PWM output)
sei(); // enable global interrupts
}
void loop() {
//cli();
pot = analogRead(A0);
freq = map(pot, 0, 1023, 14, 166);
//sei();
/*
Serial.print("Pot value: ");
Serial.print(pot);
Serial.print("\tFreq value: ");
Serial.println(1500000/freq);
*/
}
ISR(TIMER4_OVF_vect){
OCR4C = freq;
OCR4A = freq / 2;
}
I am not sure exactly why you got different behavior right after programming, but the bootloader that the Arduino Micro uses (Caterina) does not perform a full reset after it runs, so changes that the bootloader made to the AVR's registers are often visible to the user's sketch.
I was able to fix the problem by removing the line that modifies PLLFRQ. Here is a simplified version of your code that always produces 3.31 kHz PWM:
void setup()
{
pinMode(5, OUTPUT);
pinMode(13, OUTPUT);
TCCR4A = 0;
TCCR4B = 0x06; // configure prescaler to 64 (CK = CLK / 64 = 1.5 MHz)
TCCR4C = 0;
TCCR4D = 0; // select Fast PWM operation (0 << WGM41)|(0 << WGM40)
OCR4C = 150; // select PWM frequency
OCR4A = 150 / 2; // set duty cycle
DT4 = 0x55; // set dead times. DT = (1 / 48Mhz) * 0...15
// This register write has to be after others.
// Otherwise the PWM generation will not work. I do not know why.
// COM4A1..0 = 01, OC4A and !OC4A connected.
// PWM4A = 1 (activate channel A PWM output)
TCCR4A = 0x42;
}
void loop()
{
}
It's not a great idea to mess with the PLL postscaler since it will probably affect every other Arduino library that uses timers, including the USB stack.

Enabling I2C interrupt within timer interrupt on nRF52 Arduino

I am writing a program for an nRF52 based board using the Redbear Arduino Library. Effectively treating my board as a BLE Nano 2.
I have a timer that ticks every x milliseconds, for example 50ms.
Inside that timer I would like to read data from an I2C sensor and add the reading to a buffer.
I am aware that by default, when inside the timer ISR, interrupts are disabled. I would like to know how to briefly re-enable the I2C interrupt, and get the sensor reading, then disable the interrupts again.
The interval between sensor readings is critical, and I don't just want to set a flag in the timer ISR as I don't know how long it will be before that flag is checked.
Please can someone instruct me on how to breifly enable the I2C interrupts from within a timer ISR?
I have experimented with:
__disable_irq();
__enable_irq();
NVIC_EnableIRQ(SPI0_TWI0_IRQn);
NRF_TWI0->INTENSET = 1;
No joy with any of these, I understand that some of them only work from certain locations in software, so were not functioning correctly within an ISR.
void setup() {
// put your setup code here, to run once
NVIC_SetPriority (TIMER0_IRQn, 2);
NVIC_SetPriority (TIMER1_IRQn, 2);
NVIC_SetPriority (TIMER2_IRQn, 2);
NVIC_SetPriority (SPI0_TWI0_IRQn, 3);
NVIC_SetPriority (SPI1_TWI1_IRQn, 3);
Serial.begin(9600);
Serial.println("Start ");
Serial.print("Priority of TWI0: ");
Serial.println(NVIC_GetPriority (SPI0_TWI0_IRQn));
Serial.println(NVIC_GetPriority (SPI1_TWI1_IRQn));
Serial.print("Priority of Ticker: ");
Serial.println(NVIC_GetPriority (TIMER0_IRQn));
Serial.println(NVIC_GetPriority (TIMER1_IRQn));
Serial.println(NVIC_GetPriority (TIMER2_IRQn));
Wire.begin();
__disable_irq();
__enable_irq();
ticker1s.attach(task_handle, 1);
Wire.requestFrom(0x02,6);
}
void task_handle(void) {
__enable_irq();
Serial.println("Task handle ");
Serial.println("-IRQ enable status: ");
Serial.println(NVIC_GetEnableIRQ(SPI0_TWI0_IRQn));
Serial.println(NVIC_GetEnableIRQ(SPI1_TWI1_IRQn));
NVIC_EnableIRQ(SPI0_TWI0_IRQn);
NRF_TWI0->INTENSET = 1;
NVIC_EnableIRQ(SPI1_TWI1_IRQn);
NRF_TWI1->INTENSET = 1;
Serial.println("-IRQ enable status: ");
Serial.println(NVIC_GetEnableIRQ (SPI0_TWI0_IRQn));
Serial.println(NVIC_GetEnableIRQ (SPI1_TWI1_IRQn));
delay(1000);
Wire.requestFrom(0x02,6);
}
By default, interrupts are not disabled in ISRs on Cortex-M, but rather interrupts are pre-empted based on the priorities set in NVIC (hence the "Nested" part of NVIC). This means that you should simply enable the I2C interrupt as you usually do, and then set the priority using NVIC_SetPriority(IRQn, priority).
Note however that priority numbers are ordered in reverse, so a priority of 0 would pre-empt a priority of say 6. In this case you would set the Timer interrupt priorities to say 1 and the I2C interrupt to 0.

Arduino UNO analogRead always returns 1023

So my problem is as title says: Arduino UNO analogRead always returns 1023.
But when I burn the same sketch in Arduino Mega 2650 everything works like a charm.
I have tried to change Atmel chips on the UNO, have tried like 3 of them (ATMEGA328P-PU) and nothing changes.
I'm trying to count signals from a hall effect sensor and display the count on a 7 segment display.
Here is the code:
#include "SevSeg.h"
SevSeg sevseg;
volatile int rpmcount;
void setup() {
Serial.begin(9600);
pinMode(2,INPUT_PULLUP);
rpmcount = 0;
sevseg.Begin(1,3,4,5,6,7,8,9,10,11,12,13);
}
int border=15;
void loop() {
int tmp=0;
tmp = analogRead(0);
if(!digitalRead(2))rpmcount=0;
Serial.println(tmp,DEC);
if(tmp<=border && res >border){
rpmcount++;
if(rpmcount>9999)rpmcount=0;
}
res=tmp;
sevseg.NewNum(rpmcount,(byte) 0);
sevseg.PrintOutput();
}
Any help would be much appreciated
This sounds to me as if you had the internal pullup resistor on the ADC pin enabled.
generic checklist:
ACD bit in ACSR is 0 (comparator enable)
MUX bits in ADMUX set properly
correct AREF selected
ADC pin set as input
internal pull up resistors are deselected

Resources