Arduino servo library conflict - timer

In my code I use only LiquidCrystal library and Servo library. When I try to compile the code following error appears
Servo/Servo.cpp.o: In function `Servo::attached()':
/usr/share/arduino/libraries/Servo/Servo.cpp:336: multiple definition of `__vector_42'
robot_v2.cpp.o:/usr/share/arduino/robot_v2.ino:662: first defined here
/usr/lib/gcc/avr/5.4.0/../../../avr/bin/ld: Disabling relaxation: it will not work with multiple definitions
Servo/Servo.cpp.o: In function `Servo::attached()':
/usr/share/arduino/libraries/Servo/Servo.cpp:336: multiple definition of `__vector_47'
robot_v2.cpp.o:/usr/share/arduino/robot_v2.ino:662: first defined here
collect2: error: ld returned 1 exit status
Apart from above mentioned libraries, I use a 16 bit timer as follows.
DDRC |= B01010101;
......
cli();
TCCR4A = 0;
TCCR4B = 0;
TCCR5A = 0;
TCCR5B = 0;
OCR4A = l_target;
OCR5A = l_target;
TCCR4B = _BV(WGM42) | _BV(CS41);
TCCR5B = _BV(WGM52) | _BV(CS51);
TIMSK4 |= (1 << OCIE4A);
TIMSK5 |= (1 << OCIE5A);
sei();
Let me know what I'm doing wrong and how to fix this issue? Are there any other libraries than the standard Servo library for arduino? I'm using Arduino Mega board.
Here are the ISRs.
ISR(TIMER4_COMPA_vect)
{
if (OCR4A != l_target) {
OCR4A = l_target;
}
PORTC ^= B00000001;
}
ISR(TIMER5_COMPA_vect)
{
if (OCR5A != r_target) {
OCR5A = r_target;
}
PORTC ^= B00010000;
}

The servo library allocates timers only as needed. Each timer supports up to 12 servos so on the Mega, 1 to 12 servos use timer5, 13 to 24 will also use timer1 and so on. The allocation order is defined in the file ServoTimers.h as follows: timer5, timer1, timer3, timer4
Assuming you are not using more than 24 servos, timers 3 and 4 are available. If you change all references to timer 5 (TCCR5x, OCR5x, TIMSK5) to timer 3 TCCR3x, OCR3x, TIMSK3) you should not have the conflict.

I found the related code from Servo library. According to that it uses 1,3,4 and 5 timers. So, I cannot use them for any other purpose. I will have to use Timer 2 or any other solution. I'm answering my own question as a reference for any one else who faces similar situation.
// Say which 16 bit timers can be used and in what order
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#define _useTimer5
#define _useTimer1
#define _useTimer3
#define _useTimer4

Related

Using Timers for Blink & Count

I'm creating part of a program right now for a personal project. This is my first year ACTUALLY coding and have been studying for hours to understand many concepts so please be nice and try not to be rude as others have...
The project of mine is an AVRDUDE using a chip called ATMEGA328P in a program called Microchip studios.
[This project is having 3 LED count every time a switch is pressed, I should have them count continuously and change to the next number every second using a TIMER 1.
**counting in binary from 0-8 then rolling over **
I need some help on one aspect of it which is using interrupts after I have already created a blinking LED to use a TIMER0 instead of delays.
I have made my fourth LED flash at 5Hz which is the blinking part of my code include below at the end of this question.
Now the problem I am running into is trying to create interrupts for the 3 LEDS count every time a switch is pressed, I should instead have them continually count, changing to the next number approximately every second, using TIMER1.]
This is my code for the project
Again please be nice and at least lead me in some type of direction...
#include <avr/io.h>
#include <avr/interrupt.h>
#define F_CPU 1000000UL
int global = 0 ;
volatile uint8_t overflow0; // Variable for counting overflows for first timer
volatile uint8_t overflow1; // Variable for counting overflows for second timer
void FirstTimer() // Initialize Timer 1
{
TCCR0B |= (1<<CS02) | (1<<CS00); // Prescaler of 1024
TCNT0 = 0;
}
void SecondTimer() //Initialize Timer 2
{
TCCR1B |= (1<<CS11) | (1<<CS10);
TCNT1 = 0;
}
int main(void)
{
DDRB |= (1 << DDB0);
FirstTimer(); // Calling timer 1 and 2 initialization
SecondTimer();
while (1)
{
{
if (TCNT0 >= 195) // Amount of ticks needed
{
PORTB ^= (1 << PORTB0); //LED on
TCNT0 = 0; //Reset counter
}
}
{
if (TCNT1 >= 15625) // Ticks needed
{
TCNT1 = 0; // Timer reset
}
}
}
}
An approach you could use is keeping a count variable (unsigned char - assuming 8 bit register width) and in your Timer ISR simply increment the count and write that value to the output register. Once count > 8 set to zero.
Configure the Timer mode to trigger on overflow w/ reset to zero.
Then you do some basic math using the clock speed and timer ceiling (overflow value) to calculate the frequency you want the overflow ISR to occur (increment the LED count values).
Note that for this to work the LEDs need to be on the first 3 pins.
i.e.
P1.0 = LED1
P1.1 = LED2
P1.2 = LED3
...
If not connected like this then you can still make it work with additional bit manipulation (shifts and masks).

compiling error while declaring sfr address

i am working on a 8051 based MCU STC16C65A and i want it to emit PWM thru P2.0, sp i copy and modify this code from their manual:
#include "8051.h"
sfr AUXR = 0x8e;
//Auxiliary register T0 interrupt service routine
void t0int() interrupt 1 //(location at 000BH)
{
}
void main()
{
AUXR = 0x80;
//timer0 work in 1T mode
TMOD = 0x06; //set timer0 as counter mode2 (8-bit auto-reload)
TL0 = TH0 = 0xff; //fill with 0xff to count one time
TR0 = 1; //timer0 start run
ET0 = 1; //enable T0 interrupt
EA = 1; //open global interrupt switch
IF P2.0 = 0 THEN P2.0 = 1 ELSE P2.0 = 0;
while (1);
}
but when i compile it using MCU 8051 ide comes out a syntax error, declaration ignored in sfr AUXR = 0x8e; i think is that the library used (8051.reg) is a generic one instead of the reg51.h (oudated) that they suggest in the manual which i dont use because cames a error message of library out to date
Who can hep me? maybe i have to choose another adress for that pointer?
thanks in advance
SDCC is a different compiler than Keil C51, you have to expect differences in extensions to the C standard. And the declaration of SFRs is an extension.
Please use the documented syntax:
__sfr __at(0x8e) AUXR;

I need to know why my interrupts are not working here

I am using stm8l - discovery and i have created a code for toggling a led for every 1 second using timer (TIM1) but this is not working properly. I am missing something here in my configuration
I could enter the interrupt function for the first time but after that it does not enter the interrupt function. Someone please look into and help me out
enter code here
#include <iostm8l.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "defs.h"
unsigned int count = 0;
#svlreg #interrupt void TIM1(void)
{
count += 1;
TIM1_SR1 &= ~(0x01);
}
main()
{
CLK_DIVR = 0x00; // Set the frequency to 16Mhz
CLK_PCKENR2 = 0x02; // clock for timer1
PC_DDR = 0x80; // direction output for led
PC_CR1 = 0x80; // fast push pull mode
PE_DDR = 0x80; // direction output for led
PE_CR1 = 0x80; // fast push pull mode
TIM1_PSCRH = 0x3e; //to create a frequency for 1000 hz
TIM1_PSCRL = 0x80; // so prescalar is 16000
TIM1_CR1 = 0x01;
TIM1_IER = 0x01;
_asm("rim\n");
while(1)
{
if (count == 1000)
{
PE_ODR ^= 0x80;
count = 0;
}
}
}
The interrupt enters only one time but after that it does not enter. So variable "count" remains at value 1
You are using magic numbers for the bit masks instead of defined constants, so the code is pretty damn hard to read for you and me both. Change this so that the code ends up like for example
TIM1_SR1 &= ~TIM1_SR1_UIF;
Since this is a 8 bit MCU it is also absolutely essential that you u suffix all integer contants, or they will be of type signed int, which you don't want.
For example this code TIM1_SR1 &= ~(0x01); is equivalent to TIM1_SR1 &= -2. Very easy to write accidental subtle bugs this way. I recommend studying Implicit type promotion rules.
It is highly recommended to disassemble every ISR you write to see what machine code you actually end up with, and single step it through the debugger while watching the register as well. This particular register seems to ignore having 1 written to it, so you could probably just do TIM1_SR = TIM1_SR1_UIF;. Incorrectly cleared timer flags inside ISRs is one of the most common bugs in embedded systems.
Quoting the manual:
UIF: Update interrupt flag
– At overflow or underflow if UDIS = 0 in the TIM1_CR1 register
– When CNT is re-initialized by software using the UG bit in TIM1_EGR register, if URS = 0 and UDIS = 0 in the TIM1_CR1 register.
– When CNT is re-initialized by a trigger event (refer to the TIM1_SMCR register description), if URS = 0 and UDIS = 0 in the TIM1_CR1 register
Your code doesn't appear to do any of this, so it is pretty safe to assume the timer counter isn't reset.
Another problem is that count must be declared as volatile or otherwise the compiler might optimize out this code completely:
if (count == 1000)
{
PE_ODR ^= 0x80;
count = 0;
}

Problem with toggle led on Arduino nano Atmega328P with timer/counter0 interrupt

I try to toggle the LED on Arduino nano ATmega328P without success.
(Timer 0 is 8bit timer)
I managed to execute it with Timer 1
with code from here.
#define ledPin 13
void setup() {
pinMode(ledPin, OUTPUT);
cli(); // disable interrupts
TCCR0A = 0;
TCCR0B = 0;
TCCR0B |= (1 << CS02 | 1 << CS00); //clkI/O/1024
TIMSK0 = 0;
TIMSK0 |= (1 << TOIE0); // Overflow Interrupt Enable
TCNT0 = 0;
sei(); // enable interrupts
Serial.begin(9600);
}
ISR(TIM0_OVF_vect) {
digitalWrite(ledPin, digitalRead(ledPin) ^ 1);
}
void loop() {
}
I also tried to change the interrupt vector to: TIMER0_OVF_vect
and got this error:
Arduino: 1.8.9 (Windows 10), Board: "Arduino Nano, ATmega328P (Old Bootloader)"
wiring.c.o (symbol from plugin): In function `__vector_16':
(.text+0x0): multiple definition of `__vector_16'
sketch\tests.ino.cpp.o (symbol from plugin):(.text+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board Arduino Nano.
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
I expect the led to toggle.
Your first block of code with:
ISR(TIM0_OVF_vect) {
digitalWrite(ledPin, digitalRead(ledPin) ^ 1);
}
has the wrong ISR name. TIM0_OVF_vect is only valid for some ATtiny microcontrollers. You can find a complete list of interrupts and which controllers they are used with here.
You then tried to change it to use TIMER0_OVF_vect, which is valid for the ATMEGA328P, but you cannot use this with Arduino, because Arduino ATMEGA328P builds use Timer0 for millis() and related timing. This is why you get a multiple definition of error - the linker is telling you that there are two TIMER0_OVF_vect ISRs defined by your program (one by your sketch, one in wiring.c).
Timer2 is not used by default, so you should be able to use that instead. The only default Arduino library that used Timer2 is the Tone library.

Interrupt-driven USART using UDRE with ATmega328P

I am having a problem with programming the serial for Arduino in C. The application demands speed, so I need to do it in C. I am using Codeblocks for it as it is easy to compile.
As I also want the serial feature not blocking the application and giving debug capability, I am trying to write a circular buffer type of serial library. The application does not need to receive data, only print.
The problem begins with the serial interrupt apparently not firing and the program blocks, the compiler does give a warning:
main.c|11|warning: ‘UART_UDRE_vect’ appears to be a misspelled signal handler|
The test program is as follows:
#include<avr/io.h>
#include<avr/interrupt.h>
#define USART_BAUDRATE 9600
#define BAUD_PRESCALE (((F_CPU/(USART_BAUDRATE*16UL)))-1)
char ok = 0;
ISR(UART_UDRE_vect) {
ok = 1;
UCSR0B &= ~(1<<5);
}
int main(void) {
UBRR0H = (BAUD_PRESCALE >> 8);
UBRR0L = BAUD_PRESCALE;
UCSR0B |= (1<<TXEN0);
UCSR0C |= (1<<UCSZ00) | (1<<UCSZ01);
sei();
while(1){
// write the byte to the serial port
UDR0 = '0';
UCSR0B |= (1<<5);
while(ok != 1){}
ok = 0;
UDR0 = '\n';
UCSR0B |= (1<<5);
while(ok != 1){}
ok = 0;
}
return 0;
}
The configuration and baud rates are correct because the echo example found here does work.
Also other examples does not use UDRE, only RX interruption and that is not what I am looking for.
Am I forgetting something?
Two problems:
According to the documentation you linked, the interrupt vector for your particular part ATmega328p should be named USART_UDRE_vect.
Variables shared with an interrupt must always be declared as volatile or the compiler might not understand that they are used and therefore break your program upon optimization. Basically the compiler thinks that the variable ok can never get another value than 0, since it doesn't see a function call to the ISR anywhere (since it is called by hardware, not by the program). Change the declaration to static volatile char ok;

Resources