PIC_Initialisierung

Gast #3026217
Lesenswert?

Wie der Betreff schon sagt versuche ich gerade mein PWM vom PIC12f1501 
zu Initialisieren und wollte euch bitten, den Code kurz anzuschauen und 
mir zusagen ob ich soweit richtig bin.


1
#include <pic12f1501.h>
2
#include <stdio.h>
3
#include <stdlib.h>
4
#include <delays.h>
5

6

7
// PIC12F1501 Configuration Bit Settings
8

9
#include <xc.h>
10

11
// CONFIG1
12
#pragma config FOSC = INTOSC    // Oscillator Selection Bits (INTOSC oscillator: I/O function on CLKIN pin)
13
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
14
#pragma config PWRTE = ON       // Power-up Timer Enable (PWRT enabled)
15
#pragma config MCLRE = ON       // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
16
#pragma config CP = ON          // Flash Program Memory Code Protection (Program memory code protection is enabled)
17
#pragma config BOREN = OFF      // Brown-out Reset Enable (Brown-out Reset disabled)
18
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
19

20
// CONFIG2
21
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
22
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
23
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
24
#pragma config LPBOR = OFF      // Low-Power Brown Out Reset (Low-Power BOR is disabled)
25
#pragma config LVP = OFF        // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
26

27

28

29
/*
30
 * 
31
 */
32
void main(void) {
33

34
    //INITIALIZING PORTA
35
    PORTA   =   0x00;
36
    LATA    =   0x00;
37
    ANSELA  =   0x00;
38
    TRISA   =   0x00;
39

40

41
    //INITIALIZING PWM
42
    // Fosc = 16000000 Hz
43
    // Fpwm = 10000.00 Hz
44
    // Duty Cycle = 50 %
45
    
46
    PWM1CON =   0x00;
47
    PWM2CON =   0x00;
48
    PWM3CON =   0x00;
49
    PWM4CON =   0x00;
50
    
51
    PR2 = 0x7C ;
52

53
    PWM1DCH =   0x00;
54
    PWM2DCH =   0x00;
55
    PWM3DCH =   0x00;
56
    PWM4DCH =   0x00;
57

58
    PWM1DCL =   0x3F;
59
    PWM2DCL =   0x3F;
60
    PWM3DCL =   0x3F;
61
    PWM4DCL =   0x3F;
62

63
    T2CON = 0x05 ;
64
    
65
   
66

67
    while(1);
68

69
}


Bedanke mich schon im vorraus!!!

Gruß
Gast #3026269
Lesenswert?

Weil die Schaltung noch nicht da ist kommt erst nächste Woche und so 
wird es ja noch nicht funktionieren denn es ist ja noch nicht alles 
fertig!!!

Bitte Euch bloß die Initialisierung anzusehen und mir zusagen ob es so 
richtig ist!!
Gast #3026318
Lesenswert?

>Bitte Euch bloß die Initialisierung anzusehen und mir zusagen ob es so
>richtig ist!!

Willst du jetzt für jede Zeile Programmcode die du schreibst
hier erst einmal Absolution einholen? Probier deinen
Kram aus bevor du hier fragst.
Gast #3036274
Lesenswert?

Das ist mein erstes Programm mit PWM  bitte um Nachsicht!!!

Ich bekomme es nich auf die Reihe das da ein Signal rauskommt!!!

Bitte um Hilfe !!!
1
#include <pic12f1501.h>
2
#include <stdio.h>
3
#include <stdlib.h>
4

5

6

7
// PIC12F1501 Configuration Bit Settings
8

9
#include <xc.h>
10

11
// CONFIG1
12
#pragma config FOSC = INTOSC    // Oscillator Selection Bits (INTOSC oscillator: I/O function on CLKIN pin)
13
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
14
#pragma config PWRTE = ON       // Power-up Timer Enable (PWRT enabled)
15
#pragma config MCLRE = ON       // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
16
#pragma config CP = ON          // Flash Program Memory Code Protection (Program memory code protection is enabled)
17
#pragma config BOREN = OFF      // Brown-out Reset Enable (Brown-out Reset disabled)
18
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
19

20
// CONFIG2
21
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
22
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
23
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
24
#pragma config LPBOR = OFF      // Low-Power Brown Out Reset (Low-Power BOR is disabled)
25
#pragma config LVP = OFF        // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
26

27
#define _XTAL_FREQ 8000000
28

29

30

31

32
void main(void) {
33

34
    OSCCON  =  0x72;
35

36

37
    //INITIALIZING PORTA
38
    PORTA   =   0x00;
39
    LATA    =   0x00;
40
    ANSELA  =   0x00;
41
    TRISA   =   0x00;
42

43
    //INITIALIZING PWM
44
    PR2     =   0x65;       //Laut Datenblatt = 4.90 kHz
45
    PWM1DCH =   0x00;
46
    PWM1DCL =   0x00;
47
    T2CON   =   0x01;       //Laut Datenblatt = 4.90 kHz
48
    PWM1CON =   0xC0;
49

50

51

52
    while(1){
53

54
        unsigned char   zahl;
55

56
        for(zahl = 0; zahl>=128; zahl++)
57
        {
58
            PWM1DCH =   zahl;
59
            __delay_ms(100);
60
        }
61

62

63
        unsigned char   zahl;
64

65
        for(zahl = 128; zahl<=0; zahl--)
66
        {
67
            PWM1DCH =   zahl;
68
            __delay_ms(100);
69
        }
70
        
71
    }
72

73

74

75
    
76

77

78
    
79
    
80
   
81

82
    
83

84
}

Da muss sich am Ausgang was tuen oder???
Gast #3036642
Lesenswert?

So habe das PWM Signal was ich wollte, nur da kommt schon das nächste 
Problem, wenn die LED leuchtet dann geht die nach einer gewissen Zeit 
aus ohne das ich was im Programm ändere.

Kann mir da vielleicht jemand weiterhelfen?



1
 /* 
2
 * File:   LED_PWM.c
3
 * Author: 
4
 *
5
 * Created on 1. Februar 2013, 18:39
6
 */
7

8
#include <pic12f1501.h>
9
#include <stdio.h>
10
#include <stdlib.h>
11

12

13

14
// PIC12F1501 Configuration Bit Settings
15

16
#include <xc.h>
17

18
// CONFIG1
19
#pragma config FOSC = INTOSC    // Oscillator Selection Bits (INTOSC oscillator: I/O function on CLKIN pin)
20
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
21
#pragma config PWRTE = ON       // Power-up Timer Enable (PWRT enabled)
22
#pragma config MCLRE = ON       // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
23
#pragma config CP = ON          // Flash Program Memory Code Protection (Program memory code protection is enabled)
24
#pragma config BOREN = OFF      // Brown-out Reset Enable (Brown-out Reset disabled)
25
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
26

27
// CONFIG2
28
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
29
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
30
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
31
#pragma config LPBOR = OFF      // Low-Power Brown Out Reset (Low-Power BOR is disabled)
32
#pragma config LVP = OFF        // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
33

34
#define _XTAL_FREQ 8000000
35

36

37

38

39
void main(void) {
40

41
    OSCCON  =  0x72;
42

43

44
    //INITIALIZING PORTA
45
    PORTA   =   0x00;
46
    LATA    =   0x00;
47
    ANSELA  =   0x00;
48
    TRISA   =   0x00;
49

50
    //INITIALIZING PWM
51
    PWM1CON =   0;
52
    PWM3CON =   0;
53
    PWM4CON =   0;
54
    PR2     =   0x10;
55
    PWM1DCH =   0x06;
56
    PWM1DCL =   0;
57
    PWM3DCH =   0x06;
58
    PWM3DCL =   0;
59
    PWM4DCH =   0x06;
60
    PWM4DCL =   0;
61
    T2CON   =   0x05;
62
    PWM1EN  =   1;
63
    PWM1POL =   0;
64
    PWM1OE  =   1;
65
    PWM3EN  =   1;
66
    PWM3POL =   0;
67
    PWM3OE  =   1;
68
    PWM4EN  =   1;
69
    PWM4POL =   0;
70
    PWM4OE  =   1;
71

72

73
    int dc;
74

75
    while(1){
76

77
         PWM1EN  =   0;
78
         PWM3EN  =   0;
79
         for(dc = 0 ; dc < 200 ; dc++)
80
                        {
81
                        PWM4DCH = dc ;
82
                        __delay_ms(100);
83

84
                        }
85
          
86
    }
#3039283
Lesenswert?

PIC_18_24 schrieb:
> PWM1EN  =   0;
> PWM3EN  =   0;

Wieso setzt du die nich schon weiter oben 0?

PIC_18_24 schrieb:
> dann geht die nach einer gewissen Zeit
> aus ohne das ich was im Programm ändere.

Du kannst ja auch nicht wärend das Programm läuft was am Programm 
ändern.

N bisl mehr Info wäre gut. Z.b. ob die LED aus bleibt oder wieder an 
geht (Laut deinem Programm geht sie aus, aber auch wieder an). Evtl 
Schaltplan.

Guck auch mal im DB auf Seite 101, Table 11-2 (PORTA OUTPUT PRIORITY) 
bei RA5. Von Oben nach unten ist die Priorität, vielleicht funkt dir da 
noch was anderes Zwischen, vielleicht der CLC2-Ausgang.
Also auch mal probieren CLC2OE = 0 und CLC2EN = 0 zu setzen.

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