0% found this document useful (0 votes)
4 views2 pages

Mikro

The document defines a Serial class for handling serial communication on an STM32 microcontroller. It includes methods for initializing the serial port, reading data, checking availability of data, and printing messages. The main function demonstrates the usage of the Serial class by reading incoming data in a loop and storing it in an array.

Uploaded by

Selimbašić
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Mikro

The document defines a Serial class for handling serial communication on an STM32 microcontroller. It includes methods for initializing the serial port, reading data, checking availability of data, and printing messages. The main function demonstrates the usage of the Serial class by reading incoming data in a loop and storing it in an array.

Uploaded by

Selimbašić
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#ifndef SERIAL_H

#define SERIAL_H
class Serial{
public:
Serial();
~Serial();
void begin(int baudrate);
char read();
bool available();
void println(char *msg, ...);

private:

};
#endif SERIAL
---------------------------------------
#include "serial.h"
#include "stm32f10x.h" // Device header
#include "stdlib.h"
#include "stdio.h"
#include "stdint.h"
#include "string.h"
#include "stdarg.h"

Serial::Serial(){}
Serial::~Serial(){}
void Serial::begin(int baudrate){
RCC->APB2ENR|=RCC_APB2ENR_IOPAEN|RCC_APB2ENR_USART1EN|
RCC_APB2ENR_AFIOEN;
USART1->BRR=(SystemCoreClock/baudrate);
USART1->CR1|=USART_CR1_RE|USART_CR1_TE|USART_CR1_UE;
GPIOA->CRH&=~(15<<4);
GPIOA->CRH|=(11<<4);
}
char Serial::read()
{
char data;

data=USART1->DR;
//while(!(USART1->SR & USART_SR_TC));
return data;
}
bool Serial::available()
{
if(USART1->SR&USART_SR_RXNE)
{
return true;
}
else
{
return false;
}
}
void Serial::println(char *msg, ...)
{
char buffer [64];
va_list args;
va_start(args,msg);
vsprintf(buffer,msg,args);
for(int i=0; i<strlen(buffer); i++)
{
USART1->DR=buffer[i];
while(!(USART1->SR & USART_SR_TXE));
}
}
---------------------------------------
#include "stm32f10x.h"
#include "serial.h"

char data[10];
int i=0;

int main(void)
{
Serial mySerial;
mySerial.begin(9600);
while(1)
{
if(mySerial.available())
{

data[i]=mySerial.read();
i++;
if(i==10)
{
i=0;
}
//mySerial.println("%i",data);
}
}
}

You might also like