Forum: Mikrocontroller und Digitale Elektronik dsPIC30F4011 & 3 Wire Display mit 74HC595 & Code Frage


von No Y. (noy)


Lesenswert?

Also ich habe hier auf meinem Breadboard einen dsPIC30F4011 und versuche 
über einen 74HC595 ein HD44780 kompatibles Display anzusteuern.

Bisher klappt es leider nicht ich versuche erstmal mit meinem OLS von DP 
die gesendeten Bit Kombinationen anzuzeigen. Leider klappt das bisher 
noch nicht so ganz ich seh zwar was aber nicht das was ich sehen sollte. 
Habe auch schon ewig mit verschiedenen Trigger kombinationen gespielt 
aber eine Kombi die vorkommen müsste kommt nie. Von daher vll habe ich 
einen Fehler in meinem Code:

In user.h definierten Befehlscodes:
1
void InitApp(void); /* I/O and Peripheral Initialization */
2
signed int read_analog_channel(int n); /* AN6 Sampling & Converting*/
3
void Tick(void);
4
void Out_Tick(void);
5
void Send_Data(void);
6
void BIT_Send(char Init);
7
8
char Init_4Bit[]={0,0,0,1,0,1,0,0};
9
char Init_4BitEN[]={0,0,0,0,0,1,0,0};
10
char Init_Line[]={0,0,0,1,0,1,1,1};
11
char Init_LineEN[]={0,0,0,0,0,1,1,1};
12
char Init_Disp[]={0,0,0,1,0,0,0,0};
13
char Init_DispEN[]={0,0,0,0,0,0,0,0};
14
char Init_Cursor1[]={0,0,0,1,1,1,1,1};
15
char Init_Cursor1EN[]={0,0,0,0,1,1,1,1};
16
char Init_Clear[]={0,0,0,1,0,0,0,1};
17
char Init_ClearEN[]={0,0,0,0,0,0,0,1};


in user.c auszuführender Code:
1
/******************************************************************************/
2
/* Files to Include                                                           */
3
/******************************************************************************/
4
5
#include <p30F4011.h>        /* Device header file                            */
6
#include <stdint.h>          /* For uint32_t definition                       */
7
#include <stdbool.h>         /* For true/false definition                     */
8
#include <libpic30.h>
9
            /* variables/params used by user.c               */
10
11
/******************************************************************************/
12
/* User Functions                                                             */
13
/******************************************************************************/
14
15
/* <Initialize variables in user.h and insert code for user algorithms.> */
16
17
/* TODO Initialize User Ports/Peripherals/Project here */
18
19
void InitApp(void)
20
{
21
    /* Setup analog functionality and port direction */
22
    /*Port B Init*/
23
            LATB = 0;
24
            TRISB = 0b111000000;
25
     /* Configure AN6 as analog inputs*/
26
      ADPCFG = 0xFFBF;     // PORTB pins AN6 is analog input
27
      ADCON1 = 0x0000;     // Manually clear SAMP to end sampling, start conversion
28
      ADCON2 = 0x0100;     // Voltage reference from AVDD and AVSS
29
      ADCON3 = 0x0005;     // Manual Sample, ADCS=5 -> Tad = 3*Tcy
30
     /*Configure PWM for free running mode
31
     PWM period = Tcy * prescale * PTPER = 0.33ns * 64 * PTPER
32
     PWM pulse width = (Tcy/2) * prescale * PDC*/
33
      
34
      PWMCON1 = 0x00FF; // Enable all PWM pairs in complementary mode
35
      PTCON = 0;
36
      _PTCKPS = 3;      // prescale=1:64 (0=1:1, 1=1:4, 2=1:16, 3=1:64)
37
      PTPER = 9470;     // 20ms PWM period (15-bit period value)
38
      PDC1 = PTPER;     // 50% duty cycle on PWM channel 1
39
      PDC2 = PTPER;     // 50% duty cycle on PWM channel 2
40
      PDC3 = PTPER;     // 50% duty cycle on PWM channel 3
41
      _PTEN = 1;        // Enable PWM time base
42
    /*Configure Timer 1
43
    In this example, I'm setting PR1 and TCKPS for 8Hz*/
44
      T1CON = 0;            // Clear the Timer 1 configuration
45
      TMR1 = 0;             // Reset Timer 1 counter
46
      PR1 = 14648;          // Set the Timer 1 period (max 65535)
47
      T1CONbits.TCS = 0;    // Select internal clock (Fosc/4)
48
      T1CONbits.TCKPS = 3;  // Prescaler (0=1:1, 1=1:8, 2=1:64, 3=1:256)
49
      _T1IP = 1;            // Set the Timer 1 interrupt priority
50
      _T1IF = 0;            // Clear the Timer 1 interrupt flag
51
      _T1IE = 1;            // Enable Timer 1 interrupt
52
      T1CONbits.TON = 1;    // Turn on Timer 1
53
    /*LCD Init*/
54
55
            __delay32(6000);
56
            LATBbits.LATB0=1;
57
            void BIT_Send(Init_4Bit);
58
            __delay32(3500);
59
            void BIT_Send(Init_4BitEN);
60
            __delay32(3500);
61
            void BIT_Send(Init_4Bit);
62
            __delay32(3500);
63
            void BIT_Send(Init_4BitEN);
64
            __delay32(3500);
65
            void BIT_Send(Init_Line);
66
            __delay32(3500);
67
            void BIT_Send(Init_LineEN);
68
            __delay32(3500);
69
            void BIT_Send(Init_Disp);
70
            __delay32(3500);
71
            void BIT_Send(Init_DispEN);
72
            __delay32(3500);
73
            void BIT_Send(Init_Cursor1);
74
            __delay32(3500);
75
            void BIT_Send(Init_Cursor1EN);
76
            __delay32(3500);
77
            void BIT_Send(Init_Disp);
78
            __delay32(3500);
79
            void BIT_Send(Init_DispEN);
80
            __delay32(3500);
81
            void BIT_Send(Init_Clear);
82
            __delay32(3500);
83
            void BIT_Send(Init_ClearEN);
84
            __delay32(3500);
85
            void BIT_Send(Init_Disp);
86
            __delay32(3500);
87
            void BIT_Send(Init_DispEN);
88
            __delay32(3500);
89
            void BIT_Send(Init_Line);
90
            __delay32(3500);
91
            void BIT_Send(Init_LineEN);
92
            __delay32(3500);
93
94
        
95
    /* Initialize peripherals */
96
}
97
98
99
   
100
  /* This function reads a single sample from the specified
101
   analog input. It should take less than 2.5us if the chip
102
   is running at about 30 MIPS.
103
   Because the dsPIC30F4011 has a 10-bit ADC, the value
104
  returned will be between 0 and 1023.*/
105
  signed int read_analog_channel(int channel)
106
  {
107
108
      ADCHS = channel;           // Select the requested channel
109
            ADCON1bits.ADON = 1;      // Turn ADC ON
110
      ADCON1bits.SAMP = 1;      // Start sampling
111
      __delay32(30);            // 1us delay @ 30 MIPS
112
      ADCON1bits.SAMP = 0;      // Start Converting
113
      while (!ADCON1bits.DONE);
114
            ADCON1bits.ADON = 0;      // Turn ADC ON
115
      return ADCBUF0;
116
  }
117
        void Tick(void)
118
        {
119
            __delay32(30);
120
            LATBbits.LATB4 = 0;
121
            __delay32(30);
122
            LATBbits.LATB4 = 1;
123
            __delay32(30);
124
            LATBbits.LATB4 = 0;
125
            __delay32(30);
126
        }
127
        void Out_Tick(void)
128
        {
129
            __delay32(30);
130
            LATBbits.LATB5 = 0;
131
            __delay32(30);
132
            LATBbits.LATB5 = 1;
133
            __delay32(30);
134
            LATBbits.LATB5 = 0;
135
            __delay32(30);
136
        }
137
        void Send_Data(void)
138
        {
139
140
141
        }
142
        void BIT_Send(char Init[])
143
        {
144
            
145
            int i;
146
            for (i=0;i<9;i++)
147
            {
148
            LATBbits.LATB3 = Init[i];
149
            Tick();
150
            }
151
         Out_Tick();
152
         while(1);
153
        }

meine main.c:

1
/******************************************************************************/
2
/* Files to Include                                                           */
3
/******************************************************************************/
4
5
#include <p30F4011.h>      /* Device header file                              */
6
#include <stdint.h>        /* Includes uint16_t definition                    */
7
#include <stdbool.h>       /* Includes true/false definition                  */
8
#include <adc10.h>
9
#include <delay.h>
10
#include <timer.h>
11
#include <pwm.h>
12
13
#include "system.h"        /* System funct/params, like osc/peripheral config */
14
#include "user.h"          /* User funct/params, such as InitApp              */
15
16
/******************************************************************************/
17
/* Global Variable Declaration                                                */
18
/******************************************************************************/
19
20
/* i.e. uint16_t <variable_name>; */
21
22
/******************************************************************************/
23
/* Main Program                                                               */
24
/******************************************************************************/
25
26
int16_t main(void)
27
{
28
29
    /* Configure the oscillator for the device */
30
    //ConfigureOscillator();
31
32
    /* Initialize IO ports and peripherals */
33
    InitApp();
34
35
    /* TODO <INSERT USER APPLICATION CODE HERE> */
36
    signed int voltage;
37
    while(1)
38
    {
39
    voltage = read_analog_channel(6);
40
    __delay32(6000000); // 200ms delay
41
42
    }
43
}

vielleicht seht Ihr ja meinen Fehler.

Achja die Kombination beim Trigger (da dieser schon sehr früh auslöst 
sobald nur ein wenig gezucke da ist) wäre:

LATB0 = 1, LATB3 = 0, LATB4 = 0, LATB5 = 1

also der erste fertige Befehl.

von 1234 (Gast)


Lesenswert?

Code grausig!

Ich würde das Programm zuerst mal strukturieren.

Also z.B. in folgende Basisroutinen:

void Init74HC595Interface(void);

// Routine zum Schreiben eines Bytes auf den 74HC595
char SendByte(unsigned char Data);
void Delayus(unsigned long us);

Wenn du die mal zum Laufen gebracht hast, dann muss du wahrscheinlich 
nur mehr eine der generischen LCD-Libraries für diesen LCD-Controller 
downloaden und du wirst Erfolg haben.

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.