PDA

View Full Version : switch ?



barneyfrance
- 12th July 2011, 12:39
Hello,

I'm trying to play with my pic and active some outputs with a switch, but it does not work... I do not see what I am doing wrnog here... can someone help me to figure it out ? it must be something silly ?! (using a pic18F4331)


TRISA = %00000001
TRISB = %00000000
TRISC = %00000000
TRISD = %00000000
TRISE = %00000000


'-> I/O Symbols

Symbol switch PORTA.0 'IN
Symbol led1 PORTB.5 'OUT
Symbol led2 PORTB.4 'OUT
Symbol led3 PORTB.3 'OUT
Symbol led4 PORTB.2 'OUT
Symbol led5 PORTB.1 'OUT
Symbol led6 PORTB.0 'OUT

Symbol led7 PORTD.7 'OUT
Symbol led8 PORTD.6 'OUT
Symbol led9 PORTD.5 'OUT
Symbol led10 PORTD.4 'OUT
Symbol led11 PORTC.7 'OUT
Symbol led12 PORTC.6 'OUT

Low led1
Low led2
Low led3
Low led4
Low led5
Low led6
Low led7
Low led8
Low led9
Low led10
Low led11
Low led12

GoTo Main

Main:
If switch = 1 Then
High led7
Else
Low led7
EndIf

GoTo Main

rsocor01
- 12th July 2011, 14:24
Can you show your schematics?

barneyfrance
- 12th July 2011, 14:29
i'm sorry I am at university, and can't scan my schematics right now. But just to let you know I can successfully run this program on an 16F877...
I really don't know where is the problem. I do the PWM on the 18F but it seems it's not taking the instruction "IF" ??...
Thank you anyway...
If anyone else has a hint or something ?

Bruce
- 12th July 2011, 14:51
Problem #1: RA0 (and more pins) are anlog inputs by default at power-up, and all pins configured as analog inputs will read 0 when used for digital I/O.

Solution = ANSEL0 = 0

Problem #2: This can cause read-modify-write issues since you have all pin aliases pointing to port pins VS port LATches;

Low led1
Low led2
Low led3
Low led4
Low led5
Low led6
Low led7
Low led8
Low led9
Low led10
Low led11
Low led12

Change your pin aliases to LAT like this and include the = sign;

Symbol led1 = LATB.5 'OUT, etc.

barneyfrance
- 12th July 2011, 15:25
Thank you very much for your answer ! it works now !
If I need help again about another thing should I start another thread ? (gonna need to change duty cycle of pwm with potentiometer)
however, I do not want to be invasive.

Bruce
- 12th July 2011, 15:47
You're welcome. For your other question, just continue on in the previous thread you started here http://www.picbasic.co.uk/forum/showthread.php?t=15149

This isn't exactly what you asked for, but here's a link to another thread for using the A/D on this PIC http://www.picbasic.co.uk/forum/showthread.php?t=6768