thibraani
Newbie level 5

pic16f877a code
Here is the situation, my PIC is connected to a bluetooth module. The bluetooth module acts as a tranceiver for user to control an LED remotely from a personal computer. The system works like this:
1. user prompt command from the computer using hyperterminal [press 1 LED ON while 2 LED OFF]
2. the command will be sent to the bluetooth module via bluetooth dongle attached to USB
3. the transceiver received the command [the ASCII 1 or 2] and send to PIC
4. PIC translate this command and execute the instruction to turn ON or OFF the LED
To connect the bluetooth module, the UART must be enable that is setting the TX and RX pins of PIC HIGH. The bluetooth dongle will send everything it receive to the PIC.
The problem is, the LED failed to turn ON and OFF but the 7-segment was able to display each number being entered on the computer keyboard.
Can anyone out there look at my coding and help me modify it?? To assign logic HIGH and logic LOW on PIC isn't an easy task. I need your help.
_________________________________________
#include <pic.h>
__CONFIG(0x3F32);
#define seg PORTD // define 7 segment as PORTD
#define led PORTB // define led as PORTB
unsigned char a;
void init(void) // subroutine to initialize
{
SPBRG=0x81; // set baud rate as 9600 baud
BRGH=1;
TXEN=1;
CREN=1;
SPEN=1;
TRISD = 0b00000000; // set PORTD as output
seg = 0b00000000;
TRISB = 0b00000000; // set PORTB as output
led = 0b00000000;
}
void display(unsigned char c) // to display the text on the screen
{
while (TXIF == 0);
TXREG = c;
}
unsigned char receive(void) // to receive command from PC
{
while (RCIF == 0);
a = RCREG;
return a;
}
void main(void)
{
init();
{
while(1) // wait for user enter letter "x"
{
a = receive();
if (a == 'x') break;
}
}
display('W'); // display text on screen
display('E');
display('L');
display('C');
display('O');
display('M');
display('E');
display(0x0a);
display(0x0d);
display('E');
display('n');
display('t');
display('e');
display('r');
display(0x20);
display('p');
display('a');
display('s');
display('s');
display(0x20);
display('k');
display('e');
display('y');
display(0x0a);
display(0x0d);
display(0x0a);
display(0x0d);
{
while(1) // wait for passkey "fkee" to be entered
{
a = receive();
if (a == 'f')
{
a = receive();
if (a == 'k')
{
a = receive();
if (a == 'e')
{
a = receive();
if (a == 'e') break;
}
}
}
}
display('W'); // display text on screen
display('h');
display('a');
display('t');
display(0x20);
display('w');
display('o');
display('u');
display('l');
display('d');
display(0x20);
display('y');
display('o');
display('u');
display(0x20);
display('l');
display('i');
display('k');
display('e');
display(0x20);
display('t');
display('o');
display(0x20);
display('d');
display('o');
display('?');
display(0x0a);
display(0x0d);
display(0x5B);
display('1');
display(0x5D);
display(0x20);
display('L');
display('I');
display('G');
display('H');
display('T');
display(0x20);
display('O');
display('N');
display(0x0a);
display(0x0d);
display(0x5B);
display('2');
display(0x5D);
display(0x20);
display('L');
display('I');
display('G');
display('H');
display('T');
display(0x20);
display('O');
display('F');
display('F');
display(0x0a);
display(0x0d);
display(0x0a);
display(0x0d);
seg = 0; // 7 segement show "0" on its display
led = 0; // led is off
while(1) // wait for user to enter command
{
a = receive();
if (a=='1')
display('C');
display('O');
display('N');
display('D');
display('I');
display('T');
display('I');
display('O');
display('N');
display(0X3a);
display('L');
display('I');
display('G');
display('H');
display('T');
display(0X20);
display('O');
display('N');
display(0x0a);
display(0x0d);
display(0x0a);
display(0x0d);
seg = a-0x30; // 7 segment display number "1"
led = 0b11111111; // led on
display('W'); // display text on screen
display('h');
display('a');
display('t');
display(0x20);
display('w');
display('o');
display('u');
display('l');
display('d');
display(0x20);
display('y');
display('o');
display('u');
display(0x20);
display('l');
display('i');
display('k');
display('e');
display(0x20);
display('t');
display('o');
display(0x20);
display('d');
display('o');
display('?');
display(0x0a);
display(0x0d);
display(0x5B);
display('1');
display(0x5D);
display(0x20);
display('L');
display('I');
display('G');
display('H');
display('T');
display(0x20);
display('O');
display('N');
display(0x0a);
display(0x0d);
display(0x5B);
display('2');
display(0x5D);
display(0x20);
display('L');
display('I');
display('G');
display('H');
display('T');
display(0x20);
display('O');
display('F');
display('F');
display(0x0a);
display(0x0d);
display(0x0a);
display(0x0d);
{
a = receive();
if (a == '2')
display('C');
display('O');
display('N');
display('D');
display('I');
display('T');
display('I');
display('O');
display('N');
display(0X3a);
display('L');
display('I');
display('G');
display('H');
display('T');
display(0X20);
display('O');
display('F');
display('F');
display(0x0a);
display(0x0d);
display(0x0a);
display(0x0d);
seg = a-0x30; // 7 segment display number "2"
led = 0b00000000; // led off
display('W'); // display text on screen
display('h');
display('a');
display('t');
display(0x20);
display('w');
display('o');
display('u');
display('l');
display('d');
display(0x20);
display('y');
display('o');
display('u');
display(0x20);
display('l');
display('i');
display('k');
display('e');
display(0x20);
display('t');
display('o');
display(0x20);
display('d');
display('o');
display('?');
display(0x0a);
display(0x0d);
display(0x5B);
display('1');
display(0x5D);
display(0x20);
display('L');
display('I');
display('G');
display('H');
display('T');
display(0x20);
display('O');
display('N');
display(0x0a);
display(0x0d);
display(0x5B);
display('2');
display(0x5D);
display(0x20);
display('L');
display('I');
display('G');
display('H');
display('T');
display(0x20);
display('O');
display('F');
display('F');
display(0x0a);
display(0x0d);
display(0x0a);
display(0x0d);
}
}
}
}
Here is the situation, my PIC is connected to a bluetooth module. The bluetooth module acts as a tranceiver for user to control an LED remotely from a personal computer. The system works like this:
1. user prompt command from the computer using hyperterminal [press 1 LED ON while 2 LED OFF]
2. the command will be sent to the bluetooth module via bluetooth dongle attached to USB
3. the transceiver received the command [the ASCII 1 or 2] and send to PIC
4. PIC translate this command and execute the instruction to turn ON or OFF the LED
To connect the bluetooth module, the UART must be enable that is setting the TX and RX pins of PIC HIGH. The bluetooth dongle will send everything it receive to the PIC.
The problem is, the LED failed to turn ON and OFF but the 7-segment was able to display each number being entered on the computer keyboard.
Can anyone out there look at my coding and help me modify it?? To assign logic HIGH and logic LOW on PIC isn't an easy task. I need your help.
_________________________________________
#include <pic.h>
__CONFIG(0x3F32);
#define seg PORTD // define 7 segment as PORTD
#define led PORTB // define led as PORTB
unsigned char a;
void init(void) // subroutine to initialize
{
SPBRG=0x81; // set baud rate as 9600 baud
BRGH=1;
TXEN=1;
CREN=1;
SPEN=1;
TRISD = 0b00000000; // set PORTD as output
seg = 0b00000000;
TRISB = 0b00000000; // set PORTB as output
led = 0b00000000;
}
void display(unsigned char c) // to display the text on the screen
{
while (TXIF == 0);
TXREG = c;
}
unsigned char receive(void) // to receive command from PC
{
while (RCIF == 0);
a = RCREG;
return a;
}
void main(void)
{
init();
{
while(1) // wait for user enter letter "x"
{
a = receive();
if (a == 'x') break;
}
}
display('W'); // display text on screen
display('E');
display('L');
display('C');
display('O');
display('M');
display('E');
display(0x0a);
display(0x0d);
display('E');
display('n');
display('t');
display('e');
display('r');
display(0x20);
display('p');
display('a');
display('s');
display('s');
display(0x20);
display('k');
display('e');
display('y');
display(0x0a);
display(0x0d);
display(0x0a);
display(0x0d);
{
while(1) // wait for passkey "fkee" to be entered
{
a = receive();
if (a == 'f')
{
a = receive();
if (a == 'k')
{
a = receive();
if (a == 'e')
{
a = receive();
if (a == 'e') break;
}
}
}
}
display('W'); // display text on screen
display('h');
display('a');
display('t');
display(0x20);
display('w');
display('o');
display('u');
display('l');
display('d');
display(0x20);
display('y');
display('o');
display('u');
display(0x20);
display('l');
display('i');
display('k');
display('e');
display(0x20);
display('t');
display('o');
display(0x20);
display('d');
display('o');
display('?');
display(0x0a);
display(0x0d);
display(0x5B);
display('1');
display(0x5D);
display(0x20);
display('L');
display('I');
display('G');
display('H');
display('T');
display(0x20);
display('O');
display('N');
display(0x0a);
display(0x0d);
display(0x5B);
display('2');
display(0x5D);
display(0x20);
display('L');
display('I');
display('G');
display('H');
display('T');
display(0x20);
display('O');
display('F');
display('F');
display(0x0a);
display(0x0d);
display(0x0a);
display(0x0d);
seg = 0; // 7 segement show "0" on its display
led = 0; // led is off
while(1) // wait for user to enter command
{
a = receive();
if (a=='1')
display('C');
display('O');
display('N');
display('D');
display('I');
display('T');
display('I');
display('O');
display('N');
display(0X3a);
display('L');
display('I');
display('G');
display('H');
display('T');
display(0X20);
display('O');
display('N');
display(0x0a);
display(0x0d);
display(0x0a);
display(0x0d);
seg = a-0x30; // 7 segment display number "1"
led = 0b11111111; // led on
display('W'); // display text on screen
display('h');
display('a');
display('t');
display(0x20);
display('w');
display('o');
display('u');
display('l');
display('d');
display(0x20);
display('y');
display('o');
display('u');
display(0x20);
display('l');
display('i');
display('k');
display('e');
display(0x20);
display('t');
display('o');
display(0x20);
display('d');
display('o');
display('?');
display(0x0a);
display(0x0d);
display(0x5B);
display('1');
display(0x5D);
display(0x20);
display('L');
display('I');
display('G');
display('H');
display('T');
display(0x20);
display('O');
display('N');
display(0x0a);
display(0x0d);
display(0x5B);
display('2');
display(0x5D);
display(0x20);
display('L');
display('I');
display('G');
display('H');
display('T');
display(0x20);
display('O');
display('F');
display('F');
display(0x0a);
display(0x0d);
display(0x0a);
display(0x0d);
{
a = receive();
if (a == '2')
display('C');
display('O');
display('N');
display('D');
display('I');
display('T');
display('I');
display('O');
display('N');
display(0X3a);
display('L');
display('I');
display('G');
display('H');
display('T');
display(0X20);
display('O');
display('F');
display('F');
display(0x0a);
display(0x0d);
display(0x0a);
display(0x0d);
seg = a-0x30; // 7 segment display number "2"
led = 0b00000000; // led off
display('W'); // display text on screen
display('h');
display('a');
display('t');
display(0x20);
display('w');
display('o');
display('u');
display('l');
display('d');
display(0x20);
display('y');
display('o');
display('u');
display(0x20);
display('l');
display('i');
display('k');
display('e');
display(0x20);
display('t');
display('o');
display(0x20);
display('d');
display('o');
display('?');
display(0x0a);
display(0x0d);
display(0x5B);
display('1');
display(0x5D);
display(0x20);
display('L');
display('I');
display('G');
display('H');
display('T');
display(0x20);
display('O');
display('N');
display(0x0a);
display(0x0d);
display(0x5B);
display('2');
display(0x5D);
display(0x20);
display('L');
display('I');
display('G');
display('H');
display('T');
display(0x20);
display('O');
display('F');
display('F');
display(0x0a);
display(0x0d);
display(0x0a);
display(0x0d);
}
}
}
}