Forum: Mikrocontroller und Digitale Elektronik stm32 SPI keine Reaktion


von Seb (Gast)


Lesenswert?

Hallo, ich möchte den DAC7811 gern mittels SPI ansteuern. Dazu habe ich 
als Test erst mal den im Datenblatt empfohlenen OPA277 angeschlossen. 
Ich versorge ihn mit ca +-14V und habe am Ausgang einen 1k R gegen Masse 
geschaltet, der Rest wie im Datenblatt beschrieben. Die Signale des SPI 
sehen ganz gut aus, allerdings gibt mir mein OPA ständig ein Signal von 
ca. -1,5V aus egal was ich ihm sende. Ich denke der Fehler liegt im 
Code, vielleicht hat jemand etwas Zeit mir zu sagen woran es liegen 
könnte. Ich habe bisher noch nichts mit SPI gemacht.

1
/* Includes ------------------------------------------------------------------*/
2
#include "stm32f10x.h"
3
4
5
6
/* Private typedef -----------------------------------------------------------*/
7
8
/* Private define ------------------------------------------------------------*/
9
#define BufferSize       32
10
11
/* Private macro -------------------------------------------------------------*/
12
/* Private variables ---------------------------------------------------------*/
13
SPI_InitTypeDef  SPI_InitStructure;
14
DMA_InitTypeDef  DMA_InitStructure;
15
GPIO_InitTypeDef GPIO_InitStructure;
16
17
18
/* Private function prototypes -----------------------------------------------*/
19
void RCC_Configuration(void);
20
void GPIO_Configuration(void);
21
22
/* Private functions ---------------------------------------------------------*/
23
24
/**
25
  * @brief  Main program
26
  * @param  None
27
  * @retval None
28
  */
29
int main(void)
30
{
31
32
33
  /* System clocks configuration ---------------------------------------------*/
34
  RCC_Configuration();
35
36
  /* GPIO configuration ------------------------------------------------------*/
37
  GPIO_Configuration();
38
39
40
41
// SPI_MASTER configuration ------------------------------------------------------
42
  SPI_InitStructure.SPI_Direction              = SPI_Direction_1Line_Tx;
43
  SPI_InitStructure.SPI_Mode                   = SPI_Mode_Master;
44
  SPI_InitStructure.SPI_DataSize               = SPI_DataSize_16b;
45
  SPI_InitStructure.SPI_CPOL                  = SPI_CPOL_High;
46
  SPI_InitStructure.SPI_CPHA                   = SPI_CPHA_1Edge;
47
  SPI_InitStructure.SPI_NSS                    = SPI_NSS_Soft;   
48
  SPI_InitStructure.SPI_BaudRatePrescaler      = SPI_BaudRatePrescaler_4;
49
  SPI_InitStructure.SPI_FirstBit               = SPI_FirstBit_MSB;
50
  SPI_InitStructure.SPI_CRCPolynomial          = 7;
51
  SPI_Init(SPI1, &SPI_InitStructure);
52
53
  // Take NSS line high by default
54
    GPIO_SetBits(GPIOA,  GPIO_Pin_4);
55
56
57
58
  /* Enable SPI_MASTER */
59
  SPI_Cmd(SPI1, ENABLE);
60
61
62
  while (1)
63
  {
64
65
66
   
67
    GPIO_ResetBits(GPIOA, GPIO_Pin_4);
68
69
//   SPI_I2S_SendData(SPI1, 0b1100000000000000);
70
SPI_I2S_SendData(SPI1, 0b1011000000000000); //Clear DAC Output to zero
71
72
73
    while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET)
74
    {}
75
76
77
    GPIO_SetBits(GPIOA, GPIO_Pin_4);
78
79
80
  }
81
82
83
}
84
85
86
void RCC_Configuration(void)
87
{
88
  /* PCLK2 = HCLK/2 */
89
  RCC_PCLK2Config(RCC_HCLK_Div2);
90
91
  /* Enable peripheral clocks --------------------------------------------------*/
92
93
94
  /* Enable SPI_MASTER clock and GPIO clock for SPI_MASTER and SPI_SLAVE */
95
96
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 ,ENABLE);
97
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA ,ENABLE);
98
99
}
100
101
void GPIO_Configuration(void)
102
{
103
  GPIO_InitTypeDef GPIO_InitStructure;
104
105
106
107
  /* Configure SPI_MASTER pins: NSS, SCK and MOSI */
108
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7  | GPIO_Pin_5 ;
109
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
110
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
111
  GPIO_Init(GPIOA, &GPIO_InitStructure);
112
113
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_4  ;
114
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
115
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
116
  GPIO_Init(GPIOA, &GPIO_InitStructure);
117
118
119
}

von (prx) A. K. (prx)


Lesenswert?

Kontrolliere mal, ob die TXE Abfrage beim Transmit wirklich wartet, bis 
alle Bits raus sind, oder bloss so lange, bis wieder ein Byte in den 
Puffer passt - dann nämlich würde CS zu früh wieder deaktiviert.

von Seb (Gast)


Lesenswert?

Ich glaube du hast recht, was bei 8 Bit noch sehr gut aussah schein bei 
16 Bit murks zu werden. Auf dem kleinen Oszi sahs sogar passen aus, auf 
einem größeren sieht man dass mitten in der Datenübertragung das NSS 
wieder hoch geht bzw sich wieder absenkt. Ich habe jetzt den Code so 
abgeändert:

    while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET)
    {}

    while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == RESET)
    {}


sprich die 2. Zeile ergänzt. So lese ich das zumindest auf der 
Dämonischen Seite 666 des RM0008 heraus. Sieht jetzt zumindest passend 
aus, ich hoffe jetzt funzt es. Danke!

von Seb (Gast)


Lesenswert?

Achso, gibt es sonst noch Dinge die ich beachten muss? Im Artikel SPI 
steht dass man Dummy Bytes senden muss. Trifft das auch hier zu bzw. ist 
das allgemein so oder kann das den Datenblättern entnommen werden?

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.