本來是想通過串口輸入資料,然后將資料顯示到數碼管上的,但是代碼寫好后,LED沒有任何反應,而且串口也沒有回傳我發送的資料,這是一些截圖
,PA0和PA1還沒有用到。代碼如下:main.c:
#include "stm32f10x.h"
#include "smart.h"
#define uchar unsigned char
#define uint unsigned int
int main()
{
Usart_Init();
Usled_Clock_Init();
Usled_Gpio_Init();
while(1)
{
uchar i;
uint a,b;
i=RecvChar();
SendChar(i);
discuss(i);
}
}
#include "stm32f10x.h"
#include "smart.h"
#include <stdio.h>
unsigned char seg[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
void Usled_Clock_Init(void)
{
/*ê±?óê1?ün */
RCC_DeInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOC, ENABLE);
}
void Usled_Gpio_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_DeInit(GPIOA);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_DeInit(GPIOC);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_Write(GPIOC, seg[3]);
}
//void NVI_Init(void)
//{
// NVIC_InitTypeDef NVIC_InitStructure;
// NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
// NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
// NVIC_Init(&NVIC_InitStructure);
//}
void Usart_Init(void)
{
USART_InitTypeDef USART_InitStructure;
USART_DeInit(USART1);
/* The following example illustrates how to configure the USART1 */
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_Mode_Rx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
unsigned char RecvChar(void)
{
unsigned char RxData;
while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)
RxData = USART_ReceiveData(USART1);
return RxData;
}
int fputc(int ch, FILE *f )
{
USART_SendData(USART1, ch);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
return (ch);
}
void delay(unsigned int i)
{
unsigned char j;
while(i--)
for(j = 0;j < 100; j++);
}
void SendChar(unsigned char ch)
{
USART_SendData(USART1, ch);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
}
void discuss(unsigned char dat)
{
switch(dat)
{
case 'a':
GPIO_Write(GPIOC, seg[1]);
break;
case '2':
GPIO_Write(GPIOC, seg[2]);
break;
case '3':
GPIO_Write(GPIOC, seg[3]);
break;
case '4':
GPIO_Write(GPIOC, seg[4]);
break;
case '5':
GPIO_Write(GPIOC, seg[5]);
break;
case '6':
GPIO_Write(GPIOC, seg[6]);
break;
case '7':
GPIO_Write(GPIOC, seg[7]);
break;
case '8':
GPIO_Write(GPIOC, seg[8]);
break;
case '9':
GPIO_Write(GPIOC, seg[9]);
break;
default:
printf("Error");
break;
}
}
main.h:
#ifndef __USART_H_
#define __USART_H_
#include <stdio.h>
void Usled_Clock_Init(void);
void Usled_Gpio_Init(void);
void Usart_Init(void);
unsigned char RecvChar(void);
int fputc(int ch, FILE *f );
void delay(unsigned int i);
void NVI_Init(void);
void discuss(unsigned char dat);
void SendChar(unsigned char ch);
#endif
uj5u.com熱心網友回復:
我想先一直顯示一個數字3,然后通過串口輸入數字,將這個數字顯示到LED上uj5u.com熱心網友回復:
單步跟蹤一下試試uj5u.com熱心網友回復:
這個也沒有變化
uj5u.com熱心網友回復:
放棄Proteus用開發板吧轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/29050.html
標籤:單片機/工控
上一篇:嵌入式學習及作業規劃的困惑
下一篇:求合作:飛思卡爾的8串口方案設計
