UART Soon finish
This commit is contained in:
parent
e8800d0c58
commit
f979a89e5f
4 changed files with 117 additions and 32 deletions
|
@ -3,9 +3,18 @@
|
||||||
|
|
||||||
#include "stm32f10x.h"
|
#include "stm32f10x.h"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
USART_TypeDef * UART;
|
||||||
|
unsigned int baudrate;
|
||||||
|
}MyUART_Struct_Typedef;
|
||||||
|
|
||||||
void UART_send(char data);
|
void UART_send(char data);
|
||||||
void UART_init(void);
|
char UART_read(char data, MyUART_Struct_Typedef * UART);
|
||||||
void UART_ITHandler(void);
|
void UART_init(MyUART_Struct_Typedef * UART);
|
||||||
|
void USART1_IRQHandler(void);
|
||||||
|
void USART2_IRQHandler(void);
|
||||||
|
void USART3_IRQHandler(void);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,15 +1,25 @@
|
||||||
#include "Driver_UART.h"
|
#include "Driver_UART.h"
|
||||||
|
|
||||||
|
|
||||||
void UART_init(void)
|
char received_data1, received_data2, received_data3;
|
||||||
|
|
||||||
|
void UART_init(MyUART_Struct_Typedef * UART)
|
||||||
{
|
{
|
||||||
|
if(UART->UART ==USART1)
|
||||||
|
RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
|
||||||
|
if(UART->UART ==USART2)
|
||||||
|
RCC->APB2ENR |= RCC_APB1ENR_USART2EN;
|
||||||
|
if(UART->UART ==USART3)
|
||||||
|
RCC->APB2ENR |= RCC_APB1ENR_USART3EN;
|
||||||
|
|
||||||
|
|
||||||
RCC->APB2ENR |= RCC_APB2ENR_USART1EN; // Validation horloge USART1
|
RCC->APB2ENR |= RCC_APB2ENR_USART1EN; // Validation horloge USART1
|
||||||
USART1->CR1 |= USART_CR1_UE; // Activer l'USART
|
USART1->CR1 |= USART_CR1_UE; // Activer l'USART
|
||||||
USART1->CR1 &= ~USART_CR1_M; // Choisir la taille 8bits de donnée
|
USART1->CR1 &= ~USART_CR1_M; // Choisir la taille 8bits de donnée
|
||||||
USART1->CR2 |= USART_CR2_STOP; // 1 seul bit de stop
|
USART1->CR2 |= USART_CR2_STOP; // 1 seul bit de stop
|
||||||
USART1->BRR |= 468 << 4; // Fixe le baud rate à 9600bps partie entière
|
//USART1->BRR |= 468 << 4; // Fixe le baud rate à 9600bps partie entière
|
||||||
USART1->BRR |= 75; // Fixe le baud rate à 9600bps partie fractionnaire
|
//USART1->BRR |= 75; // Fixe le baud rate à 9600bps partie fractionnaire
|
||||||
//USART1->BRR = 72000000/9600; // Fixer le Baudrate à 9600
|
UART->UART->BRR = 72000000/(UART->baudrate);
|
||||||
USART1->CR1 |= USART_CR1_TE; // Autoriser la transmission
|
USART1->CR1 |= USART_CR1_TE; // Autoriser la transmission
|
||||||
USART1->CR1 |= USART_CR1_RE; // Activer la réception
|
USART1->CR1 |= USART_CR1_RE; // Activer la réception
|
||||||
USART1->CR1 |= USART_CR1_TCIE; // Activer l'interruption de transmission
|
USART1->CR1 |= USART_CR1_TCIE; // Activer l'interruption de transmission
|
||||||
|
@ -18,16 +28,29 @@ void UART_init(void)
|
||||||
|
|
||||||
void UART_send(char data)
|
void UART_send(char data)
|
||||||
{
|
{
|
||||||
|
while(!(USART1->SR & USART_SR_TXE)){} //Attendre l'autorisation de transmission
|
||||||
USART1->DR |= data;
|
USART1->DR |= data;
|
||||||
while(!(USART1->SR & USART_SR_TC)){} //Attendre la fin de transmission
|
while(!(USART1->SR & USART_SR_TC)){} //Attendre la fin de transmission
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char UART_read(char data, MyUART_Struct_Typedef * UART)
|
||||||
|
{
|
||||||
|
if(UART->UART == USART1)
|
||||||
|
return received_data1;
|
||||||
|
else if (UART->UART == USART2)
|
||||||
|
return received_data2;
|
||||||
|
else if (UART->UART == USART3)
|
||||||
|
return received_data3;
|
||||||
|
else
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
void UART_ITHandler(void)
|
|
||||||
|
void USART1_IRQHandler(void)
|
||||||
{
|
{
|
||||||
if (USART1->SR & USART_SR_RXNE) // si une donnée a été reçue
|
if (USART1->SR & USART_SR_RXNE) // si une donnée a été reçue
|
||||||
{
|
{
|
||||||
char received_data = USART1->DR; // lire la donnée reçue
|
received_data1 = USART1->DR; // lire la donnée reçue
|
||||||
}
|
}
|
||||||
|
|
||||||
if (USART1->SR & USART_SR_TC) // si la transmission est terminée
|
if (USART1->SR & USART_SR_TC) // si la transmission est terminée
|
||||||
|
@ -35,3 +58,29 @@ void UART_ITHandler(void)
|
||||||
USART1->SR &= ~USART_SR_TC; // effacer le bit de transmission terminée
|
USART1->SR &= ~USART_SR_TC; // effacer le bit de transmission terminée
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void USART2_IRQHandler(void)
|
||||||
|
{
|
||||||
|
if (USART2->SR & USART_SR_RXNE) // si une donnée a été reçue
|
||||||
|
{
|
||||||
|
received_data2 = USART2->DR; // lire la donnée reçue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (USART2->SR & USART_SR_TC) // si la transmission est terminée
|
||||||
|
{
|
||||||
|
USART2->SR &= ~USART_SR_TC; // effacer le bit de transmission terminée
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void USART3_IRQHandler(void)
|
||||||
|
{
|
||||||
|
if (USART3->SR & USART_SR_RXNE) // si une donnée a été reçue
|
||||||
|
{
|
||||||
|
received_data3 = USART3->DR; // lire la donnée reçue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (USART3->SR & USART_SR_TC) // si la transmission est terminée
|
||||||
|
{
|
||||||
|
USART3->SR &= ~USART_SR_TC; // effacer le bit de transmission terminée
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -75,7 +75,7 @@
|
||||||
<OPTFL>
|
<OPTFL>
|
||||||
<tvExp>1</tvExp>
|
<tvExp>1</tvExp>
|
||||||
<tvExpOptDlg>0</tvExpOptDlg>
|
<tvExpOptDlg>0</tvExpOptDlg>
|
||||||
<IsCurrentTarget>1</IsCurrentTarget>
|
<IsCurrentTarget>0</IsCurrentTarget>
|
||||||
</OPTFL>
|
</OPTFL>
|
||||||
<CpuCode>18</CpuCode>
|
<CpuCode>18</CpuCode>
|
||||||
<DebugOpt>
|
<DebugOpt>
|
||||||
|
@ -143,24 +143,7 @@
|
||||||
<Name>-U -O206 -S8 -C0 -P00 -N00("") -D00(00000000) -L00(0) -TO65554 -TC10000000 -TT10000000 -TP21 -TDS8007 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -FO7 -FD20000000 -FC1000 -FN1 -FF0STM32F10x_128.FLM -FS08000000 -FL020000 -FP0($$Device:STM32F103RB$Flash\STM32F10x_128.FLM)</Name>
|
<Name>-U -O206 -S8 -C0 -P00 -N00("") -D00(00000000) -L00(0) -TO65554 -TC10000000 -TT10000000 -TP21 -TDS8007 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -FO7 -FD20000000 -FC1000 -FN1 -FF0STM32F10x_128.FLM -FS08000000 -FL020000 -FP0($$Device:STM32F103RB$Flash\STM32F10x_128.FLM)</Name>
|
||||||
</SetRegEntry>
|
</SetRegEntry>
|
||||||
</TargetDriverDllRegistry>
|
</TargetDriverDllRegistry>
|
||||||
<Breakpoint>
|
<Breakpoint/>
|
||||||
<Bp>
|
|
||||||
<Number>0</Number>
|
|
||||||
<Type>0</Type>
|
|
||||||
<LineNumber>51</LineNumber>
|
|
||||||
<EnabledFlag>1</EnabledFlag>
|
|
||||||
<Address>134219346</Address>
|
|
||||||
<ByteObject>0</ByteObject>
|
|
||||||
<HtxType>0</HtxType>
|
|
||||||
<ManyObjects>0</ManyObjects>
|
|
||||||
<SizeOfObject>0</SizeOfObject>
|
|
||||||
<BreakByAccess>0</BreakByAccess>
|
|
||||||
<BreakIfRCount>1</BreakIfRCount>
|
|
||||||
<Filename>U:\INSA\Microcontrôleur\Projet_Voilier_grp\Voilier\GPIO_Test\Sources\Main.c</Filename>
|
|
||||||
<ExecCommand></ExecCommand>
|
|
||||||
<Expression>\\GPIO_Test\Sources/Main.c\51</Expression>
|
|
||||||
</Bp>
|
|
||||||
</Breakpoint>
|
|
||||||
<Tracepoint>
|
<Tracepoint>
|
||||||
<THDelay>0</THDelay>
|
<THDelay>0</THDelay>
|
||||||
</Tracepoint>
|
</Tracepoint>
|
||||||
|
@ -274,7 +257,7 @@
|
||||||
<OPTFL>
|
<OPTFL>
|
||||||
<tvExp>1</tvExp>
|
<tvExp>1</tvExp>
|
||||||
<tvExpOptDlg>0</tvExpOptDlg>
|
<tvExpOptDlg>0</tvExpOptDlg>
|
||||||
<IsCurrentTarget>0</IsCurrentTarget>
|
<IsCurrentTarget>1</IsCurrentTarget>
|
||||||
</OPTFL>
|
</OPTFL>
|
||||||
<CpuCode>18</CpuCode>
|
<CpuCode>18</CpuCode>
|
||||||
<DebugOpt>
|
<DebugOpt>
|
||||||
|
@ -347,14 +330,47 @@
|
||||||
<Name>-U -O206 -S8 -C0 -P00 -N00("") -D00(00000000) -L00(0) -TO65554 -TC10000000 -TT10000000 -TP21 -TDS8007 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -FO7 -FD20000000 -FC1000 -FN1 -FF0STM32F10x_128.FLM -FS08000000 -FL020000 -FP0($$Device:STM32F103RB$Flash\STM32F10x_128.FLM)</Name>
|
<Name>-U -O206 -S8 -C0 -P00 -N00("") -D00(00000000) -L00(0) -TO65554 -TC10000000 -TT10000000 -TP21 -TDS8007 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -FO7 -FD20000000 -FC1000 -FN1 -FF0STM32F10x_128.FLM -FS08000000 -FL020000 -FP0($$Device:STM32F103RB$Flash\STM32F10x_128.FLM)</Name>
|
||||||
</SetRegEntry>
|
</SetRegEntry>
|
||||||
</TargetDriverDllRegistry>
|
</TargetDriverDllRegistry>
|
||||||
<Breakpoint/>
|
<Breakpoint>
|
||||||
|
<Bp>
|
||||||
|
<Number>0</Number>
|
||||||
|
<Type>0</Type>
|
||||||
|
<LineNumber>62</LineNumber>
|
||||||
|
<EnabledFlag>1</EnabledFlag>
|
||||||
|
<Address>134218958</Address>
|
||||||
|
<ByteObject>0</ByteObject>
|
||||||
|
<HtxType>0</HtxType>
|
||||||
|
<ManyObjects>0</ManyObjects>
|
||||||
|
<SizeOfObject>0</SizeOfObject>
|
||||||
|
<BreakByAccess>0</BreakByAccess>
|
||||||
|
<BreakIfRCount>1</BreakIfRCount>
|
||||||
|
<Filename>U:\INSA\Microcontrôleur\Projet_Voilier_grp\Voilier\GPIO_Test\Sources\Main.c</Filename>
|
||||||
|
<ExecCommand></ExecCommand>
|
||||||
|
<Expression>\\GPIO_Test\Sources/Main.c\62</Expression>
|
||||||
|
</Bp>
|
||||||
|
<Bp>
|
||||||
|
<Number>1</Number>
|
||||||
|
<Type>0</Type>
|
||||||
|
<LineNumber>53</LineNumber>
|
||||||
|
<EnabledFlag>1</EnabledFlag>
|
||||||
|
<Address>0</Address>
|
||||||
|
<ByteObject>0</ByteObject>
|
||||||
|
<HtxType>0</HtxType>
|
||||||
|
<ManyObjects>0</ManyObjects>
|
||||||
|
<SizeOfObject>0</SizeOfObject>
|
||||||
|
<BreakByAccess>0</BreakByAccess>
|
||||||
|
<BreakIfRCount>0</BreakIfRCount>
|
||||||
|
<Filename>.\Sources\Main.c</Filename>
|
||||||
|
<ExecCommand></ExecCommand>
|
||||||
|
<Expression></Expression>
|
||||||
|
</Bp>
|
||||||
|
</Breakpoint>
|
||||||
<Tracepoint>
|
<Tracepoint>
|
||||||
<THDelay>0</THDelay>
|
<THDelay>0</THDelay>
|
||||||
</Tracepoint>
|
</Tracepoint>
|
||||||
<DebugFlag>
|
<DebugFlag>
|
||||||
<trace>0</trace>
|
<trace>0</trace>
|
||||||
<periodic>1</periodic>
|
<periodic>1</periodic>
|
||||||
<aLwin>1</aLwin>
|
<aLwin>0</aLwin>
|
||||||
<aCover>0</aCover>
|
<aCover>0</aCover>
|
||||||
<aSer1>0</aSer1>
|
<aSer1>0</aSer1>
|
||||||
<aSer2>0</aSer2>
|
<aSer2>0</aSer2>
|
||||||
|
@ -422,7 +438,7 @@
|
||||||
<GroupNumber>1</GroupNumber>
|
<GroupNumber>1</GroupNumber>
|
||||||
<FileNumber>2</FileNumber>
|
<FileNumber>2</FileNumber>
|
||||||
<FileType>1</FileType>
|
<FileType>1</FileType>
|
||||||
<tvExp>0</tvExp>
|
<tvExp>1</tvExp>
|
||||||
<tvExpOptDlg>0</tvExpOptDlg>
|
<tvExpOptDlg>0</tvExpOptDlg>
|
||||||
<bDave2>0</bDave2>
|
<bDave2>0</bDave2>
|
||||||
<PathWithFileName>..\Drivers\Sources\Driver_Timer.c</PathWithFileName>
|
<PathWithFileName>..\Drivers\Sources\Driver_Timer.c</PathWithFileName>
|
||||||
|
|
|
@ -10,8 +10,15 @@ int main (void){
|
||||||
//Déclaration d'une LED et d'un BP par structure GPIO
|
//Déclaration d'une LED et d'un BP par structure GPIO
|
||||||
MyGPIO_Struct_TypeDef LED;
|
MyGPIO_Struct_TypeDef LED;
|
||||||
MyGPIO_Struct_TypeDef BP;
|
MyGPIO_Struct_TypeDef BP;
|
||||||
|
|
||||||
//Déclaration d'un Timer 500 ms
|
//Déclaration d'un Timer 500 ms
|
||||||
MyTimer_Struct_TypeDef TIM500ms;
|
MyTimer_Struct_TypeDef TIM500ms;
|
||||||
|
|
||||||
|
//Déclaration de l'UART
|
||||||
|
MyUART_Struct_Typedef * UART;
|
||||||
|
UART->UART=USART1;
|
||||||
|
UART->baudrate=9600;
|
||||||
|
|
||||||
//Config LED PA5
|
//Config LED PA5
|
||||||
LED.GPIO_Conf = Out_Ppull;
|
LED.GPIO_Conf = Out_Ppull;
|
||||||
LED.GPIO_Pin = 5;
|
LED.GPIO_Pin = 5;
|
||||||
|
@ -42,7 +49,8 @@ int main (void){
|
||||||
MyTimer_PWM(&TIM500ms, 50);
|
MyTimer_PWM(&TIM500ms, 50);
|
||||||
|
|
||||||
|
|
||||||
UART_init();
|
|
||||||
|
UART_init(UART);
|
||||||
|
|
||||||
//NVIC_EnableIRQ(USART1_IRQn);// Activer les interruptions
|
//NVIC_EnableIRQ(USART1_IRQn);// Activer les interruptions
|
||||||
|
|
||||||
|
@ -50,6 +58,9 @@ int main (void){
|
||||||
for (i = 0; i < len; i++) {
|
for (i = 0; i < len; i++) {
|
||||||
UART_send(data[i]);
|
UART_send(data[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UART_read(data[i], UART);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue