Hi all,
I am writing in HITECH C and I am using a PIC 16F690.
Does anyone have any pointers or examples of how to read a string from UART?
This is how I'm doing it at the moment (which is just checking the characters that come in and making sure they are in the right order):
I am writing in HITECH C and I am using a PIC 16F690.
Does anyone have any pointers or examples of how to read a string from UART?
This is how I'm doing it at the moment (which is just checking the characters that come in and making sure they are in the right order):
Rich (BB code):
#include <stdio.h>
#include <htc.h>
#include "usart.h"
#include <string.h>
#define _XTAL_FREQ 4000000
unsigned char sGPIO;
unsigned char input;
unsigned char data;
unsigned char data2[3] = {0};
int flag =0;
struct ee_map_s {
char sn[80];
long l;
float f;
char c;
};
#define EE_ADDR(member) (offsetof(struct ee_map_s, (member)))
void ee_read(unsigned char ee_addr, void *vp, unsigned char n)
{
unsigned char *p = vp;
while (n--) {
*p++ = eeprom_read(ee_addr++);
}
}
void ee_write(unsigned char ee_addr, void *vp, unsigned char n)
{
unsigned char *p = vp;
while (n--) {
eeprom_write(ee_addr++, *p++);
}
}
void main(void){
// ee_write(EE_ADDR(f), &f, sizeof f);
// ee_read(EE_ADDR(f), &f, sizeof f);
ANS11 = 0;
SYNC = 0;
TRISA3 = 1;
TRISC0 = 0;
TRISB5 = 0;
TRISC7 = 1;
TRISC1 = 0;
T0IE = 1;
PEIE = 1;
RCIF = 0;
RCIE = 1;
IOCA3 = 1;
RABIF = 0;
SPEN = 1;
CREN=0;
CREN=1;
ei();
init_comms();
RC0 =1;
__delay_ms(12000);
RC1 = 1;
__delay_ms(1200);
RC1 = 0;
__delay_ms(1200);
RC1 = 1;
RC0=0;
printf("AT+CMGF=1\r");
__delay_ms(1000);
printf("AT+CNMI=2,2,2,1,0\r");
__delay_ms(1000);
printf("AT+CMGD=1,4\r");
__delay_ms(1000);
while(1){
while(RA3 == 0)
{
RC0 = 1;
printf("AT+CMGF=1\r");
__delay_ms(1000);
printf("AT+CMGS=phonenum\r");
__delay_ms(1000);
printf("The alarm has been tripped!\r");
__delay_ms(1000);
printf("\x001A");
__delay_ms(20000);
}
if(flag==3)
{
if(strstr(data2,"qst")!= NULL)
{
__delay_ms(1000);
printf("AT+CMGF=1\r");
__delay_ms(1000);
printf("AT+CMGS=phonenum\r");
__delay_ms(1000);
printf("I'm fine!\r");
__delay_ms(1000);
printf("\x001A");
__delay_ms(8000);
printf("AT+CMGD=1,4\r");
__delay_ms(1000);
flag =0;
}
memset(data2,0,sizeof(data2));
}
PORTC = sGPIO;
}
}
void interrupt isr(void)
{
PORTA;
RABIF = 0;
if(RA3==0)
{
sGPIO ^= 0b000001;
}
if( RCIE || RCIF )
{
if(OERR)
{
CREN=0;
TXEN=0;
TXEN=1;
RCIE=1;
CREN=1;
}
while(RCIF)
{
data = getch();
if(data == 'q')
{
flag = 1;
data2[0] = 'q';
}
if(data == 's' && data2[0] == 'q')
{
flag++;
data2[1] = 's';
}
if(data == 't' && data2[1] == 's')
{
flag++;
data2[2] = 't';
}
}
}
}