Discussion in "PIC Microcontroller Discussion" started by    laki    Dec 14, 2007.
Fri Dec 14 2007, 12:38 pm
#1
i'm using micro c compiler for pic16f877a. i don't know how to get input from a port i.e my statement in C is
variable=PORTB;
but it won't work.. what do i do??
Fri Dec 14 2007, 12:49 pm
#2
well that code is correct for reading value from PORTB.
make sure you have made it as input port..

also may i see your program..to know better what you are doing..?
Fri Dec 14 2007, 12:59 pm
#3
here's the prog. i'm trying to read a value from port B and control a motor depending on the value. PORTA is connected to an l293d.

void forward();
void right();
void left();
void stopped();

void forward(void)
{
PORTA=0B11010100;
}
void stopped(void)
{
PORTA=0x00;
}
void right(void)
{
PORTA=0b10010000;
}
void left(void)
{
PORTA=0b01000100;
}

void main()
{
TRISA=0X00;
TRISB=0B11111111;
CMCON=0x07;

while(1)
{
 getval=PORTB;
 Delay_500us();
 if(getval==0)
 {
 stopped();
 }
 else if(getval==1)
 {
 forward();
 }
 else if(getval==3)
 {
 right();
 }
 else
 {
 left();
 }
 }
 }



[ Edited Fri Dec 14 2007, 01:10 pm ]
Fri Dec 14 2007, 01:11 pm
#4
can you tell me the name of compiler you are using? are you sure you are not getting any compiler errors?
Fri Dec 14 2007, 01:17 pm
#5
Micro C compiler. i'm getting an error in the line
getval=PORTB;
Fri Dec 14 2007, 01:24 pm
#6
what is the error?
make sure you include the header file relevant to your controller.

tell me is it the same compiler?
http://www.programmersheaven.com/download/2670/download.aspx
or
http://www.ccsinfo.com/


[ Edited Fri Dec 14 2007, 01:38 pm ]
Fri Dec 14 2007, 01:44 pm
#7
http://www.mikroe.com/

i think i didn't declare the getval variable...
Fri Dec 14 2007, 01:45 pm
#8
oops! even i did not see it lol!
define it as a
unsigned char getval;
Fri Dec 14 2007, 01:48 pm
#9
why shouldn't i declare it as unsigned int... ??
Fri Dec 14 2007, 02:16 pm
#10
because your port is 8-bit.. means data will be of 1 byte only.. so why to waste 2 bytes using INT?
always try to keep your variables as unsigned char or char..when you are dealing with 8-bit data... this is a kind of optimization tip. RAM usage will be less in this case..
 laki like this.

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

ChrisLub
Tue Apr 23 2024, 05:21 pm
Davidbab
Tue Apr 23 2024, 10:41 am
Richardrit
Tue Apr 23 2024, 09:54 am
HenryLaf
Mon Apr 22 2024, 03:50 pm
bleradrar
Mon Apr 22 2024, 06:38 am
ppu-pro_ka
Sun Apr 21 2024, 07:39 pm
Infewow
Sun Apr 21 2024, 06:30 pm
HumanTak
Sun Apr 21 2024, 12:26 pm