display temperature and humidity on lcd

Go To Last Post
59 posts / 0 new

Pages

Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

this code for displaying temperature and humidity and clock  on lcd. I use humidity sensor dh11 and temperature sensor lm35 connected to atmega 8. i displayed on lcd but there is oscillation in humidity and temperature (they are not constant on lcd) ,reading can appear randomly on lcd sometimes on different locations. and i don't know which one affect the other. i use lm35 to give high precision. Is the problem with declaring variable on lcd or it is a hardware problem? circuit attached

/*****************************************************

Chip type               : ATmega8
Program type            : Application
AVR Core Clock frequency: 8.000000 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 256
*****************************************************/
#include <mega8.h>
 #include <delay.h>
 #include <stdint.h>
 #include <stdlib.h>
  #include <stdio.h>
 #include <delay.h>
// Alphanumeric LCD functions
#include <alcd.h>
#define ADC_VREF_TYPE 0x00
// Read the AD conversion result
#define DHT11       //define DHT22 or DHT11

#define DHT_PORT        PORTC
#define DHT_DDR         DDRC
#define DHT_PIN         PINC
#define DHT_BIT         2

       float adc;
       char str[16]; //allocate on lcd
       int currentValue ;
        unsigned char buff [16];
        float h=0;
        float t=0;
        bit hum=0;
        uint8_t data[5]={0,0,0,0,0};  

// Declare your global variables here
     unsigned char second=0; //
     unsigned char minute=0; //ïåðåìåííàÿ äëÿ õðàíåíèÿ ìèíóò
     unsigned char hour=0; //ïåðåìåííàÿ äëÿ õðàíåíèÿ  ÷àñîâ
     char lcd_buffer[32];   //ïåðåìåííàÿ áóôåð  äëÿ âûâîäà íà äèñïëåé
// Timer1 overflow interrupt service routine
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
// Reinitialize Timer1 value
TCNT1 = 34286; // Preload timer with precalculated value
PORTC ^= (1 << 3); // Toggle the LED
  second ++;
    if ( second == 59) // Check if one minute has elapsed
    {
    second = 0; // Reset counter variable
    if (++minute==59)
     {minute = 0;
       if (++hour==2)
       {
        hour = 0;
       }
     }
    }

    lcd_clear(); //
    lcd_gotoxy(0,0);  // x=0 y=0
    sprintf(lcd_buffer,"%i:%i:%i",hour,minute,second); //
    lcd_puts(lcd_buffer);  // 

}
unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}

   //////////////////////////////////////////////////////////////////////////////////////////
int read_dht_hum()
{

unsigned char i,j;

//=============MCU send START
DHT_DDR|=(1<<DHT_BIT); //pin as output
DHT_PORT&=~(1<<DHT_BIT); //0
delay_ms(18);
DHT_PORT|=(1<<DHT_BIT); //1
DHT_DDR&=~(1<<DHT_BIT); //pin as input
//=============check DHT11 response
delay_us(50);
if (DHT_PIN&(1<<DHT_BIT))
    {
    return 0;
    }
delay_us(80);
if (!(DHT_PIN&(1<<DHT_BIT)))
    {
    return 0;
    }
//===============receive 40 data bits
while (DHT_PIN&(1<<DHT_BIT));
for (j=0; j<5; j++)
    {
    data[j]=0;
    for(i=0; i<8; i++)
        {
        while (!(DHT_PIN&(1<<DHT_BIT)));
        delay_us (30);
        if (DHT_PIN&(1<<DHT_BIT))
            data[j]|=1<<(7-i);
        while (DHT_PIN&(1<<DHT_BIT));
        }
    }

return 1;
//DHT11 data[0]-humidity, data[2]-temperature
//DHT22 data[0]-data[1]-humidity, data[2]-data[3]-temperature
}

void dht_read()
{
   hum=0;
       #asm("cli")
        hum = read_dht_hum();
       #asm("sei")
    if(hum==1)
    {
      #ifdef DHT22
     h = (float) data[0] * 256 + data[1];
     h = h/10;
     t = (float) data[2] * 256 + data[3];
     t = t/10;
     #else
     h =  data[0];
     t =  data[2];
     #endif
     }
}

void pr()
{
   dht_read();

  //  lcd_gotoxy(0,0);
  // sprintf(buff,"MEGA-AVR.COM.UA ");
   //lcd_puts(buff);
   lcd_gotoxy(0,1);
   #ifdef DHT22
   sprintf(buff,"DHT22 %.1f%C/%.1f%%  ",t,h);  //DHT22
   #else
   sprintf(buff,"H=%.1f%% ",h);//DHT11  //"T=%.1f C 

    #endif
       lcd_puts(buff);
       }
void main(void)
{

// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTB=0x00;
DDRB=0xFF;
// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTD=0x00;
DDRD=0xFF;

// ADC initialization
// ADC Clock frequency: 1000.000 kHz
// ADC Voltage Reference: AREF pin
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x83;
///////////
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 31.250 kHz
// Mode: Normal top=0xFFFF
// OC1A output: Disconnected
// OC1B output: Disconnected
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer Period: 1 s
// Timer1 Overflow Interrupt: On
TCCR1A=(0<<COM1A1) | (0<<COM1A0) | (0<<COM1B1) | (0<<COM1B0) | (0<<WGM11) | (0<<WGM10);
TCCR1B=(0<<ICNC1) | (0<<ICES1) | (0<<WGM13) | (0<<WGM12) | (1<<CS12) | (0<<CS11) | (0<<CS10);
TCNT1 = 34286; // Preload timer with precalculated value
////////////////

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=(0<<OCIE2) | (0<<TOIE2) | (0<<TICIE1) | (0<<OCIE1A) | (0<<OCIE1B) | (1<<TOIE1) | (0<<TOIE0);

// Global enable interrupts
#asm("sei")
  lcd_init(16);
      lcd_clear(); 

while (1)
{
     currentValue = read_adc(0); 

     lcd_gotoxy(8,1);
     lcd_putsf("T=");
    lcd_gotoxy(10,1);
     adc=currentValue/2.046;
     ftoa(adc, 0x02, str);
    lcd_puts(str);
     lcd_gotoxy(14,1);
    lcd_putchar(0xdf);//        //degree symbol
    lcd_gotoxy(15,1);
         lcd_putsf("C");
     pr();
  }

    }

 

Last Edited: Sat. Jan 20, 2018 - 09:49 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can’t access the lcd both from your isr and main(). Its like two people talking to you at once - who do you listen to?
I’d suggest you perform all lcd operations in the main line code.

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

I did all lcd operation in the main (while loop). i  displayed zero humidity(H) and there is oscillation on lcd in all results and the clock didn't count just display zero.

besides, this problem existed before i use the ISR. it was just humidity and temperature

Last Edited: Sat. Jan 20, 2018 - 10:53 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 2

It could be a number of things - both hardware and software. Your proteus sim doesn’t count as a schematic - if your hardware is built to that, then i’m not surprised you have problems. Stripback your code to the bare minimum and work through the problems logically. This stuff is complex, my job would be easy if i could wave a magic wand and solve problems.

Last Edited: Sat. Jan 20, 2018 - 11:08 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Are you using real sensors? Are you near the sensors when you see the changes? Are you breathing near the sensors?

 

If yes to these questions... place the two sensors inside a glass bottle and cover the top. Retest.

 

Ross McKenzie ValuSoft Melbourne Australia

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

engmado wrote:
i  displayed zero humidity(H) and there is oscillation on lcd in all results and the clock didn't count just display zero.

Use your Proteus simulator to step through your code, and examine your variables.

 

From that, you should be able to tell whether all of the above are just problems in the display, or if you have problems in reading your sensors.

 

It should be obvious that, if your sensor-reading code is giving "oscillation" on the results, then that is going to appear on the display!

 

 

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

engmado wrote:
the clock didn't count just display zero

Is this also your thread (although under a slightly different user name) on that very topic: http://www.avrfreaks.net/forum/digital-clock-doesnt-count-lcd

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

yes, i use real sensors. i isolated them, i tested each sensor separately with its code, the problem still exists.

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

yes, i created new project for just the clock to be able to detect the bug with the help of the forum friends and it worked. now i want to build the whole project for hunidity, temperature and clock.

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

engmado wrote:

...besides, this problem existed before i use the ISR. it was just humidity and temperature

 

A different problem may have existed before you had an ISR HOWEVER writing to an LCD from three different places will not work so change it.

'This forum helps those who help themselves.'

 

pragmatic  adjective dealing with things sensibly and realistically in a way that is based on practical rather than theoretical consideration.

Last Edited: Sat. Jan 20, 2018 - 12:13 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

engmado wrote:
now i want to build the whole project for hunidity, temperature and clock.

You have already build the clock part separately and had it working.

 

Now, as two separate small projects, build the temperature part and the humidity part. Then start putting things together.

 

Doing a big thing as a sequence of the smallest parts possible/reasonable is just common engineering practice.

 

Fault seeking becomes much simpler.

Asking questions here becomes much simpler, and you will get better answers.

 

Taking it all on as one big project is over-ambitious and will probably set you up for failure (see my sig/footer). A rewrite of that is "going for one big satisfying kaboom at the end is vain. Ir will only lead to one extended frustration. Seek many small satisfying moments along the way instead.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

Last Edited: Sat. Jan 20, 2018 - 12:19 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have already tested temperature code with lm35 and there is oscillation. i tested it with humidity sensor and its code and the problem exists too.

Last Edited: Sat. Jan 20, 2018 - 12:41 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

engmado wrote:
i tested each sensor separately with its code, the problem still exists.

So you need to debug each one separately and fix them so that they do work individually!

 

There is no point combining things that don't work separately - that will just give you a bigger non-working project!

 

Have you tried debugging the individual parts in the simulator?

If you get the same symptoms in the simulator, then that will help you to track down the causes.

 

Your ATMega8 does not have on-chip debug - so why did you choose this part?

Sorting out problems like this is so much easier when you can see inside the chip and watch what's going on ...

 

 

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

proteus doesn't show the problem. what chips that have on chip debug?

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

engmado wrote:
proteus doesn't show the problem

That, in itself, must give you a clue ...

 

what chips that have on chip debug?

Actually, most of them do - so it shouldn't take you long to look through suitable alternatives before you find one!

 

The easiest solution would be to just buy an XPlained Pro or Mini, as that comes with the debug adaptor built in - all for just a few dollars.

 

http://www.microchip.com/development-tools/xplained-boards-home

 

ATMegas specifically:

 

http://www.microchip.com/DevelopmentTools/ProductDetails.aspx?PartNO=ATMEGA324PB-XPRO

 

Xplained Mini range: http://www.microchip.com/Develop...

 

 

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

i have doubts of declaration of variables on lcd is wrong , i want you to assure that from your experience after that i will check hardware. 

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

engmado wrote:
i have doubts of declaration of variables on lcd is wrong

So what "doubts", exactly, do you have?

 

If you have doubts, whta have you done to resolve them?

 

You said the individual sensors don't work on their own - so why do you think it has anything to do with the LCD ?

 

Your profile says you joined here in 2012 (and the other account in 2011) - so you must have been doing this stuff for at least 6 years by now.

Shouldn't you have got the hang of the basics by now?

 

 

 

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

Oh-my-word! This clock stuff is highly suspicious, does your clock run fast ?

interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
// Reinitialize Timer1 value
TCNT1 = 34286; // Preload timer with precalculated value
PORTC ^= (1 << 3); // Toggle the LED
  second ++;
    if ( second == 59) // Check if one minute has elapsed
    {
    second = 0; // Reset counter variable
    if (++minute==59)
     {minute = 0;
       if (++hour==2)
       {
        hour = 0;
       }
     }
    }

    lcd_clear(); //
    lcd_gotoxy(0,0);  // x=0 y=0
    sprintf(lcd_buffer,"%i:%i:%i",hour,minute,second); //
    lcd_puts(lcd_buffer);  // 

}

Firstly incrementing of seconds (and also minutes) might be bugged:

  second ++;
    if ( second == 59) // Check if one minute has elapsed

Well that's wrong surely, perhaps you meant

    if (second++ == 59) // Check if one minute has elapsed

Others have already said so but it's worth repeating. You shouldn't update the LCD inside the ISR, that could create all sorts of confusion if the ISR gotoxy() interrupts an already executing lcd_puts().

Just comment out LCD time updates for the moment, at least until you get the rest of it working.

 

Last Edited: Sat. Jan 20, 2018 - 02:57 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

N.Winterbottom wrote:
Well that's wrong surely, perhaps you meant if (second++ == 59) // Check if one minute has elapsed ...

Style suggestion ... WHENEVER YOU CAN, use a "safer" comparison (in this case >=) versus ==.  Yes, if things never go amok it will make no difference.  But if e.g. "second" has a value of 77 for some reason (noise; stack overflow; rogue pointer; ...) I'd much have my clock show :77 for a glitch second, than have it never match at all.

 

Defensive coding usually costs nothing.

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.

Last Edited: Sat. Jan 20, 2018 - 03:14 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok,i updated lcd in the main and it keeps clear the lcd and display random counts

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

Just coming late to this but once again it looks like a project that has grown organically with bits being tacked on as you think of things. If you'd sketched out a complete design first and tested each part in isloation you likely wouldn't have the problems experienced now and you wouldn't have made the mistake of using non-reentrant from an ISR as you'd have all your display composition stuff as a low priority service in the main loop.

 

It may sound drastic but consider splitting it into distinct modules then re-implement the high level "glue" code that pulls it all together.

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

awneil wrote:

engmado wrote:
i have doubts of declaration of variables on lcd is wrong

So what "doubts", exactly, do you have?

 

If you have doubts, whta have you done to resolve them?

 

You said the individual sensors don't work on their own - so why do you think it has anything to do with the LCD ?

 

 

 

 


No i didnot ,i said i tried them sparatley and problem is still exists (post #13) so i have doubts.that's why i asked you for help. I am not doing this since 2012 it's a hoppy in my leasure time

Last Edited: Sat. Jan 20, 2018 - 04:20 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

awneil wrote:

You said the individual sensors don't work on their own

engmshh wrote:
No i didnot ,i said i tried them sparatley and problem is still exists

Then I'm afraid I don't understand what you mean by that, then!

 

To me, "problem is still exists" sounds like just another way of saying "doesn't work".

 

So perhaps you need to give a clear & detailed explanation of what, exactly you have tried and what you observed in each case.

 

However, to be honest, I think it'd be best to follow the advice from clawson (and others): stop, take a step back, and do a proper design.

 

Then tackle that design one step at a time - making sure you have everything fully tested & working before moving on to the next step.

 

And be sure to keep backups - so that you can always go back to the "last known working" state.

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

As has been suggested by several people:

 

Build your clock, (only a clock), and make it work reliably.

 

Then build a Temperature display, and make it work reliably.

 

Then build a Humidity display, and make it work reliably.

 

Once all three of those "small" projects work, then combine them.

 

You will likely get more help if you use that approach, as has been suggested by just about everyone who has replied on this Thread.

 

I doubt you will get much further assistance until you use the above approach.

 

Finally, hardware wise: Don't tie Vref to Vcc.

Just connect Vref to a 0.1 uF cap to ground.

 

JC

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

Sorry,you are right it is a reading error

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

Posts still seem to be chopping & changing between 2 usernames: engmshh and engmado.

Are there 2 different people, or are you just using 2 accounts?

 

 

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

I count 7 different IP addresses against those two accounts. Two yahoo email addresses.

Ross McKenzie ValuSoft Melbourne Australia

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

I called the lcd of ISR operation in the main loop but it doesn't count. however when i called lcd inside the ISR it counts.

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

That doesn’t give us much information. We can’t see what you’ve done with your code. Are you still using codevision?

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

engmado wrote:

I called the lcd of ISR operation in the main loop but it doesn't count. however when i called lcd inside the ISR it counts.

I'll bet that it is a variable used in main and the ISR code that needs to be declared volatile.  Post you latest code please.

"void transmigratus(void) {transmigratus();} // recursio infinitus" - larryvc

"It's much more practical to rely on the processing powers of the real debugger, i.e. the one between the keyboard and chair." - JW wek3

"When you arise in the morning think of what a privilege it is to be alive: to breathe, to think, to enjoy, to love." -  Marcus Aurelius

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

Sorry, i used facebook account to log in. I didnnot notice the difference. If you can unify the last comment user name

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

engmado wrote:
Sorry, i used facebook account to log in. I didnnot notice the difference. If you can unify the last comment user name

You could not use your Facebook account details to log in unless you had also registered a new account with those details! And no... with the current set of moderator tools I am unable to unify your posts. Maybe in a year or five.

 

Ross McKenzie ValuSoft Melbourne Australia

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

I still use codevision. this is just the clock code that didn't count

/*******************************************************

Chip type               : ATmega8
Program type            : Application
AVR Core Clock frequency: 8.000000 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 256
*******************************************************/

#include <mega8.h>
  #include <stdio.h>

// Alphanumeric LCD functions
#include <alcd.h>

// Declare your global variables here
 unsigned char second=0; //
     unsigned char minute=0; 
     unsigned char hour=0; 
     char lcd_buffer[32];   
// Declare your global variables here
// Timer1 overflow interrupt service routine

// Timer1 overflow interrupt service routine
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
// Reinitialize Timer1 value
TCNT1 = 34286; // Preload timer with precalculated value
PORTC ^= (1 << 3); // Toggle the LED
  
    if (++ second == 59) // Check if one minute has elapsed
    {
    second = 0; // Reset counter variable
    if (++minute==59)
     {minute = 0;
       if (++hour==2)
       {
        hour = 0;
       }
     }
    }

}

void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port B initialization
// Function: Bit7=In Bit6=In Bit5=In Bit4=In Bit3=In Bit2=In Bit1=In Bit0=In
DDRB=(0<<DDB7) | (0<<DDB6) | (0<<DDB5) | (0<<DDB4) | (0<<DDB3) | (0<<DDB2) | (0<<DDB1) | (1<<DDB0);
// State: Bit7=T Bit6=T Bit5=T Bit4=T Bit3=T Bit2=T Bit1=T Bit0=T
PORTB=(0<<PORTB7) | (0<<PORTB6) | (0<<PORTB5) | (0<<PORTB4) | (0<<PORTB3) | (0<<PORTB2) | (0<<PORTB1) | (0<<PORTB0);

// Port C initialization
// Function: Bit6=In Bit5=In Bit4=In Bit3=In Bit2=In Bit1=In Bit0=In
DDRC=(0<<DDC6) | (0<<DDC5) | (0<<DDC4) | (1<<DDC3) | (0<<DDC2) | (0<<DDC1) | (0<<DDC0);
// State: Bit6=T Bit5=T Bit4=T Bit3=T Bit2=T Bit1=T Bit0=T
PORTC=(0<<PORTC6) | (0<<PORTC5) | (0<<PORTC4) | (0<<PORTC3) | (0<<PORTC2) | (0<<PORTC1) | (0<<PORTC0);

// Port D initialization
// Function: Bit7=In Bit6=In Bit5=In Bit4=In Bit3=In Bit2=In Bit1=In Bit0=In
DDRD=(1<<DDD7) | (1<<DDD6) | (1<<DDD5) | (0<<DDD4) | (0<<DDD3) | (0<<DDD2) | (0<<DDD1) | (0<<DDD0);
// State: Bit7=T Bit6=T Bit5=T Bit4=T Bit3=T Bit2=T Bit1=T Bit0=T
PORTD=(0<<PORTD7) | (0<<PORTD6) | (0<<PORTD5) | (0<<PORTD4) | (0<<PORTD3) | (0<<PORTD2) | (0<<PORTD1) | (0<<PORTD0);
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 31.250 kHz
// Mode: Normal top=0xFFFF
// OC1A output: Disconnected
// OC1B output: Disconnected
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer Period: 1 s
// Timer1 Overflow Interrupt: On
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=(0<<COM1A1) | (0<<COM1A0) | (0<<COM1B1) | (0<<COM1B0) | (0<<WGM11) | (0<<WGM10);
TCCR1B=(0<<ICNC1) | (0<<ICES1) | (0<<WGM13) | (0<<WGM12) | (1<<CS12) | (0<<CS11) | (0<<CS10);
TCNT1 = 34286; // Preload timer with precalculated value

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=(0<<OCIE2) | (0<<TOIE2) | (0<<TICIE1) | (0<<OCIE1A) | (0<<OCIE1B) | (1<<TOIE1) | (0<<TOIE0);

// Global enable interrupts
#asm("sei")
  lcd_clear(); //
lcd_init(16);

while (1)
      {
    lcd_gotoxy(0,0);  // x=0 y=0
    sprintf(lcd_buffer,"%i:%i:%i",hour,minute,second); //
    lcd_puts(lcd_buffer);  //
      }
}

 

Last Edited: Sat. Jan 27, 2018 - 08:27 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is the classic volatile variable problem. (i.e. hour, minute second should be marked volatile) I really surprised you've not been bitten by this before.

 

There MUST be a sticky post about this - it comes up so very often. Are you going to search or do you want me to look for you ?

Last Edited: Sat. Jan 27, 2018 - 10:30 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

codevision doen't recognize volatile 

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

Codevision recognises volatile but iss not as agressive as GCC.
Of course a future Release might change CV behaviour. Hence it is always wise to write safe code innthe first place.
.
I might try your code on a Uno.
.
David.

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

engmado wrote:
codevision doen't recognize volatile

That's an ambiguous statement.

 

Do you get a compiling error?

Or is it that it has no effect?

 


 

david.prentice wrote:
Codevision recognises volatile but iss not as agressive as GCC.

Does CV not emit code to actually reload value every time it is accessed?

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
 voltaile unsigned char second=0; //
    voltaile   unsigned char minute=0;
     voltaile  unsigned char hour=0; 

this gives error 

 

Error: C:\Users\mado\Documents\digital clock using timer1 ext osc\t1.c(18): '(' expected

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

That is not how to spell volatile...

 

Ross McKenzie ValuSoft Melbourne Australia

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

Well,

 

  I ran your code on a Uno with CV v3.26 and minor changes e.g.

//#include <mega8.h>
#include <io.h>                 //always wiser to use generic header
...
volatile unsigned char second = 0;       //
volatile unsigned char minute = 0;
volatile unsigned char hour = 0;
...
//    TCNT1 = 34286;              // Preload timer with precalculated value
    TCNT1H = 34286 >> 8;        //use separate 8-bit assignments
    TCNT1L = 34286 & 0xFF;
...
    // Timer(s)/Counter(s) Interrupt(s) initialization
//    TIMSK = (0<<OCIE2)|(0<<TOIE2)|(0<<TICIE1)|(0<<OCIE1A)|(0<<OCIE1B)|(1<<TOIE1)|(0<<TOIE0);
    TIMSK1 = (1<<TOIE1);        //328P has separate TIMSKn registers

(and moved your blink LED to PB5 because my LCD backlight was on PC3)

TCNT1 is in LDS/STS memory.   So you have to use separate H/L assignments.

TIMSK1 is a separate register on the 328P

 

It all seemed to operate ok.

 

I suggest that you run your program on Proteus if your school has a Proteus licence.

 

When you get it working ok on your hardware,  come back.    There is a better way to operate a Timer.

 

David.

Last Edited: Sat. Jan 27, 2018 - 12:31 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

s:voltaile:volatile:

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

engmado wrote:

 voltaile unsigned char second=0; //
    voltaile   unsigned char minute=0;
     voltaile  unsigned char hour=0; 

this gives error 

 

Error: C:\Users\mado\Documents\digital clock using timer1 ext osc\t1.c(18): '(' expected

That is just sloppy, letting your hands down, not being careful with details and not checking the obvious - a spelling error. You shouldn't bother people with such trivialities.

 

Read slowly - check every single character against some reference material on the web, e.g. Wikipedia or perhaps a dictionary of with a good reputation like Merriam-Websters (which also gives you a sound track on how it can be pronounced - something that might help with learning and remembering the spelling).

 

(Doesn't CodeVision do syntax highlighting?)

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

Last Edited: Sat. Jan 27, 2018 - 03:10 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I tried volatile and it didn't count 

/*****************************************************

Chip type               : ATmega8
Program type            : Application
AVR Core Clock frequency: 8.000000 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 256
*****************************************************/
#include <mega8.h>
#include <delay.h>
#include <stdint.h>
#include <stdlib.h>   
#include <stdio.h>   
#include <io.h>       
#include <delay.h>
// Alphanumeric LCD functions
#include <alcd.h>

       
        
// Declare your global variables here
     volatile unsigned char second=0; //
    volatile unsigned char minute=0; //ïåðåìåííàÿ äëÿ õðàíåíèÿ ìèíóò
    volatile unsigned char hour=0; //ïåðåìåííàÿ äëÿ õðàíåíèÿ  ÷àñîâ 
     char lcd_buffer[32];   //ïåðåìåííàÿ áóôåð  äëÿ âûâîäà íà äèñïëåé
// Timer1 overflow interrupt service routine
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
// Reinitialize Timer1 value
TCNT1 = 34286; // Preload timer with precalculated value
TCNT1H=34286 >>8;
TCNT1H=34286 &0xFF;

PORTC ^= (1 << 3); // Toggle the LED

    if (second++ == 59) // Check if one minute has elapsed
    {
    second = 0; // Reset counter variable
    if (minute++==59)
     {minute = 0;  
       if (hour++==2)
       {
        hour = 0;
       }
     }
    }



} 

void main(void)
{

// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out 
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0 
PORTB=0x00;
DDRB=0xFF;
// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTC=0x00;
DDRC=(1<<3);
// Port D initialization// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out 
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0 
PORTD=0x00;
DDRD=0xFF;

// ADC initialization
// ADC Clock frequency: 1000.000 kHz
// ADC Voltage Reference: AREF pin
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x83;
///////////
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 31.250 kHz
// Mode: Normal top=0xFFFF
// OC1A output: Disconnected
// OC1B output: Disconnected
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer Period: 1 s
// Timer1 Overflow Interrupt: On
TCCR1A=(0<<COM1A1) | (0<<COM1A0) | (0<<COM1B1) | (0<<COM1B0) | (0<<WGM11) | (0<<WGM10);
TCCR1B=(0<<ICNC1) | (0<<ICES1) | (0<<WGM13) | (0<<WGM12) | (1<<CS12) | (0<<CS11) | (0<<CS10);
TCNT1 = 34286; // Preload timer with precalculated value
////////////////

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=(0<<OCIE2) | (0<<TOIE2) | (0<<TICIE1) | (0<<OCIE1A) | (0<<OCIE1B) | (1<<TOIE1) | (0<<TOIE0);


// Global enable interrupts
#asm("sei")
  lcd_init(16); 
      lcd_clear(); 

while (1)
{
    
    
    lcd_gotoxy(0,0);  // x=0 y=0
    sprintf(lcd_buffer,"%i:%i:%i",hour,minute,second); //
    lcd_puts(lcd_buffer);  // 
    }

    }  
    

 

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

Do you ever read through the code you write, or is the fact that you got it to compile good enough ?

 

1. Anyway there's more suspicious code here on TCNT1 & TCNT1H that wasn't in the original. This needs fixing up.

// Timer1 overflow interrupt service routine
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
    // Reinitialize Timer1 value
    TCNT1 = 34286; // Preload timer with precalculated value
    TCNT1H = 34286 >> 8;
    TCNT1H = 34286 & 0xFF;

2. You tell us the RTC doesn't count. Is the ISR actually running ? I.e. Does the LED on PORTC flash ?

 

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

The mega8 has TCNT1 in IN/OUT space.   CV can treat it as 16-bit.

The mega328 has TCNT1 in LDS/STS space.   CV needs to handle as two 8-bit values.

 

I created a CV project in AS7.   Selected ATmega328P.   Edited for the 328/8 register differences.   Configured for my LCD wiring on PD4-PD7, PC0-PC2.

 

The project builds and runs as expected on a real-life LCD and LED.    I can't see where the OP should have any problem.

 

It looks as if the OP has a Proteus licence.    Proteus should Simulate perfectly.   The OP would see the time on the LCD,  the LED blinking.

 

We don't know the CV version number.    But CV v2.x.x has been available for about 10 years.   CV v3.x.x for about 4 years.    (from memory.  I have not checked the version history)

I would expect this program to work with any CV version that has the "alcd.h" library.

 

David.

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

Actually thinking about it now -  Perhaps  the final 2 writes to TCNT1H will have no effect. They only write to a holding register don't they.

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

no it doesn't run, no led blinking, it was working i don't know what happened.

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

You have spent a week on this subject. I ran your code from #34 as described in #41.
Please take 10 minutes to report your actual results e.g.
1. Does LED light?
2. Does LCD display 0:0:0
3. Does Proteus work?
4. What CV version?
5. What AS7 version?
.
It is always wise to test hardware at the start of any program e.g. lcd_print("Hello lcd");
Likewise blink your LED once.
.
David.

Last Edited: Sun. Jan 28, 2018 - 08:06 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I tried to read just only the temperature using the code  and there is oscillation of temperature on lcd. when i replaced the sensor with potentiomter it doesn't oscillate so I think the problem in interfacing sensor lm35 to the chip. what is best design to prevent this oscillation.

Last Edited: Wed. Feb 7, 2018 - 12:20 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Google the term "hysteresis" or possibly "running average"

Pages