Programming Failed.... Blinking LED in Atmega32A using TIMER1 with Output Compare Register

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

Hi,

       I'm new to assembly language program and learning with the help of atmega32 + dragon. I wrote a asm program to blink a LED for every seconds using 16-bit timer1 with Output Compare Value= 15620.

After typing the program, i tried to debug it. Please the following Program.

 

;--------------------------- Staring of Program------------------------------------

.include "m32Adef.inc"

.EQU OCR_Value = 15620

.DEF TEMP = R16

;----------------------------Initialize Interrupts---------------------------------

.CSEG
.ORG 0x0000 RJMP RESET
.ORG 0x000E RJMP TIMER1A_COMPA

;--------------------------- Main Program -------------------------------------------

.ORG 0X0030

WAIT: RJMP WAIT                 // Main Program. Simply waiting for OCM interrupt

;--------------------------- Define ISR -------------------------------------------

RESET:
                    
    LDI TEMP, High(RAMEND)            // SET-UP STACK (MISSED)
    OUT SPH, TEMP
    LDI TEMP, Low(RAMEND)
    OUT SPL, TEMP    

 

    SEI                                     // Enable the Globle Interrupt

 

    SER TEMP                
    OUT DDRA, TEMP                // Define the PORT Direction and Value
    OUT PORTA, TEMP 

       

    CLR R17                    // TIMER1 Initialization to ZERO
    OUT TCNT1H, R17
    OUT TCNT1L, R17

 

    LDI TEMP, 0x3D                // Set 0X3D04 (15620) in Output Compare 16-bit Register
    OUT OCR1AH, TEMP    
    LDI TEMP, 0x04                
    OUT OCR1AL, TEMP
    
    CLR TEMP
    LDI TEMP, (1<<OCF1A)            // Clear any pending interrupts
    OUT TIFR, TEMP

                

    CLR TEMP
    LDI TEMP, (1<<CS10 | 1<<CS11 | 1<<WGM12)         // CTC MODE.4 + Prescale of 64 is set
    OUT TCCR1B, TEMP

                

    CLR TEMP
    IN TEMP, TIMSK                // OC1A Interrupt is Enabled without disturbing other bits
    ORI TEMP, (1<<OCIE1A)
    OUT TIMSK, TEMP

 

    SER TEMP
    
    RETI       // Returning from "RESET - ISR"    [ <== after this instruction, the PC is not returning to the next location 0X000E (TIMER1 COMPA INTERRUPT) ]

 

TIMER1A_COMPA:

 

    IN R17, SREG                // Storing Status Register values in RAM
    PUSH R17

 

    LDI R17, 0X00                // Make Timer1 reads to zero
    OUT TCNT1H, R17
    OUT TCNT1L, R17

 

    IN TEMP, PORTA
    NEG TEMP                // All LED's on PORTA is TOGGLED each sec (approx.)
    OUT PORTA, TEMP

 

    POP R17                    // Put SREG values back
    OUT SREG, R17
    
    RETI                    // Returning from ISR

;--------------------------------------- End of Program ----------------------------------------------------

 

 

When debugging this program, first, the RESET interrupt codes are verified and every things are OK. But after RETI (for RESET), the PC is not going to the next INTERRUPT Location 0X000E (TIMER1 COMPA INTERRUPT).  Instead of going to next interrupt location values, the PC is set to location 0X0000 (again it is going to ".ORG 0x0000 RJMP RESET ". It is going like a cycle.

 

I'm couldn't find reason that why the  programming is not running properly. 

 

I'm having one more doubt that, when the interrupt is occuerred , the SREG will be stored in the RAM by programmer. How about PC values. Will it be stored by programmer or automatically will be stored in stack ?

 

Please help me to complete the program correctly.

 

Thanking you...

 

have a nice day

- Lenin Chakravarthy

Last Edited: Mon. Feb 23, 2015 - 07:03 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

 

       Now i was trying to find out why the program was cycling within "RESET".... I put RETI  instruction wrongly inside of RETI. I think thats why, it was 'Looping within RESET'.. Now i changed the program also. I programmed into mega32A also. But still the program is not working.Please the modified program and help to find the cause.

 

;--------------------------- Staring of Program------------------------------------

.include "m32Adef.inc"

.EQU OCR_Value = 15620

.DEF TEMP = R16

;----------------------------Initialize Interrupts---------------------------------

.CSEG
.ORG 0x0000 RJMP RESET
.ORG 0x000E RJMP TIMER1A_COMPA

;--------------------------- Main Program -------------------------------------------

.ORG 0X0030

RESET:
                    
    LDI TEMP, High(RAMEND)            // SET-UP STACK (MISSED)
    OUT SPH, TEMP
    LDI TEMP, Low(RAMEND)
    OUT SPL, TEMP    

 

    SEI                    // Enable the Globle Interrupt

 

    SER TEMP                
    OUT DDRA, TEMP                // Define the PORT Direction and Value
    OUT PORTA, TEMP        

 

    CLR R17                    // TIMER1 Initialization to ZERO
    OUT TCNT1H, R17
    OUT TCNT1L, R17

 

    LDI TEMP, 0x3D                // Set 0X3D04 (15620) in Output Compare 16-bit Register
    OUT OCR1AH, TEMP    
    LDI TEMP, 0x04                
    OUT OCR1AL, TEMP
    
    CLR TEMP
    LDI TEMP, (1<<OCF1A)            // Clear any pending interrupts
    OUT TIFR, TEMP  

             

    CLR TEMP
    LDI TEMP, (1<<CS10 | 1<<CS11 | 1<<WGM12)         // CTC MODE.4 + Prescale of 64 is set
    OUT TCCR1B, TEMP     

            

    CLR TEMP
    IN TEMP, TIMSK                // OC1A Interrupt is Enabled without disturbing other bits
    ORI TEMP, (1<<OCIE1A)
    OUT TIMSK, TEMP

 

    SER TEMP

 

    WAIT: RJMP WAIT

 

;--------------------------- Define ISR -------------------------------------------

 

TIMER1A_COMPA:

 

    IN R17, SREG                // Storing Status Register values in RAM
    PUSH R17

 

    LDI R17, 0X00                // Make Timer1 reads to zero
    OUT TCNT1H, R17
    OUT TCNT1L, R17

 

    IN TEMP, PORTA
    NEG TEMP                // All LED's on PORTA is TOGGLED each sec (approx.)
    OUT PORTA, TEMP

 

    POP R17                    // Put SREG values back
    OUT SREG, R17
    
    RETI                    // Returning from ISR

 

;---------------------------------------------- End of Program ----------------------------------------------------------

 

i checked the voltage across a LED.   It is read as 1.95V.... 

 

Thanking you

-

lenin

- Lenin Chakravarthy

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

As it's a mega32 is AVcc connected? You cannot use IO on PORTA without it. (don't think that it's only for ADC - it isn't).

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

Hi,

 

Thank u for your reply.

 

Yes, i 've connected AVcc and GND (with capacitor) to the power source.

 

After writing the modified program in mega32, the LED is continuously in ON condition. It is not blinking for every seconds.

- Lenin Chakravarthy

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

So, let us see the modified program.  And tell how your LED is connected.

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

Hi, thanks for ur reply.

 

LED Connection:
 

             PA0 is connected to GND through 330R resistor and an LED.. similarly from PA0-7, i connected LED's

 

Modified Program:

 

 

;--------------------------- Staring of Program------------------------------------

.include "m32Adef.inc"

.EQU OCR_Value = 15620

.DEF TEMP = R16

;----------------------------Initialize Interrupts---------------------------------

.CSEG
.ORG 0x0000 RJMP RESET
.ORG 0x000E RJMP TIMER1A_COMPA

;--------------------------- Main Program -------------------------------------------

.ORG 0X0030

RESET:
                    
    LDI TEMP, High(RAMEND)            // SET-UP STACK (MISSED)
    OUT SPH, TEMP
    LDI TEMP, Low(RAMEND)
    OUT SPL, TEMP    

 

    SEI                    // Enable the Globle Interrupt

 

    SER TEMP                
    OUT DDRA, TEMP                // Define the PORT Direction and Value
    OUT PORTA, TEMP        

 

    CLR R17                    // TIMER1 Initialization to ZERO
    OUT TCNT1H, R17
    OUT TCNT1L, R17

 

    LDI TEMP, 0x3D                // Set 0X3D04 (15620) in Output Compare 16-bit Register
    OUT OCR1AH, TEMP    
    LDI TEMP, 0x04                
    OUT OCR1AL, TEMP
    
    CLR TEMP
    LDI TEMP, (1<<OCF1A)            // Clear any pending interrupts
    OUT TIFR, TEMP  

             

    CLR TEMP
    LDI TEMP, (1<<CS10 | 1<<CS11 | 1<<WGM12)         // CTC MODE.4 + Prescale of 64 is set
    OUT TCCR1B, TEMP     

            

    CLR TEMP
    IN TEMP, TIMSK                // OC1A Interrupt is Enabled without disturbing other bits
    ORI TEMP, (1<<OCIE1A)
    OUT TIMSK, TEMP

 

    SER TEMP

 

    WAIT: RJMP WAIT

 

;--------------------------- Define ISR -------------------------------------------

 

TIMER1A_COMPA:

 

    IN R17, SREG                // Storing Status Register values in RAM
    PUSH R17

 

    LDI R17, 0X00                // Make Timer1 reads to zero
    OUT TCNT1H, R17
    OUT TCNT1L, R17

 

    IN TEMP, PORTA
    NEG TEMP                // All LED's on PORTA is TOGGLED each sec (approx.)
    OUT PORTA, TEMP

 

    POP R17                    // Put SREG values back
    OUT SREG, R17
    
    RETI                    // Returning from ISR

 

;---------------------------------------------- End of Program ----------------------------------------------------------

 

Have a nice day.

- Lenin Chakravarthy

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

 

The program works on my board with mega8 (after changing porta to portb).

Is AVCC connected to VCC ?

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

Hi,

       Thanks for trying my program :). I will also change porta to protb and try again.

 

I'm trying to do simulation of this program in avr studio. After entering into debugging mode (Tool: simulator), i clicked "Step Into" many times to reach " WAIT: RJMP WAIT " line in program.

 

Now the program waits there, till the Compare Match occurs (OCIE1A). For each Click on "Step Into", the "Stop Watch" in "Processor view" tab is incremented slowly (1 click = 1 usec).

 

Is there any way to modify the TCNT1 value near to 1 sec in debugging mode and check my program working or not?

 

Have good Day...

- Lenin Chakravarthy

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

Yes my both Vcc-GND and AVcc-GND ( with decoupling Capacitors ) are connected with power source...

- Lenin Chakravarthy

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

<< Is there any way to modify the TCNT1 value near to 1 sec in debugging mode and check my program working or not?

 

In the simulator put cursor to line " IN R17, SREG ".

Do Menu-Debug-Run to cursor".

Then you can step through ISR.      

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

The program works on my board with mega8 (after changing porta to portb).

Interesting, as OP was testing with active-high LED on PA0.  I looked at  the "toggling" code...

 

    IN TEMP, PORTA
    NEG TEMP                // All LED's on PORTA is TOGGLED each sec (approx.)
    OUT PORTA, TEMP

 

From the instruction set document

 

lNEG – Two’s Complement
Description:
Replaces the contents of register Rd with its two’s complement; the value $80 is left unchanged.

 

So, then, -1 => +1.  +1 => -1.

0xff => ox01.  0x01 => 0xff.

 

Isn't the low bit always on, as OP says?

 

If you don't want to do the dance to get EOR to work (needs another register), try COM.

 

lCOM – One’s Complement
Description:
This instruction performs a One’s Complement of register Rd.

 

 

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

theusch:

 

You are right !

I have tested on PB1.

Last Edited: Tue. Feb 24, 2015 - 05:06 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi, thanks for your reply.... yes it is working. SO i can check my ISR.

- Lenin Chakravarthy

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

I shifted the LED connection to PORTD & replaced NEG to COM..... and it is working very fine :):)... thanks to all

- Lenin Chakravarthy