标题:
STM32F103硬件UART和I2C模拟
[打印本页]
作者:
mslan10390
时间:
2018-9-12 22:23
标题:
STM32F103硬件UART和I2C模拟
本帖最后由 mslan10390 于 2018-9-12 22:25 编辑
最近有空
把之前杂乱的专案比较常用的部分整理出来
目前是硬件UART和I2C模拟
之后有时间再整理其馀的
#include "UART.h"
void USART1_Config(){
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = USART1_TX_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(USART1_Port, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = USART1_RX_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(USART1_Port, &GPIO_InitStructure);
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 115200;
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_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
}
void USART1_SendByte(uint8_t txByte){
USART_ClearFlag(USART1, USART_FLAG_TC);
USART_SendData(USART1, txByte);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
}
void USART1_SendStr(char* pStr){
uint8_t txByte;
while(1){
txByte = *pStr++;
if(txByte == 0) break;
USART1_SendByte(txByte);
}
}
void USART1_SendADC(uint16_t ADCVar){
uint8_t Hbit;
uint8_t Lbit;
// 1st bits ( 1 1 _ _ _ _ _ _ )
Hbit = ( ADCVar >> 6 ) | 0xC0;
USART1_SendByte(Hbit);
// 2nd bits ( 0 0 _ _ _ _ _ _ )
Lbit = ( ADCVar & 0x3F);
USART1_SendByte(Lbit);
}
void USART1_Send_32Var(uint32_t txVar){
uint8_t sBit;
// 1st ( 1 1 _ _ _ _ _ _ ) 6 bits
sBit = (txVar >> 26) | 0xC0;
USART1_SendByte(sBit);
// 2nd ( 1 0 _ _ _ _ _ _ ) 6 bits
sBit = ((txVar >> 20) & 0x3F ) | 0x80;
USART1_SendByte(sBit);
// 3rd ( 0 1 _ _ _ _ _ _ ) 6 bits
sBit = ((txVar >> 14) & 0x3F ) | 0x40;
USART1_SendByte(sBit);
// 4th ( 1 _ _ _ _ _ _ _ ) 7 bits
sBit = (txVar >> 7) | 0x80;
USART1_SendByte(sBit);
// Last ( 0 _ _ _ _ _ _ _ ) 7 bits
sBit = txVar & 0x7F;
USART1_SendByte(sBit);
// 6 + 6 + 6 + 7 + 7 = 32 bit
}
void USART1_IRQHandler(){
uint16_t data_16b;
//uint32_t data_32b;
data_16b = 0xAAA;
//data_32b = 0xAAAAAAAA;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET){
if((char)USART_ReceiveData(USART1) == 'S'){
USART1_SendADC(data_16b);
//USART1_Send_32Var(data_32b);
}
}
}
复制代码
STM32F103_testFunction.rar
(8.73 KB, 下载次数: 39)
2018-9-12 22:22 上传
点击文件名下载附件
下载积分: 黑币 -5
手边没有现成模块
所以我用两块STM32F103互传进行测试
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1