TinyReiny
Newbie level 2
I am currently using a SIM900 and an ATMega88PA to conduct serial communication. I have ran into multiple problems, which are...
1) I changed the baud rate of the SIM900 by sending the AT command
"AT+IPR=115200"
and now I cannot change the baud rate back to 9600 because the characters that I type into the hyperterminal are not showing up as what they should be.
2) I have set up uart in code, but am confused as to how I can read back characters after I have sent data to the SIM900 from the ATMega88PA. For example, if I send the AT command
"AT" I should expect to receive "OK" back. But how exactly do I go about reading that data then printing it onto an LCD? My uart code is as follows...
3) My SIM900 module is on a KUNS Design board. I am having trouble finding documentation on this. If you know where I can find the datasheet please let me know.
Someone, anyone, please help me. This is a school project and worth a large chunk of my grade.
1) I changed the baud rate of the SIM900 by sending the AT command
"AT+IPR=115200"
and now I cannot change the baud rate back to 9600 because the characters that I type into the hyperterminal are not showing up as what they should be.
2) I have set up uart in code, but am confused as to how I can read back characters after I have sent data to the SIM900 from the ATMega88PA. For example, if I send the AT command
"AT" I should expect to receive "OK" back. But how exactly do I go about reading that data then printing it onto an LCD? My uart code is as follows...
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 #include <avr/io.h> #include <avr/interrupt.h> #include <avr/pgmspace.h> #include "uart.h" /* * constants and macros */ /* size of RX/TX buffers */ #define UART_RX0_BUFFER_MASK ( UART_RX0_BUFFER_SIZE - 1) #define UART_TX0_BUFFER_MASK ( UART_TX0_BUFFER_SIZE - 1) #if ( UART_RX0_BUFFER_SIZE & UART_RX0_BUFFER_MASK ) #error RX0 buffer size is not a power of 2 #endif #if ( UART_TX0_BUFFER_SIZE & UART_TX0_BUFFER_MASK ) #error TX0 buffer size is not a power of 2 #endif #define UART0_RECEIVE_INTERRUPT USART_RX_vect #define UART0_TRANSMIT_INTERRUPT USART_UDRE_vect #define UART0_STATUS UCSR0A #define UART0_CONTROL UCSR0B #define UART0_DATA UDR0 #define UART0_UDRIE UDRIE0 static volatile uint8_t UART_TxBuf[UART_TX0_BUFFER_SIZE]; static volatile uint8_t UART_RxBuf[UART_RX0_BUFFER_SIZE]; static volatile uint8_t UART_TXHead; static volatile uint8_t UART_TxTail; static volatile uint8_t UART_RxHead; static volatile uint8_t UART_RxTail; static volatile uint8_t UART_LastRxError; ISR(UART0_RECEIVE_INTERRUPT) /************************************************************************* Function: UART Receive Complete interrupt Purpose: called when the UART has received a character **************************************************************************/ { uint16_t tmphead; uint8_t data; uint8_t usr; uint8_t lastRxError; /* read UART status register and UART data register */ usr = UART0_STATUS; data = UART0_DATA; lastRxError = (usr & (_BV(FE0)|_BV(DOR0)) ); /* calculate buffer index */ tmphead = ( UART_RxHead + 1) & UART_RX0_BUFFER_MASK; if ( tmphead == UART_RxTail ) { /* error: receive buffer overflow */ lastRxError = UART_BUFFER_OVERFLOW >> 8; } else { /* store new index */ UART_RxHead = tmphead; /* store received data in buffer */ UART_RxBuf[tmphead] = data; } UART_LastRxError = lastRxError; } ISR(UART0_TRANSMIT_INTERRUPT) /************************************************************************* Function: UART Data Register Empty interrupt Purpose: called when the UART is ready to transmit the next byte **************************************************************************/ { uint16_t tmptail; if ( UART_TxHead != UART_TxTail) { /* calculate and store new buffer index */ tmptail = (UART_TxTail + 1) & UART_TX0_BUFFER_MASK; UART_TxTail = tmptail; /* get one byte from buffer and write it to UART */ UART0_DATA = UART_TxBuf[tmptail]; /* start transmission */ } else { /* tx buffer empty, disable UDRE interrupt */ UART0_CONTROL &= ~_BV(UART0_UDRIE); } } /************************************************************************* Function: uart0_init() Purpose: initialize UART and set baudrate Input: baudrate using macro UART_BAUD_SELECT() Returns: none **************************************************************************/ void uart0_init(uint16_t baudrate) { UART_TxHead = 0; UART_TxTail = 0; UART_RxHead = 0; UART_RxTail = 0; /* Set baud rate */ if ( baudrate & 0x8000 ) { UART0_STATUS = (1<<U2X0); //Enable 2x speed baudrate &= ~0x8000; } UBRR0H = (uint8_t)(baudrate>>8); UBRR0L = (uint8_t) baudrate; /* Enable USART receiver and transmitter and receive complete interrupt */ UART0_CONTROL = _BV(RXCIE0)|(1<<RXEN0)|(1<<TXEN0); /* Set frame format: asynchronous, 8data, no parity, 1stop bit */ //#ifdef URSEL0 //UCSR0C = (1<<URSEL0)|(3<<UCSZ00); //#else UCSR0C = (3<<UCSZ00); } /* uart0_init */ /************************************************************************* Function: uart0_getc() Purpose: return byte from ringbuffer Returns: lower byte: received byte from ringbuffer higher byte: last receive error **************************************************************************/ uint16_t uart0_getc(void) { uint16_t tmptail; uint8_t data; if ( UART_RxHead == UART_RxTail ) { return UART_NO_DATA; /* no data available */ } /* calculate /store buffer index */ tmptail = (UART_RxTail + 1) & UART_RX0_BUFFER_MASK; UART_RxTail = tmptail; /* get data from receive buffer */ data = UART_RxBuf[tmptail]; return (UART_LastRxError << 8) + data; } /* uart0_getc */ /************************************************************************* Function: uart0_peek() Purpose: Returns the next byte (character) of incoming UART data without removing it from the ring buffer. That is, successive calls to uartN_peek() will return the same character, as will the next call to uartN_getc() Returns: lower byte: next byte in ring buffer higher byte: last receive error **************************************************************************/ uint16_t uart0_peek(void) { uint16_t tmptail; uint8_t data; if ( UART_RxHead == UART_RxTail ) { return UART_NO_DATA; /* no data available */ } tmptail = (UART_RxTail + 1) & UART_RX0_BUFFER_MASK; /* get data from receive buffer */ data = UART_RxBuf[tmptail]; return (UART_LastRxError << 8) + data; } /* uart0_peek */ /************************************************************************* Function: uart0_putc() Purpose: write byte to ringbuffer for transmitting via UART Input: byte to be transmitted Returns: none **************************************************************************/ void uart0_putc(uint8_t data) { uint16_t tmphead; tmphead = (UART_TxHead + 1) & UART_TX0_BUFFER_MASK; while ( tmphead == UART_TxTail ) { ;/* wait for free space in buffer */ } UART_TxBuf[tmphead] = data; UART_TxHead = tmphead; /* enable UDRE interrupt */ UART0_CONTROL |= _BV(UART0_UDRIE); } /* uart0_putc */ /************************************************************************* Function: uart0_puts() Purpose: transmit string to UART Input: string to be transmitted Returns: none **************************************************************************/ void uart0_puts(const char *s ) { while (*s) { uart0_putc(*s++); } } /* uart0_puts */ /************************************************************************* Function: uart0_puts_p() Purpose: transmit string from program memory to UART Input: program memory string to be transmitted Returns: none **************************************************************************/ void uart0_puts_p(const char *progmem_s ) { register char c; while ( (c = pgm_read_byte(progmem_s++)) ) { uart0_putc(c); } } /* uart0_puts_p */ /************************************************************************* Function: uart0_available() Purpose: Determine the number of bytes waiting in the receive buffer Input: None Returns: Integer number of bytes in the receive buffer **************************************************************************/ uint16_t uart0_available(void) { return (UART_RX0_BUFFER_SIZE + UART_RxHead - UART_RxTail) & UART_RX0_BUFFER_MASK; } /* uart0_available */ /************************************************************************* Function: uart0_flush() Purpose: Flush bytes waiting the receive buffer. Acutally ignores them. Input: None Returns: None **************************************************************************/ void uart0_flush(void) { UART_RxHead = UART_RxTail; } /* uart0_flush */
3) My SIM900 module is on a KUNS Design board. I am having trouble finding documentation on this. If you know where I can find the datasheet please let me know.
Someone, anyone, please help me. This is a school project and worth a large chunk of my grade.
Last edited by a moderator: