I connected 4 buttons to PORTA's first four pin and also I connected 8 leds to PORTB and a buzzer PORTD 2. pin. You know that PIC16F877's PORTA comes in a analog mode so I used ADCON1=7 and I turned them to digital mode. (If you use PORTA and you do not use ADCON1=7, the code won't work. I tried and I saw that.)
Then, I used this code and it worked, Thanks everybody...
Code:
MY CODE:
'---> PORT DECLARATIONS
TRISB = %00000000 ' Make Port B's all pins output
OUTPUT PORTD.2 ' Make Port D's 2. pin output
TRISA = %00001111 ' Make Port A's 0-4 pins input
PORTB = 0 ' Make Port B's all pins 0 Volt
PORTD.2 = 0 ' Make Port D's 2. pin 0 Volt
'---> MAIN CODE
counter VAR BYTE ' Counter for different sounds.
PortBPin VAR BYTE ' This is for turning on leds sequential order.
'Any PICmicro MCU with analog inputs, such as the PIC16C7xx,
'PIC16F87x and PIC12C67x series devices, will come up in analog mode.
'You must set them to digital if that is how you intend to use them:
ADCON1 = 7
loop: 'Our main loop
' For Button T0
IF PORTA.0 = 0 THEN ' If button T0 is pressed then generate different sounds. Counter increment rate is 10.
FOR counter=10 to 130 STEP 10
SOUND PORTD.2, [counter,10,60,10] ' Generate Sound
NEXT counter
ENDIF
' For Button T1
IF PORTA.1 = 0 THEN ' If button T1 is pressed then turn on leds 0 to 7
PORTD.2 = 0 ' Make Port D's 2. pin 0 Volt
FOR PortBPin=0 to 7 STEP 1
HIGH PortBPin
PAUSE 200
LOW PortBPin
NEXT PortBPin
ENDIF
' For Button T2
IF PORTA.2 = 0 THEN ' If button T1 is pressed then turn on leds 7 to 0
PORTD.2 = 0 ' Make Port D's 2. pin 0 Volt
FOR PortBPin=7 to 0 STEP -1
HIGH PortBPin
PAUSE 200
LOW PortBPin
NEXT PortBPin
ENDIF
' For Button T3
IF PORTA.3 = 0 THEN ' If button T3 is pressed then turn on leds inside to outside then outside to inside.
PORTD.2 = 0 ' Make Port D's 2. pin 0 Volt (Buzzer'ı kapatır.)
PORTB = %00011000 ' Port B's middle 4. and 5. pins are turns on.
PAUSE 300 ' Wait 300 ms
PORTB = %00100100 ' Port B's middle 3. and 6. pins are turns on.
PAUSE 300 ' Wait 300 ms
PORTB = %01000010 ' Port B's middle 2. and 7. pins are turns on.
PAUSE 300 ' Wait 300 ms
PORTB = %10000001 ' Port B's middle 1. and 8. pins are turns on.
PAUSE 300 ' Wait 300 ms
PORTB = %01000010 ' Port B's middle 2. and 7. pins are turns on.
PAUSE 300 ' Wait 300 ms
PORTB = %00100100 ' Port B's middle 3. and 6. pins are turns on.
PAUSE 300 ' Wait 300 ms
PORTB = %00011000 ' Port B's middle 4. and 5. pins are turns on.
PAUSE 300 ' Wait 300 ms
ENDIF
GOTO loop ' Do it forever
END ' End of our program.
Bookmarks