PIC18F14k22 Beispiele C

OP #3656098
Lesenswert?

Hallo,

ich wollte fragen, ob wer eine gute Seite kennt wo gut erklärt wird wie 
ich mit meinem PICkit 3 StarterKit in MPLAB X ein einfaches Programm in 
C schreibe, zB nur eine LED zum Blinken zu bringen?

Kenne mich da nicht so gut aus, und lerne am besten von 
Beispielprogrammen.

Schonmal Danke
#3656115
Lesenswert?

Sollte so funktionieren...
1
#include <xc.h>
2

3
#pragma config FOSC = IRC       // Oscillator Selection bits (Internal RC oscillator)
4
#pragma config PLLEN = OFF      // 4 X PLL Enable bit (PLL is under software control)
5
#pragma config PCLKEN = ON      // Primary Clock Enable bit (Primary clock enabled)
6
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor disabled)
7
#pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)
8

9
// CONFIG2L
10
#pragma config PWRTEN = OFF     // Power-up Timer Enable bit (PWRT disabled)
11
#pragma config BOREN = SBORDIS  // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
12
#pragma config BORV = 19        // Brown Out Reset Voltage bits (VBOR set to 1.9 V nominal)
13

14
// CONFIG2H
15
#pragma config WDTEN = OFF      // Watchdog Timer Enable bit (WDT is controlled by SWDTEN bit of the WDTCON register)
16
#pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)
17

18
// CONFIG3H
19
#pragma config HFOFST = ON      // HFINTOSC Fast Start-up bit (HFINTOSC starts clocking the CPU without waiting for the oscillator to stablize.)
20
#pragma config MCLRE = ON       // MCLR Pin Enable bit (MCLR pin enabled, RA3 input pin disabled)
21

22
// CONFIG4L
23
#pragma config STVREN = ON      // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
24
#pragma config LVP = ON         // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
25
#pragma config BBSIZ = OFF      // Boot Block Size Select bit (1kW boot block size)
26
#pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
27

28
// CONFIG5L
29
#pragma config CP0 = OFF        // Code Protection bit (Block 0 not code-protected)
30
#pragma config CP1 = OFF        // Code Protection bit (Block 1 not code-protected)
31

32
// CONFIG5H
33
#pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block not code-protected)
34
#pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM not code-protected)
35

36
// CONFIG6L
37
#pragma config WRT0 = OFF       // Write Protection bit (Block 0 not write-protected)
38
#pragma config WRT1 = OFF       // Write Protection bit (Block 1 not write-protected)
39

40
// CONFIG6H
41
#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers not write-protected)
42
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot block not write-protected)
43
#pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
44

45
// CONFIG7L
46
#pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 not protected from table reads executed in other blocks)
47
#pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 not protected from table reads executed in other blocks)
48

49
// CONFIG7H
50
#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot block not protected from table reads executed in other blocks)
51

52
#define _XTAL_FREQ  1000000
53

54

55
#include <stdio.h>
56
#include <stdlib.h>
57

58

59
void main(void)
60
{
61
  ANSEL=0x00;
62
  ANSELH=0x00;
63
  TRISC=0x00;
64
  LATC=0x00;
65

66
  while(1);
67
  {
68
    LATC=~LATC;
69
    __delay_ms(500);
70
  }
71

72
}
OP #3656145
Lesenswert?

Er rennt unendlich lang weiter, ich muss doch eh nur Debuggen oder?

Zuvor ist ein Fehler gekommen wegen der Betriebsspannung, diese habe ich 
dann behoben durch das Häckchen in den Einstellungen bei Power und jetzt 
kommen diese Fehler, muss ich vielleicht noch etwas einstellen?
Angehängte Dateien:
#3656296
Lesenswert?

Chris B. schrieb:
> Viel kann sich nach " while(1); " ja nicht tun ;-)

Hätte man schöner kaum ausdrücken können.

Hier richtig:
1
 while(1)
2
  {
3
    LATC=~LATC;
4
    __delay_ms(500);
5
  }

Ausserdem kann ich dir nur Empfehlen dich mal kurz über Breakpoints und 
den ganzen Kram einzulesen. Wenn du die IDE nicht verstehst mit der du 
arbeitest, hast du kaum eine Chance richtig und effizient zu 
Programmieren.
OP #3658905
Lesenswert?

Habe mich mal eingelesen, und das Programm funktioniert jetzt auch 
super!

Die Beispiel-Programme von Microchip habe ich mir ebenfalls angeschaut.

Nur suche ich jetzt sowas wie einen Bit-Zähler, finde aber überall nur 
8Bit-Zähler, brauche aber einen 4Bit-Zähler..

Kennt da jemand ein C-Beispiel dazu?

Würde mir sehr gerne mal anschauen wie sowas geht und dadurch dann mal 
selber ein Programm zu erstellen,

danke schonmal!
#3658969
Lesenswert?

Dreko K. schrieb:
> jop, finde aber überall nur 8-Bit-Zähler

Weil es diese als fertig konfektionierte Zähler in einem 
8-Bit-Mikrokontroller nicht gibt.

Du kannst dir aber jeden (fast) beliebig großen / kleinen Zähler aus der 
vorgegebenen Zählerbreite von 8-Bit basteln.

Wie du einen 4-Bit-Zähler realisiertst wurde dir schon gezeigt indem du 
die nicht benötigten, höherwertigen Bits auf Null setzt. Kannst mit der 
Methode auch einen 3-Bit Zähler oder 7-bit Zähler basteln.
Gast #3659278
Lesenswert?

Dreko K. schrieb:
> Wie bekomme ich die Headerdateien?
> Mir fehlt zB avr/io.h und util/delay.h?
> Habe ich die Bibliotheken nicht installiert?

Dazu solltest du das AVR Studio installieren, das aber nicht mit einem 
PICKIT3 kompatibel ist.

Arbeite doch einfach das Tutorial vom PICKIT3 inkl. Beispielen durch. 
Wenn deine Fragen dann immer noch bestehen solltest du dir ein anderes 
Hobby suchen. Sorry, klingt hart, ist aber so.

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren