找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3004|回复: 0
打印 上一主题 下一主题
收起左侧

STM32F103硬件UART和I2C模拟

[复制链接]
跳转到指定楼层
楼主
ID:281548 发表于 2018-9-12 22:23 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 mslan10390 于 2018-9-12 22:25 编辑

最近有空
把之前杂乱的专案比较常用的部分整理出来
目前是硬件UART和I2C模拟
之后有时间再整理其馀的

  1. #include "UART.h"


  2. void USART1_Config(){
  3.        
  4.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1, ENABLE);
  5.        
  6.        
  7.         GPIO_InitTypeDef GPIO_InitStructure;
  8.        
  9.         GPIO_InitStructure.GPIO_Pin = USART1_TX_Pin;
  10.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  11.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  12.         GPIO_Init(USART1_Port, &GPIO_InitStructure);
  13.        
  14.         GPIO_InitStructure.GPIO_Pin = USART1_RX_Pin;
  15.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  16.         GPIO_Init(USART1_Port, &GPIO_InitStructure);
  17.        
  18.        
  19.         USART_InitTypeDef USART_InitStructure;
  20.        
  21.         USART_InitStructure.USART_BaudRate = 115200;
  22.           USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  23.           USART_InitStructure.USART_StopBits = USART_StopBits_1;
  24.         USART_InitStructure.USART_Parity = USART_Parity_No;
  25.           USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  26.           USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  27.        
  28.         USART_Init(USART1, &USART_InitStructure);
  29.         USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  30.         USART_Cmd(USART1, ENABLE);
  31.        
  32. }


  33. void USART1_SendByte(uint8_t txByte){
  34.        
  35.         USART_ClearFlag(USART1, USART_FLAG_TC);
  36.         USART_SendData(USART1, txByte);
  37.         while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
  38. }

  39. void USART1_SendStr(char* pStr){
  40.        
  41.         uint8_t txByte;
  42.         while(1){
  43.                 txByte = *pStr++;
  44.                 if(txByte == 0) break;
  45.                 USART1_SendByte(txByte);
  46.         }
  47. }

  48. void USART1_SendADC(uint16_t ADCVar){
  49.        
  50.         uint8_t Hbit;
  51.         uint8_t Lbit;
  52.        
  53.         // 1st bits        ( 1 1 _ _  _ _ _ _ )
  54.         Hbit = ( ADCVar >> 6 ) | 0xC0;
  55.         USART1_SendByte(Hbit);
  56.        
  57.         // 2nd bits        ( 0 0 _ _  _ _ _ _ )
  58.         Lbit = ( ADCVar & 0x3F);
  59.         USART1_SendByte(Lbit);
  60.        
  61. }

  62. void USART1_Send_32Var(uint32_t txVar){
  63.         uint8_t sBit;
  64.        
  65.         // 1st                 ( 1 1 _ _  _ _ _ _ )        6 bits
  66.         sBit = (txVar >> 26) | 0xC0;
  67.         USART1_SendByte(sBit);
  68.        
  69.         // 2nd                 ( 1 0 _ _  _ _ _ _ )        6 bits
  70.         sBit = ((txVar >> 20) & 0x3F ) | 0x80;
  71.         USART1_SendByte(sBit);
  72.        
  73.         // 3rd                ( 0 1 _ _  _ _ _ _ )        6 bits
  74.         sBit = ((txVar >> 14) & 0x3F ) | 0x40;
  75.         USART1_SendByte(sBit);
  76.        
  77.         // 4th                 ( 1 _ _ _  _ _ _ _ )        7 bits
  78.         sBit = (txVar >> 7) | 0x80;
  79.         USART1_SendByte(sBit);
  80.        
  81.         // Last                ( 0 _ _ _  _ _ _ _ )        7 bits
  82.         sBit = txVar & 0x7F;
  83.         USART1_SendByte(sBit);
  84.        
  85.         // 6 + 6 + 6 + 7 + 7 = 32 bit
  86.        
  87. }

  88. void USART1_IRQHandler(){
  89.         uint16_t data_16b;
  90.         //uint32_t data_32b;
  91.         data_16b = 0xAAA;
  92.         //data_32b = 0xAAAAAAAA;
  93.         if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET){
  94.                 if((char)USART_ReceiveData(USART1) == 'S'){
  95.                         USART1_SendADC(data_16b);
  96.                         //USART1_Send_32Var(data_32b);
  97.                 }
  98.         }
  99. }

复制代码


STM32F103_testFunction.rar (8.73 KB, 下载次数: 38)

手边没有现成模块
所以我用两块STM32F103互传进行测试


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表