Author Topic: How to assign LOGIC HIGH to PORTB (PB0) of PIC16F877A?  (Read 2917 times)

0 Members and 1 Guest are viewing this topic.

Offline thibraaniTopic starter

  • Beginner
  • *
  • Posts: 6
  • Helpful? 0
How to assign LOGIC HIGH to PORTB (PB0) of PIC16F877A?
« on: October 14, 2007, 11:32:51 PM »
Here is the situation, my PIC is connected to a bluetooth module (KC21 - from KCWirefree). 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 windows hyperterminal [press 1 to turn ON the LED while press 2 to make LED OFF]
2. the command will be sent to the bluetooth module via bluetooth dongle attached to USB.
3. the transceiver (KC21 Bluetooth module) 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 (i have succeded doing it). The bluetooth dongle will send everything it receive to the PIC (the ASCII value of "1" and "2").

The LED is connected to PB0. I have connected a 1K resistor in series with the LED.

The problem is, the LED failed to turn ON and OFF but the 7-segment is 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.

I know the program is bad. I should have used certain command to keep displaying the words "What would you like to do?" after the user prompt a command from the computer to turn ON or turn OFF the LED. I might also need to use the interrupt, but I don;t know how. I read books and still, I can't find its solution.

Attached is the circuit diagram of my system. Please be informed that I didn't display the LED and the 1K resistor in the diagram.

________________________________________________________

#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);


}
}
}
}

Offline paulstreats

  • Supreme Robot
  • *****
  • Posts: 1,381
  • Helpful? 21
Re: How to assign LOGIC HIGH to PORTB (PB0) of PIC16F877A?
« Reply #1 on: October 15, 2007, 05:05:54 AM »
by the looks of it, your if's dont have any real structure therefore the led is switching on and off so fast you cant see it (probably). i'll post back shortly with updated code

Offline paulstreats

  • Supreme Robot
  • *****
  • Posts: 1,381
  • Helpful? 21
Re: How to assign LOGIC HIGH to PORTB (PB0) of PIC16F877A?
« Reply #2 on: October 15, 2007, 05:32:23 AM »
i presume by the first include that you are using hi-tech picc

here is some updated code that should work for you.

notice that i have defined you if's near the bottom with braces(if you dont put multiple lines in braces only the single line after the if will be excecuted with the conditional true)

also notice that i have defined led differently to assign only to pin portb0

so use like "led=0" means off and "led=1" means on.

if you like i could give you a function similar to your "display()" function that allows you to send strings to it so you could do like display("hello") instead of display("h");display("e");display("l") etc..

Code: [Select]
#include <pic.h>

__CONFIG(0x3F32);

#define seg PORTD // define 7 segment as PORTD
#define led RB0 // define led as PORTB.0

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);

}//end of while 1
 

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 = 1; // 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);
}//end if

 

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 = 0; // 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);
}//end if

} //end while 1
}//end of main


Offline thibraaniTopic starter

  • Beginner
  • *
  • Posts: 6
  • Helpful? 0
Re: How to assign LOGIC HIGH to PORTB (PB0) of PIC16F877A?
« Reply #3 on: October 16, 2007, 12:45:27 AM »
Hi paulstreat,

Thank you for helping me solve my problem with the programming.

I tried to compile the corrected program by you, but an error occurred, which said "Warning[000]: Missing newline" and "Error[000]: EOF in comment".

I am happy to know that there's function that is similar to "display ()", which allows me to send strings to it to be displayed in the Hyperterminal. WIth this function, I won't have to program each letter to be displayed, but rather type the whole sentence for display. If you kindly enough to provide me the code??

This is an open question. Is there any function that allows the program to "jump" to certain command in the program. If you notice in my program, I always put "What would you like to do?" after the "if" command. So I wonder whether I can only write those commands once and everytime after the user key-in number "1" or "2", the program will "jump" to diplay that message and continue scanning for the next instruction.

Offline paulstreats

  • Supreme Robot
  • *****
  • Posts: 1,381
  • Helpful? 21
Re: How to assign LOGIC HIGH to PORTB (PB0) of PIC16F877A?
« Reply #4 on: October 16, 2007, 01:54:28 PM »
here is some improved code you might like.

if you cant get it to compile, let me know and ill give you the hex file ready for downloading onto your pic.
Quote
#include <pic.h>
#include   <conio.h>
#include   <stdio.h>
#include   <ctype.h>

__CONFIG(0x3F32);

unsigned char delayus_variable;

#define seg PORTD // define 7 segment as PORTD
#define led RB0 // define led as PORTB.0
#define WaitFor1Us asm("nop") //used for timeouts
#define DelayDivisor 2

         
unsigned char dummy;

#define clear_usart_errors_inline   \
if (OERR)                                       \
{                                                   \
   TXEN=0;                                       \
   TXEN=1;                                       \
   CREN=0;                                       \
   CREN=1;                                       \
}                                                   \
if (FERR)                                       \
{                                                   \
   dummy=RCREG;                              \
   TXEN=0;                                       \
   TXEN=1;                                       \
}


unsigned char a;
static const unsigned char passcode[4] = {'f','k','e','e'};
unsigned char passtry[4] = {'q','q','q','q'};
short int auth = 0;

void DelayUs(unsigned char tm){
   while (tm>=1){
      WaitFor1Us;
      tm--;
   }//
   
}

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 = 0;

}

void putch(unsigned char c)//writes a character to the serial port
{
   while(!TXIF)         //set when register is empty
   {
      clear_usart_errors_inline;
      
   }
   TXREG=c;
   DelayUs(60);
}

void puts(register const char *str)//writes a string to the serial port
{
   while((*str)!=0)
   {
      putch(*str);
    if (*str==13) putch(10);
    if (*str==10) putch(13);
      str++;
   }
}

unsigned char receive(void) // to receive command from PC
{
while (RCIF == 0);
a = RCREG;
return a;
}

void print_welcome_string(void)
{
puts("Welcome Enter Pass Key:-");
putch(0x0a);
putch(0x0d);
}

void logon(void){
print_welcome_string();
passtry[0] = receive();//get pass letter 1
putch('*');//provide an echo character
passtry[1] = receive();//get pass letter 2
putch('*');//provide an echo character
passtry[2] = receive();//get pass letter 3
putch('*');//provide an echo character
passtry[3] = receive();//get pass letter 3
putch('*');//provide an echo character
putch(0x0a);
putch(0x0d);
//test to see if entered pass code is correct
if(passtry[0] == passcode[0] && passtry[1] == passcode[1] && passtry[2] == passcode[2] && passtry[3] == passcode[3]){
auth=1;//set to 1 because the pass code is correct
}
else
{
auth=0;   //set to 0 because pass code is incorrect
puts("Incorrect pass code");
}
}

void main(void)
{
init();
while(1){
   
   if (auth == 0)//if the password didnt authorize
   {
      //what to do if pass word is wrong
      logon();   
   }//end of if
   else
   {
      //code here if password was correct    
      puts("Light is currently ");
      if(led == 0){
         puts("off");   
      }
      else{
         puts("on");   
      }
      putch(0x0a);
      putch(0x0d);
      puts("What would you like to do?");
      putch(0x0a);
      putch(0x0d);
      puts("1 = Light On");
      putch(0x0a);
      putch(0x0d);
      puts("2 = Light Off");    
      putch(0x0a);
      putch(0x0d);
      puts("x = Logoff");
      putch(0x0a);
      putch(0x0d);
      a = receive();
      if(a == '1'){
         led=1;   //turn led on
         seg = a-0x30; // 7 segment display number "1"
      }
      if(a == '2'){
         led=0; //turn led off
         seg = a-0x30; // 7 segment display number "2"
      }
      if(a == 'x'){
         auth = 0; //remove pass word authority, (return to logon)   
      }
   } //end of else
}//end of infinite while
}//end of main




Offline thibraaniTopic starter

  • Beginner
  • *
  • Posts: 6
  • Helpful? 0
Re: How to assign LOGIC HIGH to PORTB (PB0) of PIC16F877A?
« Reply #5 on: October 16, 2007, 04:22:31 PM »
Hi again..
I would like to thank paulstreats again for helping me with the programming code.

Looking at paulstreats' code and my code posted earlier, does it mean the user doesn't have to enter the letter "x" to start executing the instructions in the microcontroller?? But the "x" is entered as a log-out key; meaning, by pressing "x", user needs to re-enter tha passkey again (user log-out and need to log-in again by providing the pass-key)?

Thank you

Offline paulstreats

  • Supreme Robot
  • *****
  • Posts: 1,381
  • Helpful? 21
Re: How to assign LOGIC HIGH to PORTB (PB0) of PIC16F877A?
« Reply #6 on: October 16, 2007, 04:47:20 PM »
Sorry, i did redesign your code a little bit. :-\

you dont need to press x now to start it.

it will go automatically to ask for the login code (fkee).
once the login code is entered correctly, you can press 1 or 2 as many times as you like.

pressing x will return back to the login code.


Offline thibraaniTopic starter

  • Beginner
  • *
  • Posts: 6
  • Helpful? 0
Re: How to assign LOGIC HIGH to PORTB (PB0) of PIC16F877A?
« Reply #7 on: October 16, 2007, 06:31:52 PM »
Thank you.

I have downloaded the HI-TIDE. However, I am not familiar with its function. Can someone tell me how to compile the program and obtained its hex files?

I tried compiling the program provided by paulstreats using MPLAB IDE installed in my computer but failed. The error "missing newline" occured everytime I compiled the program.

For your information, all this while I wrote my program using notepad, compile it with MPLAB IDE and upload the program (the hex file) to my PIC using programmer (meLabs Programmer)

 


Get Your Ad Here

data_list