Atmega328p hang issue

Go To Last Post
133 posts / 0 new

Pages

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

Dear All,

 

I am using atmega328p controller to toggle the light. But controller is getting hand after every 2 days. I am not getting any response from the controller. Once i reset the controller it is working fine then.

 

Please look at my program and help with the solution.

 

I am operating two lights with this controller. Program written for three lights. 

 

Here is my program.

 

/*
 * uart atmega328.c
 *
 * Created: 3/23/2019 10:25:20 AM
 * Author : ramkumar.d
 */ 
#define F_CPU 16000000UL
#include <avr/io.h>
#include <stdio.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#define BAUDRATE 9600
#define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1)

/*#define relay1 PORTC0
#define relay2 PORTC1
#define relay3 PORTC2
*/

/*#define exrelay1 
#define exrelay2
#define Buzz*/ 

//Declaration of our functions

void USART_init(void);
unsigned char USART_receive(void);
void USART_send( unsigned char data);
void USART_putstring(char* StringPtr);
void USART_Flush(void);
char String[]="Hello world!!" , ch , i= 0 , C = 0, I = 0;
unsigned int count = 0 , sec = 0;
int main(void)
{
    DDRC = 0b00000111;
    DDRB = 0B00000000; 
    PORTC |= 0X20;    
    PORTB = 0X0F;
    PORTC |= (1 << 1); // Pin 1 goes low
    PORTC &= ~(1 << 0); // Pin 0 goes low
    PORTC |= (1 << 2); // Pin 2 goes low     
//    PORTC &= 0b00111111;// configuring pin as input
    
    USART_init();        //Call the USART initialization code
    //PORTC5 = 0;
    while(1)         //Infinite loop
    {
        _delay_ms(10);        // delay 10 ms
        
        ch = USART_receive();   // waiting for the character
        
        if((ch == 'R')||(ch == 'r'))   // u/U means boom UP
        {
        
            C = I = 0;
            PORTC &= ~(1 << 0); // Pin n goes high
            PORTC |= (1 << 1); // Pin 1 goes low
            PORTC |= (1 << 2); // Pin 2 goes low
            USART_putstring("ROK\n");
        }
        else if((ch == 'g')||(ch == 'G')) // d/D means boom DOWN
        {
            C = I = 0;
            PORTC &= ~(1 << 1); // Pin 1 goes high
            PORTC |= (1 << 0); // Pin 0 goes low
            PORTC |= (1 << 2); // Pin 2 goes low
            USART_putstring("GOK\n");
        }
        else if((ch == 'o')||(ch == 'O'))  //o/O means Optional third relay
        {
            C = I = 0;
            PORTC &= ~(1 << 2); // Pin 2 goes high *optional
             PORTC |= (1 << 0);  // Pin 0 goes low
            PORTC |= (1 << 1);  // Pin 1 goes low
            USART_putstring("oOK\n");
        }
        else if((ch == 'f')||(ch == 'F'))
        {
            C = I = 0;
            PORTC |= (1 << 0); // Pin 0 goes low
            PORTC |= (1 << 1); // Pin 1 goes low
            PORTC |= (1 << 2); // Pin 2 goes low
            USART_putstring("FOK\n");
        }        
        
        
        count++;
        if(count == 400)
        {
            count = 0;
            USART_putstring("SOK\n");
            if(C)
            {
                USART_putstring("SIOK\n");
                USART_Flush();        // clears the buffer
                C=0;
            }
            USART_Flush(); 
        }
        if(!(PINC & 0b00100000))   // waiting for vehicle to cross the IR sensor
        {
            C = 1;
            //USART_putstring("COK\n");
            USART_Flush();        // clears the buffer
            //_delay_ms(2000);     // delay 2 seconds
            
        }
        USART_Flush();    // clears the buffer

     }
     return 0;
}
void USART_init()
{
    
    UBRR0H = (uint8_t)(BAUD_PRESCALLER>>8);  // baud rate register
    UBRR0L = (uint8_t)(BAUD_PRESCALLER);     // baud rate register
    UCSR0B = (1<<RXEN0)|(1<<TXEN0);          // transmitter and receiver enable
    UCSR0C = (3<<UCSZ00) | (1<<UCSZ01);      // frame format: 8data , 2stop bit
}
unsigned char USART_receive()
{
    
    while(!(UCSR0A & (1<<RXC0))) return;
    return UDR0;
    
}
void USART_Flush()
{
    unsigned char dummy;
    while ( UCSR0A & (1<<RXC0) ) dummy = UDR0;
}
void USART_send( unsigned char data){
    
    while(!(UCSR0A & (1<<UDRE0)));
    UDR0 = data;
    
}
void USART_putstring(char* StringPtr)
{
    while(*StringPtr != 0x00)
    {
        USART_send(*StringPtr);
        StringPtr++;
    }
    
}

 

 

Regards,

Ram.

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

Comments like:

        if((ch == 'R')||(ch == 'r'))   // u/U means boom UP
       else if((ch == 'g')||(ch == 'G')) // d/D means boom DOWN

do not make this code easy to follow. Presumably this used to be U/D and was later changed to R/G but the comment was not updated?

 

Anyway I don't immediately see anything there that would explain the hang up. Usually something like a hang up that takes 1-2 days to reach would suggest something like a memory leak but there's nothing like that in the code.

 

So is this circuit in an electrically noisy environment (high voltage switching etc)? If so what is done to protect against electrical noise? Even if it isn't do you have a stable PSU, decoupling capacitors and so on?

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

 

ram kumar wrote:
unsigned char USART_receive()
{
    
    while(!(UCSR0A & (1<<RXC0))) return;
    return UDR0;
    
}

I don't know how you think you managed to get this to compile

" error: ‘return’ with no value, in function returning non-void"

 

Last Edited: Tue. Aug 13, 2019 - 09:51 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

 

 

Three things are happening here

 

1. No response.

2. Garbage data.

3. when i press any letter in the input it is giving all outouts

 

like input ---> 'r'

 

output ---> ROK

                       GOK 
                              oOK

 

                                     FOK

after that if i press reset witch it is working fine

 

 

I am not at all getting what is happening here.

 

Is it a program issue or hardware issue. Actually we have designed our own circuit. 

 

Regards,

Ram.

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

Hi

MrKendo wrote:

 

ram kumar wrote:
unsigned char USART_receive()
{
    
    while(!(UCSR0A & (1<<RXC0))) return;
    return UDR0;
    
}

I don't know how you think you managed to get this to compile

" error: ‘return’ with no value, in function returning non-void"

 

 

It is working fine in my atmel. No errors it is getting. Can you check it again. 

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

Surely the point is that you don't want USART_receive() to return until a character becomes available? As Mr Kendo says if you use:

unsigned char USART_receive()
{
    
    while(!(UCSR0A & (1<<RXC0))) return;
    return UDR0;
    
}

then when you build this you are warned:

		.././main.c: In function 'USART_receive':
D:\test\test\main.c(6,30): warning: 'return' with no value, in function returning non-void
		  while(!(UCSR0A & (1<<RXC0))) return;
		                              ^

But while you could correct this by maybe changing "return;" to be "return 0;" or something would you really want to do this? Back in main() you call this function as:

        ch = USART_receive();   // waiting for the character
        
        if((ch == 'R')||(ch == 'r'))   // u/U means boom UP
        {
        
            C = I = 0;
            PORTC &= ~(1 << 0); // Pin n goes high
            PORTC |= (1 << 1); // Pin 1 goes low
            PORTC |= (1 << 2); // Pin 2 goes low
            USART_putstring("ROK\n");
        }
        else if((ch == 'g')||(ch == 'G')) // d/D means boom DOWN
        {
            C = I = 0;
            PORTC &= ~(1 << 1); // Pin 1 goes high
            PORTC |= (1 << 0); // Pin 0 goes low
            PORTC |= (1 << 2); // Pin 2 goes low
            USART_putstring("GOK\n");
        }
        else if((ch == 'o')||(ch == 'O'))  //o/O means Optional third relay
        {
            C = I = 0;
            PORTC &= ~(1 << 2); // Pin 2 goes high *optional
             PORTC |= (1 << 0);  // Pin 0 goes low
            PORTC |= (1 << 1);  // Pin 1 goes low
            USART_putstring("oOK\n");
        }
        else if((ch == 'f')||(ch == 'F'))
        {
            C = I = 0;
            PORTC |= (1 << 0); // Pin 0 goes low
            PORTC |= (1 << 1); // Pin 1 goes low
            PORTC |= (1 << 2); // Pin 2 goes low
            USART_putstring("FOK\n");
        }        
        
        
        count++;
        if(count == 400)
        {
...

So if you do just return while there is no character then it won't match any of the if()/else if() so it will fall straight through to the count++, is that really what you intend? A "normal" USART_receive() looks more like:

unsigned char USART_receive()
{
    
    while(!(UCSR0A & (1<<RXC0))); // just wait here until RXC is set
    return UDR0;
    
}

 

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

ram kumar wrote:

It is working fine in my atmel. No errors it is getting. Can you check it again. 

OK it's because of different options passed to the compiler.

I was using -std=c99 -pedantic-errors, I think it's the -pedantic-errors which in ths case causes gcc to emit error and not bother trying to go any further and produce any output.

Without it, I get warning like clawson showed above, and it does continue and actually produce an output.

I think you will get returned whatever happens to be in r24 at the time.

But either way, the code is bad!

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

Hi clawson,

 

Thanks for your reply.

 

Okay i will changes like this

 

if(ch = USART_receive()!= 0 )

{

 

////code

 

 

}

 

in uart_receive i will put 'return 0'

 

Is it the reason for hang?

 

Actually i am using the same code for two different lights.

 

1st case : AC Lights  

2nd case : DC Lights

 

I am getting issue in the first case only. I do not know why.

 

DC lights are working great. But coming to the AC lights i have to reset the controller for many time to make it alive. 

 

 

 

Regards,

Ram.

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

MrKendo wrote:

ram kumar wrote:

It is working fine in my atmel. No errors it is getting. Can you check it again. 

OK it's because of different options passed to the compiler.

I was using -std=c99 -pedantic-errors, I think it's the -pedantic-errors which in ths case causes gcc to emit error and not bother trying to go any further and produce any output.

Without it, I get warning like clawson showed above, and it does continue and actually produce an output.

I think you will get returned whatever happens to be in r24 at the time.

But either way, the code is bad!

 

 

 

Thanks for your reply.

 

Can you suggest me what changes i need to do in my code. I am unable to solve the issues.

 

Regards,

Ram.

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

ram kumar wrote:
Okay i will changes like this
Why would you do it like that? Do you really need to continue on into the rest of the code if no character is immediately available?

 

Even if you do I'd split it into two functions such as UART_is_char_available() and UART_Receive. Then the calling code would have a structure more like:

while(1) {
    if (UART_is_char_available) {
        ch = UART_receive();
        // handle 'ch'
    }
    do_other_stuff();
}

That will be much clearer to the reader/maintainer of this code as to what is going on. They don't need to know that UART_receive() has a special return value meaning "nothing was available". The UART_is_char_available() function basically just reports back the state of the RXC flag.

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

Much will depend on the actual build options, but when I build with:

$ avr-gcc -W -Wall -O 1 -g -mmcu=atmega328p foo.c -o foo.elf

... I get:

$ avr-objdump -S foo.elf

.
.
.

unsigned char USART_receive()
{
  while(!(UCSR0A & (1<<RXC0))) return;
  be:	80 91 c0 00 	lds	r24, 0x00C0	; 0x8000c0 <__TEXT_REGION_LENGTH__+0x7e00c0>
  c2:	88 23       	and	r24, r24
  c4:	1c f4       	brge	.+6      	; 0xcc <USART_receive+0xe>
  return UDR0;
  c6:	80 91 c6 00 	lds	r24, 0x00C6	; 0x8000c6 <__TEXT_REGION_LENGTH__+0x7e00c6>
  ca:	08 95       	ret
}
  cc:	08 95       	ret

Note that this almost certainly does not do what you think it does.  It tests UCSR0A once, and if RXC0 is 0, it returns without an explicit return value.  Something >>is<< returned, however.  The register r24 is in fact the return value, so whatever was in UCSR0A is returned.  That is likely to be either 0x20 (if UDRE0 is set) which is an ASCII space, or 0x00, which is ASCII NULL.

 

If RXC0 is set, then the value returned will be that of UDR0, the received byte.

 

In your case, this shouldn't matter, but it still a pretty bad idea.  You cannot control how the compiler will handle a return with no value.  I've only shown one way that it can.  Changing compile options could easily change the correctness of the program.

 

 

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

ram kumar wrote:
 count++;
        if(count == 400)
        {

I would change this to:

 count++;
if(count >= 400)
{
    ......

Testing for an "=" here can be missed and will take a long time for count to roll over and possibly be "equal" again, better to look for equal or greater.

I don't think this is your problem, but a good habit to get into. 

 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

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

ram kumar wrote:

1st case : AC Lights  

2nd case : DC Lights

 

I am getting issue in the first case only. I do not know why.

Wall warts can be a bane.

If BOD is disabled then mega328P may become anomalous.

 

"Dare to be naïve." - Buckminster Fuller

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

gchapman wrote:

ram kumar wrote:

1st case : AC Lights  

2nd case : DC Lights

 

I am getting issue in the first case only. I do not know why.

Wall warts can be a bane.

If BOD is disabled then mega328P may become anomalous.

 

can you give me more info! that would be great 

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

ki0bk wrote:

ram kumar wrote:
 count++;
        if(count == 400)
        {

I would change this to:

 count++;
if(count >= 400)
{
    ......

Testing for an "=" here can be missed and will take a long time for count to roll over and possibly be "equal" again, better to look for equal or greater.

I don't think this is your problem, but a good habit to get into. 

 

Jim

 

 

Yes you are correct. I will change this one. Thank you

 

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


The quality and lifetime of wall warts varies (leakage, noise, transients, instability (oscillates, exceeds tolerance), over-voltage, warranty and/or US PC cycle duration of 3 years, lightning tolerance)

 

re BOD :

via ATmega48A, ATmega48PA, ATmega88A, ATmega88PA, ATmega168A, ATmega1688PA, ATmega328, ATmega328P datasheet

 

"Dare to be naïve." - Buckminster Fuller

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

I have few changes as per your suggestions.

 

 

I am getting only single warning.

 

Severity    Code    Description    Project    File    Line
Warning        variable 'dummy' set but not used [-Wunused-but-set-variable]    signal_light_induction    C:\Users\ramkumar.d\Documents\Atmel Studio\7.0\signal_light_induction\signal_light_induction\main.c    154
 

Code

 

/*
 * uart atmega328.c
 *
 * Created: 3/23/2019 10:25:20 AM
 * Author : ramkumar.d
 */ 
#define F_CPU 16000000UL
#include <avr/io.h>
#include <stdio.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#define BAUDRATE 9600
#define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1)

/*#define relay1 PORTC0
#define relay2 PORTC1
#define relay3 PORTC2
*/

/*#define exrelay1 
#define exrelay2
#define Buzz*/ 

//Declaration of our functions

void USART_init(void);
unsigned char USART_receive(void);
void USART_send( unsigned char data);
void USART_putstring(char* StringPtr);
void USART_Flush(void);
char String[]="Hello world!!" , ch , i= 0 , C = 0, I = 0;
unsigned int count = 0 , sec = 0;
void Rst()
{
    USART_putstring("Rst\n");
    
    wdt_enable(WDTO_15MS);
    //asm("wdr");
    while(1);
}
void disable_wt()
{
    MCUSR = 0;
    wdt_disable();
    return;
}

int main(void)
{
    DDRC = 0b00000111;
    DDRB = 0B00000000; 
    PORTC |= 0X20;    
    PORTB = 0X0F;
    PORTC |= (1 << 1); // Pin 1 goes low
    PORTC &= ~(1 << 0); // Pin 0 goes low
    PORTC |= (1 << 2); // Pin 2 goes low     
//    PORTC &= 0b00111111;// configuring pin as input
    
    USART_init();        //Call the USART initialization code
    disable_wt();       // added
    //PORTC5 = 0;
    while(1)         //Infinite loop
    {
        _delay_ms(10);        // delay 10 ms
        
        ch = USART_receive();   // waiting for the character
        if(ch != 0)
        {
            if((ch == 'R')||(ch == 'r'))   // r--red , g--green , o --- orange, f-- off
            {
                C = I = 0;
                PORTC &= ~(1 << 0); // Pin n goes high
                PORTC |= (1 << 1); // Pin 1 goes low
                PORTC |= (1 << 2); // Pin 2 goes low
                USART_putstring("ROK\n");
                USART_Flush();
            }
            else if((ch == 'g')||(ch == 'G'))             {
                C = I = 0;
                PORTC &= ~(1 << 1); // Pin 1 goes high
                PORTC |= (1 << 0); // Pin 0 goes low
                PORTC |= (1 << 2); // Pin 2 goes low
                USART_putstring("GOK\n");
                USART_Flush();
            }
            else if((ch == 'o')||(ch == 'O'))  
            {
                C = I = 0;
                PORTC &= ~(1 << 2); // Pin 2 goes high *optional
                 PORTC |= (1 << 0);  // Pin 0 goes low
                PORTC |= (1 << 1);  // Pin 1 goes low
                USART_putstring("oOK\n");
                USART_Flush();
            }
            else if((ch == 'f')||(ch == 'F'))
            {
                C = I = 0;
                PORTC |= (1 << 0); // Pin 0 goes low
                PORTC |= (1 << 1); // Pin 1 goes low
                PORTC |= (1 << 2); // Pin 2 goes low
                USART_putstring("FOK\n");
                USART_Flush();
            }
            else if((ch=='t')||(ch=='T'))   // Added  t-- reset
            {
                Rst();
            }    
        }
        count++;
        if(count <= 400)
        {
            count = 0;
            USART_putstring("SOK\n");
            if(C)
            {
                USART_putstring("SIOK\n");
                USART_Flush();        // clears the buffer
                C=0;
            }
            USART_Flush(); 
        }
        if(!(PINC & 0b00100000))   // waiting for vehicle to cross the IR sensor
        {
            C = 1;
            //USART_putstring("COK\n");
            //USART_Flush();        // clears the buffer
            //_delay_ms(2000);     // delay 2 seconds
            
        }
        //USART_Flush();    // clears the buffer
        return 0;

     }
    }
     
void USART_init()
{
    
    UBRR0H = (uint8_t)(BAUD_PRESCALLER>>8);  // baud rate register
    UBRR0L = (uint8_t)(BAUD_PRESCALLER);     // baud rate register
    UCSR0B = (1<<RXEN0)|(1<<TXEN0);          // transmitter and receiver enable
    UCSR0C = (3<<UCSZ00) | (1<<UCSZ01);      // frame format: 8data , 2stop bit
}
unsigned char USART_receive()
{
    
    while(!(UCSR0A & (1<<RXC0))) return 0;
    return UDR0;
    
}
void USART_Flush()
{
    unsigned char dummy;
    while ( UCSR0A & (1<<RXC0) ) dummy = UDR0;
}
void USART_send( unsigned char data){
    
    while(!(UCSR0A & (1<<UDRE0)));
    UDR0 = data;
    
}
void USART_putstring(char* StringPtr)
{
    while(*StringPtr != 0x00)
    {
        USART_send(*StringPtr);
        StringPtr++;
    }
    
}

 

Is it okay now?

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
    if(count <= 400)

Are you sure?

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

/*
 * uart atmega328.c
 *
 * Created: 3/23/2019 10:25:20 AM
 * Author : ramkumar.d
 */ 
#define F_CPU 16000000UL
#include <avr/io.h>
#include <stdio.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#define BAUDRATE 9600
#define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1)

/*#define relay1 PORTC0
#define relay2 PORTC1
#define relay3 PORTC2
*/

/*#define exrelay1 
#define exrelay2
#define Buzz*/ 

//Declaration of our functions

void USART_init(void);
unsigned char USART_receive(void);
void USART_send( unsigned char data);
void USART_putstring(char* StringPtr);
void USART_Flush(void);
char String[]="Hello world!!" , ch , i= 0 , C = 0, I = 0;
unsigned int count = 0 , sec = 0;
void Rst()
{
    USART_putstring("Rst\n");
    
    wdt_enable(WDTO_15MS);
    //asm("wdr");
    while(1);
}
void disable_wt()
{
    MCUSR = 0;
    wdt_disable();
    return;
}
void handle_ch(unsigned char ch)
{
    
    if((ch == 'R')||(ch == 'r'))   // r--red , g--green , o --- orange, f-- off
    {
        C = I = 0;
        PORTC &= ~(1 << 0); // Pin n goes high
        PORTC |= (1 << 1); // Pin 1 goes low
        PORTC |= (1 << 2); // Pin 2 goes low
        USART_putstring("ROK\n");
        USART_Flush();
    }
    else if((ch == 'g')||(ch == 'G'))             {
        C = I = 0;
        PORTC &= ~(1 << 1); // Pin 1 goes high
        PORTC |= (1 << 0); // Pin 0 goes low
        PORTC |= (1 << 2); // Pin 2 goes low
        USART_putstring("GOK\n");
        USART_Flush();
    }
    else if((ch == 'o')||(ch == 'O'))
    {
        C = I = 0;
        PORTC &= ~(1 << 2); // Pin 2 goes high *optional
        PORTC |= (1 << 0);  // Pin 0 goes low
        PORTC |= (1 << 1);  // Pin 1 goes low
        USART_putstring("oOK\n");
        USART_Flush();
    }
    else if((ch == 'f')||(ch == 'F'))
    {
        C = I = 0;
        PORTC |= (1 << 0); // Pin 0 goes low
        PORTC |= (1 << 1); // Pin 1 goes low
        PORTC |= (1 << 2); // Pin 2 goes low
        USART_putstring("FOK\n");
        USART_Flush();
    }
    else if((ch=='t')||(ch=='T'))   // Added  t-- reset
    {
        Rst();
    }

}
void check_induction()
{
    if(!(PINC & 0b00100000))   // waiting for vehicle to cross the IR sensor
    {
        C = 1;
        //USART_putstring("COK\n");
        //USART_Flush();        // clears the buffer
        //_delay_ms(2000);     // delay 2 seconds
        
    }
}
void heart_beat()
{
    USART_putstring("SOK\n");
    if(C)
    {
        USART_putstring("SIOK\n");
        USART_Flush();        // clears the buffer
        C=0;
    }
    USART_Flush();
}
void pin_init()
{
    DDRC = 0b00000111;
    DDRB = 0B00000000;
    PORTC |= 0X20;
    PORTB = 0X0F;
    PORTC |= (1 << 1); // Pin 1 goes low
    PORTC &= ~(1 << 0); // Pin 0 goes low
    PORTC |= (1 << 2); // Pin 2 goes low
    //    PORTC &= 0b00111111;// configuring pin as input
    
}
int main(void)
{
    pin_init();
    USART_init();        //Call the USART initialization code
    disable_wt();       // added
    //PORTC5 = 0;
    while(1)         //Infinite loop
    {
        _delay_ms(10);        // delay 10 ms
        
        ch = USART_receive();   // waiting for the character
        if(ch != 0)
        {
            handle_ch(ch);    
        }
        count++;
        
        if(count <= 10)
        {
            count = 0;
            heart_beat();
        }
        check_induction();  //induction loop
        //USART_Flush();    // clears the buffer
        return 0;

     }
    }
     
void USART_init()
{
    
    UBRR0H = (uint8_t)(BAUD_PRESCALLER>>8);  // baud rate register
    UBRR0L = (uint8_t)(BAUD_PRESCALLER);     // baud rate register
    UCSR0B = (1<<RXEN0)|(1<<TXEN0);          // transmitter and receiver enable
    UCSR0C = (3<<UCSZ00) | (1<<UCSZ01);      // frame format: 8data , 2stop bit
}
unsigned char USART_receive()
{
    
    while(!(UCSR0A & (1<<RXC0))) return 0;
    return UDR0;
    
}
void USART_Flush()
{
    unsigned char dummy;
    while ( UCSR0A & (1<<RXC0) ) dummy = UDR0;
}
void USART_send( unsigned char data){
    
    while(!(UCSR0A & (1<<UDRE0)));
    UDR0 = data;
    
}
void USART_putstring(char* StringPtr)
{
    while(*StringPtr != 0x00)
    {
        USART_send(*StringPtr);
        StringPtr++;
    }
    
}

joeymorin wrote:

    if(count <= 400)

Are you sure?

 

I am not i have changed it completely now!! 

 

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

Added

 

if(count >= 400)
        {
            count = 0;
            heart_beat();
        }

 

 

removed return 0 in while

 

now it is working fine

 

 

Is my code okay now?

 

Last Edited: Wed. Aug 14, 2019 - 02:43 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ram kumar wrote:

I am not i have changed it completely now!! 

You've got the wrong comparison.  Should be >= should it not?

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

Last Edited: Wed. Aug 14, 2019 - 02:50 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ram kumar wrote:
Actually we have designed our own circuit. 

you might want to post pictures and schematic of your circuit. I'd guess you have hardware problems as well since there is problems switching the ac lights.

 

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

joeymorin wrote:

ram kumar wrote:

I am not i have changed it completely now!! 

You've got the wrong comparison.  Should be >= should it not?

Yes i have corrected it. Also Return 0 in while.

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

Kartman wrote:

ram kumar wrote:
Actually we have designed our own circuit. 

you might want to post pictures and schematic of your circuit. I'd guess you have hardware problems as well since there is problems switching the ac lights.

 

 

I have attached my schematice please take a look at it and let me know your valuable feedback.

Attachment(s): 

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

I can't see any bypass caps on the mega328. The optos to drive the transistors do nothing for your circuit.

 

My guess is transients are getting in via the power supply module.

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

Kartman wrote:

I can't see any bypass caps on the mega328. The optos to drive the transistors do nothing for your circuit.

 

My guess is transients are getting in via the power supply module.

 

Okay, One ore doubt i have, Will the unused pin causes the controller to hang?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
Will the unused pin causes the controller to hang?

Only if you try to test their logic state.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

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

Not too bad, though why have you xtal floating around in some random spot---it should be shown connected right to the processor.  

 

The when someone looks at the processor, they can see you are using a crystal right away, not when they happen to see it later on page 5 of a schematic.

 

 

1K seems a bit high for the opto leds (especially at 3.3V drive)....might want to double check (I did not)...They'll only get a bit more than 1 ma.

 

How does power get from U2 (reg) to U3  (AVR).?.I don't see a path  Showing clear connection wires will eliminate having to hunt & guess.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

avrcandies wrote:

How does power get from U2 (reg) to U3  (AVR).?.I don't see a path  Showing clear connection wires will eliminate having to hunt & guess.

 

iN con10 we will short it to 3.3v

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

What may can resolve this hang issue? Connecting 0.1uf at vcc and ground!!! Will it resolve?

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

 

ram kumar wrote:
iN con10 we will short it to 3.3v

Is 3.3v and 16MHz xtal in spec for M328P......

I don't think so!

If I'm reading that correctly, you need a VCC of at least 3.78v for 16MHz to get it in the safe zone.

Jim

 

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

Last Edited: Wed. Aug 14, 2019 - 12:55 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why are there no part numbers for the optocouplers?

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

avrcandies wrote:

Why are there no part numbers for the optocouplers?

 

Part number is : Pc817

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

Hi Guys,

 

our circuit is also working with 5V, we have an option. 

Please Suggest, as per the Speed grade , Can we go with 5V as VCC.

 

 

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

Atmega328p drives  Pin low (with very Low sink current, so that no loading Effect on Micro controller) ,then opto coupler working and Relay is ON.

 

But  Why this is hang while running. Please suggest your valuable Answers.

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

Are you guys working on the same project? Or is this a hijack by Srinivas?

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

Srinivas_nipun wrote:
But  Why this is hang while running. Please suggest your valuable Answers.

Well an AVR does not hang, but it can be caught in a loop with no exit condition, so look at your code and see where you have a while() loop that its exit condition is not being met.

Most likely in one of your USART functions, so start there, and see which one is not exiting its loop.

 

Yes, you can run at 16MHz with a vcc of 5v.

 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

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

ram kumar wrote:
frame format: 8data , 2stop bit

Why 2 stop bits, that is unusual these days, was common back in the teleprinter days at 110 baud?

Note the default for this register is 8 bit, no parity, and 1 stop bit, no need to change this register if using this common format!

 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

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

Kartman wrote:
Are you guys working on the same project? Or is this a hijack by Srinivas?
IP addresses suggest they are 4,300km apart so it suggests that Srinivas is not in any way related to ram kumar but with things like VPN these days one never knows.

 

(Having said that I spend a lot of my life working with colleagues who are 5,000 miles away so in this day and age physical location maybe doesn't mean so much? You can have collegaues working on the same project on opposite sides of the globe (if you can handle the time difference!))

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

Kartman wrote:

Are you guys working on the same project? Or is this a hijack by Srinivas?

 

We are a team. I works on  programming. he works on hardware.

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

ki0bk wrote:

Srinivas_nipun wrote:
But  Why this is hang while running. Please suggest your valuable Answers.

Well an AVR does not hang, but it can be caught in a loop with no exit condition, so look at your code and see where you have a while() loop that its exit condition is not being met.

Most likely in one of your USART functions, so start there, and see which one is not exiting its loop.

 

Yes, you can run at 16MHz with a vcc of 5v.

 

Jim

 

 

But it is working great with DC lights. What is the difference switching between AC lights and DC lights. Same code same circuit but AVR hanging with only AC switching.

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

I still think that 5 second cycle strongly suggests watchdog. Are you unable to use a debugger?

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

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

Did i not suggest earlier that the power supply module might be part of the problem as well as poor circuit layout? Current flows in a loop - but probably not where you expect it. Understand where it flows and you might find the solution.

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

ka7ehk wrote:

I still think that 5 second cycle strongly suggests watchdog. Are you unable to use a debugger?

 

Jim

 

Yes, I will change that one. Actually we are facing this problem from the last 2 to 3 months. I have added this WDT just after i posted this thread.

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

Kartman wrote:
Did i not suggest earlier that the power supply module might be part of the problem as well as poor circuit layout? Current flows in a loop - but probably not where you expect it. Understand where it flows and you might find the solution.

 

Yeah you suggested. We are discussing about this also. I am very confusing in this switching too. Because the same device, same controller, Same power supply only the difference is light type(AC and DC)!!

 

 

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

If we can’t see your layout, how can we suggest a fix?

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

Severity    Code    Description    Project    File    Line
Warning        variable 'dummy' set but not used [-Wunused-but-set-variable]  

 

What is this warning for. 
 

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

ram kumar wrote:

Severity    Code    Description    Project    File    Line
Warning        variable 'dummy' set but not used [-Wunused-but-set-variable]  

 

What is this warning for. 

It should be clear, no?  Your variable 'dummy' is set, but it is never referenced by other code.

 

 void USART_Flush()
{
    unsigned char dummy;
    while ( UCSR0A & (1<<RXC0) ) dummy = UDR0;
}

That is, you write it, but never read it.

 

You can fix this in a few ways.  One way is to use a variable attribute:

unused

This attribute, attached to a variable, means that the variable is meant to be possibly unused. GCC does not produce a warning for this variable.

So:

 void USART_Flush()
{
    unsigned char dummy __attribute__ ((__unused__));
    while ( UCSR0A & (1<<RXC0) ) dummy = UDR0;
}

 

Or, you can omit the variable altogether:

 void USART_Flush()
{
    while ( UCSR0A & (1<<RXC0) ) UDR0;
}

Since UDR0 is volatile, the compiler will still reference it as required.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

It is used to alert you that you have never used a value that is set. For example:

 

dummy = foo(MyVariable);

And then never using "dummy" for anything. Remember that, in C, you DO NOT have to assign a variable to a value returned from a function. For example, you can  just do:

 

foo(MyVariable);

As I have found, somewhat painfully, assigning a variable to a return value, and then never using it, except for checking it with the debugger, is a waste because the compiler will never use, or perhaps even create, that variable, anyway.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

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

Kartman wrote:
If we can’t see your layout, how can we suggest a fix?

 

I will update tomorrow morning. 

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

Hi  Kartman ,

 

find the Attachments for Layout files.(PDF)

Attachment(s): 

Pages