Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

16f877 programming (seven segment display)

Status
Not open for further replies.

overmind

New Member
hi everyone... what i want to do is that whenever i press a number from my computer, it will be displayed from the 7 segment display.. i have my own customized development board. Can anybody tell me what is wrong with my code?

Code:
#if defined(__PCM__)
#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)  
#byte port_b=6

BYTE CONST LED_MAP[10] = {0x9,0xED,0x98,0xA8,0x6C,0x2A,0xA,0xAD,0x8,0x28};


void main() {
  char pos1;


  set_tris_b(0);
  port_b=0;

  pos1='0';
  while(TRUE) {
      port_b=LED_MAP[pos1-'0'];
     if(kbhit()) {
         delay_us(100);
        pos1 = getch();
        }
      }
}

something seems to be wrong with this code since the upper right part of the seven segment will not light.. so like when i press the number 1, only the lower half is shown.. i am using PIC MCU C compiler and a 16f877 microprocessor.. please help..
 
Looking at you data, you have active low outputs and the DP on RB3. If this is the case then you either have a wiring fault or your display/pic is/are damaged.

Mike.
 
hi.. thanks for the reply..
actually, if i use this code:
Code:
#if defined(__PCM__)
#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) 

#byte port_b=6

BYTE CONST LED_MAP[10] = {0x9,0xED,0x98,0xA8,0x6C,0x2A,0xA,0xAD,0x8,0x28};


void main() {
  set_tris_b(0);
  port_b=0;

  while(TRUE) {
      port_b=LED_MAP[1];
     
      }
}
or change LED_MAP[1] to any number from 0 to 9, it will display the correct number but if i revert to my previous code it wont display the upper right segment of the display.. please help..
 
That suggest that you getchar is faulty.

What happens if you do,
port_b=LED_MAP[0x38-'0'];
does it display an 8?

Mike.
 
In that case your getchar routine must be returning an invalid value. I assume kbhit checks for any key pressed and getchar returns the pressed key. Can you post these routines?

Mike.
 
Where is the keyboard? In your first post you suggest it is on your PC and you send data serially to the pic. If this is the case how can kbhit work?

Which compiler are you using.

Mike.
 
yes.. the keyboard is in my PC and I send data serially to the pic.. i have an PCW compiler IDe where i write my code and a PIC downloader to send it to the PIC.. I use a PIC MCU C Compiler.. and in the reference manual of this compiler it has a kbhit() function to return true if a character is received from any hardware like the keyboard.
 
is there another way to do this? if you have any suggestions please help..
i just want to display in the 7 segment the number that i press from the keyboard.. please help..
 
You could try doing it the old way,

Code:
  pos1='0';
  while(TRUE) {
    port_b=LED_MAP[pos1-'0'];
    if(PIR1.RCIF){     // this tests bit 5 of PIR1
      pos1 = RXREG;
    }
  }

I don't know how bits are managed on your compiler so the if statement is probably wrong.
 
hi.. i would love to test your code but could you be so kind to explain a few things for me? what is PIR1? is that a port? how about RCIF AND RXREG? please help... thanks in advance..
 
PIR1 is the Peripheral Interrupt Register and RCIF is bit 5 of that register. RCIF gets set whenever a byte is received via RS232. The received byte is in RCREG (whoops ,typo earlier) which is another register. For more info see section 10.2.2 of the data sheet.

You could try,
Code:
if(PIR1&32==32){

Mike.
 
hi.. i've got it.. i just a added a delay. here is my final code.. thanks for your help..

Code:
#if defined(__PCM__)
#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#byte port_b=6

BYTE CONST LED_MAP[10] = {0x9,0xED,0x98,0xA8,0x6C,0x2A,0xA,0xAD,0x8,0x28};

//============================
void main()
{

   char pos1;

   set_tris_b(0);
   port_b=0;
   pos1='0';


   while(1)
      {
         if(kbhit()) {
            pos1 = getch();
            delay_us(500);
            port_b=LED_MAP[pos1-'0'];
            
         }

      }
      
}
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top