OK, this has to be explained to me.
@MianQi's code works when I added a Serial.print to see what was going on on PORTD.
This is the code with only Serial.begin and one Serial.print added, blinks an LED on digital output 6:
#include<avr/io.h>
#include<util/delay.h>
#include<avr/interrupt.h>
#define SET(port,bit) port|=(1<<bit)
#define CLR(port,bit) port&=~(1<<bit)
#define TOGGLE(PORT,BIT) PORT^=(1<<BIT)
int count = 0;
void timer0_init()
{
CLR(TCCR0A, WGM00); //SET TIMER0 TO NORMAL MODE
CLR(TCCR0A, WGM01);
CLR(TCCR0B, WGM02);
TCNT0 = 0X00;
SET(SREG, 7);
SET(TIMSK0, TOIE0); // overflow interrupt enable
}
void enable_timer()
{
SET(TCCR0B, CS00); //set prescalar to 1024
CLR(TCCR0B, CS01);
SET(TCCR0B, CS02);
}
int main()
{
Serial.begin(9600);
DDRD |= (1 << PD6); //LED CONNECTED TO DIGITAL PIN 6 ,and all the
//bits of the port is set so as to make it an
//output port
timer0_init();
enable_timer();
while (1)
{
// the LED doesn't blink if this Serial.print is removed
Serial.println(PORTD);
//if(count==62)
if (count == 31)
{
TOGGLE(PORTD, PD6);
count = 0;
}
}
}
ISR(TIMER0_OVF_vect)
{
count++;
}
It also fails if the printing line is moved to before the TOGGLE inside the if block.
Real UNO.
Rejiggering it as a traditional setup/loop sketch raises this error
wiring.c.o (symbol from plugin): In function __vector_16': (.text+0x0): multiple definition of
__vector_16'
weird.ino.cpp.o (symbol from plugin):(.text+0x0): first defined here
collect2: error: ld returned 1 exit status
Error during build: exit status 1
Which I make no sense of
#include<avr/io.h>
#include<util/delay.h>
#include<avr/interrupt.h>
#define SET(port,bit) port|=(1<<bit)
#define CLR(port,bit) port&=~(1<<bit)
#define TOGGLE(PORT,BIT) PORT^=(1<<BIT)
int count=0;
void timer0_init()
{
CLR(TCCR0A,WGM00);//SET TIMER0 TO NORMAL MODE
CLR(TCCR0A,WGM01);
CLR(TCCR0B,WGM02);
TCNT0=0X00;
SET(SREG,7);
SET(TIMSK0,TOIE0); // overflow interrupt enable
}
void enable_timer()
{
SET(TCCR0B,CS00);//set prescalar to 1024
CLR(TCCR0B,CS01);
SET(TCCR0B,CS02);
}
void setup()
{
Serial.begin(9600);
DDRD|=(1<<PD6); //LED CONNECTED TO DIGITAL PIN 6 ,and all the
//bits of the port is set so as to make it an
//output port
timer0_init();
enable_timer();
}
void loop() {
Serial.println(PORTD);
//if(count==62)
if(count==31)
{
TOGGLE(PORTD,PD6);
count=0;
}
}
ISR(TIMER0_OVF_vect)
{
count++;
}
Waiting on the heavies now...
a7