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.

Can anyone look at my PIC16F877A code??

Status
Not open for further replies.

thibraani

Newbie level 5
Joined
Oct 8, 2007
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,403
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);


}
}
}
}
 

There are some basic you need to tell us. That is please give us complete information about your ckt diagram. And which value of resistor you have connected to led. you can't drive direct lcd to pIC
 

Attached is the schematic diagram of the project. The LED is connected to PORTB. To be precise, it is connected at port PB0 of the PIC

 

This is my first post/reply and I am happy to help but yninish asked for some details that are not on the ckt diag. Specifically he asked for the value of the resistor used to limit the current through the LED. I am not sure about the '877 but some pics can sink/source 25 mA. If this is the case for the '877 and as most LEDs work at around 10 mA a 470 Ohm resistor should work well. If you suspect B0 could be damaged measure B1, B2, etc. with a voltmeter. Regards.
 

Actually, I have put a 1K resistor in series with the LED. The problem is, when I turn on the network, PBO as well as the other PORT B failed to produce logic high.

So, there might be some problem with my program. I really need your opinion at my programming code
 

If I noticed correctly, you never actually reach the part of the code where you blink the LED? To be more precise it seems to me, you reach it only once and then bye bye...

How many while(1) are you using? Do you find this to be good programming? Let me guess... your background is assembly language, right? C is different, I suggest you to read about C, lots of sources, btw.
 

You sould try to use Interupt! Your code is bad!
 

you have a couple of brackets in the wrong place
and you need to add two more break; statements

also to gain high and low state etc a variables to use

try to type define what you need

add these to your header

and these keywords will become defined and type bound

so can be used in your class

like
if(blabla ==TRUE) { }
else if(blabla == FALSE) { }
else if(blabla == NO) {}
else if((blabla==FALSE)&&(foo= YES))
else
etc....

////////////////////////////////
// : CASE LOGIC DEFINITIONS : //
////////////////////////////////
#case
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#ifndef YES
#define YES 1
#endif
#ifndef NO
#define NO 0
#endif
#ifndef HIGH
#define HIGH 1
#endif
#ifndef LOW
#define LOW 0
#endif
#ifndef ON
#define ON 1
#endif
#ifndef OFF
#define OFF 0
#endif
#ifndef UP
#define UP 1
#endif
#ifndef DOWN
#define DOWN 0
#endif
#ifndef UCHAR
#define UCHAR char
#endif
#ifndef UINT
#define UINT long
#endif
#ifndef BIT
#define BIT short
#endif
#ifndef SCHAR
#define SCHAR signed int
#endif
#ifndef SINT
#define SINT signed long
#endif
#ifndef FLOAT
#define FLOAT float
#endif
/////////////////////////////////
/////////////////////////////////
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top