This commit is contained in:
Alix Chagot 2023-03-27 16:49:42 +02:00
parent e3014aefef
commit b49cd84c09
5 changed files with 153 additions and 4 deletions

View file

@ -0,0 +1,27 @@
#ifndef UART_H
#define UART_H
#include "stm32f10x.h"
typedef struct {
USART_TypeDef * UART;
unsigned int BaudRate;
unsigned char Wlengh;
unsigned char Wparity;
unsigned char Wstop;
}MyUART_Struct_Typedef;
#define parity_none 0x0
#define parity_even 0x1
#define parity_odd 0x2
#define Wlengh8 0x0
#define Wlengh9 0X1
#define stop1b 0x0
#define stop2b 0x2
void MyUART_Init(MyUART_Struct_Typedef * UARTStructPtr);
void USART1_IRQHandler(void);
void USART2_IRQHandler(void);
void USART3_IRQHandler(void);
char MyUART_Read(MyUART_Struct_Typedef * UARTStructPtr);
#endif

View file

@ -0,0 +1,91 @@
#include "UART.h"
#include "gpio.h"
//faire GPIO
char data1 = 0x00;
char data2 = 0x00;
char data3 = 0x00;
void MyUART_Init(MyUART_Struct_Typedef * UARTStructPtr){
if (UARTStructPtr->UART == USART1)
RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
if (UARTStructPtr->UART == USART2)
RCC->APB2ENR |= RCC_APB1ENR_USART2EN;
if (UARTStructPtr->UART == USART3)
RCC->APB2ENR |= RCC_APB1ENR_USART3EN;
UARTStructPtr->UART->BRR = 72000000/(UARTStructPtr->BaudRate);
UARTStructPtr->UART->CR1 |= ((UARTStructPtr->Wlengh)<<12);
UARTStructPtr->UART->CR1 |= (0x1<<10);
if(UARTStructPtr->Wparity == parity_none)
UARTStructPtr->UART->CR1 &= ~(0x1<<10);
if(UARTStructPtr->Wparity == parity_odd)
UARTStructPtr->UART->CR1 |= (0x1<<9);
if(UARTStructPtr->Wparity == parity_even)
UARTStructPtr->UART->CR1 &= ~(0x1<<9);
if(UARTStructPtr->Wstop == stop1b)
UARTStructPtr->UART->CR2 &= ~(0x3<<12);
if(UARTStructPtr->Wstop == stop2b){
UARTStructPtr->UART->CR2 &= ~(0x3<<12);
UARTStructPtr->UART->CR2 |= (0x1<<13);
}
UARTStructPtr->UART->CR1 |= (USART_CR1_TE | USART_CR1_RE | USART_CR1_UE | USART_CR1_RXNEIE);
NVIC_EnableIRQ(USART1_IRQn);
}
void USART1_IRQHandler(void)
{
// Check if receive data register not empty
if(USART1->SR & USART_SR_RXNE)
{
data1 = USART1->DR; // read received data
// do something with received data
}
}
void USART2_IRQHandler(void)
{
// Check if receive data register not empty
if(USART2->SR & USART_SR_RXNE)
{
data2 = USART2->DR; // read received data
// do something with received data
}
}
void USART3_IRQHandler(void)
{
// Check if receive data register not empty
if(USART3->SR & USART_SR_RXNE)
{
data3 = USART3->DR; // read received data
// do something with received data
}
}
char MyUART_Read(MyUART_Struct_Typedef * UARTStructPtr)
{
if(UARTStructPtr->UART == USART1)
return data1;
else if(UARTStructPtr->UART == USART2)
return data2;
else if(UARTStructPtr->UART == USART3)
return data3;
else
return 0xFF;
}
/* exemple utilisation fonction :
UART1.UART = USART1; // choix UART (USART1, USART2, USART3)
UART1.BaudRate = 9600; // Vitesse de communication MAX 118200
UART1.Wlengh = Wlengh8; // longeur du mot (Wlengh8 ,Wlengh9)
UART1.Wparity = parity_even; // parité (parity_even, parity_odd, parity_none)
UART1.Wstop = stop1b; // nombre bit de stop (stop1b, stop2b)
MyUART_Init(&UART1); // initialisation
*/

View file

@ -1,6 +1,7 @@
#include "stm32f10x.h"
#include "gpio.h"
#include "timer.h"
#include "UART.h"
void changer_led(void);
@ -8,6 +9,8 @@ int main(void) {
MyGPIO_Struct_TypeDef led2;
MyGPIO_Struct_TypeDef b1;
MyTimer_Struct_Typedef t3;
MyUART_Struct_Typedef UART1;
MyGPIO_InitClock();
@ -29,6 +32,12 @@ int main(void) {
MyTimer_DutyCycle(TIM1,1,10);
MyTimer_Base_Start(t3);
UART1.UART = USART1;
UART1.BaudRate = 9600;
UART1.Wlengh = Wlengh8;
UART1.Wparity = parity_even;
UART1.Wstop = stop1b;
MyUART_Init(&UART1);
do
{

View file

@ -125,7 +125,7 @@
<SetRegEntry>
<Number>0</Number>
<Key>DLGDARM</Key>
<Name>(1010=-1,-1,-1,-1,0)(1007=-1,-1,-1,-1,0)(1008=-1,-1,-1,-1,0)(1009=-1,-1,-1,-1,0)(100=-1,-1,-1,-1,0)(110=-1,-1,-1,-1,0)(111=-1,-1,-1,-1,0)(1011=-1,-1,-1,-1,0)(180=-1,-1,-1,-1,0)(120=920,358,1341,785,0)(121=-1,-1,-1,-1,0)(122=-1,-1,-1,-1,0)(123=-1,-1,-1,-1,0)(140=-1,-1,-1,-1,0)(240=105,137,504,482,0)(190=-1,-1,-1,-1,0)(200=-1,-1,-1,-1,0)(170=120,153,405,449,0)(130=624,173,1218,924,0)(131=931,121,1525,872,0)(132=973,161,1567,912,0)(133=-1,-1,-1,-1,0)(160=-1,-1,-1,-1,0)(161=-1,-1,-1,-1,0)(162=-1,-1,-1,-1,0)(210=-1,-1,-1,-1,0)(211=-1,-1,-1,-1,0)(220=-1,-1,-1,-1,0)(221=-1,-1,-1,-1,0)(230=-1,-1,-1,-1,0)(234=-1,-1,-1,-1,0)(231=-1,-1,-1,-1,0)(232=-1,-1,-1,-1,0)(233=-1,-1,-1,-1,0)(150=-1,-1,-1,-1,0)(151=-1,-1,-1,-1,0)</Name>
<Name>(1010=-1,-1,-1,-1,0)(1007=-1,-1,-1,-1,0)(1008=-1,-1,-1,-1,0)(1009=-1,-1,-1,-1,0)(100=-1,-1,-1,-1,0)(110=-1,-1,-1,-1,0)(111=-1,-1,-1,-1,0)(1011=-1,-1,-1,-1,0)(180=-1,-1,-1,-1,0)(120=920,358,1341,785,0)(121=-1,-1,-1,-1,0)(122=-1,-1,-1,-1,0)(123=-1,-1,-1,-1,0)(140=-1,-1,-1,-1,0)(240=105,137,504,482,0)(190=-1,-1,-1,-1,0)(200=-1,-1,-1,-1,0)(170=120,153,405,449,0)(130=624,173,1218,924,0)(131=931,121,1525,872,0)(132=973,161,1567,912,0)(133=-1,-1,-1,-1,0)(160=767,440,1215,854,0)(161=-1,-1,-1,-1,0)(162=-1,-1,-1,-1,0)(210=-1,-1,-1,-1,0)(211=-1,-1,-1,-1,0)(220=-1,-1,-1,-1,0)(221=-1,-1,-1,-1,0)(230=-1,-1,-1,-1,0)(234=-1,-1,-1,-1,0)(231=-1,-1,-1,-1,0)(232=-1,-1,-1,-1,0)(233=-1,-1,-1,-1,0)(150=-1,-1,-1,-1,0)(151=-1,-1,-1,-1,0)</Name>
</SetRegEntry>
<SetRegEntry>
<Number>0</Number>
@ -378,7 +378,7 @@
<Group>
<GroupName>MesSources</GroupName>
<tvExp>0</tvExp>
<tvExp>1</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<cbSel>0</cbSel>
<RteFlg>0</RteFlg>
@ -406,7 +406,7 @@
<GroupNumber>2</GroupNumber>
<FileNumber>2</FileNumber>
<FileType>1</FileType>
<tvExp>1</tvExp>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<bDave2>0</bDave2>
<PathWithFileName>..\Drivers\src\gpio.c</PathWithFileName>
@ -418,7 +418,7 @@
<GroupNumber>2</GroupNumber>
<FileNumber>3</FileNumber>
<FileType>1</FileType>
<tvExp>1</tvExp>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<bDave2>0</bDave2>
<PathWithFileName>..\Drivers\src\timer.c</PathWithFileName>
@ -426,6 +426,18 @@
<RteFlg>0</RteFlg>
<bShared>0</bShared>
</File>
<File>
<GroupNumber>2</GroupNumber>
<FileNumber>4</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<bDave2>0</bDave2>
<PathWithFileName>..\Drivers\src\UART.c</PathWithFileName>
<FilenameWithoutPath>UART.c</FilenameWithoutPath>
<RteFlg>0</RteFlg>
<bShared>0</bShared>
</File>
</Group>
<Group>

View file

@ -403,6 +403,11 @@
<FileType>1</FileType>
<FilePath>..\Drivers\src\timer.c</FilePath>
</File>
<File>
<FileName>UART.c</FileName>
<FileType>1</FileType>
<FilePath>..\Drivers\src\UART.c</FilePath>
</File>
</Files>
</Group>
<Group>
@ -810,6 +815,11 @@
<FileType>1</FileType>
<FilePath>..\Drivers\src\timer.c</FilePath>
</File>
<File>
<FileName>UART.c</FileName>
<FileType>1</FileType>
<FilePath>..\Drivers\src\UART.c</FilePath>
</File>
</Files>
</Group>
<Group>