attiny1616 UART PROGRAM NOT WORKING IN ATMEL STUDIO 7

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

Hai,

       i am trying to interface BM70 BLUETOOTH module with attiny1616. i have used the sample UART program to send a script to BM7O MODULE. But in my code there are some mistakes. i couldnt understand that. Please find the solution for it. 

1. i have used alternate UART PIN POSITION.

My code as follows:

 

#define F_CPU 20000000
#define USART0_BAUD_RATE(BAUD_RATE) (float)(20000000 * 64 /16 *(float)BAUD_RATE) + 0.5)

#include <avr/io.h>
#include<util/delay.h>
#include<string.h>
#include<stdint.h>
void USART0_init(void);
void USART0_sendchar(char C);
void USART0_sendString(char *str);

void USART0_init(void)
{
    
    USART0_CTRLC = USART_CMODE_ASYNCHRONOUS_gc ;
    PORTMUX.CTRLB |= PORTMUX_USART0_ALTERNATE_gc;
    PORTA_DIR &= ~PIN1_bm;
    PORTA_DIR |= PIN2_bm;
    USART0_BAUD = (uint16_t) USART0_BAUD_RATE(115200);
    USART0_CTRLB |= USART_TXEN_bm;    

}
void USART0_sendchar(char C)
{
    while (!(USART0.STATUS & USART_DREIF_bm))
    {
        ;
    }
    USART0.TXDATAL=C ;
}
void USART0_sendString(char *str)
{
    for (size_t i=0; i<strlen(str); i++)
    {
        USART0_sendString(str[i]) ;
    }
}
int main(void)

{
    USART0_init();
    while(1)
    {
        USART0_sendString("hello world!/r/n") ;
        _delay_ms(500);
   }

}

Attachment(s): 

Last Edited: Thu. Aug 22, 2019 - 12:24 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

First, eliminate the build error.
The mistake is on the second and 35th lines.

Last Edited: Thu. Aug 22, 2019 - 10:44 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes there is some error.i am having doubt with how to set the baud rate also..i want to baud rate of 115200 and i am using internal clock of 20MHz. is it corect way to represent baud rate in my program?

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

How do you know it's 20MHz?

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

Ramu1234 wrote:
i am using internal clock of 20MHz

 

Have you ever heard about the clock divisor? You should open the datasheet and learn about it.

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

Modify lines 1 and 2 like this:

#define F_CPU 20000000UL
#define USART0_BAUD_RATE(BAUD_RATE) (float)(F_CPU * 64 / (16f * (float)BAUD_RATE) + 0.5)

Modify line 35 like this:

        USART0_sendchar(str[i]) ;

And the clock must be changed to 20MHz by your program. At startup, the 1/6 prescaler is enabled.

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

Thank you so much for your input. i am a beginner in embedded C

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

HI,

    i have made the above corrections. I have checked the clkout by using a DSO.It is getting 3.33Mhz. I tried to read the Tx output using a Oscilloscope. Nothing displayed in it. What to do?

Code is written as below.

I selected the 20Mhz internal oscillator. Prescaler value is 6 by default. I haven't selected the frame rate as it is using the default 8bit data and one stop bit.

Anything more i have to do to work this code?

 

#define F_CPU 3333333
#define USART0_BAUD_RATE(BAUD_RATE) ((float)(F_CPU * 64 / (16 * (float)BAUD_RATE)) + 0.5)

#include <avr/io.h>
#include <util/delay.h>
#include <string.h>

void USART0_init(void);
void USART0_sendChar(char c);
void USART0_sendString(char *str);

void USART0_init(void)
{
    PORTB_DIR &= ~PIN3_bm;  //Rx Pin
    PORTB_DIR |= PIN1_bm;    //Tx Pin
    
    USART0_BAUD = (uint16_t)USART0_BAUD_RATE(9600);

    USART0_CTRLB |= USART_TXEN_bm;
}

void USART0_sendChar(char c)
{
    while (!(USART0_STATUS & USART_DREIF_bm))
    {
        ;
    }
    USART0_TXDATAL = c;
}

void USART0_sendString(char *str)
{
    for(size_t i = 0; i < strlen(str); i++)
    {
        USART0_sendChar(str[i]);
    }
}

int main(void)
{
    USART0_init();
    
    while (1)
    {
        USART0_sendString("Hello World!\r\n");
        _delay_ms(500);
    }
}

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

Ramu1234 wrote:
 PORTB_DIR |= PIN1_bm;    //Tx Pin

 

Tx is PB2, not PB1.

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

TX pin is PB2(PIN2_bm).

 

    PORTB_DIR |= PIN1_bm;    //Tx Pin

 

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

Isn't it a shame that Xmega/Xtiny are not like tiny/mega which automatically set pin direction at RXEN/TXEN? I'm sure there must be some hugely technical reason why leaving the direction setting to be done separately is a "better idea" but, off-hand I can't quite think what. (I suppose it opens the possibility of HI-Zing usually driven outputs if something else might be coming onto the bus or something??). It might have been better if the UART registers had a special "relinquish control bit" to be used as an exception in the 0.0001% of cases when just setting IN/OUT is not required.

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

Thank you all. its qorked