PDA

View Full Version : How do I get 18f452 pin 10 set as digital input?



jessey
- 11th May 2007, 03:41
Hello,

I've been going over the Data Sheet for the 18F452 trying to figure out how to set up PORTE.2 as a digital input and can't seem to make any head way with it. Could anyone suggest how I could accomplish this? I'm using PORTE.0 (pin 8) for my Lcd Register Select and PORTE.1 (pin 9) for the Lcd Enable. I'm using all the other in/out pins of the 452 and just need this last one to complete my project. Any help will be greatly appreciated.

Thanks
jessey

mister_e
- 11th May 2007, 03:46
all available solution are listed in ADCON1 section.(register 17-2) Since we don't know your whole analog and/or digital need...

RE.2=AN7

jessey
- 12th May 2007, 09:27
Hi Steve,

Sorry for not getting back to you sooner but I got real busy at work the last few days and just got some time to reply. Below is listed all my config, defines and tris settings. I read this in the data sheet for the 18f452 but I don't understand what I'm reading. Maybe if you have the time you could explain what I need to do.

RE2/CS/AN7
PORTE has three pins (RE0/RD/AN5, RE1/WR/AN6
and RE2/CS/AN7) which are individually configurable
as inputs or outputs. These pins have Schmitt Trigger
input buffers.
The PORTE pins become the I/O control inputs for the
microprocessor port when bit PSPMODE (TRISE<4>) is
set. In this mode, the user must make certain that the
TRISE<2:0> bits are set and that the pins are configured
as digital inputs. Also, ensure that ADCON1 is configured
for digital I/O. In this mode, the input buffers are
TTL.
Register 4-1 shows the TRISE register which also
controls the Parallel Slave Port operation.
PORTE pins are multiplexed with analog inputs. When
selected for analog input, these pins will read as ‘0’s.
TRISE controls the direction of the RE pins, even when
they are being used as analog inputs. The user must
make sure to keep the pins configured as inputs when
using them as analog inputs.

Thanks
jessey



--------------
| 18F452 |
MCLR/Vpp ------> |1 40| <-----> RB7/PGD
RA0/AN0 <-----> |2 39| <-----> RB6/PGC
RA1/AN1 <-----> |3 38| <-----> RB5/PGM
RA2/AN2/Vref- <-----> |4 37| <-----> RB4
RA3/AN3/Vref+ <-----> |5 36| <-----> RB3/CCP2*
RA4/TOCK1 <-----> |6 35| <-----> RB2/INT2
RA5/AN5/SS/LVDIN <-----> |7 34| <-----> RB1/INT1
RE0/RD/AN5 <-----> |8 33| <-----> RB0/INT0
RE1/WR/AN6 <-----> |9 32| <----- Vdd +
RE2/CS/AN7 <-----> |10 31| <----- Vss -
+ Vdd -----> |11 30| <-----> RD7/PSP7
_ VSS -----> |12 29| <-----> RD6/PSP6
OSC1/CLK1 -----> |13 28| <-----> RD5/PSP5
OSC2/CLKO/RA6 <------ |14 27| <-----> RD4/PSP4
RC0/T1OSO/TICK1 <-----> |15 26| <-----> RC7/RX/DT
RC1/T1OSI/CCP2* <-----> |16 25| <-----> RC6/TX/CK
RC2/CPP1 <-----> |17 24| <-----> RC5/SDO
RC3/SCK/SCL <-----> |18 23| <-----> RC4/SD1/SDA
RD0/PSP0 <-----> |19 22| <-----> RD3/PSP3
RD1/PSP1 <-----> |20 21| <-----> RD2/PSP2
--------------

--------------
| 18F452 |
MCLR |1 40| Water_Valve_1
Buzzer |2 39| Water_Valve_2
Up_Push_Button |3 38| Water_Valve_3
Down_Push_Button |4 37| Water_Valve_4
Instructions_Push_Button |5 36| Water_Valve_5
Select2_Push_Button |6 35| Water_Valve_6
Select_Push_Button |7 34| The_Main_Water_Valve
Lcd |8 33| The_Nutrient_Pump
Lcd |9 32| <----- Vdd +
Need Digital Input here |10 31| <----- Vss -
+ Vdd -----> |11 30| Lcd
- VSS -----> |12 29| Lcd
OSC1 |13 28| Lcd
OSC2 |14 27| Lcd
Communications_Port_1 |15 26| Nutrient_Valve_1
Communications_Port_2 |16 25| Nutrient_Valve_2
Communications_Port_3 |17 24| Nutrient_Valve_3
Communications_Port_4 |18 23| Nutrient_Valve_4
Communications_Port_5 |19 22| Nutrient_Valve_5
Communications_Port_6 |20 21| Nutrient_Valve_6
--------------

'-------------------- PORTA PINS --------------------'
TRISA.0 = 0 '(pin 2) ................ Buzzer
TRISA.1 = 1 '(pin 3) ................ Up_Push_Button
TRISA.2 = 1 '(pin 4) ................ Down_Push_Button
TRISA.3 = 1 '(pin 5) ................ Instructions_Push_Button
TRISA.4 = 1 '(pin 6) ................ Select2_Push_Button
TRISA.5 = 1 '(pin 7) ................ Select_Push_Button

'-------------------- PORTB PINS --------------------'

TRISB.0 = 0 '(pin 33) ............... The_Nutrient_Pump
TRISB.1 = 0 '(pin 34) ............... The_Main_Water_Valve
TRISB.2 = 0 '(pin 35) ............... Water_Valve_6
TRISB.3 = 0 '(pin 36) ............... Water_Valve_5
TRISB.4 = 0 '(pin 37) ............... Water_Valve_4
TRISB.5 = 0 '(pin 38) ............... Water_Valve_3
TRISB.6 = 0 '(pin 39) ............... Water_Valve_2
TRISB.7 = 0 '(pin 40) ............... Water_Valve_1

'-------------------- PORTC PINS --------------------'
TRISC.0 = 1 '(pin 15) ............... Communications_Port_1
TRISC.1 = 1 '(pin 16) ............... Communications_Port_2
TRISC.2 = 1 '(pin 17) ............... Communications_Port_3
TRISC.3 = 1 '(pin 18) ............... Communications_Port_4
TRISC.4 = 0 '(pin 23) ............... Nutrient_Valve_4
TRISC.5 = 0 '(pin 24) ............... Nutrient_Valve_3
TRISC.6 = 0 '(pin 25) ............... Nutrient_Valve_2
TRISC.7 = 0 '(pin 26) ............... Nutrient_Valve_1

'-------------------- PORTD PINS --------------------'

TRISD.0 = 1 '(pin 19) ............... Communications_Port_5
TRISD.1 = 1 '(pin 20) ............... Communications_Port_6
TRISD.2 = 0 '(pin 21) ............... Nutrient_Valve_6
TRISD.3 = 0 '(pin 22) ............... Nutrient_Valve_5
TRISD.4 = 0 '(pin 27) ............... Lcd Data bit 4
TRISD.5 = 0 '(pin 28) ............... Lcd Data bit 5
TRISD.6 = 0 '(pin 29) ............... Lcd Data bit 6
TRISD.7 = 0 '(pin 30) ............... Lcd Data bit 7

'-------------------- PORTE PINS --------------------'

TRISE.0 = 0 '(pin 8) ............... Lcd Register select
TRISE.1 = 0 '(pin 9) ............... Lcd Enable
TRISE.2 = 1 '(pin 10) ............... Need Digital Input here

@ __CONFIG _CONFIG1H, _OSCS_ON_1H & _XT_OSC_1H
@ __CONFIG _CONFIG2L, _BOR_OFF_2L & _BORV_20_2L & _PWRT_ON_2L
@ __CONFIG _CONFIG2H, _WDT_ON_2H & _WDTPS_128_2H
@ __CONFIG _CONFIG3H, _CCP2MX_OFF_3H
@ __CONFIG _CONFIG4L, _STVR_OFF_4L & _LVP_OFF_4L & _DEBUG_OFF_4L
@ __CONFIG _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L & _CP2_OFF_5L & _CP3_OFF_5L
@ __CONFIG _CONFIG5H, _CPB_OFF_5H & _CPD_OFF_5H
@ __CONFIG _CONFIG6L, _WRT0_OFF_6L & _WRT1_OFF_6L & _WRT2_OFF_6L & _WRT3_OFF_6L
@ __CONFIG _CONFIG6H, _WRTC_OFF_6H & _WRTB_OFF_6H & _WRTD_ON_6H
@ __CONFIG _CONFIG7L, _EBTR0_OFF_7L & _EBTR1_OFF_7L & _EBTR2_OFF_7L & _EBTR3_OFF_7L
@ __CONFIG _CONFIG7H, _EBTRB_OFF_7H

DEFINE OSC 4
Define LCD_DREG PORTD
Define LCD_DBIT 4
Define LCD_RSREG PORTE
Define LCD_RSBIT 0
Define LCD_EREG PORTE
Define LCD_EBIT 1

ADCON1 = 7 ' Sets portA & E outputs to Digital

'-------------------------------------------'
'----------- Configure Port Pins -----------'
'-------------------------------------------'
Up_Push_Button VAR PORTA.1 '(pin 3)
Down_Push_Button VAR PORTA.2 '(pin 4)
Instructions_Push_Button VAR PORTA.3 '(pin 5)
Select_Push_Button VAR PORTA.5 '(pin 7)
Select2_Push_Button VAR PORTA.4 '(pin 6)
Buzzer VAR PORTA.0 '(pin 2)
Need Digital Input Here VAR PORTE.1 '(pin 10)
The_Nutrient_Pump VAR PORTB.0 '(pin 33)
The_Main_Water_Valve VAR PORTB.1 '(pin 34)
Water_Valve_1 VAR PORTB.7 '(pin 40)
Water_Valve_2 VAR PORTB.6 '(pin 39)
Water_Valve_3 VAR PORTB.5 '(pin 38)
Water_Valve_4 VAR PORTB.4 '(pin 37)
Water_Valve_5 VAR PORTB.3 '(pin 36)
Water_Valve_6 VAR PORTB.2 '(pin 35)
Communications_Port_1 VAR PORTC.0 '(pin 15)
Communications_Port_2 VAR PORTC.1 '(pin 16)
Communications_Port_3 VAR PORTC.2 '(pin 17)
Communications_Port_4 VAR PORTC.3 '(pin 18)
Communications_Port_5 VAR PORTD.0 '(pin 19)
Communications_Port_6 VAR PORTD.1 '(pin 20)
Nutrient_Valve_1 VAR PORTC.7 '(pin 26)
Nutrient_Valve_2 VAR PORTC.6 '(pin 25)
Nutrient_Valve_3 VAR PORTC.5 '(pin 24)
Nutrient_Valve_4 VAR PORTC.4 '(pin 23)
Nutrient_Valve_5 VAR PORTD.3 '(pin 22)
Nutrient_Valve_6 VAR PORTD.2 '(pin 21)

INCLUDE "EE_Vars.PBP"

mister_e
- 12th May 2007, 09:44
so basically you need everything digital right?

ADCON1=7
and for safety sake
SSPCON1=0

this have to do the trick.. if not, you have a faulty PIC, faulty hardware.. or i need another drink ... 5:00 AM Here... still trying to forget those beauties that Lerameur bring to the night club tonight... mmmmmm


INCLUDE "EE_Vars.PBP"
:D once you're done, please post some of your code that show/use that nice feature... at very least for others forum member benefit.. and Darrel's pleasure.

jessey
- 12th May 2007, 15:49
Hi Steve,

That's great Steve, thanks for the code, I really appericate it. I'm off to work now (Saturday), I've worked every day this week and now the weekend!!.... Oh well. I'll be sure to post my code after I get a chance to try your suggestion and clean it up a bit. Give me a couple of days.

Thanks Again
jessey

Pic_User
- 12th May 2007, 16:01
Hi jessey,

Oh well. I'll be sure to post my code after I get a chance to try your suggestion and clean it up a bit. Give me a couple of days.
I have to mention; Your coding style is quite easy to read and contains a lot of helpful information. So even though you are fairly new to PICs you code style is extremely helpful to us beginners. I am learning from it, and looking forward to your code posting.
Thanks,
-Adam-

mister_e
- 12th May 2007, 18:38
well commented/documented code is indeed the trick. Well done!

jessey
- 26th May 2007, 07:59
Hi jessey,
looking forward to your code posting.
Thanks,
-Adam-

Hello Steve & Adam,

Sorry for taking so long to post my code but I've been pretty busy lately and it took a bit to go through the program and try and make the comments make sense. I just hope you can follow the flow of the program. If you have any questions then feel free to ask.

This program might be a little hard to follow because of all the Dummy variables that I used, I'm not sure if I'm using them correctly or if there's a better way of doing it but using the Dummy var's has saved me a lot of code space and the program seems to be working perfectly. Without them I definitely wouldn't have enough code space on my 18f452 for all the features I want to implement. There is still a lot I can do to save code space but its still a work in progress, I still have timing issues to resolve as well. Any comments or suggestions from anyone will be very much appreciated especially when it comes to using the Dummy variables.

I didn't have time to go through Darrels Strings routine to comment it properly but I can say it works great. When I get all the features in my program that I want then I'll finish the Strings by including an overview of the programs features and instructions on how to set it up. Its great to have a manual programmed into the chip, better than written instructions because you can't loose them. In Darrels Strings I can get 142 prints in GetInstructions1 alone, plus probably well over 100 in the other GetInstructions ones that I'm using. That's a great routine Darrel and I thank you again for it.

Oh, Steve I tried using SSPCON1=1 and I still can't seem to get pin 10 working as either a digital input or output. I tried it on a couple of different 452's with the same results?

Thanks
jessey

skimask
- 26th May 2007, 08:25
Oh, Steve I tried using SSPCON1=1 and I still can't seem to get pin 10 working as either a digital input or output. I tried it on a couple of different 452's with the same results?
Thanks
jessey

Very well commented code indeed...BUT...the code, as a whole, looks entirely too over-written, over-designed, over-complicated, for the end result. This almost looks like something written for the Space Shuttle or something :)

At any rate, back to the PortE.2 thing...
Back to the basics is all I can say. Get a fresh breadboard, get a fresh PIC, a push button and an LED. Put the button on PortE.2 with a pullup (or a pulldown, whatever works), put the LED on another Port, whichever you want. Write a program that makes the LED follow the button's input, and ONLY the button's input...like:

button var porte.2:input button:led var portb.0: output led:led=0

main:
if button = 1 then LED = 1
if button = 0 then LED = 0
goto main

If that doesn't work right off the bat, then start playing with the config registers until things start working.

I've read other posts from you, I know you've got a decent grasp on how to run these PICs. Sometimes a guy just forgets something stupid and silly and it kicks him in the arse for days...gotta break it back down to the basics.
For all you know, the lead going to PortE.2 is open somewhere, or the switch itself is shot.

jessey
- 26th May 2007, 15:29
Very well commented code indeed...BUT...the code, as a whole, looks entirely too over-written, over-designed, over-complicated, for the end result. This almost looks like something written for the Space Shuttle or something :)

Hi skimask,

Is the code that complicated? Yea I guess it is somewhat but what I was thinking to do when I was writing it, well for one code segment below:

The Dummy_Water_Valve holds the value of Water_Valve_1 to 6 for PORTB.2 to PORTB.7 so I use the Dummy_Water_Valve var to turn On or Off any of the 6 Water valves. The same thing applies to the Dummy_Nutrient_Valve variable but with different PORT pins of course.

I wanted to know if a Water Valve on PORTB.2 to PORTB.7 was involved in a watering cycle, so for that I'm using the Dummy_Water_Valve_Number variable. What happens is when a Moisture meter sends a request to start a watering cycle then its valve is turned on using the Dummy_Water_Valve var and at the same time the
Dummy_Water_Valve_Number var is set to Is_Watering so the program can use this var to know that there's an active watering cycle happening for that Moisture Meter. One way that the watering cycle can be turned Off is when the Moisture Meter that started the watering cycle sends a + signal for 2500ms to stop the watering and that sets Dummy_Water_Valve = Is_Turned_Off and at the same time the Dummy_Water_Valve_Number = Is_Not_Watering so the program knows what's going on. The other way of turning off the watering cycle is to use the Dummy_Off_Tm timer and that's pretty straight forward, its just a timer that when set it enables the auto Off, it counts up to a set time and shuts off the watering cycle using the exact same code segment that the Moisture meter uses to shut Off the watering cycle.

Using that logic then when we're implementing the watering cycle then we first check if the Dummy_Water_Valve_Number is watering and if so then if the Dummy_Water_Valve is On, then we implement the On_Time_counter or if the Dummy_Water_Valve is Off then we implement the Off_Time_Counter. Also (ELSE) if the Dummy_Water_Valve_Number is not watering then we just implement a Calibration Pause to maintain a one second time base for the timers.

That seems pretty basic to me but that's only one segment of the code. What part of the code were you having problems following or think is over done or complicated (not that it isn't somewhat complicated)? I'm sure that some of the other beginners here might be interested, I know I would if you have the time to try and follow the flow of the program. If you (or anyone else here for that mater) finds a part of the code that's confusing or redundant then I'll try and explain my logic for that part and or change it to flow better.

Back to the PortE.2 thing, I'm sure its not the bread board because when I set pin 10 to an output then I test a Led to make sure that its working then I touched the led directly on the port pin and it doesn't turn On/Off like it should. Real strange, I tried 4 different 452's and the same results, mind you they are all ones that are used but still? I'll try getting a few more new ones and play around with the config settings to see what results I get.

Thanks
jessey

skimask
- 26th May 2007, 18:32
Back to the PortE.2 thing, I'm sure its not the bread board because when I set pin 10 to an output then I test a Led to make sure that its working then I touched the led directly on the port pin and it doesn't turn On/Off like it should. Real strange, I tried 4 different 452's and the same results, mind you they are all ones that are used but still? I'll try getting a few more new ones and play around with the config settings to see what results I get.

Thanks
jessey

That's the thing...you tested for an output, not an input. Have you tried something really silly like reloading PBP and MPLAB? Ya never know there either.

As far as the '452 goes, you might want to switch over to a '4620, twice the code space, lot more ram, bunch more options, same price, almost 100% firmware compatible to the '452, and it is pin compatible with the '452. When I built my player, I started off with a 16F877, switched to a '452, then a '4620, all on the same PCB, and very little firmware change during the process.

As far as the program goes, I'm not saying that what you wrote is crap...far from it. I can easily follow everything you want to do. I'm just thinking that a lot of what you wrote can be rewritten a lot smaller and more compact. If I get some time over this long weekend, I'll take a deeper look at the code and see what I can come up with. One thing you might want to do, and I don't know if it'll have anything to do with anything or fix anything, is move some of the blocks around...

1-CONFIGs
2-DEFINEs
3-Variable assignments
4-Jump over everything to the main loop
5-Interrupt code and subroutines (don't forget the disable)
6-Subroutines (don't forget the enable)
7-Main Loop