Hi,
When loading same code in two different versions of ATMega168 I get different results:
Case 1:
Loading code in ATMega168P-20PU, i.e. DIP package - works and I get result shown on LCD (Both through Arduino and directly by ISP Dragon)
Case 2:
Loading same code in ATMega168V-10AU, i.e. TQFP32 package - Does not work at all and I get nothing shown on LCD (Both through Arduino and directly by ISP Dragon)
The code is as follows:
#include <LiquidCrystal.h>
#include <avr/io.h>
#include <avr/interrupt.h>
/******************** Analoga kanaler /
#define GV 0
#define SYS_V 1
#define C 2
#define B 3
#define A 4
#define REF 5
/*************************************/
#define ISR_TIMER1_COUNT 15625 // 16.0MHz clock / (prescaler = 1024)
#define lowBattLED 6 // digtal utgång för indikering av låg batteri
volatile int Values[6] = {
0,0,0,0,0,0};
volatile int seconds;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// A real time clock implemented based on
// 16bit Timer1 and the cpu's clock frequency. Since both the
// dedicated rtc and the this rtc are both dependent on temperature
// variations and since both are using crystals for their time source,
// the amount of error should be acceptible.
void setupTimer1(void)
{
// Setup Clear Timer on Compare Match mode. We should interrupt each
// time TCNT1 is equal to the value in OC1A register. TCNT1 will
// automaticall be reset to 0 each time a compare match happens.
// Because this is done in hardware, the interrupt frequency should
// be very stable (no interrupt service routine overhead to deal
// with).
// WGM13:0 = 4 for CTC on OCF1A value. Prescaler set to divide by 1024.
TCCR1A &= ~(1<<COM1A1) & // Clearing bits
~(1<<COM1A0) &
~(1<<COM1B1) &
~(1<<COM1B0) &
~(1<<WGM11) &
~(1<<WGM10);
TCCR1B &= ~(1<<ICNC1) & // Clearing bits
~(1<<ICES1) &
~(1<<WGM13) &
~(1<<CS11);
TCCR1B |= (1<<WGM12) | // Setting bits
(1<<CS12) |
(1<<CS10);
OCR1A = ISR_TIMER1_COUNT;
// OCIE1A interrupt flag set
TIMSK1 |= (1<<OCIE1A);
// Start counter at 0, not that it would matter much in this case...
TCNT1 = 0;
}
// This interrupt service routine gets called once per second
ISR(TIMER1_COMPA_vect)
{
seconds++;
}
void setup()
{
pinMode(lowBattLED, OUTPUT);
lcd.begin(16, 2);
Serial.begin(9600);
seconds = 0; // tiden börjar från 0
setupTimer1();
sei(); // allow interrupts globally
}
void loop()
{
volatile int a=0;
for (a=0;a<6;a++){
lcd_print(a);
}
lowBatt();
}
int readADC(int channel) {
analogReference(INTERNAL); // ATMegas interna ADC referens på 1,1 V. Ingångsspänning är delad i HW: 6v = 1,1 V
return map(analogRead(channel), 0, 1023, 0, 6000)/20*20; // minsta värdeändring blir 20 mV
}
void lcd_print(int kanal)
{
volatile int x=0,y=0;
volatile int a = readADC(kanal);
if (kanal == SYS_V && a >= 4800){ // avrunda systemspänning till 5 V i fall den avviker upp till 200 mV
a=5000;
}
switch (kanal){ // x och y är koordinater på LCD skärmen (översta linje: y=0, nedersta linje: y=1)
case GV: x=0; y=0; break;
case SYS_V: x=6; y=0; break;
case REF: x=12; y=0; break;
case C: x=0; y=1; break;
case B: x=6; y=1; break;
case A: x=12; y=1; break;
default: break;
}
lcd.setCursor(x,y);
lcd.print(" ");
lcd.setCursor(x,y);
lcd.print(a);
Values[kanal] = a;
if(seconds <=900) // GVREF inte aktiv i warmup perioden (900 sekunder = 15 minuter)
{
lcd.setCursor(12,0);
lcd.print(" ");
lcd.setCursor(12,0);
lcd.print("W-UP");
}
delay(50); // 50 ms paus. Minskar flimmer
}
void lowBatt()
{
if (readADC(6) <= 3000) {
digitalWrite(lowBattLED, HIGH); //LED ljuser om total batterispänning (4 st AA celler i serie) faller under 3 V
}
}
Any ideas why this is happening?