Enabling AVR UART tx

November 9, 2009 by admin · Leave a Comment
Filed under: Microcontrollers 

hi…
this post will cover the topic How to enable and use the inbuilt UART (Universal Asynchronous Receiver Transmitter)??? So lets start with the basic set up we need to do this experiment…

We need…
>>ATmega8 with internal clock source enabled
>>MAX232 it is the rs232->TTL and TTL->rs232 logic level converter.
>>5 Capacitors of value 1uF/63V
>>Serial cable

Now if you have the above hardware stuff with a programmer to program the AVR device; second step is to connect all these stuff to make a set up that can communicate serially with other rs232 compatible devices, once programmed. For that here is the connection scheme…

rs232

Why we need a logic converter (TTL <–> RS232)???

As our AVR microcontroller deals only with TTL signal level for communicating with external world but RS232 standard has logic level voltages somewhat different. For RS232 Logic 0 = -25V while Logic 1= +25V.

Here is the code that continously sends a string to the PC asynchronously.

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

#include<avr/io.h>
void UART_transmit(unsigned char data);
int main(void)
{
unsigned char i,message[]="devesh samaiya\r\n";
DDRD=0x00;
PORTD=0xFF;
UCSRA=0;
UCSRB=1<<TXEN;  // transmitter enable
UCSRC=1<<URSEL | 1<<UCSZ1 | 1<<UCSZ0;  // 8 data bit, a stop, none parity
UBRRH=0;
UBRRL=5; // for 9600 baud at 1MHz
while(1)
{

for(i=0;message[i];i++)
{
UART_transmit(message[i]);
}

}
}

void UART_transmit(unsigned char data)
{
while(!(UCSRA & (1<<UDRE)));
UDR=data;
}
/*****************************************************************/

Note as we are using the internal clock source of 1MHz to run the system. It is possible that 9600 baud rate may not work properly. If the above code doesn’t work fine in your case reduce the baud rate to 2400 by replacing the UBBRL value to 25.

And here is the output

rs232_tx

Output Screen on gtkTerm Ubuntu

rs232_tx

Next UART receiving function routines…..

Controlling R_G_B Led using Microcontroller

November 2, 2009 by admin · 2 Comments
Filed under: Microcontrollers, electronics 

Hi…

Here is another experiment to share. An RGB led is a electroluminescent device capable of creating any color by some combination of Red, Green and Blue LED. Actually it is a LED with 3 led’s (red, green & blue) inside one package.

RGB led has 4 Legs. The one i have has one leg for Vcc (Positive Supply) and other three terminals corresponding to R, G and B values.

Different color patterns can be generated by controlled PWM Pulses on respective terminals.

RGB-4pin1

From left to right in above picture -> 1. RED  2. Vcc   3. GREEN  4. BLUE

Now lets move onto the main part that is How to control using Microcontroller. I am going to use ATmega8l from AVR family for demo. You can use any controller the concept will remian the same.

rgb_led

And here is the code that generates almost all combination’s of R G B using PWM over three pins of MEga8.

PORTC_0 —-Controlling  RED

PORTC_1 —-Controlling GREEN

PORTC_2 —-Controlling BLUE

#include<avr/io.h>
#include<util/delay.h>
#include<compat/deprecated.h>
#include<avr/interrupt.h>
unsigned char r=255,g=0,b=0;

ISR(TIMER0_OVF_vect)
{
  sbi(PORTC,0);
  _delay_ms(255-r);
  cbi(PORTC,0);
  _delay_ms(r); 

   sbi(PORTC,1);
  _delay_ms(255-g);
  cbi(PORTC,1);
  _delay_ms(g);

  sbi(PORTC,2);
  _delay_ms(255-b);
  cbi(PORTC,2);
  _delay_ms(b);   

}

int main(void)
{
  DDRC=0xFF;   // PORTC as output
  PORTC=0xFF;  // Initially all leds off no color
  TCCR0=0x01;  // No prescaling of timer clock
  TIMSK=0x01;  // Timer Overflow interrupt enable
  TCNT0=0;
  sei();   // Global interrupt enable
  while(1==1)    // Infinite loop
  {
    if(r==255 && g==0 && b==0)
    {
        r=r-1;g=g+1;
        if(g==255 && b==0 && r==0)
        {
          g=g-1; b=b+1;
           if(b==255 && r==0 && g==0)
           {
              b=b-1;r=r+1;
           }
         }
    }
  }
  return (0);
}

« Previous PageNext Page »