Ich möchte eine AD-Wandlung durchführen. Die gelesenen Daten sollen dann via USART2 an den PC übertragen werden. Das langsamste an dem ganzen system ist ja der USART und deshalb möchte ich einen Interrupt einbauen, dass immer wenn dieser mit einer übertragung fertig ist die nächste gestartet werden kann. Übertragen werden soll mein kleines Protokoll. USART_ITConfig function(USART2, USART_IT_TC, ENABLE); Dies sollte die Interrupt Konfiguration sein. Aber wie nutzt man das nun genau. Im Anhang befindet sich mein aktueller Quellcode. Vielen dank schonmal
Du musst die STM LIB für die STM32 einbinden, oder alles auf SFR-Ebene selber umsetzen ---> Datenblatt/Usermanual lesen. LIB findest Du auf den STM Seiten zum Download. Version 3.3.0 ist glaub ich die aktuelle. Ist auche eine Hilfedatei bei.
Bin zwar auch nur ein blutiger STM32-Anfaenger, aber USART-Interrupt habe ich auf meiner ToDo-Liste schon mal abgehakt :) Die Initialisierung:
1 | //*********************************
|
2 | // MyUsart_1
|
3 | //*********************************
|
4 | MyUsart_1::MyUsart_1( void ) |
5 | {
|
6 | |
7 | USART_InitTypeDef USART_InitStructure; |
8 | GPIO_InitTypeDef GPIO_InitStructure; |
9 | NVIC_InitTypeDef NVIC_InitStructure; |
10 | |
11 | |
12 | /* Enable USART1 clock */
|
13 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); |
14 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); |
15 | |
16 | /* Configure USART1 Rx as input floating */
|
17 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; |
18 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; |
19 | GPIO_Init(GPIOA, &GPIO_InitStructure); |
20 | |
21 | /* Configure USART1 Tx as alternate function push-pull */
|
22 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; |
23 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; |
24 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; |
25 | GPIO_Init(GPIOA, &GPIO_InitStructure); |
26 | |
27 | USART_InitStructure.USART_BaudRate = 9600; |
28 | USART_InitStructure.USART_WordLength = USART_WordLength_8b; |
29 | USART_InitStructure.USART_StopBits = USART_StopBits_1; |
30 | USART_InitStructure.USART_Parity = USART_Parity_No ; |
31 | USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; |
32 | USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; |
33 | |
34 | /* Configure the USART */
|
35 | USART_Init(USART1, &USART_InitStructure); |
36 | |
37 | |
38 | USART_Cmd(USART1, ENABLE); /* Enable the USART */ |
39 | //USART_ITConfig(USART1, USART_IT_TXE, ENABLE); // enable intterupt
|
40 | |
41 | /* Enable the USARTy Interrupt */
|
42 | NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; |
43 | NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; |
44 | NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; |
45 | NVIC_Init(&NVIC_InitStructure); |
46 | }
|
Der Interrupt Handler:
1 | //=============================================
|
2 | // USART1_IRQHandler
|
3 | //=============================================
|
4 | void USART1_IRQHandler(void) |
5 | {
|
6 | if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) |
7 | {
|
8 | /* Read one byte from the receive data register */
|
9 | RxBuffer1[RxCounter1++] = USART_ReceiveData(USART1); |
10 | |
11 | if(RxCounter1 == NbrOfDataToRead1) |
12 | {
|
13 | /* Disable the USARTy Receive interrupt */
|
14 | USART_ITConfig(USART1, USART_IT_RXNE, DISABLE); |
15 | }
|
16 | }
|
17 | |
18 | if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET) |
19 | {
|
20 | /* Write one byte to the transmit data register */
|
21 | USART_SendData(USART1, TxBuffer1[TxCounter1++]); |
22 | |
23 | if(TxCounter1 == NbrOfDataToTransfer1) |
24 | {
|
25 | /* Disable the USART1 Transmit interrupt */
|
26 | USART_ITConfig(USART1, USART_IT_TXE, DISABLE); |
27 | }
|
28 | }
|
29 | }
|
In ähnlicher weise habe ich es auch implementiert. verstehe aber den ablauf noch nicht so ganz. bei mir sollen immer 4 bytes nacheinander übertragen werden. bis jetzt hatte ich es einfach immer in einer while schleife. verstehe noch nicht wie das mit dem interrupt genau geht. es soll nach möglichkeit gleich nach abschluss der übertragung eines bytes das nächste übertragen 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
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.