>I am writing a program of blinking the LED on STK600 for 10 sec and then
stopping until there is required switch input.The microcontroller i am
using is Atmega2560.
I did this using 2 timer/counter interrupts TIMER/COUNTER1 AND
TIMER/COUNTER3.The LED is blinking continuously and the timer 3 interrupt
is not getting called.
Here is my program...
//ICC-AVR application builder : 3/19/2009 4:15:37 PM
// Target : M2560
// Crystal: 16.000Mhz
#include <iom2560v.h>
#include <macros.h>
#include <stdio.h>
/* This putchar works with the M2560 UART0. It will work with other
devices
* if their UART registers use the same names. If the UART IO locations
* are different from the M2560, then you will also have to change the
* #include header file statement.
*
* For device with multiple UARTs and if you want to use a different UART,
* change the IO register names as appropriate.
*/
extern int _textmode;
int putchar(char c)
{
if (_textmode && c == '\n')
putchar('\r');
/* Wait for empty transmit buffer */
while ( !(UCSR0A & (1<<UDRE0)) )
;
/* Putting data into buffer , sends the data */
UDR0 = c;
return c;
}
#ifndef BV
#define BV(bit) (1<<(bit))
#endif
#ifndef cbi
#define cbi(reg,bit) reg &= ~(BV(bit))
#endif
#ifndef sbi
#define sbi(reg,bit) reg |= (BV(bit))
#endif
#define SW0() ((PIND >> 0) & 1)
#define SW1() ((PIND >> 1) & 1)
#define SW2() ((PIND >> 2) & 1)
#define SW3() ((PIND >> 3) & 1)
#define SW4() ((PIND >> 4) & 1)
#define SW5() ((PIND >> 5) & 1)
#define SW6() ((PIND >> 6) & 1)
#define SW7() ((PIND >> 7) & 1)
#define SET_LED0() cbi(PORTB,0)
#define SET_LED1() cbi(PORTB,1)
#define SET_LED2() cbi(PORTB,2)
#define SET_LED3() cbi(PORTB,3)
#define SET_LED4() cbi(PORTB,4)
#define SET_LED5() cbi(PORTB,5)
#define SET_LED6() cbi(PORTB,6)
#define SET_LED7() cbi(PORTB,7)
#define CLR_LED0() sbi(PORTB,0)
#define CLR_LED1() sbi(PORTB,1)
#define CLR_LED2() sbi(PORTB,2)
#define CLR_LED3() sbi(PORTB,3)
#define CLR_LED4() sbi(PORTB,4)
#define CLR_LED5() sbi(PORTB,5)
#define CLR_LED6() sbi(PORTB,6)
#define CLR_LED7() sbi(PORTB,7)
#define SWITCH_ON 0
#define SWITCH_OFF 1
#define TIMER_INTERVAL 500
#define PRESCALER 1024
#define PRESCALER10sec 8
#define PROCESSOR_CLOCK 1600000
#define TIMER_COMPARE_VALUE (((PROCESSOR_CLOCK / PRESCALER) *
TIMER_INTERVAL) / 10000)
#define TIMER_COMPARE_VALUE10sec (((PROCESSOR_CLOCK /
PRESCALER10sec) * TIMER_INTERVAL) / 10000)
#define TCCR1B_VALUE BIT(WGM12)|BIT(CS12)|BIT(CS10)
#define TCCR3B_VALUE10s BIT(WGM32)|BIT(CS31)
#pragma interrupt_handler
Timer1OutputCompareA:iv_TIMER1_COMPA
#pragma interrupt_handler handler_Timer10sec:iv_TIMER3_COMPA
#define TABLE_ENTRIES 3
#define PASSWORD_ENTRIES 4
#define SWITCH_ENTRIES 8
unsigned char gucPointer = 0;
unsigned char SW[SWITCH_ENTRIES] =
{
0,1,2,3,4,5,6,7
} ;
const unsigned char LedPattern[TABLE_ENTRIES] =
{
254, 255, 254
} ;
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
PORTE = 0x00;
DDRE = 0x00;
PORTF = 0x00;
DDRF = 0x00;
PORTG = 0x00;
DDRG = 0x00;
PORTH = 0x00;
DDRH = 0x00;
PORTJ = 0x00;
DDRJ = 0x00;
PORTK = 0x00;
DDRK = 0x00;
PORTL = 0x00;
DDRL = 0x00;
}
//UART0 initialize
// desired baud rate: 9600
// actual: baud rate:9615 (0.2%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00;
UCSR0C = 0x06;
UBRR0L = 0x67; //set baud rate lo
UBRR0H = 0x00; //set baud rate hi
UCSR0B = 0x08;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
XMCRA = 0x00; //external memory
XMCRB = 0x00; //external memory
port_init();
uart0_init();
MCUCR = 0x00;
EICRA = 0x00; //pin change int edge 0:3
EICRB = 0x00; //pin change int edge 4:7
PCICR = 0x00; //pin change int enable
PCMSK0 = 0x00; //pin change mask
PCMSK1 = 0x00; //pin change mask
PCMSK2 = 0x00; //pin change mask
EIMSK = 0x00;
TIMSK0 = 0x00; //timer0 interrupt sources
TIMSK1 = 0x00; //timer1 interrupt sources
TIMSK2 = 0x00; //timer2 interrupt sources
TIMSK3 = 0x00; //timer3 interrupt sources
TIMSK4 = 0x00; //timer4 interrupt sources
TIMSK5 = 0x00; //timer5 interrupt sources
PRR0 = 0x00;
PRR1 = 0x00;
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//
void main(void)
{
int i;
int Keys_Pressed_Still=0;
init_devices();
printf("Main Start\n\r");
DDRD = 0x00;
DDRB = 0xFF;
PORTB = 0xFF;
TCCR1B = 0x00; // Stop Timer1
TCNT1H = 0x00; // Clear Timer1
TCNT1L = 0x00;
while(1)
{
//alsoif((SW0()==SWITCH_ON)&(SW1()==SWITCH_ON)&(SW2()==SWITCH_ON)&(SW3()==SWITCH_ON))
if(SW0()==SWITCH_ON)
{
if(SW1()==SWITCH_ON)
{
if(SW2()==SWITCH_ON)
{
if(SW3()==SWITCH_ON)
{
if(Keys_Pressed_Still==0)
{
Keys_Pressed_Still=1;
Timer1OutputCompareA();
OCR1A = TIMER_COMPARE_VALUE; // 100ms timer
TIMSK1 |= BIT(OCIE1A); // Compare A Interrupt
enable
TCCR1B = TCCR1B_VALUE; // Start
Timer1 with clk/1024 & CTC mode
_SEI(); // Set
the I flag in SREG
Timer10secStart();
}
}
else
{
Keys_Pressed_Still=0;
NOP();
}
}
else
NOP();
}
else
NOP();
}
else
NOP();
}
// CLI();
//printf("Sleeping\n\r");
}
void Timer1OutputCompareA(void)
{
int Timer3control;
long int timer3countH;
long int timer3countL;
PORTB = LedPattern[gucPointer++];
if(gucPointer == TABLE_ENTRIES)
{
gucPointer = 0;
}
/* timer3countH=TCNT3H;
timer3countL=TCNT3L;
printf("Timer3: %d %d %d \n\r", timer3countH,timer3countL,TCCR3B);*/
}
void Timer10secStart(void)
{
long int timer3countH;
long int timer3countL;
printf("Start Timer 10sec \n\r");
OCR3A = TIMER_COMPARE_VALUE10sec;
TIMSK3 |= BIT(OCIE3A); // Compare A Interrupt enable
TCCR3B = TCCR3B_VALUE10s; // Start Timer3 with clk/8 & CTC
mode
_SEI();
timer3countH=TCNT3H;
timer3countL=TCNT3L;
printf("TIMSK3: %d \n\r", TIMSK3);
printf("TCCR3B: %d \n\r", TCCR3B);
printf("OCIE3A: %d \n\r", OCIE3A);
printf("TCCR3B_VALUE10s: %d \n\r", TCCR3B_VALUE10s);
printf("Timer3: %d %d \n\r", timer3countH,timer3countL);
}
int handler_Timer10sec(void)
{
TCCR1B = 0x00; // Stop Timer1
TCNT1H = 0x00; // Clear Timer1
TCNT1L = 0x00;
printf("Stop Timer 1\n\r");
printf("Sleeping\n\r");
}
Kindly look at it and please let me know of my mistake in programming...
Thank you
Tripura Sundari
Reply by tripura●March 13, 20092009-03-13
I am also working on the interrupt handling using Microcontroller
ATMEGA2560.I want to read the switches input which are connected to the
PORTD of the MCU and on a particular switch press, a pattern of LED's had
to glow.Can anyone help me with this.
My C program is:
#define PORTD (*(volatile unsigned char *) 0x2B)
#define BIT(x) (1<<(x))
#include <iom2560v.h>
main (void)
{
DDRB=0xFF;
PORTB=0xFF;
DDRD=0xFF;
PORTD = BIT(0);
buttonpress();
while (1)
{
toggle_led();
}
}
void buttonpress(char *buffer)
{
while(*buffer) /* loop until buffer is empty */
{
PORTD = *buffer; /* write data to LCD data port */
switchpress_interrupt();
buffer++; /* increment the pointer to the next character */
}
}
toggle_led(void)
{
PORTB=0x10;
}
switchpress_interrupt(void)
{
unsigned char i;
SREG=0x80;
EIMSK=0x01;
EICRA=PORTD;
if(EICRA==0x00)
return 1;
}
Kindly help me with this.
Thank you
Tripura
>On Tue, 02 Sep 2008 07:05:42 -0500, "Radha_Purnima"
><chinnari_dasari@yahoo.co.in> wrote:
>
>>how exactly are the interrupts get served in the real time embedded
>>products like a washing machine,refrigirator?
>>
>>is it like, we will write a program where we continuously check a
>>particular status bit of the device has been set or not...or those
>>interrupts will be hard wired to a interrupt pin (INT pin kind of)??
>>
>>if it is hard wired how a variety of divices are connected to that
>>particular pin and how exactly the interrupt by a particular device get
>>serviced??
>>
>>
>>
>
>A simple state machine, run some moderate number of times per second,
>can check all input and output levels and decide what to do. It may be
>run by a periodic interrupt, or it may just pace itself with a wait
>loop. Interrupts from hardware inputs, like pushbuttons maybe, are
>often more trouble than simple polling from within a state machine.
>
>And proper periodic polling can give you debouncing for free.
>
>John
>
>
Reply by Jeff Jonas●September 11, 20082008-09-11
John:
>A simple state machine, run some moderate number of times per second,
>can check all input and output levels and decide what to do. It may be
>run by a periodic interrupt, or it may just pace itself with a wait
>loop. Interrupts from hardware inputs, like pushbuttons maybe, are
>often more trouble than simple polling from within a state machine.
>And proper periodic polling can give you debouncing for free.
For getting something done right away, I agree
that polling is simpler to write, debug and assure it works.
But I'm still old enough to remember when CPU cycles were "precious"
and not to be wasted polling for things that mostly don't occur.
Deep down I prefer interrupts but they're trickier to handle properly
due to possible concurrency among data structures.
And interrupts allow for power saving techniques
such as using slower clock rates, sleep or suspend modes.
Reply by Walter Banks●September 3, 20082008-09-03
John Larkin wrote:
> A simple state machine, run some moderate number of times per second,
> can check all input and output levels and decide what to do. It may be
> run by a periodic interrupt, or it may just pace itself with a wait
> loop. Interrupts from hardware inputs, like pushbuttons maybe, are
> often more trouble than simple polling from within a state machine.
>
> And proper periodic polling can give you debouncing for free.
We are seeing increasing amounts of event driven code. Similar in
design to state machines but very small event initiated execution
fragments that always run to completion. These are a compromise
in timing. The code can be shorter and require less RAM but the
response time will be longer. As long as the response time meets
the system requirements it is a big win.
Regards
--
Walter Banks
Byte Craft Limited
Tel. (519) 888-6911
http://www.bytecraft.com
Reply by Walter Banks●September 3, 20082008-09-03
Vladimir Vassilevsky wrote:
> > It's been a while since you took one apart, eh?
> > Microprocessors are insinuating themselves even into wash machines and
> > dishwashers.
>
> Oh dear. Poor users. Does it have the operating system, too?
That is for big appliance micro processors like 2K. Seriously the
code I see is in two categories 1970's spaghetti and some very
clever well though out tightly implemented application code.
Both are what ever ROM size the appliance has the latter
very functional fault tolerant and reliable.
w..
Reply by Grant Edwards●September 3, 20082008-09-03
On 2008-09-02, Martin Griffith <mart_in_medina@yah00.es> wrote:
> On Tue, 02 Sep 2008 17:35:32 -0500, in comp.arch.embedded Vladimir Vassilevsky <antispam_bogus@hotmail.com> wrote:
>>It loos more that the things are going into the opposite direction: only
>>the old farts can fix gadgets and vigets of young suckers who haven't
>>got a slightest clue.
> Hey, do you want to share that beer:)
I think the beer's gone...
--
Grant Edwards grante Yow!
at BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-
visi.com
Reply by Boudewijn Dijkstra●September 3, 20082008-09-03
Op Tue, 02 Sep 2008 17:53:04 +0200 schreef Paul Keinanen <keinanen@sci.fi>:
> On Tue, 02 Sep 2008 07:05:42 -0500, "Radha_Purnima"
> <chinnari_dasari@yahoo.co.in> wrote:
>
>> is it like, we will write a program where we continuously check a
>> particular status bit of the device has been set or not...or those
>> interrupts will be hard wired to a interrupt pin (INT pin kind of)??
>
> When the micro code has finished executing the previous instruction,
> the microcode checks if any interrupt requests are active.
On some cores, some multi-cycle instructions can be cancelled and later
restarted.
> If not, the operation code pointed by the program counter is fetched,
> decoded and executed.
>
> However, if some interrupt is set, the micro code will save the
> context (typically the program counter and some registers) on the
> stack
Or it will switch register banks, leaving the decision to perform stack
operations to the specific interrupt handler.
> and then fetch an address from a known address (in the interrupt
> vector) and load it into the program counter. After that, the micro
> code will proceed normally.
But usually in a mode called 'priviledged' or 'supervisor'.
--
Gemaakt met Opera's revolutionaire e-mailprogramma:
http://www.opera.com/mail/
Reply by John Larkin●September 3, 20082008-09-03
On Tue, 02 Sep 2008 07:05:42 -0500, "Radha_Purnima"
<chinnari_dasari@yahoo.co.in> wrote:
>how exactly are the interrupts get served in the real time embedded
>products like a washing machine,refrigirator?
>
>is it like, we will write a program where we continuously check a
>particular status bit of the device has been set or not...or those
>interrupts will be hard wired to a interrupt pin (INT pin kind of)??
>
>if it is hard wired how a variety of divices are connected to that
>particular pin and how exactly the interrupt by a particular device get
>serviced??
>
>
>
A simple state machine, run some moderate number of times per second,
can check all input and output levels and decide what to do. It may be
run by a periodic interrupt, or it may just pace itself with a wait
loop. Interrupts from hardware inputs, like pushbuttons maybe, are
often more trouble than simple polling from within a state machine.
And proper periodic polling can give you debouncing for free.
John
Reply by Martin Griffith●September 2, 20082008-09-02
On Tue, 02 Sep 2008 17:35:32 -0500, in comp.arch.embedded Vladimir
Vassilevsky <antispam_bogus@hotmail.com> wrote:
>
>
>Tim Wescott wrote:
>
>> Vladimir Vassilevsky wrote:
>>> Radha_Purnima wrote:
>>>
>>>> how exactly are the interrupts get served in the real time embedded
>>>> products like a washing machine,refrigirator?
>>>
>>> What interrupts?
>>> Appliances like washing machines use mechanical timer with camshaft
>>> and set of switches.
>>>
>> It's been a while since you took one apart, eh?
>> Microprocessors are insinuating themselves even into wash machines and
>> dishwashers.
>
>Oh dear. Poor users. Does it have the operating system, too?
>
>> Soon all the old farts in the world will have to live in
>> special homes with resident 13-year olds to program all their
>> appliances, not just their VCRs.
>>
>> It's a plot.
>
>It loos more that the things are going into the opposite direction: only
>the old farts can fix gadgets and vigets of young suckers who haven't
>got a slightest clue.
>
>
>Vladimir Vassilevsky
>DSP and Mixed Signal Design Consultant
>http://www.abvolt.com
Hey, do you want to share that beer:)
martin
Reply by Vladimir Vassilevsky●September 2, 20082008-09-02
Tim Wescott wrote:
> Vladimir Vassilevsky wrote:
>> Radha_Purnima wrote:
>>
>>> how exactly are the interrupts get served in the real time embedded
>>> products like a washing machine,refrigirator?
>>
>> What interrupts?
>> Appliances like washing machines use mechanical timer with camshaft
>> and set of switches.
>>
> It's been a while since you took one apart, eh?
> Microprocessors are insinuating themselves even into wash machines and
> dishwashers.
Oh dear. Poor users. Does it have the operating system, too?
> Soon all the old farts in the world will have to live in
> special homes with resident 13-year olds to program all their
> appliances, not just their VCRs.
>
> It's a plot.
It loos more that the things are going into the opposite direction: only
the old farts can fix gadgets and vigets of young suckers who haven't
got a slightest clue.
Vladimir Vassilevsky
DSP and Mixed Signal Design Consultant
http://www.abvolt.com