USART with Timers Atmega32

Go To Last Post
62 posts / 0 new

Pages

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

I was trying to use USART to communicate between 2 Atmega32 Microcontrollers, But i'm facing some problems like whenever i use 16 mhz crystal it shows excessive CPU usage, and when i use lower internal clock it slow down the transmitting speed . Apart from this i was sending data to another microcontroller and receiving it using timers like every some millisecond but it creates some problem like "UDR read without transmitting" and My main loop function of 2nd microcontroller aint working it stucks at some point. I am so confused whats happening. I just want to know if there is any way possible like whenever i press button data transmits and received without delay and without disturbing main loop cycle.

This topic has a solution.

Manish verma

Last Edited: Thu. Sep 7, 2017 - 04:53 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

for start show your code 

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

staring.net@gmail.com wrote:
received without delay and without disturbing main loop cycle.

Usart receive interrupt could solve that proplem.

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

staring.net@gmail.com wrote:
whenever i use 16 mhz crystal it (sic) shows excessive CPU usage

What shows that?

 

i was sending data to another microcontroller and receiving it using timers like every some millisecond

Remember that UART transmissions take some time; eg, at 9600 baud, it takes 104us for each bit - so over a millisecond per byte.

 

So: what baud rate are you using? what does "some millisecond" actually mean?

 

but it creates some problem like "UDR read without transmitting"

Again, where is that message coming from?

 

Are you, perhaps, running this in a simulator ... ?

 

 it stucks at some point

If you are running in a simulator, then you should be able to see exactly where that point is.

 

If you are running on real hardware, then remember that the ATmega32 has on-chip debug - so use it! Again, that would allow you to see exactly where it is sticking.

 

 

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

yes i am using Proteus as an simulator.  And those are some run time messages (u can say error) when i start simulation it occurs 

Manish verma

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

You've given us no further information regarding your problem. We do the impossible everyday, but miracles take a little longer. How about showing us some code so we can at least have some idea.

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

staring.net@gmail.com wrote:
those are some run time messages (u can say error) when i start simulation it occurs 

 

So pay attention to those messages!

 

Presumably, they also tell you where the reported problems are occurring?

 

Proteus is a tool - take some time to learn how to use it properly!

 

https://www.labcenter.com/simulation/

 

https://www.labcenter.com/tutorials/

 

http://support.labcenter.co.uk/forums/

 

EDIT

 

Add quote

Last Edited: Wed. Sep 6, 2017 - 01:15 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Setting up comm's between two micros can be tricky if using unproven comm's functions, you don't know if the problem is not correctly sending or not correctly receiving.

So, separate the two and work on proving one, then the other.   How?  Use known good h/w + s/w, i.e. your PC,  use a terminal program (hyperterm, teraterm, putty or others) and a USB to serial TTL (TTL is important, not rs232) and connect it to one micros usart TX/RX pins (note: PC TX connects to micro RX, micro TX connects to PC rx)

and then begin testing your putchar() function by sending a "U" char over and over again.    If you can see this on your PC, then the baud rate init is working correctly.

Then test the getchar() function by doing the reverse sending "U" from the PC to the micro.

 

Once that is working, you now have a base of working h/w + s/w to begin working on the second micro, duplicate the same tests on it.

 

Good luck with your project, and for more help, post your code using the "<>" icon above.

 

Jim

 

 

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

ki0bk wrote:
you don't know if the problem is not correctly sending or not correctly receiving

Or both!

 

post your code using the "<>" icon 

Full, illustrated instructions here: http://www.avrfreaks.net/comment...

 

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

Oh this Proteus, please use there support, after all your paying for it!   https://www.labcenter.com/contact/

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

ki0bk wrote:
 you're paying for it!

Or, if your school is paying for it - get your teachers to show you how to use it properly.

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

well i am not paying anything to anyone.

 

Manish verma

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

So you have stolen your copy of Proteus?

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

what do you mean stolen.?

 

Manish verma

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

Proteus is not free software - so, if you haven't paid for it (and your employer or school hasn't paid for it), then you must have stolen it.

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

Please try to understand i am beginner and trying to understand the concepts.  



ISR (TIMER1_OVF_vect)    // Timer1 ISR
    {
   if(PINA!=0x1f)                                // this is for button sense
        {
	 val=PINA;                              
	 button(val);
        }
	while(!(UCSRA&(1<<RXC)));                        //this is what i am trying for receiving 
		  received_data=UDR;                     //passing UDR value to another variable
		  if(received_data==0)                   
		  {
		    counta1=1;
			PORTD=1<<7;
		  }
		  if(received_data==1)
		  {
		    countb1=1;
		  }
		  if(received_data==2)
		  {
		    countc1=1;
		  }
		  if(received_data==3)
		  {
		    countd1=1;
		  }
      TCNT1 = 63974;
     }

This is my ISR for Timer1 in here i m trying to receive data from 1st microcontroller and passing those value  into another variables.

 

 

 

 

void logic()
{ 
    if(counta>7)
     {
      	  while(!(UCSRA&(1<<UDRE)))
	  {
	   UDR=0;
	  }
     }
     else if(countb>7)
     {
	  while(!(UCSRA&(1<<UDRE)))
	  {
	   UDR=1;
	  }
     }
     else if(countc>7)
     {
	  while(!(UCSRA&(1<<UDRE)))
	  {
	   UDR=2;
	  }
     }
     else if(countc>7)
     {
	  while(!(UCSRA&(1<<UDRE)))
	  {
	   UDR=3;
	  }
     }
}

This is how i m sending my data from 1st microcontroller.

 

int UBBRValue = 25;
   UBRRH = (unsigned char) (UBBRValue >> 8);
   UBRRL = (unsigned char) UBBRValue; 
   UCSRB = (1 << RXEN) | (1 << TXEN);
   UCSRC = (1 << USBS) | (3 << UCSZ0);
   

And this my USART setting i did on both controllers with F_CPU 1000000UL (i.e. 1MHz).

 

 

void cycle()
  {
    send_a_command(0xcf);
    send_a_string("A");
    PORTC=1<<2;
   if(PORTC==1<<2)
   {
     if(counta1!=1)
	 {
      PORTD=1<<3;
      PORTC=1<<6|1<<3|1<<2;
      _delay_ms(gtim[0]*1000);
      PORTC=1<<6|1<<3|1<<1;
      _delay_ms(ytim*1000);
      PORTC=1<<5;
      counta=0;
     gtim[0]=6;
	 }
	 else
     PORTC=1<<5;
   }
   send_a_command(0xcf);
   send_a_string("B");
   if(PINC==1<<5)
   {
    if(countb1!=1)
	{
      PORTD=1<<3;
      PORTC=1<<6|1<<0|1<<5;
      _delay_ms(gtim[1]*1000);
      PORTC=1<<6|1<<0|1<<4;
      _delay_ms(ytim*1000); 
	  PORTD=1<<2;
      countb=0;
    gtim[1]=6;
	}
	else
	PORTD=1<<2;
   }
   send_a_command(0xcf);
   send_a_string("C");
   if(PORTD==1<<2)
   {
    if(countc1!=1)
	{
      PORTD=1<<2|1<<3;
      PORTC=1<<0|1<<3;
      _delay_ms(gtim[2]*1000);
      PORTD=1<<3;
      PORTC=1<<0|1<<3|1<<7;
      _delay_ms(ytim*1000); 
      PORTD=1<<5;
      countc=0;
     gtim[2]=6;
	 }
	 else
	 PORTD=1<<5;
   }
    send_a_command(0xcf);
    send_a_string("D");
   
   if(PORTD==1<<5)
   {
    if(countd1!=1)
	 {
      PORTD=1<<5;
      PORTC=1<<0|1<<3|1<<6;
      _delay_ms(gtim[3]*1000);
      PORTD=1<<4;
      PORTC=1<<0|1<<3|1<<6;
      _delay_ms(ytim*1000);
       PORTD=1<<2;
      countd=0;
     gtim[3]=6;
	 }
	 else
	 PORTC=1<<2;
   }
  }

And this is what i am trying to do with received values. This is my main while loop and i dont want to interrupt it while receiving and transmitting data

Manish verma

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

It looks like you wrote the code before you designed the system. I seem to recall suggesting you use the timer CTC mode rather than interrupting on overflow and reloading the timer. You would do this on an 8051 cpu because there was no other choice. The AVR has a better and simpler solution -CTC mode.
Why would you wait for a rx character in an isr? The usual solution would be to use a rx interrupt - that's why they put it there. Your reading of the button is effectively useless. The usual method is to have debounce code in the isr.
Did you read the manual on the delay functions? They say something about the delay value being constant. You broke that rule.
I can't make much sense of of the code - there's few comments and i can't understand what you were trying to achieve. Design the system first then write the code.

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

Kartman wrote:
 Design the system first then write the code.

+999 !!!

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

SEE this system 

Attachment(s): 

Manish verma

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

 will you please help me by sharing how to use CTC mode with interrupt ISR. Actually i tried searching for it but aint got anything related to it.

Manish verma

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

You seem to be doing a great job of wasting not only your time but ours as well. Where did you search? Did you read the mega32 datasheet? Or google avr ctc ?

As for the picture of your system - all i see is blocks. What are they supposed to do? am. Am I to guess this is a school project and you have to make a traffic light system?

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

first thing is i m not in school second thing i m doing this for my own . there is no school , no office, no college kind of thing involved. and last but not the least i really wanna do something in it thats y i am working on it but not wasting my time.

Manish verma

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

And instead of wires i used wire labels so i guess thats why it seems like boxes only but if u zoom that image u can see there is a label on every single wire.

Manish verma

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

Aw traffic lights, perhaps this tutorial would help!   http://www.avrfreaks.net/forum/t...

 

Jim

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

ty so much ki0bk its really very helpful....thanks a ton i'll be back after reading that stuff. thanks again

Manish verma

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

Hello friends i did try using CTC mode but due to less information and examples i am unable to understand that without using any particular pin like OCP1 and OC1A ....i want to use any pin as input (as i have numbers of pin as an input. ) But in CTC mode i onlu understand that whenever interrupts occur on these pin OCP1 and OC1A/B it will genrate interrupt but i want if i press button on any pin like PORTA=1<<PINA1 then interrupt occur and receive data by another microcontroller.

 

Manish verma

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

Please help me with this code...



#include <avr/io.h>
#include <avr/interrupt.h>

// initialize timer, interrupt and variable
void timer1_init()
{
    // set up timer with prescaler = 64 and CTC mode
    TCCR1B |= (1 << WGM12)|(1 << CS11)|(1 << CS10);

    // initialize counter
    TCNT1 = 0;

    // initialize compare value
    OCR1A = 24999;

    // enable compare interrupt
    TIMSK |= (1 << OCIE1A);

    // enable global interrupts
    sei();
}

// this ISR is fired whenever a match occurs
// hence, toggle led here itself..
ISR (TIMER1_COMPA_vect)
{
    // toggle led here
    PORTC ^= (1 << 0);
{

int main(void)
{
    // connect led to pin PC0
    DDRC |= (1 << 0);

    // initialize timer
    timer1_init();

    // loop forever
    while(1)
    {
        // do nothing
        // whenever a match occurs, ISR is fired
        // toggle the led in the ISR itself
        // no need to keep track of any flag bits here
        // done!
    }
}

i got this after searching over net.....now i want that if somehow i can modify this code as if button pressed interrupt call and that led toggle not on its own but after getting my button input

Manish verma

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

May I ask why you decide to use CTC for your button?
Your proteus schematics showed you use buttons, traffic leds and LCD. Have you succeded to integrate all that task but having trouble with the buttons or you don't know what you're doing at all?
.
When someone try to help you he should know how far you understand about the problem you're facing.
Please tell us your progress so far. Messing with code you found on the net won't suddenly solve your problem.
If you want to learn then learn it in the right way.
.
MG

I don't know why I'm still doing this hobby

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

hey friends i acheived what i wanted. Now i want to explain you what i've got and how it works.....i will make attach an document here and show you with schemtic diagram what did i acheive please let me know if i can improve more on that. 

Thanks

Manish verma

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#define F_CPU 1000000UL                          //set at 1Mhz 
#define DATA_PORT	    PORTB                //LCD DATA PORT
#define ThreePIN        PORTA                   //RS RW E PORT of LCD
#define RS              5                       
#define RW              6
#define E               7
#define MAX_vehicles    10                     //MAX value set used in logic formation
#define fix_time        8                       // MAX time set used in logic formation

#include<avr/io.h>
#include<util/delay.h>
#include<avr/lcd.h>                           //user made library for LCD
#include<avr/interrupt.h>                     

unsigned char counta=0,countb=0,countc=0,countd=0;  
unsigned char gtim[]={6,6,6,6};                     // global variables
unsigned char ytim=2;                               
unsigned char finalvalue,val,val1;
unsigned char i=0,received_data;
char counta1=0,countb1=0,countc1=0,countd1=0,stop_A=0,stop_B=0,stop_C=0,stop_D=0;
         
         

void button(unsigned char val);
void display();
void cycle();
void logic();                                     // function declaration 
void logicoccur();
void timeslicecycle();
void formula(unsigned char current_count, unsigned char i);
void decide_func(unsigned char val1);

ISR (TIMER1_COMPA_vect)    // Timer1 ISR in CTC mode
    {
   if(PINA!=0x1f)                                      
      {
	 val=PINA;
	 button(val);                                   //passing button number 
      }	
	  if((UCSRA&(1<<RXC)))                      // receiving using UART
	  {
	   received_data=UDR;
	   decide_func(received_data);             // passing received value 
	  }
    }
     
int main()
{
   DDRA=0xe0;                                       //PORT for button and lcd rs rw e
   PORTA=0x1f;
   DDRB=0xff;                                       //PORT for DATA PORT of lcd
   PORTB=0x00;
   DDRC=0xff;                                       //PORT for traffic lights
   PORTC=0x00;
   DDRD=0xfe;                                        //PORT for Traffic lights
   PORTD=0x01;
   
   TCCR1B |= (1 << WGM12)|(1 << CS11)|(1 << CS10);    // timers setting for CTC mode
   TCNT1 = 0;                                          
   OCR1A = 2499;                                     // value to compare
   TIMSK |= (1 << OCIE1A);                           
    
   unsigned char UBRR_value=25;                     //UART with baud 2400 in 1MHz
   UBRRH=(unsigned)(UBRR_value>>8);
   UBRRL=(unsigned)UBRR_value;
   UCSRB = (1 << RXEN) | (1 << TXEN);              // Enabling RXEN and TXEN
   UCSRC = (1 << USBS) | (3 << UCSZ0);             //START bit, 8-bit register
   
   lcd_init();  
   send_a_string("WELCOME");                      //this command will display "welcome" on lcd very first time 
   _delay_ms(500);                               // delay of .5 sec 
   send_a_command(0x01);                        // clearing lcd
   display();                                   //calling display function to display A,B,C,D values which is 0 in initial.
   
    sei();                                      //global interrupt enable
   
   while(1)
   {     
      cycle();                                 //this is the main cycle (endless) for traffic signals
   }
 }
 
void button(unsigned char val)               // this function we used to get the button number and take action according to desired input
{
   send_a_command(0x80);
   switch(val)
   {
      case 0x1e:
	          counta++;                 // if this button pressed counta will increase
       send_a_command(0x82);                // and cursor moved to this point of LCD
       send_a_integer(counta, 3);           // and here we displaying the counta value with upto 3 places
     logicoccur();                          // calling another function to check if count is high i.e. greater than 7
      break;
      case 0x1d:
	          countb++;
       send_a_command(0x86);
       send_a_integer(countb, 3);
       logicoccur();
      break;
      case 0x1b:
	          countc++;
       send_a_command(0xc2);
       send_a_integer(countc, 3);                 // all same as above 
      logicoccur();
      break;
      case 0x17:
	          countd++;
       send_a_command(0xc6);
       send_a_integer(countd, 3);
      logicoccur();
      break;
      case 0x0f: 
	          display();
      break;
   }
}

void logicoccur()
{
   if((counta>7)|(countb>7)|(countc>7)|(countd>7))           // here we check if any count is greater than 7
   {
      logic();                                                // if yes then go to this function which is to know at which point count is high
   }
}

void display()                                              //this is the function to display A,B,C,D values over lcd
   {
       send_a_command(0x80);
       send_a_string("A=");
       send_a_integer(counta, 3);
       send_a_string("B=");
       send_a_integer(countb, 3);
       send_a_command(0xc0);
       send_a_string("C=");
       send_a_integer(countc, 3);
       send_a_string("D=");
       send_a_integer(countd, 3);
}

void logic()
{ 
   send_a_command(0x01);
    if(counta>7)
     {
      send_a_string("count high at A");                       // if counta high then display this 
      send_a_command(0x01);
	  while(!(UCSRA&(1<<UDRE)));                         // and send this data to another microcontroller 
	  UDR=0;                                             //this the value stroed in  UDR and transmitted

      timeslicecycle();                                          // after transmitting go to another function which will be used 
     }                                                        //to save the time from other signals
     if(countb>7)
     {
      send_a_string("count high at B");
      send_a_command(0x01);
	  while(!(UCSRA&(1<<UDRE)));
	  UDR=1;
      timeslicecycle();
     }
     if(countc>7)
     {
      send_a_string("count high at C");
      send_a_command(0x01);
	  while(!(UCSRA&(1<<UDRE)));
	  UDR=2;                                          //same as above
      timeslicecycle();
     }
     if(countd>7)
     {
      send_a_string("count high at D"); 
      send_a_command(0x01);
	  while(!(UCSRA&(1<<UDRE)));
	  UDR=3;
      timeslicecycle();
     }
}
  void timeslicecycle()
  {
	   formula(counta,0);
	   formula(countb,1);                  // here we used this to pass value of counta, countb,c,d 
	   formula(countc,2);
	   formula(countd,3);
  }
  void formula(unsigned char current_count,unsigned char i)
  {
     if((current_count==0)|(current_count==1))            //we used this if any count is 0 or 1 then 
     {
	gtim[i]=1;                                         //pass 1 to it and move on
     }
     else
     {
     gtim[i]=((fix_time*current_count)/MAX_vehicles);       // if any count is greater than 0 or 1 then use this formula to calculate desired time to pass vehicles
     ytim=2;
     }
  }
void cycle()                                                //this is the cycle which is start from the very begining and never ends
  {
    send_a_command(0xcf);
    send_a_string("A");                                    // this dsiplay A on LCD at right most corner 
    PORTC=1<<2;
   if(PORTC==1<<2)                                         
   {
    if(stop_A==0)                                       
	 {
      PORTD=1<<3;
      PORTC=1<<6|1<<3|1<<2;                         // this is where 1 greeen and 3 red glows
      _delay_ms(gtim[0]*1000);                     // after calculating desired value of gtim the delay occurs
      PORTC=1<<6|1<<3|1<<1;                          //here 1 yellow and 3 red will glow     
      _delay_ms(ytim*1000);
      PORTC=1<<5;
      counta=0;
     gtim[0]=6;
     }
	 else
      PORTC=1<<5;
	  stop_A=0;
   }
   send_a_command(0xcf);
   send_a_string("B");
   if(PORTC==1<<5)
   {
    if(stop_B==0)
	{
      PORTD=1<<3;                         //same process
      PORTC=1<<6|1<<0|1<<5;
      _delay_ms(gtim[1]*1000);
      PORTC=1<<6|1<<0|1<<4;
      _delay_ms(ytim*1000); 
	  PORTD=1<<2;
      countb=0;
    gtim[1]=6;
	}
	else
	  PORTD=1<<2;
	  stop_B=0;
   }
   send_a_command(0xcf);
   send_a_string("C");
   if(PORTD==1<<2)
   {
     if(stop_C==0)
	 {
      PORTD=1<<2|1<<3;
      PORTC=1<<0|1<<3;
      _delay_ms(gtim[2]*1000);
      PORTD=1<<3;
      PORTC=1<<0|1<<3|1<<7;
      _delay_ms(ytim*1000); 
      PORTD=1<<5;
      countc=0;
     gtim[2]=6;
	 }
	 else
	   PORTD=1<<5;
	   stop_C=0;

   }
   send_a_command(0xcf);
   send_a_string("D");
   
   if(PORTD==1<<5)
   {
    if(stop_D==0)
	{
      PORTD=1<<5;
      PORTC=1<<0|1<<3|1<<6;
      _delay_ms(gtim[3]*1000);
      PORTD=1<<4;
      PORTC=1<<0|1<<3|1<<6;
      _delay_ms(ytim*1000);
       PORTD=1<<2;
      countd=0;
     gtim[3]=6;
	 }
	 else
     PORTD=1<<2;
	 stop_D=0;
   }
  }
void decide_func(unsigned char val1)
{
 switch(val1)
 {
 case 0: PORTD^=1<<6;
         stop_A=1;
 break;
 case 1: PORTD^=1<<6;                            // receive data will be compared and according to received data desired action taken i.e. glow led to show that 
         stop_B=1;                             //count goes at high at this point.
 break;
 case 2: PORTD^=1<<7;
         stop_C=1;
 break;
 case 3: PORTD^=1<<7;
         stop_D=1;
 break;
 }
}

 

Attachment(s): 

Manish verma

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

It is unwise to post .docx on the internet - they could contain a virus. Anyway I risked it and all it contains is:

 

Communication between 2 microcontrollers using UART and Timer in CTC mode.

 

Here I am using two Atmega32 microcontrollers with two 16x2 LCD, 8 traffic lights, 4 LEDS, and 10 Buttons.

Connection made using wire labels in proteus.

 

======

Not sure why you need to put that in a .docx anyway?

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

Here I am using two Atmega32 microcontrollers with two 16x2 LCD, 8 traffic lights, 4 LEDS, and 10 Buttons.

Tells us absolutely nothing that isn't immediately obvious from the diagram!

 

clawson wrote:
Not sure why you need to put that in a .docx anyway?

Indeed!

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

ok my bad again. but there is no reason to say it again and again.

 

Manish verma

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

You've not told us how you want the system to operate. You seem to think we can read your mind and know exactly what you want. Once we understand how you want the system to operate, then we can begin to assist with the code. At the moment, your code is defective and looks like you wrote it before considering what you want the system to do.

1. Describe the system - you have pushbuttons and lights. What do they do? How do they operate? Draw a state diagram how you want the traffic lights to work. At this stage, do not think about implementation- ie coding. All you should describe is buttons,lights and sequences.

2. decompose the problem - what are the major operations you need to perform? Button debouncing, outputs to lights, timing etc.

3. abstraction - view the system as boxes. what do these boxes do and how do they interact? For each of your traffic lights - what do you want them to do? red,amber,green,flash? all off? Then you might have a box the controls the logic - this has inputs and outputs. define these and describe the operations.

Once you've got this far, the code basically writes itself.

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

ok kartman i'll try... give me 5 mins

Manish verma

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

Just to note that you may not be the first person in the world (with access to the internet) who has wanted to simulate a four way junction with traffic lights (and presumably "user present" buttons?) so you can probably google and find 10+ working solutions for this stuff to compare against.

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

staring.net@gmail.com wrote:
give me 5 mins

You'll need more than that.

 

Don't skimp on the design phase.

 

Getting the design right should be the major part of the project; given a good design, it's the coding that should be just "5 mins" ...

 

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

clawson wrote:

Just to note that you may not be the first person in the world (with access to the internet) who has wanted to simulate a four way junction with traffic lights (and presumably "user present" buttons?) ...

 

I've lost track a bit here in the thread.  The original question(s) were USART and timers -- how to work with AVR peripheral subsystems.

 

Later in the thread, it seems to be more of the application.  In this case, traffic control.

 

Over 40 years ago, traffic signal control was always [ ;) at least IME] used for learning state machines.  If the more formal class, "Automata Theory".  It is a good exercise as the student can relate to the application.  And we didn't have any new-fangled "vehicle present" sensors back then, so I suppose it was easier.  ;)

 

Another typical/classic is passenger elevator operation.

 

I don't know how a post here, even extensive, can outline the content of a semester course in automata theory.  There are different ways of laying out the states, and formal and informal methods for optimization.

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.

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

I think that's what Jim was getting at in #24 ?

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

Lee,

   DDRC=0xff;                                       //PORT for traffic lights
   PORTC=0x00;
   DDRD=0xfe;                                        //PORT for Traffic lights
   PORTD=0x01;

etc.

 

It seems he is trying to wire together 5 micros to operate these lights. Each set being operated by a separate micro and then they all "talk" on some kind of "network" (which may be MPCM) to let each other know what they are doing or be commanded what to do.

 

The skill of engineering is surely to analyse and simplify the thing whereas this seems to be a case of over-engineering to see just how complex I can make something that is actually trivially easy.

 

Only OP knows why he thinks there needs to be 5 micros in this.

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

clawson wrote:
The skill of engineering is surely to analyse and simplify the thing

Indeed.

 

Hence the old saying,

An Engineer is someone who can make for a penny what any fool could make for a pound

The clever bit is to make it as simple as possible - but not simpler.

 

The opposite of over-complication is over-simplification; where necessary details are omitted - usually the "corner cases"

 

whereas this seems to be a case of over-engineering to see just how complex I can make something that is actually trivially easy.

It certainly does!

 

Probably due, largely, to the all-too-common failure to analyse  the problem before leaping in and throwing code and components at it.

 

 

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

clawson wrote:
It seems he is trying to wire together 5 micros to operate these lights ... Only OP knows why he thinks there needs to be 5 micros in this. 

Are you mixing this thread up with his other one: http://www.avrfreaks.net/forum/m... - which mentions the 5 micros, but nothing about traffic lights ... 

 

But, even so, I still don't see why this would require two micros ?

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

awneil wrote:
Are you mixing this thread up with his other one
I am - oops blush

 

(EDIT: but that is the same poster? I thought this was all for a common goal?)

Last Edited: Wed. Sep 13, 2017 - 04:15 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Are you guys out of your mind ? i mean really ?

 

Try to put some common sense. let me help you, if you saw that diagram you understand that there are 8 traffic signals which means i am doing that for 2 four way crossings i.e. 4 for 1 crossing.

4 traffic signals controlling 1 crossing and 4 for another crossing. 4 traffic signals controlled by 1 microcontroller which means I have the only option to control them is using 5 microcontrollers for 5 crossings.

And its really important for me to communicate with every microcontroller or you can say crossing to know the traffic status to control according to the current condition.

Hope this time i am clear about my needs. 

Manish verma

Last Edited: Thu. Sep 14, 2017 - 05:07 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I don't think you act right, they just tried to help you and look what you said to them...

 

 

 

 

promotional signature removed by moderator.

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

mormin wrote:

I don't think you act right, they just tried to help you and look what you said to them...

 

 

 

 

I am so pissed, i need their help thats why i am here. I am so new with this stuff , may be i am not familiar with those technical terms  and my communication skills are not that good but i am trying my best to explain them atleast try to understand my problem.

Manish verma

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

Okey, I can understand..

promotional signature removed by moderator.

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

my conclusion so far:

 

the OP is a total noob in electronics and is self learning, so the internet is most likely the only source of education here.

He has found a freeware version of protheus somewhere that he is using for self education.

 

to the OP....

your schematic was copy past form 1 'block of lights+lcd' to the second. keep that in mind while writing the FW you want.

I personally think you have bit way more of than you can chew. So you need to take a massive step back and start with the basics.

That is not fun and will not give you a "show off" project in the short term, but you are already frustrated, and if you continue in this direction that will only grow and most likely beyond what you can handle to keep going.

 

start with the basics......

get 1 led to be on / off and flashing in the speed you want it to.

then extend that to a full trafic light, make that do what you want it to do ( get a flashing pattern on it for instance)

that will give you IO knowledge and can help you in getting timer knowledge.

Note that the LEDs can be used as debug source also once you got them going.

Then you might choose to get the LCD up and running to show some stuff there for indication

Then perhaps start doing a traffic situation were no buttons are required. So a Dump crossing, were there is a certian time a direction can drive.

Then make it smart by adding the buttons.

and last but not least start worring about how to have 2 intersections communicate with eachother.

 

 

 

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

Yep, when dealing with this stuff, you need to ve a little crazy. I'm quite sure most of us fully understand how traffic lights work, but it seems like you struggle as you've not adequately explained how you want your system to work. If we were standing next to you, we would get a pencil and paper to sketch out the operation then decompose and abstract the problem. Then we'd start with some code. We's start with one micro and get that working first before tackling multiple processors. Whilst the task appears simple, you're having trouble solving it, so that suggests it might not be as simple as it seems. The human brain has trouble solving multiple problems at once, so we need to divide the problem. That's why we have a design phase. Even with experience you go through a design phase even though it may not be on paper.

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

I already did what you said above.

1. I started with 4 leds and tried them to blink with expected delay.

2. then i used buttons for increasing counts. 

3. then i used lcd to display my counts.

4. then i tried to do these things together. light blinks, lcd display while button press, with button number(i.e. which button is pressed if 1st button pressed then counta++,if 2nd button pressed countb++) .

5. then i tried controlling 4 traffic signals at once and implemented it with above progress.

6. then i used timers so i can receive button inputs in every ms so there is no interrupt in traffic signal cycle.

7.then i tried using uart to communicate between 2 micros.

8. after that i used it in above progress.

9. then my whole system works as i expected.

10. but now i want 5 micros to communicate but unfortunately something unexpected happens.(5th micro is communicating with 4 micros (i.e. transmiting and receiving) but 4 micros can't transmit data to       that 5th micro but they are receiving ......so you can say i almost acheived what i want, except this thing. just a single step behind my goal.  

Manish verma

Last Edited: Thu. Sep 14, 2017 - 09:44 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

anyone here use whatsapp or email id ? if yes please share your details with me in personal.

Thank you

 

Manish verma

Last Edited: Thu. Sep 14, 2017 - 10:30 AM

Pages