Atmega16 interrupts

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

I am new to programming. Can you help me to program the interrupts INT0 and INT1 for transmission and receiving in ATmega16? I am using codevisionavr for this. 

Ritwisha Mukherjee

Last Edited: Tue. Apr 6, 2021 - 10:03 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Ritwisha18 wrote:

I am new to programming. Can you help me to program the interrupts INT0 and INT1 for transmission and receiving in ATmega16? I am using codevisionavr for this. 

 

You wouldn't normally use those for transmission and reception.

 

Please explain EXACTLY what you are trying to do.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

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

I need to create a transmission and receiving port via INT0 and INT1 in atmega16. It would be working similar to PD0(RXD) and PD1(TXD) which are used for data transmission using usart. I cannot use these PD0 and PD1 or the MOSI, MISO pins for data transferring since they have already been into use. hence I am left with these INT0 and INT1 pins to use for transmission and receiving. 
Also, can you refer me to any site which can guide me from the very scratch about working with interrupts?

Ritwisha Mukherjee

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

So you need to have two USARTs running at the same time?

 

What baud rates do they each run at?

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

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

It would be working similar to PD0(RXD) and PD1(TXD) 

You mean you want another UART?

 

Google "soft UART" or "bit-banged UART"

 

Could you not just use a chip which has sufficient UARTs for your project? It's not like they are hard to find these days.

 

Does it actually need to be a UART, or could it be something like I2C ?

 

I2C can also be bit-banged interface?

 

 

I cannot use ... MOSI, MISO pins for data transferring since they have already been into use.

You realise that SPI is designed to work as a bus; ie, to have many devices on it ?

 

 

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

why is this in tutorials ?  (please move)

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


That's what the 'Report' button is for:

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Download the AVR App Note AVR136 for how to do soft-USART.  But as others have pointed out, the project should have identified the need for two usarts and selected an AVR model that has the needed peripherals.

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

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

Have you considered mega164p? It's like an upgraded mega16 and one if the upgrades us a second UART.

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

awneil wrote:

It would be working similar to PD0(RXD) and PD1(TXD) 

Do you mean you want another UART?

 

Google "soft UART" or "bit-banged UART"

 

Could you not just use a chip that has sufficient UARTs for your project? It's not like they are hard to find these days.

 

Does it actually need to be a UART, or could it be something like I2C?

 

I2C can also be a bit-banged interface?

 

 

I cannot use ... MOSI, MISO pins for data transferring since they have already been into use.

You realize that SPI is designed to work as a bus; ie, to have many devices on it?

 

 

 

 

I am working on a project which has been done initially with ATmega16 and I am trying to do this without changing the microcontroller else I will have to redo the entire one.

I am seeking whether this can be done somehow without implementing another UART.

I have figured out the process partially: 

  • enable interrupt for INT0
  • disable interrupt to restrict further incoming of interrupts
  • start timer (which I am not sure how to use)
  • start a loop which will run till 8 bits of data incoming as input and store it.
  • stop the timer and enable interrupt again

I think this can be implemented somehow but I am not sure how to proceed. 

 

                                                      

Ritwisha Mukherjee

Last Edited: Thu. Apr 8, 2021 - 07:08 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ritwisha18 wrote:
I am trying to do this without changing the microcontroller else I will have to redo the entire one.

Not necessarily: find a "close relative" AVR[1], and you can re-use most - if not all - of the code.

 

Doing bit-bang async comms - especially receive - require careful timing.

That may well impact on your existing code - so you may well end up having to re-write anyhow.

 

 

Quote:
I am seeking whether this can be done somehow without implementing another UART.

I have figured out the process partially: 

  • enable interrupt for INT0
  • disable interrupt to restrict further incoming of interrupts
  • start timer (which I am not sure how to use)
  • start a loop which will run till 8 bits of data incoming as input and store it.
  • stop the timer and enable interrupt again

I think this can be implemented somehow but I am not sure how to proceed. 

Well, that is implementing a UART!

 

EDIT

 

[1] eg, as clawson suggested.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Thu. Apr 8, 2021 - 07:46 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ritwisha18 wrote:
I am seeking whether this (sic) can be done somehow without implementing another UART.

You still haven't given any details of what, exactly, "this" is.

 

  1. Is it a UART interface (ie, async serial) that you require?
  2. Full duplex, half duplex, or simplex?
  3. Are other interfaces available - I2C, SPI, etc? You can get "bridge" chips between I2C/SPI and UART...
  4. etc, ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

clawson wrote:
Have you considered mega164p? It's like an upgraded mega16 and one if the upgrades us a second UART.

Not only is it pin compatible, but the second USART is on the int0/int1 pins just as the OP needs!!!!

swapping AVR's looks like a win/win here.

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

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

Can you help me implementing this software uart for Atmega16L?
 

 

Ritwisha Mukherjee

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

Ritwisha18 wrote:

Can you help me implementing this software uart for Atmega16L?

 

What isn't working for you?

 

If I put "AVR software uart" into a search engine I get lots of hits. Which of the solutions have you tried?

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

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

I just got the overall theoretical concept of implementing it also I have found some codes for softuart in other forums and Github too.

it's just the working of the c program is not clear to me especially the part of how registers are storing input or transmitting 8-bit data. also, the part where I need to stop for 1.5 periods after sending the start bit. 

 

Ritwisha Mukherjee

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

Ritwisha18 wrote:

it's just the working of the c program is not clear to me especially the part of how registers are storing input or transmitting 8-bit data. also, the part where I need to stop for 1.5 periods after sending the start bit. 

Give a link to the Github/forums where you have found code then.

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

Ritwisha18 wrote:

...also, the part where I need to stop for 1.5 periods after sending the start bit. 

 

You need to sit down with a piece of paper and draw some picture. But I'll give you a starting point...

 

You receive an interrupt on the falling edge of the start bit. How long do you need to wait before you sample the first data bit?

 

Also, have you looked at Atmel Application Note AVR304?

Attachment(s): 

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

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

1. https://os.mbed.com/forum/mbed/t...

2. https://github.com/blalor/avr-so...

the above are the previous ones

 

and while further searching, I think I found something good and I am going through it currently
https://www.avrfreaks.net/forum/...

 

I understood the application note, all the theoretical part is clear to me rather the implementation in c I am not understanding.

 

Ritwisha Mukherjee

Last Edited: Tue. Apr 13, 2021 - 08:14 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Number 2 is very good because it can adapt to a lot of different models of AVR. See line 8 onward in softuart.h but in providing this added utility it kind of makes the code harder to follow as when you come across code like:

ISR(SOFTUART_T_COMP_LABEL)
{

you have to go back and look up what SOFTUART_T_COMP_LABEL actually means. If it were a Tiny85 this would be TIM0_COMPA_vect while for a Mega644P it would be TIMER0_COMPA_vect and so on. That makes it harder to read/follow the code. So while it looks like a good solution I would leave that for now.

 

Number 1 is simpler because it aims to be "generic". Rather than including code for a specific CPU (the fact that it is on an MBED site suggests ARM rather than AVR already!) it includes a number of "stub functions" that you have the responsibility of implementing. So the code does things like:

 

               set_tx_pin_high();

 

but above that it just has:

 

               // 2. set_tx_pin_high()
               //    Sets the transmit pin to the high state.
               #define set_tx_pin_high()

 

so by default these functions don't exist. It's up to you to write them. If you had wanted to use bit 3 of PORTD (say) then your version of this would be something like:

void set_tx_pin_high() {
    PORTD |= (1 << 3);
}

and similarly you have to write implementations of all:

 

// Interface routines required:
// 1. get_rx_pin_status()
//    Returns 0 or 1 dependent on whether the receive pin is high or low.
#define get_rx_pin_status()     
// 2. set_tx_pin_high()
//    Sets the transmit pin to the high state.
#define set_tx_pin_high()
// 3. set_tx_pin_low()
//    Sets the transmit pin to the low state.
#define set_tx_pin_high()
// 4. idle()
#define idle
//    Background functions to execute while waiting for input.
// 5. timer_set( BAUD_RATE )
//    Sets the timer to 3 times the baud rate.
#define timer_set( BAUD_RATE ) 
// 6. set_timer_interrupt( timer_isr )
//    Enables the timer interrupt.
#define set_timer_interrupt( timer_isr )

 

Most of those are simple/obvious except for the last one. About half the code they present if a function called time_isr() but clearly the intention is that you set yup a timer interrupt and then call to that code from within the ISR.

 

To be honest I wonder if (2) might be easier for you to handle? The only thing is that while softuart.h has variants for tiny25/45/85 and mega162/324/644P it does not have code for mega16.

 

Perhaps you should look in the "Projects" section of this website. There are soft UART projects there and there's a pretty strong chance you will find at least one that already caters for mega16.

 

Or you could simply take my advice in post #9 above and use a mega164P in place of mega16 and you will already have TWO serial ports !

Last Edited: Tue. Apr 13, 2021 - 08:28 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ritwisha18 wrote:
I understood the application note, all the theoretical part is clear to me rather the implementation in c I am not understanding.

You do know the app notes come with code examples!

 

Jim

edit: link

https://www.microchip.com/wwwApp...

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

Last Edited: Tue. Apr 13, 2021 - 02:33 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thank you soo much for clarifying it. 

 

Ritwisha Mukherjee