I just accidentally burnt out my atmega168 and all i have around the house is an Attiny2313. I read that the 2313 does not have an ADC, but it does have an analog comparator, which I think it is just what I need for my project. I need to see if a specific voltage is above or below a reference voltage. Does any of you have an Arduio IDE code for that? i only use the Arduino IDE for programming Atmega s and I am not familiar with other languages. Basically, if a value is below another value, digitalWrite(ledPin, LOW) and viceversa, that is what I need.
The 2313 can do this, but not as an Arduino. AFAIK, that chip isn't supported. You can use AVR C/C++ instead though, which is what the Arduino libraries are built on. You'll have to use direct I/O instead of digitalWrite, you'll put your code in main() instead of loop(), and you'll have to initialize all the hardware stuff yourself. Not a big deal if you're comfortable with regular C development and can read the datasheet.
Download AVR Studio (free from Atmel's site) and follow some online AVR tutorials to get yourself started. Or just order a new ATmega chip.
Thank you very much, it was very kind of you to provide the solutions and the code. I will surely dig into it until a new Atmega168 arrives.
Let me tell you what I did with the last one. i accidentally connected a 470nF capacitor from VCC to ADC0. After that, i only get "Atmega not responding, check the connections and try again". I checked the connections, the progammers is ok, all I can think is that the Atmega168 burnt out consequently to the capacitor (which stayed there long enough before I figured it out). Is that possible?
claudiu_mirescu:
Thank you very much, it was very kind of you to provide the solutions and the code. I will surely dig into it until a new Atmega168 arrives.
The code is certainly a bit odd so I will understand if you have questions.
i accidentally connected a 470nF capacitor from VCC to ADC0. After that, i only get "Atmega not responding, check the connections and try again". I checked the connections, the progammers is ok, all I can think is that the Atmega168 burnt out consequently to the capacitor (which stayed there long enough before I figured it out). Is that possible?
/*
*/
//some chips use ana_comp_vect and others analog_comp_vect
#if defined(ANA_COMP_vect)
ISR(ANA_COMP_vect) {
#else
ISR(ANALOG_COMP_vect) {
#endif
//clear the flag
//_comp_isr_ptr(); //run user isr handler
//insert your code here
//example
if (ACSR & (1<<ACO)) {//aco asserted. do something}
else {//aco cleared. do something else}
}
//reset the comparator
void comp_init(void) {
//ain0/ain1 assumed to be input
//configure output pin
//configure the analog comparator
#if defined(DIDR)
DIDR = (1<<AIN1D) | //disable ain1's digital input
(1<<AIN0D) //disable ain0's digital input
;
#endif
#if defined(DIDR1)
DIDR1 = (1<<AIN1D) | //disable ain1's digital input
(1<<AIN0D) //disable ain0's digital input
;
#endif
//configure ACSRB if available
#if defined(ACSRB)
ACSRB &=~(0<<ACME); //disable acme
#endif
//configure ACSR
ACSR &=~(1<<ACIE); //disable interrupt
ACSR = (0<<ACD) | //analog comparator power on
#if defined(ACBG)
(0<<ACBG) | //bandgap reference not selected. Use AIN1 for non-inverting input
#endif
(1<<ACI) | //clear acif by writing '1' to it
(0<<ACIE) | //interrupt not yet enabled
(0<<ACIC) | //analog comparator input capture disabled
(0<<ACIS1) | (0<<ACIS0) //interrupt on output toggle
;
ACSR |= (1<<ACIE); //enable the interrupt
//rest comp_ptr
}
In your code, you would need to run comp_init() to initialize the analog comparator module. After that, the processing of ACO is done in the isr.
The code is written to run a variety of mcus and with user-installed isr so if you want, you can delete some of the stuff that's not relevant to your module.
Ok, thanks! Although I am not inititated in this language, I am willing to learn it because it looks like much more than Arduino.
My local shop only has Atmega8 in stock, the 168 will be received next week. Do any of you have the boards.txt file for the Atmega8? I found this on the internet:
...but I don't know if it is reliable, considering that 8MHz looks a bit large to me. Until now, except Arduino, I only used Atmega168 (the burnout one) and Attiny2313, at 1MHz, whithout external crystal. Could this board be ok or I might have signature problems at uploading? i am using uspasp programmer and Arduino 1.0.1.
Until now I only used the internal oscillator. I didn't use external because I didn't need it, the internal oscillator was enough for my projects. I purchased a crystal oscillator but I never used it.
1.. the Cap may have been charged to say 13volts, causing > 5v on the ADC 0
2.. if the Cap was not charged, the capacitor would slowly fill to the voltage rail, eg 5v
So does the board get any power, what still works with it? - a sudden voltage spike from a fully charged cap is the only thing that could have done any damage...
The cap was not charged, I just placed it between Vcc and ADC0, but I wanted to put it between ADC0 and GND. Not knowing what I have done, I connected the programmer and I got the message "Board not responding, check the connections." No matter how many times I checked, undid and did the connections, I still get this message. The board does nothing, even the previous sketch I uploaded on it does not work (a simple Blinky on pin 2 -Arduino pin, that is).
I hope the programmer is ok, it works on Attiny2313, so I think the usbasp is ok.
Is there any other way to test an Atmega168, other than upload the simplest sketch on it? It is not a big gap in the budget, but I want to know what caused the problem, in order to avoid it in the future.
Half of problem solved: I purchased my Atmega8 and it did not work with the boards file I posted earlier, so I had to set the board to Arduino NG/Atmega8 and upload the sketch by Upload by programmer option. It looks fine and it is a lot cheaper an Atmega168 that I initially purchased. i'll keep you posted.