A question regarding PIC programing in C

Thread Starter

Kmelhem

Joined Aug 31, 2010
15
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)

Rich (BB code):
#include <16F877A.H>
void init(void)
  {
  TRISA =0b00001111;
  TRISB =0b00000000;
  PORTA =0;
  PORTB =0;
  }
 
void main () {
 void init(void);
 
 while(true) {
 
  If ((PORTAbits.RA3==0) && (PORTAbits.RA2==0) && (PORTAbits.RA1==0) && (PORTAbits.RA0==1)) {
    If (PORTBbits.RB0==0){
     PORTBbits.RB0=1;
     else
     PORTBbits.RB0=0;
     }
   }
  If ((PORTAbits.RA3==0) && (PORTAbits.RA2==0) && (PORTAbits.RA1==1) && (PORTAbits.RA0==0)) {
    If (PORTBbits.RB1==0){
     PORTBbits.RB1=1;
     else
     PORTBbits.RB1=0;
     }
   }
  If ((PORTAbits.RA3==0) && (PORTAbits.RA2==0) && (PORTAbits.RA1==1) && (PORTAbits.RA0==1)) {
    If (PORTBbits.RB2==0){
     PORTBbits.RB2=1;
     else
     PORTBbits.RB2=0;
     }
   }
  If ((PORTAbits.RA3==0) && (PORTAbits.RA2==1) && (PORTAbits.RA1==0) && (PORTAbits.RA0==0)) {
    If (PORTBbits.RB3==0){
     PORTBbits.RB3=1;
     else
     PORTBbits.RB3=0;
     }
   }
  If ((PORTAbits.RA3==0) && (PORTAbits.RA2==1) && (PORTAbits.RA1==0) && (PORTAbits.RA0==1)) {
    If (PORTBbits.RB4==0){
     PORTBbits.RB4=1;
     else
     PORTBbits.RB4=0;
     }
   }
  If ((PORTAbits.RA3==0) && (PORTAbits.RA2==1) && (PORTAbits.RA1==1) && (PORTAbits.RA0==0)) {
    If (PORTBbits.RB5==0){
     PORTBbits.RB5=1;
     else
     PORTBbits.RB5=0;
     }
   }
  If ((PORTAbits.RA3==0) && (PORTAbits.RA2==1) && (PORTAbits.RA1==1) && (PORTAbits.RA0==1)) {
    If (PORTBbits.RB6==0){
     PORTBbits.RB6=1;
     else
     PORTBbits.RB6=0;
     }
   }
  If ((PORTAbits.RA3==1) && (PORTAbits.RA2==0) && (PORTAbits.RA1==0) && (PORTAbits.RA0==0)) {
    If (PORTBbits.RB7==0){
     PORTBbits.RB7=1;
     else
     PORTBbits.RB7=0;
     }
   }
 
  }
 }
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?
Rich (BB code):
     If (PORTBbits.RB7==0){
     PORTBbits.RB7=1;
     else
     PORTBbits.RB7=0;
     }
Thanks for your help :)
 

t06afre

Joined May 11, 2009
5,934
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
 

elementalrage

Joined Jul 30, 2009
59
i didnt know how to make it interrupt driven input
and will this following part work without a problem?
Rich (BB code):
     If (PORTBbits.RB7==0){
     PORTBbits.RB7=1;
     else
     PORTBbits.RB7=0;
     }
Thanks for your help :)
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".
 

Thread Starter

Kmelhem

Joined Aug 31, 2010
15
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
 

elementalrage

Joined Jul 30, 2009
59
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
Then you can always use the XOR operation on that bit, but without any delay you probably won't see it.

Rich (BB code):
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.
 

Thread Starter

Kmelhem

Joined Aug 31, 2010
15
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?
 

t06afre

Joined May 11, 2009
5,934
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
 

Thread Starter

Kmelhem

Joined Aug 31, 2010
15
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
 

t06afre

Joined May 11, 2009
5,934
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
By the way, all the definitions you need is in the pic168xa.h file. Do not change it, but take a look at it.
 

Thread Starter

Kmelhem

Joined Aug 31, 2010
15
am trying to figure out the way to get an input, koz when using the standard
Rich (BB code):
 if (portA==7)
it doesnt work.
other than that, everything is running smooth
 
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!
 

t06afre

Joined May 11, 2009
5,934
Did you set the ADCON1 register?
From the data sheet
Other PORTA pins are multiplexed with analog inputs
and the analog VREF input for both the A/D converters
and the comparators. The operation of each pin is
selected by clearing/setting the appropriate control bits
in the ADCON1 and/or CMCON registers.
Note:
On a Power-on Reset, these pins are configured as analog inputs and read as ‘0’. The comparators are in the off (digital) state.
 

Thread Starter

Kmelhem

Joined Aug 31, 2010
15
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
Rich (BB code):
PORTB=PORTA;
it doesnt work thx anywayz
 
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.

am trying to figure out the way to get an input, koz when using the standard
Rich (BB code):
 if (portA==7)
it doesnt work.
other than that, everything is running smooth
'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".
 

Thread Starter

Kmelhem

Joined Aug 31, 2010
15
Here is what i mean
Rich (BB code):
#include <htc.h>

void main () {
  int x=0;
  ADCON1=0;
  TRISA =0b11111111;
  TRISB =0b00000000;
  PORTA =0;
  PORTB =0;
PORTA=0x7;
 while(1) {
if (PORTA==0x7){
 PORTB=0xFF;}
else{
RB5=1;}
  
}
}
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
Rich (BB code):
if (RA1=1)
but still didnt work, although bit 1 is 1 coz 7hex is 111 :)
 

t06afre

Joined May 11, 2009
5,934
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"
 

Thread Starter

Kmelhem

Joined Aug 31, 2010
15
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
Rich (BB code):
ADCON1 = 0b00000110;
still not working
 

t06afre

Joined May 11, 2009
5,934
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
 

AlexR

Joined Jan 16, 2008
732
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.
 
Top