Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stddef.h>
- #include "stm32f10x.h"
- #include "stm32f10x_rcc.h"
- #include "stm32f10x_gpio.h"
- #include "stm32f10x_usart.h"
- void usart_init(void);
- void usart_send(const char chr);
- void print_f(const char *s, ...);
- int getchar(void);
- int putchar(int c);
- void putString(char *string);
- int znak;
- int main(void)
- {
- //Inicijalizacija USART1
- usart_init();
- //Ciklički dio
- while (1)
- {
- znak = getchar();
- if(znak==-1)continue;
- putchar(znak);
- switch(znak){
- case 'p' :
- case 'P' : {
- GPIO_WriteBit(GPIOB, GPIO_Pin_0, SET);
- print_f("Upaljeno");
- break;
- }
- case 'g' :
- case 'G' : {
- GPIO_WriteBit(GPIOB, GPIO_Pin_0, RESET);
- print_f("Ugaseno");
- break;
- }
- default : break;
- }
- }
- }
- void usart_init(void) {
- GPIO_InitTypeDef GPIO_InitStruct;
- USART_InitTypeDef USART_InitStruct;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_USART1 | RCC_APB2Periph_AFIO, ENABLE);
- //1-inicijalizacija PBO za LED
- GPIO_StructInit(&GPIO_InitStruct);
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_Write(GPIOB, 0xFFFF);
- GPIO_Init(GPIOB, &GPIO_InitStruct);
- //Inicijalizacija Tx pina (GPIOA, GPIO_Pin_9)
- GPIO_StructInit(&GPIO_InitStruct);
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_Init(GPIOA, &GPIO_InitStruct);
- //Inicijalizacija Rx pina (GPIOA, GPIO_Pin_10)
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_Init(GPIOA, &GPIO_InitStruct);
- //komunikacijski parametri USART1
- USART_StructInit(&USART_InitStruct);
- USART_InitStruct.USART_BaudRate = 9600;
- USART_InitStruct.USART_WordLength = USART_WordLength_8b;
- USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_InitStruct.USART_Parity = USART_Parity_No;
- USART_InitStruct.USART_StopBits = USART_StopBits_1;
- USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_Init(USART1, &USART_InitStruct);
- USART_Cmd(USART1, ENABLE);
- }
- // Šalje jedan bit kroz USART
- void usart_send(const char chr) {
- while(!(USART1->SR & USART_SR_TC));
- USART1->DR = chr;
- }
- // Šalje string kroz USART
- void print_f(const char *s, ...) {
- int i=0;
- while (s[i]) {
- usart_send(s[i++]);
- }
- }
- int getchar(void){
- if(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == SET){
- return USART1->DR & 0xff;
- }else{
- return -1;
- }
- }
- int putchar(int c){
- while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
- USART1->DR = c & 0xff;
- return 0;
- }
- void putString(char *string){
- }
Advertisement
RAW Paste Data
Copied
Advertisement