Hayırlı akşamlar, stm32f103c8 ile uart iletişimi kurmaya çalışıyorum, aşağıda keil ile yazdığım kod mevcut, kendi kütüphanesinden faydalandım, sanki bir şey eksik gibi. Yardımcı olabilecek birileri var mı? Teşekkürler.
#include "stm32f10x_gpio.h" // Keil::Device:StdPeriph Drivers:GPIO
#include "stm32f10x_rcc.h" // Keil::Device:StdPeriph Drivers:RCC
#include "stm32f10x.h" // Device header
void Delay(uint32_t time)
{
while(time)
time--;
}
int main()
{
GPIO_InitTypeDef GPIO_InitStructure2;
USART_InitTypeDef USART_InitTypeDef2;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure2.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure2);
USART_InitTypeDef2.USART_BaudRate = 9600;
USART_InitTypeDef2.USART_WordLength = USART_WordLength_8b;
USART_InitTypeDef2.USART_StopBits = USART_StopBits_1;
USART_InitTypeDef2.USART_Parity = USART_Parity_No;
USART_InitTypeDef2.USART_Mode = USART_Mode_Tx;
USART_Init(USART1, &USART_InitTypeDef2);
while(1)
{
USART_SendData(USART1, 5);
GPIO_SetBits(GPIOC, GPIO_Pin_13);
Delay(3525322);
GPIO_ResetBits(GPIOC, GPIO_Pin_13);
Delay(3525322);
}
}
Usart olarak kullanılacak pini Alternate Function(AF) olarak tanımlayıp
[ **
**
**
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
**
**
**]
devamına da,
GPIO_PinAFConfig(GPIOC , GPIO_PinSource13, GPIO_AF_USART1); şeklinde ilişkilendirirseniz bir sıkıntı kalmaz gibi görünüyor.
Siteye de göz atabilirsiniz. http://www.vhdl.club/2016/10/02/ders-9-stm32f4-ile-usart-kullanimi/
Karşılaştırınız bkalım nereden ne çıkacak
/*
**
** Main.c
**
**
**********************************************************************/
/*
Last committed: $Revision: 00 $
Last changed by: $Author: $
Last changed date: $Date: $
ID: $Id: $
**********************************************************************/
#include "stm32f10x_conf.h"
void Init_USART1(void) {
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1,
ENABLE);
USART_ClockInitTypeDef USART_ClockInitStructure;
GPIO_InitTypeDef UARTTX;
UARTTX.GPIO_Pin = GPIO_Pin_9;
UARTTX.GPIO_Mode = GPIO_Mode_AF_PP;
UARTTX.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitTypeDef UARTRX;
UARTRX.GPIO_Pin = GPIO_Pin_10;
UARTRX.GPIO_Mode = GPIO_Mode_IPU;
UARTRX.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &UARTTX);
GPIO_Init(GPIOA, &UARTRX);
USART_ClockStructInit(&USART_ClockInitStructure);
USART_ClockInit(USART1, &USART_ClockInitStructure);
USART_InitTypeDef UART1;
UART1.USART_BaudRate = 9600;
UART1.USART_WordLength = USART_WordLength_8b;
UART1.USART_StopBits = USART_StopBits_1;
UART1.USART_Parity = USART_Parity_No;
UART1.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
UART1.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &UART1);
USART_Cmd(USART1, ENABLE);
USART1_NVIC_Configuration();
}
void USART1_NVIC_Configuration(void) {
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
}
//USART Recieve Interrupt
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
USART_SendData(USART1, 'B');
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
}
void USART_puts(USART_TypeDef* USARTx, volatile char *s){
while(*s){
// wait until data register is empty
while( !(USARTx->SR & 0x00000040) );
USART_SendData(USARTx, *s);
*s++;
}
}
int main(void)
{
Init_USART1();
while(1)
{
//USART_SendData(USART1, "M");
//USART_puts(USART1,"Muhittin");
char dizi[10];
sprintf (dizi,"flt: %4.4fn", 1.4142135);
USART_puts(USART1,dizi);
printf("flt: %4.4fn", 1.4142135);
}
}
teşekkür ederim.
USART modülünü düzgün set etmişsin fakat sorun USART ın GPIO larını biçimlendirmemenden kaynaklı. Anladığım kadarıyla PC13 e led koymuşsun ve USARTa veri yazdığında yan sön yapıyor. USART ın GPIO larını biçimlendir. GPIOA nın 9.pini tx, 10.pini rx dir. ayrıca RCC ile de Başlatman gerekir. Ledi eğer PC13 ile yakmak istiyorsan yanlış seçim yapıyorsun datasheette özellikle dipnotta belirtmişler GPIOC 13,14,15 pinleri ile led sürmemek gerektiğini. Örnek USART kodu olarak aşağıda kendi kullandığım kodu yapıştırdım.
void init_USART()
{
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1,ENABLE);
GPIO_InitStruct.GPIO_Pin = USART1_Tx;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx;
USART_Init(USART1,&USART_InitStructure);
USART_Cmd(USART1,ENABLE);
}
///////////////////////////////////////////////////////////////////////////
SEND ETMEK İÇİNDE
USART_SendData(USART1,data);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);
Ömrüm çürüdü datasheet ve kütüphane okumaktan....
Teşekkür ederim.
Buyrun stm32f103c8 ve cp2102 ile veri yollama;
#include "stm32f10x_gpio.h" // Keil::Device:StdPeriph Drivers:GPIO
#include "stm32f10x_rcc.h" // Keil::Device:StdPeriph Drivers:RCC
#include "stm32f10x.h" // Device header
#include "stm32f10x_gpio.h" // Keil::Device:StdPeriph Drivers:GPIO
#include "stm32f10x_usart.h" // Keil::Device:StdPeriph Drivers:USART
// gecikme
void Delay(uint32_t time)
{
while(time)
time--;
}
///transfer tamamlanana kadar bekle
void UU_PutChar(USART_TypeDef* USARTx, uint8_t ch)
{
while(!(USARTx->SR & USART_SR_TXE));
USARTx->DR = ch;
}
//kelime yazma
void UU_PutString(USART_TypeDef* USARTx, uint8_t * str)
{
while(*str != 0)
{
UU_PutChar(USARTx, *str);
str++;
}
}
int main()
{
GPIO_InitTypeDef GPIO_InitStructure2;
GPIO_InitTypeDef GPIO_InitStructure3;
USART_InitTypeDef USART_InitTypeDef2;
USART_ClockInitTypeDef USART_ClockInitStructure;
USART_ClockStructInit(&USART_ClockInitStructure);
USART_ClockInit(USART1, &USART_ClockInitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);
GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure2.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure2);
GPIO_InitStructure3.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure3.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure3.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure3);
USART_InitTypeDef2.USART_BaudRate = 9600;
USART_InitTypeDef2.USART_WordLength = USART_WordLength_8b;
USART_InitTypeDef2.USART_StopBits = USART_StopBits_1;
USART_InitTypeDef2.USART_Parity = USART_Parity_No;
USART_InitTypeDef2.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitTypeDef2.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_Init(USART1, &USART_InitTypeDef2);
USART_Cmd(USART1, ENABLE);
while(1)
{
UU_PutString(USART1, "PICPROJE ");
GPIO_SetBits(GPIOA, GPIO_Pin_2);
Delay(3525322);
GPIO_ResetBits(GPIOA, GPIO_Pin_2);
Delay(3525322);
}
}
Yardımcı olan herkese teşekkür ederim.