Forum: Mikrocontroller und Digitale Elektronik MCP4151 digitalpotentiometer


von Björn B. (s2247678)


Lesenswert?

hat jemand schon mal den MCP4151 mit SPI angesteuert.Ich hab ein 200k 
Widerstand zwischen MOSI(Atmega32) und SDI/SDO(Mcp4151 bidrektional) 
gesetzt.
Nur empfange ich die gleichen Daten die ich über MOSI(Atmega32) sende an 
dem MISO(Atmega32).(ist auch irgendwie logisch nur steht das so im 
Datenblatt eigentlich müsste ich zumindest meistens 0xFFxx empfangen 
egal bei welcher Adresse+command siehe dazu Datenblatt Seite 48)

Hier mein Quelltext.Ich habs schon mit Software Spi versucht um die 
ersten 6 bits(adresse+command) zu senden und den Rest(SPI string 
Data+Cmderror bit) zu empfangen.Vielleicht kann mir jemand helfen.ICh 
befürchte das geht nur mit Soft SPI.


1
// Library includes
2
//#include <avr/io.h>
3
#include <avr/sleep.h>
4
#include <compat/deprecated.h>
5
#include <avr/pgmspace.h>
6
#include <util/delay.h>
7
#include <inttypes.h>
8
#include <avr/interrupt.h>
9
#include <stdio.h>
10
#include <stdlib.h>
11
#include <string.h>
12
#include "Font.h"
13
14
15
#define SELECT_MCP4151 PORTB &= ~(0<<PB4)
16
#define DESELECT_MCP4151 PORTB |= (1<<PB4)
17
#define DDR_SPI DDRB
18
#define DD_SCK  PB7
19
#define DD_MOSI PB5
20
#define DD_MISO PB6
21
#define DD_SS   PB4 
22
23
24
25
26
27
28
unsigned short read_adc(void);
29
void SPI_MasterInit(void);
30
uint8_t SPI_MasterTransmit(char cData);
31
32
33
// Here is the main 
34
int main (void)
35
{
36
unsigned char CharCol_1, CharCol_2, CharCol_3, CharCol_4, CharCol_5;
37
unsigned char pz, y;
38
unsigned int pos;
39
unsigned short test=0x0000;
40
unsigned char sign;  // setup all pins to outputs
41
  SPI_MasterInit();
42
  
43
  DDRD  = 0xff;
44
  DDRA  = 0xfe;
45
46
47
48
PORTD |= (0<<PD2)|(0<<PD3)|(0<<PD4)|(0<<PD5)|(0<<PD6);
49
50
51
int i=0;
52
while(i<2)
53
{i++;
54
test=read_adc();
55
}
56
57
58
if (test==0x430F)
59
sign=0x32;
60
else sign=0x31;
61
62
63
64
65
66
67
  if ((sign<0x20) || (sign>0x7F)) { sign = 0x20; } 
68
  pos = 5*(sign-0x20);
69
70
  CharCol_5 = pgm_read_byte(&code5x7[pos++]);
71
  CharCol_4 = pgm_read_byte(&code5x7[pos++]);
72
  CharCol_3 = pgm_read_byte(&code5x7[pos++]);
73
  CharCol_2 = pgm_read_byte(&code5x7[pos++]);
74
  CharCol_1 = pgm_read_byte(&code5x7[pos++]);
75
76
while (1)
77
{
78
79
PORTD |= (0<<PD2)|(0<<PD3)|(0<<PD4)|(0<<PD5)|(0<<PD6);
80
81
if (CharCol_1>0x00)
82
{
83
PORTD=(1<<PD2);
84
PORTA=(!((CharCol_1&0b01000000)>>6)<<PA1)|(!((CharCol_1&0b00100000)>>5)<<PA2)|(!((CharCol_1&0b00010000)>>4)<<PA3)|(!((CharCol_1&0b00001000)>>3)<<PA4)|(!((CharCol_1&0b00000100)>>2)<<PA5)|(!((CharCol_1&0b00000010)>>1)<<PA6)|(!((CharCol_1&0b00000001))<<PA7);
85
86
_delay_us(4000);
87
88
}
89
90
PORTD |= (0<<PD2)|(0<<PD3)|(0<<PD4)|(0<<PD5)|(0<<PD6);
91
92
if (CharCol_2>0x00)
93
{
94
PORTD=(1<<PD3);
95
PORTA=(!((CharCol_2&0b01000000)>>6)<<PA1)|(!((CharCol_2&0b00100000)>>5)<<PA2)|(!((CharCol_2&0b00010000)>>4)<<PA3)|(!((CharCol_2&0b00001000)>>3)<<PA4)|(!((CharCol_2&0b00000100)>>2)<<PA5)|(!((CharCol_2&0b00000010)>>1)<<PA6)|(!((CharCol_2&0b00000001))<<PA7);
96
_delay_us(4000);
97
98
99
}
100
101
PORTD |= (0<<PD2)|(0<<PD3)|(0<<PD4)|(0<<PD5)|(0<<PD6);
102
103
104
105
if (CharCol_3>0x00)
106
{
107
PORTD=(1<<PD4);
108
PORTA=(!((CharCol_3&0b01000000)>>6)<<PA1)|(!((CharCol_3&0b00100000)>>5)<<PA2)|(!((CharCol_3&0b00010000)>>4)<<PA3)|(!((CharCol_3&0b00001000)>>3)<<PA4)|(!((CharCol_3&0b00000100)>>2)<<PA5)|(!((CharCol_3&0b00000010)>>1)<<PA6)|(!((CharCol_3&0b00000001))<<PA7);
109
_delay_us(4000);
110
111
}
112
113
PORTD |= (0<<PD2)|(0<<PD3)|(0<<PD4)|(0<<PD5)|(0<<PD6);
114
115
if (CharCol_4>0x00)
116
{
117
PORTD=(1<<PD5);
118
119
PORTA=(!((CharCol_4&0b01000000)>>6)<<PA1)|(!((CharCol_4&0b00100000)>>5)<<PA2)|(!((CharCol_4&0b00010000)>>4)<<PA3)|(!((CharCol_4&0b00001000)>>3)<<PA4)|(!((CharCol_4&0b00000100)>>2)<<PA5)|(!((CharCol_4&0b00000010)>>1)<<PA6)|(!((CharCol_4&0b00000001))<<PA7);
120
121
122
_delay_us(4000);
123
124
}
125
126
PORTD |= (0<<PD2)|(0<<PD3)|(0<<PD4)|(0<<PD5)|(0<<PD6);
127
128
129
if (CharCol_5>0x00)
130
{
131
PORTD=(1<<PD6);
132
PORTA=(!((CharCol_5&0b01000000)>>6)<<PA1)|(!((CharCol_5&0b00100000)>>5)<<PA2)|(!((CharCol_5&0b00010000)>>4)<<PA3)|(!((CharCol_5&0b00001000)>>3)<<PA4)|(!((CharCol_5&0b00000100)>>2)<<PA5)|(!((CharCol_5&0b00000010)>>1)<<PA6)|(!((CharCol_5&0b00000001))<<PA7);
133
134
_delay_us(4000);
135
136
}
137
}
138
}
139
140
void SPI_MasterInit(void)
141
{volatile char IOReg;
142
/* Set MOSI and SCK output, all others input */
143
144
DDR_SPI =(1<<DD_MOSI)|(1<<DD_SCK)|(1<<DD_SS);
145
146
147
/* Enable SPI, Master, set clock rate fck/16 */
148
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1);
149
SPSR=(1<<SPI2X);
150
IOReg=SPSR;                   // clear SPIF bit in SPSR
151
IOReg=SPDR;
152
153
}
154
155
156
uint8_t SPI_MasterTransmit(char cData)
157
{
158
/* Start transmission */
159
SPDR = cData;
160
161
/* Wait for transmission complete */
162
while(!(SPSR & (1<<SPIF)));
163
return SPDR;
164
}
165
166
167
168
169
unsigned short read_adc(void)
170
{
171
  // select ADC wait 100 microseconds then read two bytes
172
 SELECT_MCP4151;
173
_delay_us(100);
174
unsigned char one =SPI_MasterTransmit(0x43);
175
_delay_us(100); 
176
unsigned char two = SPI_MasterTransmit(0x0F);
177
DESELECT_MCP4151;
178
179
//move into 16 bit int
180
 return ((one<<8)|two) ;
181
}

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.