user3475463
user3475463

Reputation: 45

Receive data via uart for pic16F788 :mikroC

I've written the java code to send data to pic via serial port and now I need to program the microcontroller to read the data and make PortD.RD6=1 if the it receives 1 and PortD.RD6=0 if it receives 0. I've tried this code but I get many errors . This is my first mikroC program so I really don't know how to manage these errors.

char output[1];
unsigned short i;
 void main(){
 TRISD = 0x01;
 i = 0;
UART1_Init(9600);
while (1) {
if (UART1_Data_Ready()==1) {
i = UART1_Read(); // read the received data
ByteToStr(i, output);
if (output = "1" ) // this is where I get the error 
{PortD.RD6=1;}
else {  PortD.RD6=0;}

}}}

Upvotes: 1

Views: 3185

Answers (1)

PeterJ
PeterJ

Reputation: 3789

One error that I can spot is that ByteToStr returns three characters so it is probably overwriting other memory areas and giving an undefined result. You don't need to do that conversion you can simply read a byte into a char and do a comparison directly on that as follows:

void main()
{
    char c;

    TRISD = 0x01;
    UART1_Init(9600);
    while (1) {
        if (UART1_Data_Ready()) {
            c = UART1_Read();
            if (c == '1')
                PortD.RD6=1;
            else 
                PortD.RD6=0;
        }
    }
}

Upvotes: 2

Related Questions