A question regarding PIC programing in C

Discussion in 'Embedded Systems and Microcontrollers' started by Kmelhem, Sep 2, 2010.

  1. Kmelhem

    Thread Starter New Member

    Aug 31, 2010
    15
    0
    Hello there.
    I just Want to know if this code will do what it is supposed to do (to scan input from portA, according to the input, sets a bit on output portB opposite to its current status)

    Code ( (Unknown Language)):
    1. #include <16F877A.H>
    2. void init(void)
    3.   {
    4.   TRISA =0b00001111;
    5.   TRISB =0b00000000;
    6.   PORTA =0;
    7.   PORTB =0;
    8.   }
    9.  
    10. void main () {
    11.  void init(void);
    12.  
    13.  while(true) {
    14.  
    15.   If ((PORTAbits.RA3==0) && (PORTAbits.RA2==0) && (PORTAbits.RA1==0) && (PORTAbits.RA0==1)) {
    16.     If (PORTBbits.RB0==0){
    17.      PORTBbits.RB0=1;
    18.      else
    19.      PORTBbits.RB0=0;
    20.      }
    21.    }
    22.   If ((PORTAbits.RA3==0) && (PORTAbits.RA2==0) && (PORTAbits.RA1==1) && (PORTAbits.RA0==0)) {
    23.     If (PORTBbits.RB1==0){
    24.      PORTBbits.RB1=1;
    25.      else
    26.      PORTBbits.RB1=0;
    27.      }
    28.    }
    29.   If ((PORTAbits.RA3==0) && (PORTAbits.RA2==0) && (PORTAbits.RA1==1) && (PORTAbits.RA0==1)) {
    30.     If (PORTBbits.RB2==0){
    31.      PORTBbits.RB2=1;
    32.      else
    33.      PORTBbits.RB2=0;
    34.      }
    35.    }
    36.   If ((PORTAbits.RA3==0) && (PORTAbits.RA2==1) && (PORTAbits.RA1==0) && (PORTAbits.RA0==0)) {
    37.     If (PORTBbits.RB3==0){
    38.      PORTBbits.RB3=1;
    39.      else
    40.      PORTBbits.RB3=0;
    41.      }
    42.    }
    43.   If ((PORTAbits.RA3==0) && (PORTAbits.RA2==1) && (PORTAbits.RA1==0) && (PORTAbits.RA0==1)) {
    44.     If (PORTBbits.RB4==0){
    45.      PORTBbits.RB4=1;
    46.      else
    47.      PORTBbits.RB4=0;
    48.      }
    49.    }
    50.   If ((PORTAbits.RA3==0) && (PORTAbits.RA2==1) && (PORTAbits.RA1==1) && (PORTAbits.RA0==0)) {
    51.     If (PORTBbits.RB5==0){
    52.      PORTBbits.RB5=1;
    53.      else
    54.      PORTBbits.RB5=0;
    55.      }
    56.    }
    57.   If ((PORTAbits.RA3==0) && (PORTAbits.RA2==1) && (PORTAbits.RA1==1) && (PORTAbits.RA0==1)) {
    58.     If (PORTBbits.RB6==0){
    59.      PORTBbits.RB6=1;
    60.      else
    61.      PORTBbits.RB6=0;
    62.      }
    63.    }
    64.   If ((PORTAbits.RA3==1) && (PORTAbits.RA2==0) && (PORTAbits.RA1==0) && (PORTAbits.RA0==0)) {
    65.     If (PORTBbits.RB7==0){
    66.      PORTBbits.RB7=1;
    67.      else
    68.      PORTBbits.RB7=0;
    69.      }
    70.    }
    71.  
    72.   }
    73.  }
    If there is another way to scan input like a whole number (0111 for example) instead of (bit by bit 1,1,0,1). i didnt know how to make it interrupt driven input
    and will this following part work without a problem?
    Code ( (Unknown Language)):
    1.      If (PORTBbits.RB7==0){
    2.      PORTBbits.RB7=1;
    3.      else
    4.      PORTBbits.RB7=0;
    5.      }
    Thanks for your help :)
     
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    After a quick scan of your code, it looks like you have not configured your pins as digital I/O pins. After power on reset, all pins are set up as analog inputs (if the pin has such function). Se more in the data sheet for setting up PORTA. You must set the ADCON1 register correct
     
  3. elementalrage

    Active Member

    Jul 30, 2009
    59
    3
    To my knowledge, with that particular chip you could only run Interrupts on PORTB inputs.

    Yes, that snippet looks fine. You could also do "PORTB = 0x80;" if wanted RB7 set "high" and the rest of PORTB set "low".
     
  4. Kmelhem

    Thread Starter New Member

    Aug 31, 2010
    15
    0
    thanks alot for your replies.
    and elementalrage, actually no, i want RB7 to become the opposite of what RB7 is, and the rest of PORTB to stay the same.
    anyone knows a good source that can teach me interrupt driven inputs
     
  5. elementalrage

    Active Member

    Jul 30, 2009
    59
    3
    Then you can always use the XOR operation on that bit, but without any delay you probably won't see it.

    Code ( (Unknown Language)):
    1. PORTBbits.RB7 = PORTBbits.RB7 ^ 1;
    A book that I use is Newnes Advanced PIC Microcontroller Projects in C and that shows how to use interrupts, but there should be examples elsewhere on the net.
     
  6. Kmelhem

    Thread Starter New Member

    Aug 31, 2010
    15
    0
    wow, i've just tried to compile it with hi-tech PICC lite, it gives tons of errors, it seems i cant use PORTAbits or PORTBbits commands :S anyone knows y?
     
  7. cheezewizz

    Active Member

    Apr 16, 2009
    82
    10
    For a start you'd need to include <htc.h> instead of the specific 18f877a header file to use Hitech C.
    Check out the Gooligum tutorials, they're pretty good for the basics. They cover using interupts and allsorts and IIRC they have code examples in both Hitech C and CCS. Link's here http://www.gooligum.com.au/tut_midrange_C.html
     
  8. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Hitech C does not use that naming convention for the midrange series. It use PORTA,PORTB, etc. For the port bits it is RA0,RA1, RB0, RB1 and so one. I also strongly recommend reading some documentation. Start with the document ...\HI-TECH Software\PICC\9.xx\docs\quickstart.pdf Always start by building a project in MPLAB. A very important topic is the configuration word setting refer to the data sheet for this. This have to be correct else the chip will not work. It is hard to be a beginner:confused::p
     
  9. Kmelhem

    Thread Starter New Member

    Aug 31, 2010
    15
    0
    Lol yea i know it is hard, am going through it now :p actually i dont have a chip now, am using some simulation programs (pic simulator IDE), if the program did what i want, ill buy the chip and so on.
    i did the <htc.h> thing, but still didnt fix, going to read the documentation, and ill contact u if anything came up :) thx for help
     
  10. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    By the way, all the definitions you need is in the pic168xa.h file. Do not change it, but take a look at it.
     
  11. Kmelhem

    Thread Starter New Member

    Aug 31, 2010
    15
    0
    am trying to figure out the way to get an input, koz when using the standard
    Code ( (Unknown Language)):
    1.  if (portA==7)
    it doesnt work.
    other than that, everything is running smooth
     
  12. elementalrage

    Active Member

    Jul 30, 2009
    59
    3
    I encourage you to work through the program in Assembly as well. It will allow you to better understand what is actually going on at a much more basic level.

    Good luck!
     
  13. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Did you set the ADCON1 register?
    From the data sheet
     
  14. Kmelhem

    Thread Starter New Member

    Aug 31, 2010
    15
    0
    Am not actually working on a chip, am working on a simulation program. so i can set portA bits to 0 and 1, but i cant figure out the correct input code, even if i do
    Code ( (Unknown Language)):
    1. PORTB=PORTA;
    it doesnt work thx anywayz
     
  15. elementalrage

    Active Member

    Jul 30, 2009
    59
    3
    This is why I prefer proto boards to simulation programs. With a chip programmed, it is easier to see what is going on by testing electronically.

    '7' is a decimal which equals binary '00000111' which equals RA2,RA1,RA0 being set "high".

    I can't say pressed because we don't see a schematic to know whether those pins are being pulled "high" or "low".
     
  16. Kmelhem

    Thread Starter New Member

    Aug 31, 2010
    15
    0
    Here is what i mean
    Code ( (Unknown Language)):
    1. #include <htc.h>
    2.  
    3. void main () {
    4.   int x=0;
    5.   ADCON1=0;
    6.   TRISA =0b11111111;
    7.   TRISB =0b00000000;
    8.   PORTA =0;
    9.   PORTB =0;
    10. PORTA=0x7;
    11.  while(1) {
    12. if (PORTA==0x7){
    13.  PORTB=0xFF;}
    14. else{
    15. RB5=1;}
    16.  
    17. }
    18. }
    What i mean is this program is supposed to set PORTB to FF since i already set porta to 0x7, but it doesnt,
    and i tried to do
    Code ( (Unknown Language)):
    1. if (RA1=1)
    but still didnt work, although bit 1 is 1 coz 7hex is 111 :)
     
  17. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Why are you so pigheaded. Simulator or not, if you do not set the ADCON1 register correct. The result will be that pins multiplexed with analog inputs will read "0"
     
  18. Kmelhem

    Thread Starter New Member

    Aug 31, 2010
    15
    0
    well, am sorry, but am still looking for it in the documentation, and is ADCON1 only related to porta? koz i reversed the thing, made portb input and porta output, and still same

    EDIT:- i found it
    Code ( (Unknown Language)):
    1. ADCON1 = 0b00000110;
    still not working
     
  19. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    First I think the correct setting for ADCON1 is 0x6 if you want all pins to be be digital. Second you must use the stimulus editor, to simulate input signals.
    Inspired by the "Naked Chef", I have made videos as the naked programmer:eek:, to help you. .....just joking the videos are from Microchip and contain NO nudity
    I think you should look at this video first
    http://www.youtube.com/watch?v=pnkUOL0mmyA
    then this
    http://www.youtube.com/watch?v=HwdllrufnOA
     
  20. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    How are you setting the input port conditions?
    I hope you realise that input values must be set in the simulator set-up or script rather than in your PIC program.
    Input pins are by definition high impedance so writing a 1 or 0 to an input pin through the PIC internal software has no effect on what the pin actually reads. You have to tell the simulator whether the input is at a 1 or a 0.
     
Loading...