Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

how to initialize PORT A of 18F4520 as input

Status
Not open for further replies.

samz kazi

Newbie level 6
Newbie level 6
Joined
Feb 26, 2013
Messages
12
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,405
hii... i m using PIC 18f4520 in my project...i m writing the code in C language using MikroC software...i have to give output of comparator(0v or 5v) as an input to PIC 18f4520...when i run a comparator separately on simulator, it works..but when i connect it to PIC as an input to port A, it doesnt work....will someone help me out of this? i need a code to initialize PORT A of PIC as an input from comparator...plzzz plzzz help.
 

you have to assign the particular pin as input.
by defining,
TRISA = 0b00001000; // 0 - output , 1 - input. here RA3 as input and rest as output.

Best wishes :)
 

i tried this my friend...but it is not working..e.g.when i connect switch to any pin of port A, then according to the program the o/p LED should turn on when the switch is closed..this is not happening when i m using port A....but same function works when i use port B as an input port...i need more port pins, that's why i m looking for PORTA otherwise i could have used port B as input....so plzzz plzzzz help.
 

Hi Kazi,
PORTA is bidirectional port, can be digital or analog,
Data direction register is TRISA, if you set "1" in TRISA reg the pin will be input, if you set "0" pin will be output (Hemnath explaine this in previous post)
ADCON1 register configures the function of pins on PORTA, to be digital or analog,
if you need digital input you must set PCFG<3:0> bits on ADCON1 register like ADCON1 = 0b00001111 to use PORTA pins as digital,
you must read 18F4520 manual detailed in AD converter module,
best regards
 

I assume that output of comparator is digital. In that case, you have declare associate PORTA pin as digital pin.
 

thanks friends...ADCON1 = 0b00001111 statement was missing...so its doing well now..thanks..

- - - Updated - - -

guys...please help..the following program is not working..if you guys finding some error..let me know plz..we have made that code by our own..so there is a possiblity of getting more error...plzzzzzzzz help.
according to program, if any one sensor is dry, or couple of sensors or 3 or 4 sensors are dry, the motor should be on and the solenoid valve corresponding to that sensor should on..eg.if sensor 1 and 2 are dry, then solenoid 1 and 2 should on and all other should off.....so plz check out and let me know if there is any error...
for this i m using MikroC software and trying to run it on Proteus simulator.....thankss





/* PIC 18F4520
Internal Oscillator Initialized
Solenoid Status > 0 = ON
All LEDs Common Anode
Sensor Dry > Output 0
Sensor Wet > Output 1
TRIS Initialization Important
*/

#define s0 PORTA.F0 /* Level Sensor */
#define s1 PORTA.F1 /* Sensor 1 */
#define s2 PORTA.F2 /* Sensor 2 */
#define s3 PORTA.F3 /* Sensor 3 */
#define s4 PORTA.F4 /* Sensor 4 */
#define motor LATB.F0 /* Motor */
#define motor_s LATB.F1 /* Motor Status */
#define sol_1 LATC.F1 /* Solenoid 1 */
#define sol_2 LATC.F2 /* Solenoid 2 */
#define sol_3 LATC.F3 /* Solenoid 3 */
#define sol_4 LATC.F4 /* Solenoid 4 */

void main(){
TRISA = 0xff; /* PORTA Input */
TRISB = 0x00; /* PORTB Output */
TRISC = 0x00; /* PORTC Output */
PORTA = 0x00;
PORTB = 0x00;
PORTC = 0x00;
CMCON = 0x07; /* Disable Comparator */
ADCON0 = 0b00000000; /* Configure all I/O Ports as Digital */
ADCON1 = 0b00001111; /* Configure all I/O Ports as Digital */

while (1) /* Continuous Loop */
{

if (s0==1) /* Sufficient Water In Tank */
{

if ((s1||s2||s3||s4)==0) /* Either Sensor 1, Sensor 2, Sensor 3 or Sensor 4 Dry */
{
motor = 1; /* Motor ON */
motor_s = 0; /* Motor Status ON */
}
else if (s1==0) /* Sensor 1 Dry */
{
sol_1=0; /* Solenoid 1 ON */
sol_2=1; /* Solenoid 2 OFF */
sol_3=1; /* Solenoid 3 OFF */
sol_4=1; /* Solenoid 4 OFF */
}
else if (s2==0) /* Sensor 2 Dry */
{
sol_1=1; /* Solenoid 1 OFF */
sol_2=0; /* Solenoid 2 ON */
sol_3=1; /* Solenoid 3 OFF */
sol_4=1; /* Solenoid 4 OFF */
}
else if (s3==0) /* Sensor 3 Dry */
{
sol_1=1; /* Solenoid 1 OFF */
sol_2=1; /* Solenoid 2 OFF */
sol_3=0; /* Solenoid 3 ON */
sol_4=1; /* Solenoid 4 OFF */
}
else if (s4==0) /* Sensor 4 Dry */
{
sol_1=1; /* Solenoid 1 OFF */
sol_2=1; /* Solenoid 2 OFF */
sol_3=1; /* Solenoid 3 OFF */
sol_4=0; /* Solenoid 4 ON */
}
else if ((s1&&s2)==0) /* Sensor 1 And Sensor 2 Dry */
{
sol_1=0; /* Solenoid 1 ON */
sol_2=0; /* Solenoid 2 ON */
sol_3=1; /* Solenoid 3 OFF */
sol_4=1; /* Solenoid 4 OFF */
}
else if ((s2&&s3)==0) /* Sensor 2 And Sensor 3 Dry */
{
sol_1=1; /* Solenoid 1 OFF */
sol_2=0; /* Solenoid 2 ON */
sol_3=0; /* Solenoid 3 ON */
sol_4=1; /* Solenoid 4 OFF */
}
else if ((s3&&s4)==0) /* Sensor 3 And Sensor 4 Dry */
{
sol_1=1; /* Solenoid 1 OFF */
sol_2=1; /* Solenoid 2 OFF */
sol_3=0; /* Solenoid 3 ON */
sol_4=0; /* Solenoid 4 ON */
}
else if ((s1&&s3)==0) /* Sensor 1 And Sensor 3 Dry */
{
sol_1=0; /* Solenoid 1 ON */
sol_2=1; /* Solenoid 2 OFF */
sol_3=0; /* Solenoid 3 ON */
sol_4=1; /* Solenoid 4 OFF */
}
else if ((s1&&s4)==0) /* Sensor 1 And Sensor 4 Dry */
{
sol_1=0; /* Solenoid 1 ON */
sol_2=1; /* Solenoid 2 OFF */
sol_3=1; /* Solenoid 3 OFF */
sol_4=0; /* Solenoid 4 ON */
}
else if ((s2&&s4)==0) /* Sensor 2 And Sensor 4 Dry */
{
sol_1=1; /* Solenoid 1 OFF */
sol_2=0; /* Solenoid 2 ON */
sol_3=1; /* Solenoid 3 OFF */
sol_4=0; /* Solenoid 4 ON */
}
else if ((s1&&s2&&s3)==0) /* Sensor 1, Sensor 2 And Sensor 3 Dry */
{
sol_1=0; /* Solenoid 1 ON */
sol_2=0; /* Solenoid 2 ON */
sol_3=0; /* Solenoid 3 ON */
sol_4=1; /* Solenoid 4 OFF */
}
else if ((s2&&s3&&s4)==0) /* Sensor 2, Sensor 3 And Sensor 4 Dry */
{
sol_1=1; /* Solenoid 1 OFF */
sol_2=0; /* Solenoid 2 ON */
sol_3=0; /* Solenoid 3 ON */
sol_4=0; /* Solenoid 4 ON */
}
else if ((s1&&s3&&s4)==0) /* Sensor 1, Sensor 3 And Sensor 4 Dry */
{
sol_1=0; /* Solenoid 1 ON */
sol_2=1; /* Solenoid 2 OFF */
sol_3=0; /* Solenoid 3 ON */
sol_4=0; /* Solenoid 4 ON */
}
else if ((s1&&s2&&s3&&s4)==0) /* Sensor 1, Sensor 2, Sensor 3 And Sensor 4 Dry */
{
sol_1=0; /* Solenoid 1 ON */
sol_2=0; /* Solenoid 2 ON */
sol_3=0; /* Solenoid 3 ON */
sol_4=0; /* Solenoid 4 ON */
}
else if ((s1&&s2&&s3&&s4)==1) /* Sensor 1, Sensor 2, Sensor 3 And Sensor 4 Wet */
{
sol_1=1; /* Solenoid 1 ON */
sol_2=1; /* Solenoid 2 ON */
sol_3=1; /* Solenoid 3 ON */
sol_4=1; /* Solenoid 4 ON */
motor=0; /* Motor OFF */
motor_s=1; /* Motor Status OFF */
}
else
{
motor=0; /* Motor OFF */
motor_s=1; /* Motor Status OFF */
}
}
else
{
motor=0; /* Motor OFF */
motor_s=1; /* Motor Status OFF */
}
}
}
 

While using any microcontroller always check what peripherals are connected to the port/pin u r using and turn off every thing else.....

I don't know why Microchip guys have kept some peripherals(like ADC/ LVP etc) ON by default....so u need to turn off them before using...
This is not the problem with atmel since they have turned off everything by default....and u turn on whatever u want to use.....
 

First use only one sensor and make it work correctly. If it works then you can expand as many sensor as you need without any problem. So, code can be short and clear. You and all can identify the error easily and make program works.

Sensor output is digital or analog?

Edit: Use
Code:
 tag to see code clear
 
Last edited:

its a digital....output of comparator( 0v or 5v) is given to pic18f4520 as an input
 

i need to use that port only...because i have few more connections on other port like gsm module, water level detector and so on

- - - Updated - - -

one more interesting thing..it takes "and function as or function" eg. (s1&&s2) function works as (s1||s2) and vice versa
 

what I mean is (0&&0&&0 => 0) , (0||0||0 => 0)...


ok..then what should i write if i want to check whether 3 sensors are 0??? (s1&&s2&&s3==0) is this correct?
if not..then plz tell what should i write?
 

thank you...thank you...thank you...thank you...thank you...thank you...very much......its workingggggggggggggg:)

- - - Updated - - -

i want a code for sending a message through GSM module....can u help me?

i m using GSM SIM 300 module, PIC18F4520 IC and writing code in C language using MiKroC....the condition is that if sensor s0=1 then the GSM Module should active and send a message that "Water Level Is Not Sufficient" to any particular number 1234567800...
 

Coding for receive, reading, parsing and act based on SMS command is little bit complicated. If you use LCD Module and Keyboard then it is fine. You can directly store the required phone numbers. But, if you don't use Keyboard, you must code for the sequence that I mentioned. Let me know whether you use LCD and Key Board. NOTE: But, still you need to code as per my sequence in order to run/off motors based on SMS Command (NOT from Ring Tone, it is quite simple compared to SMS Command).
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top