ATMega16和SM5100B-D gsm / gprs模块之间的USART连接

时间:2019-03-07 15:21:11

标签: avr gsm gprs sim900 atmega16

我一直在努力在这两个设备之间使用usart接口。运行代码后,我可以看到TX和RX闪烁,但是实际上没有消息通过,这是我的最终目标。任何想法如何解决此问题。该代码是从另一个未解决的堆栈问题中借来的

    #define F_CPU 7372800UL

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



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

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 = (1 << URSEL) | (1 << 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_Transmits(char data[] ) {
    int i;

    for(i = 0; i < strlen(data); i++) {
        USART_Transmit(data[i]);
        _delay_ms(300);
    }
}

unsigned char USART_Receive( void ){

    /* Wait for data to be received */
    while ( !(UCSRA & (1<<RXC)) )
    ;
    /* Get and return received data from buffer */
    return UDR;
}



int main( void ){
    DDRA = 0xff;
    PORTA = 0xff;
    USART_Init ( MYUBRR );

    char text_mode[] = "AT+CMGF=1";
    char send_sms[] = "AT+CMGS=";
    char phone_number[] = "0038**********";
    char sms[] = "gsm/gprs sadness";

    USART_Transmits(text_mode);
    _delay_ms(1000);

    USART_Transmits(send_sms);
    _delay_ms(1000);

    USART_Transmit(34);
    _delay_ms(300);

    USART_Transmits(phone_number);
    _delay_ms(300);

    USART_Transmit(34);
    _delay_ms(300);

    USART_Transmit(13);
    _delay_ms(300);

    USART_Transmits(sms);
    _delay_ms(1000);

    USART_Transmit(26);//ctrl+z
    _delay_ms(300);

    //USART_Transmit(13);//enter
    _delay_ms(3000);

    while(1){   
        //USART_Transmits("alfa");
        //_delay_ms(200);
    }

    return 0;
}

0 个答案:

没有答案