Forum: Mikrocontroller und Digitale Elektronik Eingang lesen beim PIC


von Karlheinz D. (kdruschel)


Lesenswert?

Hallo,

ich habe ein kleines Problerm beim Lesen eines Eingangs bei einem 
PIC16F887.
Zum Testen will ich einfach nur eine LED leuchten lassen wenn ein Taster 
gedrückt wird. Also was ganz banales. Die LED hängt an Portbit D7, sie 
leuchtet auch wenn ich sie per Software ansteuere. Der Taster hängt an 
B4 und ist so verdrahtet, dass er im gedrückten Zustand 5 V an den Pin 
legt. Mit dem Multimeter gemessen funktioniert das auch. Aber die 
Software will halt nicht.....
1
/******************************************************************************/
2
/* Files to Include                                                           */
3
/******************************************************************************/
4
5
#include <xc.h>         /* XC8 General Include File */
6
#define _XTAL_FREQ 20000000
7
8
#include  <pic.h>
9
//#include <stdlib.h>
10
#include <stdio.h>
11
#include <stdint.h>        /* For uint8_t definition */
12
#include <stdbool.h>       /* For true/false definition */
13
#include "Main.h"
14
15
/******************************************************************************/
16
/* User Global Variable Declaration                                           */
17
/******************************************************************************/
18
19
//
20
// PROCESSOR : PIC16F877A
21
22
#pragma config FOSC = INTRC_NOCLKOUT// Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
23
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
24
#pragma config PWRTE = ON       // Power-up Timer Enable bit (PWRT enabled)
25
#pragma config MCLRE = ON       // RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
26
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
27
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
28
#pragma config BOREN = ON       // Brown Out Reset Selection bits (BOR enabled)
29
#pragma config IESO = ON        // Internal External Switchover bit (Internal/External Switchover mode is enabled)
30
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)
31
#pragma config LVP = OFF        // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)
32
33
// CONFIG2
34
#pragma config BOR4V = BOR40V   // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
35
#pragma config WRT = OFF        // Flash Program Memory Self Write Enable bits (Write protection off)
36
37
38
39
int main()
40
{
41
    Init();
42
43
//    PORTDbits.RD7 = 1;
44
    
45
    while (1)
46
    {
47
        if (PORTBbits.RB4)
48
            PORTDbits.RD7 = 1;
49
        else
50
            PORTDbits.RD7 = 0;
51
    }
52
}
53
54
/////////////////////////////////////////////////////////////////////
55
// Initialisierungen
56
/////////////////////////////////////////////////////////////////////
57
void Init(void)
58
{
59
    TRISB = 0xff;
60
     PORTD = 0x0;    
61
    TRISD = 0x0;
62
    TRISE = 0xff;
63
}

von Volker S. (vloki)


Lesenswert?

Kann der Eingang auch analog verwendet werden?
Dann ist das im Normalfall standardmäßig aktiviert!
In Datasheet ist da normalerweise auf der ersten Seite (zum Port) eine 
hervorgehobene "Note"

: Bearbeitet durch User
von Christian M. (Gast)


Lesenswert?

Dieser Eingang kann auch Analogeingang sein. Prüfe im Datenblatt, ob im 
ANSELH Register auf Digital I/O eingestellt werden muss.

Gruss Chregu

von Karlheinz D. (kdruschel)


Lesenswert?

Yap, das war's
1000Dank

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.