Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

convert receive USART data to control PIC ports

Status
Not open for further replies.

bbgil

Full Member level 2
Full Member level 2
Joined
Mar 11, 2006
Messages
135
Helped
13
Reputation
26
Reaction score
9
Trophy points
1,298
Activity points
2,321
lcd_cmd(130)

GUys, i want to convert received data from USART to control individual ports in PIC. i used the library functions in MikroC. the problem is how make the PIC read the individual data to compare them to the PIC char and then control their function. in this code, just simply turn on LED on porta bit 0 and bit1. really need to make this work. thanks in advance

unsigned short pos, i;
char lcdtext [41];
char *tc1 ="on";
char *tc2 ="ot";
void main (){
TRISA =0;
PORTA =0;
Usart_Init (9600);
Lcd_Init (&PORTB);
i=0;
while (1)
{
if(Usart_Data_Ready ())
{
lcdtext = Usart_Read ();

i++;
if (i==2)
{
i=0;
Lcd_Cmd (LCD_RETURN_HOME);
for (pos=0;pos<40;pos++ )
{
Lcd_Chr_CP (lcdtext[pos]);
if (lcdtext == tc1) portA.f0 = 1;
if (lcdtext == tc1) portA.f1 = 1;
if (pos==16) Lcd_Cmd (LCD_SECOND_ROW);
}
Usart_Write('Y');
}
}
}
}

Added after 1 hours 41 minutes:

i think i need to create a buffer register to contain all the receive data from USART then compare it to my text character. But how to do this in MikroC? need help
 

usart_data_ready +mikroc problem

Hi

What you are looking to do is depend on the data you send to PIC whether you are using a protocol to communicate with PIC or or sending a single byte command

If you are using a protocol you will need a circular buffer and a parser soft rotuine to handle data comming from PIC uart

If you are only using a command byte it is more easy to do the job

All the best

Bobi
 

    bbgil

    Points: 2
    Helpful Answer Positive Rating
avr usart receive 0x00

Bobcat,
thanks for the info. But how to do this. doing only byte commands and i tried putting a buffer but can't say i know exactly what i'm doing here. the LED 2 is blinking but i don't think its the carry way. hope u can direct me to a link or a sample to do this buffer and parsing. thanks

unsigned short pos, i;
char lcdtext [41];
unsigned short lcd, buf ;
unsigned char buffer[]={0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00};


char *tc1 ="on";
char *tc2 ="ot";
void main (){
TRISA =0;
PORTA =0;
Usart_Init (9600);
Lcd_Init (&PORTB);
i=0;
while (1)
{
if(Usart_Data_Ready ())
{
lcdtext = Usart_Read ();
lcd = Usart_Read ();
i++;
buffer[buf]= lcd;
buf++;
if (buf == tc1) portA.f0 = 1;
else if (buf == tc2) portA.f1 = 1;

if (i==40)
{
i=0;
Lcd_Cmd (LCD_RETURN_HOME);
for (pos=0;pos<40;pos++ )
{
Lcd_Chr_CP (lcdtext[pos]);

if (pos==16) Lcd_Cmd (LCD_SECOND_ROW);

}
Usart_Write('t');
}
}
}
}
 

convert char to byte mikroc

First:
You need transmit and receive circular queues (buffers) that are interrupt driven, that is you use the char received interrupt to place it into the receive queue and the transmit char complete interrupt to send the next char from the transmit queue.

Second:
You can then use the main loop of your program to look for commands in the receive queue. If you are searching for text commands you can use the strcmp() or strncmp() string compare commands, pay attention to text case.

I switched to Atmel's AVR micros and haven't used PIC micros in a long time. I have never used MikroC, so I cannot help you with exact syntax. Have you tried looking for app notes on the MicroChip web site to find some examples? Good luck.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top