Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
amirsun
PostPosted: Nov 27, 2012 - 11:18 AM
Newbie


Joined: Feb 16, 2010
Posts: 12


Dear Friends;
I wrote a program for ATMEGA8 using atmel studio 6, after compile and simulate with proteus, every thing is ok, but when I programming the uc it does not work!!!!
I tested the circuit by a simple test program with codevision, the test is success and not any problem found in circuit.
The code that I wrote is here, hope anyone can help me to resolve the problem.
Regards;
Amir;

Code:


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

#define FOSC 8000000// Clock Speed
#define BAUD 9600
#define MYUBRR FOSC/16/BAUD-1


int a,b,c,d;
void USART_Init( unsigned int ubrr);
void USART_Transmit( unsigned char data );
void USART_putstring(char* StringPtr);

 int main( void )
{
   DDRB = 0;
   PORTB = 0;
   USART_Init ( MYUBRR );

    USART_putstring("USART test string!\n\r");
   
   while(1)
   {

//Normally Positive
              if(bit_is_clear(PINB,PB0)& b==0)
              {
                 USART_putstring("ip\n\r");
                 b=1;
              }
              if(bit_is_set(PINB,PB0)) b=0;

//Normally Negative       
            if(!(bit_is_clear(PINB,PB1))& a==0)
              {
                 USART_putstring("in\n\r");
                 a=1;
              }
              if(bit_is_clear(PINB,PB1)) a=0;

//Exit Codes

//Normally Positive
              if(bit_is_clear(PINB,PB2)& c==0)
              {
                 USART_putstring("OPin\n\r");
                 c=1;
              }
              if(!(bit_is_clear(PINB,PB2))& c==1)
            {
                 USART_putstring("OPout\n\r");
               c=0;
            }            
//Normally Negative
              if(!(bit_is_clear(PINB,PB3))& d==0)
              {
                 USART_putstring("ONin\n\r");
               d=1;
              }
              if(bit_is_clear(PINB,PB3)& d==1)
              {
                 USART_putstring("ONout\n\r");
                 d=0;
            }            

      }      
}

void USART_Init( unsigned int ubrr)
{
   /* Set baud rate */
   UBRRH = (unsigned char)(ubrr>>8);
   UBRRL = (unsigned char)ubrr;
   /* Enable receiver and transmitter */
   UCSRB = (1<<RXEN)|(1<<TXEN);
   /* Set frame format: 8data, 2stop bit */
   UCSRC = (0<<URSEL)|(0<<USBS)|(3<<UCSZ0);
}

void USART_Transmit( unsigned char data )
{
   /* Wait for empty transmit buffer */
   while ( !( UCSRA & (1<<UDRE)) )
   ;
   /* Put data into buffer, sends the data */
   UDR = data;
}

void USART_putstring(char* StringPtr)
{
   while(*StringPtr != 0x00)
   {
      USART_Transmit(*StringPtr);   
      StringPtr++;
   }        //We increment the pointer so we can read the next char
}
 
 View user's profile Send private message  
Reply with quote Back to top
david.prentice
PostPosted: Nov 27, 2012 - 11:28 AM
10k+ Postman


Joined: Feb 12, 2005
Posts: 16323
Location: Wormshill, England

Code:
   UCSRC = (0<<URSEL)|(0<<USBS)|(3<<UCSZ0);


You have initialised a really SLOW baud rate.

Read your data sheet. Hint: ctrl-F URSEL

Compare with what the CodeVision Wizard produces for initialising the USART.

David.
 
 View user's profile Send private message Send e-mail  
Reply with quote Back to top
clawson
PostPosted: Nov 27, 2012 - 11:57 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62348
Location: (using avr-gcc in) Finchingfield, Essex, England

Code:
#include <avr/iom8.h>

Don't do that.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
meslomp
PostPosted: Nov 27, 2012 - 11:58 AM
Raving lunatic


Joined: May 02, 2007
Posts: 3022
Location: Nieuwegein, Netherlands

what are the fuses set to?
and what hapens if you put the terminal program (PC side) to 1200 baud.

_________________
1)Datasheet and application notes checked?
2)tutorial forum
3)Newbie start here
 
 View user's profile Send private message  
Reply with quote Back to top
amirsun
PostPosted: Nov 27, 2012 - 12:03 PM
Newbie


Joined: Feb 16, 2010
Posts: 12


clawson wrote:
Code:
#include <avr/iom8.h>

Don't do that.


What I must include instead of...?


Last edited by amirsun on Nov 27, 2012 - 12:13 PM; edited 2 times in total
 
 View user's profile Send private message  
Reply with quote Back to top
amirsun
PostPosted: Nov 27, 2012 - 12:06 PM
Newbie


Joined: Feb 16, 2010
Posts: 12


meslomp wrote:
what are the fuses set to?
and what hapens if you put the terminal program (PC side) to 1200 baud.


The fuses are correct, because it work with codevision program, and noting happened when changing the baud...
 
 View user's profile Send private message  
Reply with quote Back to top
amirsun
PostPosted: Nov 27, 2012 - 12:06 PM
Newbie


Joined: Feb 16, 2010
Posts: 12


david.prentice wrote:
Code:
   UCSRC = (0<<URSEL)|(0<<USBS)|(3<<UCSZ0);


You have initialised a really SLOW baud rate.

Read your data sheet. Hint: ctrl-F URSEL

Compare with what the CodeVision Wizard produces for initialising the USART.

David.


Let me check...
 
 View user's profile Send private message  
Reply with quote Back to top
meolsen
PostPosted: Nov 27, 2012 - 12:21 PM
Resident


Joined: Jul 27, 2011
Posts: 541
Location: Atmel, Norway

Quote:

What I must include instead of...?

Nothing. avr/io.h includes the required io-files based on the device you have chosen

_________________
:: Morten
 
 View user's profile Send private message  
Reply with quote Back to top
amirsun
PostPosted: Nov 27, 2012 - 04:22 PM
Newbie


Joined: Feb 16, 2010
Posts: 12


david.prentice wrote:
Code:
   UCSRC = (0<<URSEL)|(0<<USBS)|(3<<UCSZ0);


You have initialised a really SLOW baud rate.

Read your data sheet. Hint: ctrl-F URSEL

Compare with what the CodeVision Wizard produces for initialising the USART.

David.



Tanks David!
You right, the problem solved Wink
Regards;
Amir;


Original Code:
Code:
   UCSRC = (0<<URSEL)|(0<<USBS)|(3<<UCSZ0);


Replaced with:
Code:
   UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits