ImageCraft C LED Blink Using Interrupt Not Working

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi, i im new to ImageCraft C Compiler For AVR, and would like to use LED blink using timer every 1second for start...using this code it compile successfully but LED blink on protoboard does not...so i see that my interrupt routine does not work..i im using ATmega328P and 18.432MHz quarz.

 

#include <iom328pv.h>

#include <macros.h>

#pragma interrupt_handler aaaa:12

// interrupt handlers

void aaaa(void) {

CLI();

// toggle LED

PORTD ^= (1 << 1);

SEI();

}

// initialize timer, interrupt and variable

void timer1_init(void) {

// set up timer with prescaler = 1024 and CTC mode

TCCR1B |= (1 << WGM12)|(1 << CS12)|(1 << CS10);

// initialize counter

TCNT1 = 0;

// initialize compare value 1sec

OCR1A = 18000;

// enable compare interrupt

TIMSK1 |= (1 << OCIE1A);

// enable global interrupts

SEI();

}

int main(void) {

// connect led to pin PD0

    DDRD |= 0xFF;

// initialize timer1

timer1_init();

// loop forever

while(1) {

}

return 0;

}

 

Last Edited: Sat. Apr 14, 2018 - 01:57 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I would not choose your writing style.
But the code "looks" as if it should work.
Your comment says PC0. Your code blinks PC1. Of course your LED must be connected the right pin and the right way around.
.
David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for responde...i connected LED to PC1...and comment was wrong (PC0)...because i set DDRD all ports as output.

Strange happends...yesterday at night later i disconnected programmer from AVR, now i connect and led start flashing...very nice...i im beginner in ImageCraft compiler....and would like to ask where is defined quart frequency in compiler?

 

Because i use Timer1 as prescaler and counter value compiler need somwhere defined crystal frequency..and i could not find it in compiler...but hey now led blinks and in interval 1sec.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OCR1A = F_CPU / 1024 / 1 - 1 = 17999
.
In the days of Bob Gardner he had no concept of F_CPU. I bet that Imagecraft IDE can be told that F_CPU is 1843200
Read your Imagecraft docs.
.
You have been a member for 12 years. Surely you have used one compiler or another.
.
David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:
Your comment says PC0. Your code blinks PC1.
robydream wrote:
...i connected LED to PC1...and comment was wrong (PC0)...because i set DDRD all ports as output.

???  If the LED is connected to PCn, then why is port D being made outputs?

 

What is connected to all the port D pins?  Are you creating hard shorts by making all the pins outputs?

 

Practice using the Code tags <> on the formatting toolbar.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok..i updated code above using <> tags, change comment so that program now looks like it should be...i have added LED on PIN2 on ATMEGA328P, and that is PORTD.2...usign above program it works very well..setting Quarz is undefined..i just burned LOW Bits to atmega MCU to use external crystal and that is all about..if you look in AVR Assembly there is not defined quarz so using low bits avr mcu get external crystal..so timers are functioning very well.

 

About above formula it is 18000 not 17999 using this formula:

 

T = 1 / 18.432MHz

N = 1sec / T

P = 1024

n = N / P = 18000

 

Because Timer1 is 16-bit counting is from 0-65536 not 0-65535 so this is why must be extra +1 in timer value.

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

robydream wrote:
Ok..i updated code above using <> tags, change comment so that program now looks like it should be...

In the future, please do not alter the original code, but make a new post with the changes so newcomers to the thread can understand the time line of posts.

 

Jim

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

robydream wrote:
Because Timer1 is 16-bit counting is from 0-65536 not 0-65535 so this is why must be extra +1 in timer value.

No, a 16-bit timer cannot count to 65536.

 

So, does it work now?  What is your question in particular?  I re-read the thread and cannot tell.

 

robydream wrote:
and that is PORTD.2.

If you want to use PD2, why are you toggling PD1?  Note that using in numbers sich as "pin2" are meaningless.  For one thing, it depends on package.

 

CLI() and SEI() in ISRs as you have used them are useless.  In a full app, the can be dangerous.  Read your datasheet section on interrupt handling.

 

And I still want to know why you are making all the pins in the port D as outputs?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

All is working...16-bit timer is 2^16 = 65536 so it can count using prescaler....i know that each register in AVR is 8 -bit (0-255) but using prescaler and OCR1AL and OCR1AH stores two 8-bit numbers..i have enought expiriance with this timer because i spend last whole week reading about timers...so this is how it works.

 

I write wrong again i put LED on PIN3 on ATmega328P...if you look carefully that is PORTD.1 so as you suggested why i use 0xFF to set whole DDRD output? Yes that make sense..i forget to put this way:

 

DDRD |= 0x02; 

This defines PORTD.1 As Output and rest pins on portd as input...

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

robydream wrote:
i know that each register in AVR is 8 -bit (0-255) but using prescaler and OCR1AL and OCR1AH stores two 8-bit numbers..i have enought expiriance with this timer because i spend last whole week reading about timers...so this is how it works.

No, that is not how it works.  By your own discussion, an 8-bit register counts 0-255.  NOT 0-256.  A 16-bit pair of 8-bit registers counts 0-65535.  NOT 65536.

 

robydream wrote:
I write wrong again i put LED on PIN3 on ATmega328P
theusch wrote:
Note that using in numbers sich as "pin2" are meaningless. For one thing, it depends on package.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Because i use Timer1 as prescaler and counter value compiler need somwhere defined crystal frequency..and i could not find it in compiler...but hey now led blinks and in interval 1sec.

You have manually set the timer registers with values you computed from the real frequency.

So the compiler need not to know the frequency.  

Last Edited: Mon. Apr 16, 2018 - 06:21 AM