ATMEGA128( C compiler Code Vision AVR )

Go To Last Post
35 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi everybody,
I need a help from any one use ATmega128 with code vision AVR ( c complier )...
I need to write my program of my microcontroller,so I need a good examples code for initialize UART with normal test... Moreover, the microcontroller receives signal from bluetooth receiver and the microcontroller tests the signal and gives output as delay.... What I know is the signal which inter the microcontroller is digital signal... what I don't know which type of variable I should initialize....

I need a similar code using UART and connect it with simple test.... Also I need to know which libraries and intialization I should do it ......

So please if anyone help me as fast as possible , that mean your are helping disabled people....
I am waiting any help :?: :?: :?: :?:

Thank you for anyone want to help me :)

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

eime wrote:

So please if anyone help me as fast as possible , that mean your are helping disabled people....
I am waiting any help :?: :?: :?: :?:

Seems another home work problem and a newbie who wants to fly before he can crawl.

I would suggest you start with the beginners tutorial by stu stan.The link is here
https://www.avrfreaks.net/index.p...

change without any change is no change ;-)

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

Hi pradheep ,

Thank you a lot pradheep :D , Really I appreciate your helps...

Thank you again pradheep ,

Have a Nice Day :D :D

See you again if I need help :lol: :lol:

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

Look at the example directories of CodeVision for code.

Use the Help system, and you will find other Code examples.

Use the CodeWizard to create your USART initialisation. Use the library delay routines.

If you need help with understanding any of these topics, just ask with a specific question.

David.

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

pradheep wrote:
eime wrote:

So please if anyone help me as fast as possible , that mean your are helping disabled people....
I am waiting any help :?: :?: :?: :?:

Seems another home work problem and a newbie who wants to fly before he can crawl.

I would suggest you start with the beginners tutorial by stu stan.The link is here
https://www.avrfreaks.net/index.p...

So do I, I suggest you to go to that link firstly.
Enjoy! :twisted:

ArcticSoul
Industrial Electronic Engineering, College Student

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

Hi Everybody,
Thank you rotasidteach :D , and Thank you david.prentice :D :D .
Can anyone see my code if it's correct or there are some missing things.....
Really I give up :? :? :? ........... I am trying my best from months... So please David can you see my code if you don't mind.... to help me......

The Question is Can any one Check the code for me please.... :?: :?: :?: :?:

Thank you a lot For everyone help me...

Attachment(s): 

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

dear Eime,

You can post your code directly into a message. The instructions are in the Sticky topics. If you make it look readable, you will get help.

No-one wants to download a ".doc" file with viruses. So do not be surprised if no-one downloads it or no-one helps.

David.

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

You say that you are using CodeVision, but you have some include files that are obviously from avr-gcc.

You have a UART init routine, but there is nothing in it, so your UART is never turned on.

Does the data from the bluetooth really use 9 bits?

You read once from the UART, but then replace the value you read from PORTD before ever using it. Your subsequent values are always taken from PORTD and you never read the UART again.

You are going way too fast. Take it one step at a time making sure each part works first, then add something else. Trying to do the whole thing in one go will get you nowhere.

Regards,
Steve A.

The Board helps those that help themselves.

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

I will posted

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

eime wrote:
I will posted

Hi Everybody,
I posted the code...

What is the difference if I read from ports and USART/UART?
Can I only read from ports without USART..
The signal is 8 bits.

If anyone see any errors please correct it for me.
Thank you a lot

thank you...

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

/* quartz crystal frequency [Hz] */
#define xtal 4000000L

/* Baud rate */
#define baud 9600 /*Baudrate of Bluetooth*/
#define MYUBRR 103 /*Predefined USART baudrate of the MCU Registers*/
#include // ATmega128 MCU
#include // Delay functions
#include // For UART initialization
#include /*Additional libraries need to be initialized*/
#include
#include
#include
#include
void port_init(void);/*Need to initialize all ports. Not sure if ATMega 128 and ATMega 64 have the same port initialization but this is the port initialization for ATMega 128 */
{
// ------- PORT INIT ---------------------

PORTA = 0x00;
DDRA = 0x00;
PORTB = 0xFF;
DDRB = 0x00;
PORTC = 0x00;
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
PORTE = 0x00;
DDRE = 0x00;
PORTF = 0x00;
DDRF = 0x00;
PORTG = 0x00;
DDRG = 0x00;

}

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

Complete the code......

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

void uart0_init(unsigned int ubrr); /*Initialize UART port
{
}

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

void init_devices(void) //initialize all the devices internal to the mcu
{
//stop errant interrupts until set up
cli(); //disable all interrupts
XDIV = 0x00; //xtal divider
XMCRA = 0x00; //external memory
TCCR2 = 0x00; //stop int 2
TCCR3A = 0x00; //stop int 3
// ------- ADC INIT ---------------------

ADCSRA = 0x00; //disable adc
ADMUX = 0x00; //select adc input 0
ACSR = 0x00;
ADCSRA = 0x00; //enable adc

// ------- TIMER INIT ---------------------
//TIMER1 initialize - prescale:8
// WGM: 6) PWM 9bit fast, TOP=0x01FF
// actual value: 3906,250Hz
TCCR1B = 0x00; // stop int 1
TCNT1H = 0x00; // setup
TCNT1L = 0x00; // setup init timer start value
OCR1AH = 0x00;
OCR1AL = 0x00;
OCR1BH = 0x00;
OCR1BL = 0x00;
OCR1CH = 0x00;
OCR1CL = 0x00;
ICR1H = 0x00;
ICR1L = 0x00;
MCUCR = 0x00;
EICRA = 0x00; //extended ext ints
EICRB = 0x00; //extended ext ints
EIMSK = 0x00;
}

void main(void)
{
port_init();
init_devices();
uart0_init();
unsigned int k;
k = USART_Receive ();

while (1) /*main loop*/
{

unsigned int x;
DDRB=0x00; // PORTB AS OUTPUT
PORTB=0xFF; // ALL MOTORS OFF

//TO READ DATA FROM PORTA
DDRD=0x00; //SET PORTD AS INPUT
x=PIND; //READ CONTENTS OF PORTD
k=x;
// Loop
while (1)
{
switch (k)
{
case 0:
{
PORTB=0x06 //0B0000 0110 ON RIGHT VIBRATOR
delay_ms(1000); //DELAY 1 SEC
break;
}
case 1:
{
PORTB=0x04; //0B0000 0100 ON LEFT VIBRATOR
delay_ms(1000); //DELAY 1 SEC
break;
};
case 2:
{
PORTB=0x02; //0B0000 0010 ON FRONT VIBRATOR
delay_ms(1000); //DELAY 1 SEC
break;
}
case 3:
{
PORTB=0x08; //0B0000 1000 ON BACK VIBRATOR
delay_ms(1000); //DELAY 1 SEC
break;
}
}
}
};

}

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

The code finished.... So if anyone want help me just delete the errors and correct it...
Thank you a lot for your helping me :) :) :)

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

This is what I meant by showing your code. It is neatly formatted so that people may choose to read it.

I have not read it. Just made it pretty. But I can see you have not even #included any header files.

void init_devices(void)         //initialize all the devices internal to the mcu
{
    //stop errant interrupts until set up 
    cli();                      //disable all interrupts 
    XDIV = 0x00;                //xtal divider 
    XMCRA = 0x00;               //external memory 
    TCCR2 = 0x00;               //stop int 2 
    TCCR3A = 0x00;              //stop int 3
    // ------- ADC INIT --------------------- 

    ADCSRA = 0x00;              //disable adc 
    ADMUX = 0x00;               //select adc input 0 
    ACSR = 0x00;
    ADCSRA = 0x00;              //enable adc

    // ------- TIMER INIT --------------------- 
    //TIMER1 initialize - prescale:8 
    // WGM: 6) PWM 9bit fast, TOP=0x01FF 
    // actual value: 3906,250Hz 
    TCCR1B = 0x00;              // stop int 1 
    TCNT1H = 0x00;              // setup 
    TCNT1L = 0x00;              // setup init timer start value 
    OCR1AH = 0x00;
    OCR1AL = 0x00;
    OCR1BH = 0x00;
    OCR1BL = 0x00;
    OCR1CH = 0x00;
    OCR1CL = 0x00;
    ICR1H = 0x00;
    ICR1L = 0x00;
    MCUCR = 0x00;
    EICRA = 0x00;               //extended ext ints 
    EICRB = 0x00;               //extended ext ints 
    EIMSK = 0x00;
}


void main(void)
{
    port_init();
    init_devices();
    uart0_init();
    unsigned int k;
    k = USART_Receive();

    while (1) {                 /*main loop */

        unsigned int x;
        DDRB = 0x00;            // PORTB AS OUTPUT 
        PORTB = 0xFF;           // ALL MOTORS OFF  

        //TO READ DATA FROM PORTA
        DDRD = 0x00;            //SET PORTD AS INPUT
        x = PIND;               //READ CONTENTS OF PORTD   
        k = x;
        // Loop  
        while (1) {
            switch (k) {
            case 0:
                {
                    PORTB = 0x06        //0B0000 0110 ON RIGHT VIBRATOR
                    delay_ms(1000);     //DELAY 1 SEC
                    break;
                }
            case 1:
                {
                    PORTB = 0x04;       //0B0000 0100 ON LEFT VIBRATOR                    
                    delay_ms(1000);     //DELAY 1 SEC
                    break;
                };
            case 2:
                {
                    PORTB = 0x02;       //0B0000 0010 ON FRONT VIBRATOR
                    delay_ms(1000);     //DELAY 1 SEC
                    break;
                }
            case 3:
                {
                    PORTB = 0x08;       //0B0000 1000 ON BACK VIBRATOR
                    delay_ms(1000);     //DELAY 1 SEC
                    break;
                }
            }
        }
    };

}

David.

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

David, there are two previous posts that also include some of the code. The #include statements are in one of those.

Quote:
What is the difference if I read from ports and USART/UART?
Can I only read from ports without USART..
The signal is 8 bits.

@eime: if you don't know this, then you are trying way more complicated things than you are ready for.

Regards,
Steve A.

The Board helps those that help themselves.

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

The Code:

/* quartz crystal frequency [Hz] */ 
#define xtal 4000000L 

/* Baud rate */ 
#define baud 9600 /*Baudrate of Bluetooth*/ 
#define MYUBRR 103 /*Predefined USART baudrate of the MCU Registers*/ 
#include  // ATmega128 MCU 
#include  // Delay functions 
#include  // For UART initialization 
#include  /*Additional libraries need to be initialized*/ 
#include  
#include  
#include  
#include  
void port_init(void);/*Need to initialize all ports. Not sure if ATMega 128 and ATMega 64 have the same port initialization but this is the port initialization for ATMega 128 */ 
{ 
// ------- PORT INIT --------------------- 

PORTA = 0x00; 
DDRA = 0x00; 
PORTB = 0xFF; 
DDRB = 0x00; 
PORTC = 0x00; 
DDRC = 0x00; 
PORTD = 0x00; 
DDRD = 0x00; 
PORTE = 0x00; 
DDRE = 0x00; 
PORTF = 0x00; 
DDRF = 0x00; 
PORTG = 0x00; 
DDRG = 0x00; 

}
void uart0_init(unsigned int ubrr); /*Initialize UART port 
{ 
}
void init_devices(void) //initialize all the devices internal to the mcu 
{ 
//stop errant interrupts until set up 
cli(); //disable all interrupts 
XDIV = 0x00; //xtal divider 
XMCRA = 0x00; //external memory 
TCCR2 = 0x00; //stop int 2 
TCCR3A = 0x00; //stop int 3 
// ------- ADC INIT --------------------- 

ADCSRA = 0x00; //disable adc 
ADMUX = 0x00; //select adc input 0 
ACSR = 0x00; 
ADCSRA = 0x00; //enable adc 

// ------- TIMER INIT --------------------- 
//TIMER1 initialize - prescale:8 
// WGM: 6) PWM 9bit fast, TOP=0x01FF 
// actual value: 3906,250Hz 
TCCR1B = 0x00; // stop int 1 
TCNT1H = 0x00; // setup 
TCNT1L = 0x00; // setup init timer start value 
OCR1AH = 0x00; 
OCR1AL = 0x00; 
OCR1BH = 0x00; 
OCR1BL = 0x00; 
OCR1CH = 0x00; 
OCR1CL = 0x00; 
ICR1H = 0x00; 
ICR1L = 0x00; 
MCUCR = 0x00; 
EICRA = 0x00; //extended ext ints 
EICRB = 0x00; //extended ext ints 
EIMSK = 0x00; 
} 


void main(void) 
{ 
port_init(); 
init_devices(); 
uart0_init(); 
unsigned int k; 
k = USART_Receive (); 

while (1) /*main loop*/ 
{ 

unsigned int x; 
DDRB=0x00; // PORTB AS OUTPUT 
PORTB=0xFF; // ALL MOTORS OFF 

//TO READ DATA FROM PORTA 
DDRD=0x00; //SET PORTD AS INPUT 
x=PIND; //READ CONTENTS OF PORTD 
k=x; 
// Loop 
while (1) 
{ 
switch (k) 
{ 
case 0: 
{ 
PORTB=0x06 //0B0000 0110 ON RIGHT VIBRATOR 
delay_ms(1000); //DELAY 1 SEC 
break; 
} 
case 1: 
{ 
PORTB=0x04; //0B0000 0100 ON LEFT VIBRATOR 
delay_ms(1000); //DELAY 1 SEC 
break; 
}; 
case 2: 
{ 
PORTB=0x02; //0B0000 0010 ON FRONT VIBRATOR 
delay_ms(1000); //DELAY 1 SEC 
break; 
} 
case 3: 
{ 
PORTB=0x08; //0B0000 1000 ON BACK VIBRATOR 
delay_ms(1000); //DELAY 1 SEC 
break; 
} 
} 
} 
}; 

}

Sorry about this mistake...
Can you see it Dived

[cliff: I edited CODE tags into that but the indentation is still missing?]

Last Edited: Fri. Apr 24, 2009 - 06:27 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Koshchi,

Really I am confuse more and more.....
At the begining I am thinking just I should read from ports, my program simple but all websites make me confuse more.... the program should read input signal and test this signal with some integers and then output the results..... What make me confuse the bluetooth receiver sends signal and the microcontroller receives this signal.... The most things is important the transmit of the signal should be correct....So how I write the program tell me ... if you don't mind....Just helping me.....

Thank you Koshchi For your supporting me...

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

Quote:
What make me confuse the bluetooth receiver sends signal

What does the datasheet for the bluetooth receiver say about how it sends the signal and what signals are sent? We can't really answer questions about how to receive the signals if we have no clue what that signal is.

Regards,
Steve A.

The Board helps those that help themselves.

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

Hi Koshchi,
I hope you are fine.....
Koshchi this is the website you can download or see the data sheet of the bluetooth receiver..
http://www.lemosint.com/bluetoot...

Koshchi I am waiting your help.....Really I want anybody guide me in correct way..

Thank you Koshchi for your supporting me...

Tomorrow I have two exams....Too Much.....

Thank you again Koshchi :) :) :) :) .....

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

Hi Everybody ,
I hope all of you are fine.....
There is no person wants to help me....
Please I am waiting any helps....

Thank you :( :( :(

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

Hi Everybody,
I hope all of you are fine....
I need fast help.....do you know from where I can download "avr/io.h".....I need this header...

So please if anyone know the website allow me to download it from it...Just tell me....
Thank you for anyone help me...
Have a Nice Day..

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

Quote:

do you know from where I can download "avr/io.h".....I need this header...

It's the central header file of the GCC compiler. As far as I understand it you are NOT using the GCC compiler so simply taking a copy of that file (and the 100+ others that it depends on) would not be a great idea as many parts use GCC only syntax that your compiler will not understand.

If you are using CodeVision then its equivalent of is simply

If you are using ImageCraft then its equivalent of is

If you are using IAR then best of luck (I'm sure their equivalent is documented in the user manual)

In fact, looking back at a previous post I see you have:

#include  // ATmega128 MCU 
#include  // Delay functions 
#include  // For UART initialization 
#include  /*Additional libraries need to be initialized*/ 
#include 

I *think* the first two of those are for the CodeVision compiler, the middle one is common to every C compiler ever written and the last two are definitely from GCC - you simply cannot just "mix and match" header files from different C compilers in this way. Even the would need to be the one for your specific compiler as it may have local differences embedded.

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

I suspect that you are using CodeVision and CV supplies the standard "special function register" names like PORTB but not the "bit names"

You have two choices:
1. You just read the data sheet and add appropriate #defines for the "bit name" you need.
2. You construct a suitable "mega32_bits.h" header file from the Atmel official XML files.

I still think that you should make the small effort of making your code look pretty. I know that the compiler does not care, but it really will make it easier for you to see what you are doing.

"fast help" generally means "I want to do no work myself". And tends to get a poor response.

David.

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

Quote:

2. You construct a suitable "mega32_bits.h" header file from the Atmel official XML files

In case it's not obvious, the Studio installation has xmlconvert.exe for this very purpose - it will generate a device header file for the C compiler from the relevant XML with both register AND bit names.

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

Hi everybody,

Thank you all for helping me.

Quote:
David
"fast help" generally means "I want to do no work myself". And tends to get a poor response.

David you don't know my situation.......So if you don't want to help me just don't do.....

I don't know if you believe me or not but this is the first time I deal with microcontroller..... Moreover, This microcontroller is no one in my university deal with it....... Also,this microcontroller arrived to me at the beginning of April 2009, so I need to submit my project at the beginning of May2009.... Don't Say I am lazy....of course you will say where are you from the beginning..... I will say all components of my project is totally change at March 2009......Outs of my hands.......

Thank you for insult me like this way......I don't want any help any more.........

Thank you all of you for helping me....Sorry for any annoyed I made it for you....

Best Regards

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

Eime,

I do not want to offend you. Just offer you some advice. You can search this forum for "fast help" and see just how effective it is.

You can also search for "indented code with Code Tags", and see that those people who do this, get a massive amount of help.

I have no problem with helping a student with his or her assignments. They just have to show what they are trying to do.

David.

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

David

whose tell you I don't search........ You said remember that ...

Anyway, thank you David....I don't want any help from you ....

It's enough for me after you insult me....

Thank you.....

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

Hi Everybody,
I hope you are fine ....

Question:
I have this error.....
Error: C:\Users\Eime\Desktop\PWM\PWM.c(70): missing '('

The line is :
Line : uint16_t queue = 0x0000;

Moreover there is no brackets missing ... So where is the error....

Thank you.....

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

Quote:

So where is the error....

Almost certainly on one of the lines before this one (maybe in a completely different #include's .h file). Errors often have this kind of "knock on" effect where the error isn't reported until the apparent end of some punctuation might have been expected.

I'd start with the lines immediately above this one and work backwards. Watch out for things like unbalanced /*...*/ and #define's that inadvertently have a semi-colon on the end - these are the common kind of errors.

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

Hi clawson ,

I hope you are Fine....
Thank you clawson for your helping me...

I will check the code again as you said backward.....

Thank you clawson again :) :) :)

Have a Nice Day,

Best Regards,

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#define UART_BEGIN_STX        0xBB
#define UART_END_STX        0xEE
#define RX_BUFFER_SIZE0        6

char rx_buffer0[RX_BUFFER_SIZE0];
unsigned char rx_counter;
bit Uart_RecvFlag

// USART Receiver interrupt service routine
#pragma savereg-
interrupt [USART_RXC] void uart_rx_isr(void)
{
  unsigned char status,data;
  #asm
    push r26
    push r27
    push r30
    push r31
    in   r26,sreg
    push r26
  #endasm

  status=UCSRA;
  data=UDR;
  if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
  {
    if (!Uart_RecvFlag)
    {    
       rx_buffer[rx_counter] = data;
    switch (rx_counter)
    {
            case 0:
        if (data == UART_BEGIN_STX)     rx_counter = 1;
        break;
       case 1:
       case 2:
       case 3:
       case 4:
        rx_counter++;
        break;
       case 5:
        rx_counter = 0;
        if (data == UART_END_STX)  Uart_RecvFlag = 1;
        break;
    }
      }
   }
   else
      rx_counter = 0;

   #asm
        pop  r26
    out  sreg,r26
    pop  r31
    pop  r30
    pop  r27
    pop  r26
  #endasm
}
#pragma savereg+
…………
void main(void)
{
  while(1)
  {
    if (Uart_RecvFlag)
    {
    …………            //处理收到的数据包
    Uart_RecvFlag = 0;        //允许USART接受新的数据包
     }
  …………            //处理其它任务
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Everybody,
I hope you are fine,

Thank you a lot metalmetallica for helping me...
I will try to compile the code....
Really thank you a lot metalmetallica :) :) :)

Have a Nice Day,
Best Regards,
Eime

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

OMG