#pragma config FOSC = HS // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#include <xc.h>
#include<string.h>
#define _XTAL_FREQ 20000000
char a[]="AT\r";
char echo[]="ATE0\r";
char sim[]="AT+CPIN?\r"; // To Check sim status
char mode_text[]="AT+CMGF=1\r"; // to set text mode
char message_rec[]="AT+CNMI=1,2,0,0,0\r"; // use to set recipient number and mesg
//char mesg[]="AT+CMGR=49\r"; // mesg we want to send
//char terminator=0x1A; // ctrl Z to send mesg
char buf,data[61],res,res1;
unsigned int i,j;
void UART_Init(int baudRate)
{
BRGH=0;
SYNC=0;
TXSTA = 0X20; // Asynchronous mode, 8-bit data & enable transmitter
RCSTA = 0X90; // Enable Serial Port and 8-bit continuous receive
SPBRG = 31; // baud rate @20MHz Clock
}
void UART_TxChar(char ch)
{
while(TXIF == 0); // Wait till the transmitter register becomes empty
TXIF = 0; // Clear transmitter flag
TXREG = ch; // load the char to be transmitted into transmit reg
}
void UART_str(char *ch) // to write data continously
{
for(i=0;ch[i]!=0;i++)
{
UART_TxChar(ch[i]);
}
}
__interrupt() void ISR(void)
{
int j,length = 61;
for(j = 0; j < length; j++)
{
while(RCIF == 0);
{
buf = RCREG; // Read The Received Data Buffer and clears RCIF flag
}
data[j] = buf; RCIE = 0;
}
}
void main(void)
{
char ch;
TRISD = 0X00;
PORTD = 0X00;
TRISC = 0x80; // Configure Rx pin(rc7) as input and Tx(rc6) as output
RCIE = 1; // UART RecIeving Interrupt Enable Bit
PEIE = 1; // Peripherals Interrupt Enable Bit
GIE = 1; // Global Interrupt Enable Bit
UART_Init(9600); // Initialize the UART module with 9600 baud rate
UART_str(a);
__delay_ms(2);
UART_str(echo);
__delay_ms(2);
UART_str(sim);
__delay_ms(2);
UART_str(mode_text);
__delay_ms(2);
UART_str(message_rec);
__delay_ms(2);
while(1)
{
}
return;
}
__interrupt() void ISR(void)
{
for(j = 0; j < length; j++)
{
while(RCIF == 0);
{
buf = RCREG; // Read The Received Data Buffer and clears RCIF flag
}
data[j] = buf;
}
}
after claring the flag only im adding data(buf) to the array.
#pragma config FOSC = HS // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#include
#include
#define _XTAL_FREQ 20000000
char a[]="AT\r";
char echo[]="ATE0\r";
char sim[]="AT+CPIN?\r"; // To Check sim status
char mode_text[]="AT+CMGF=1\r"; // to set text mode
char message_rec[]="AT+CNMI=1,2,0,0,0\r"; // use to set recipient number and mesg
//char mesg[]="AT+CMGR=49\r"; // mesg we want to send
//char terminator=0x1A; // ctrl Z to send mesg
char buf,data[61],res,res1;
unsigned int i,j;
char CharacterReceived = 0; // new variable to announce data is available
void UART_Init(int baudRate)
{
BRGH=0;
SYNC=0;
TXSTA = 0X20; // Asynchronous mode, 8-bit data & enable transmitter
RCSTA = 0X90; // Enable Serial Port and 8-bit continuous receive
SPBRG = 31; // baud rate @20MHz Clock
}
void UART_TxChar(char ch)
{
while(TXIF == 0); // Wait till the transmitter register becomes empty
TXIF = 0; // Clear transmitter flag
TXREG = ch; // load the char to be transmitted into transmit reg
}
void UART_str(char *ch) // to write data continously
{
for(i=0;ch[i]!=0;i++)
{
UART_TxChar(ch[i]);
}
}
__interrupt() void ISR(void)
{
if(RCIE) // check the USART is the cause of the interrupt
{
buf = RCREG; // Read The Received Data Buffer and clears RCIF flag
CharacterReceived = 1; // Flag to say valid data is in 'buf'
RCIE = 0; // clear the USART interrupt flag
}
}
void main(void)
{
char ch;
TRISD = 0X00;
PORTD = 0X00;
TRISC = 0x80; // Configure Rx pin(rc7) as input and Tx(rc6) as output
RCIE = 1; // UART RecIeving Interrupt Enable Bit
PEIE = 1; // Peripherals Interrupt Enable Bit
GIE = 1; // Global Interrupt Enable Bit
UART_Init(9600); // Initialize the UART module with 9600 baud rate
UART_str(a);
__delay_ms(2);
UART_str(echo);
__delay_ms(2);
UART_str(sim);
__delay_ms(2);
UART_str(mode_text);
__delay_ms(2);
UART_str(message_rec);
__delay_ms(2);
while(1)
{
if(CharacterReceived)
{
CharacterReceived = 0; // prepare for any new characters arriving
data[j] = buf; // add the character to 'buf'
j++; // point to the next location in 'data[]'
}
}
return;
}
__interrupt() void ISR(void)
{
while(RCIF == 0);
{
buf = RCREG; // Read The Received Data Buffer and clears RCIF flag
CharacterReceived = 1; // Flag to say valid data is in 'buf'
RCIF = 0; // clear the USART interrupt flag
}
}
if(CharacterReceived)
{
CharacterReceived = 0; // prepare for any new characters arriving
if(j < MAX_LENGTH){
data[j] = buf; // add the character to 'buf'
j++; // point to the next location in 'data[]'
}
}
__interrupt() void ISR(void)
{
if(RCIF) // check the USART is the cause of the interrupt
{
buf = RCREG; // Read The Received Data Buffer and clears RCIF flag
CharacterReceived = 1; // Flag to say valid data is in 'buf'
RCIF = 0; // clear the USART interrupt flag
}
}
Sorry, I used RCIE instead of RCIF, the ISR should be:
But that isn't what you put in the ISR anyway - look closely.Code:__interrupt() void ISR(void) { if(RCIF) // check the USART is the cause of the interrupt { buf = RCREG; // Read The Received Data Buffer and clears RCIF flag CharacterReceived = 1; // Flag to say valid data is in 'buf' RCIF = 0; // clear the USART interrupt flag } }
Brian.
Are you using XC8 as the compiler? If you are, try setting a breakpoint in the while() loop and see what is in the variables when you simulate data entering the USART.
Brian.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?