当前位置:   article > 正文

stm32与openmv的相互发送与接收(基于标准库)_openmv与stm32连接

openmv与stm32连接

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

        前段时间参加电赛校赛的时候题目是做21年f题智能送药小车,在openmv与stm32通信中遇到了些困难,通过看一些大佬们的文章与例程,我总结出了一套实用性不错的通信协议代码,一方面是对自己的知识进行总结,另一方面也可以将其提供给有需要的同学,如有理解错误,欢迎大佬们的指正。


提示:以下是本篇文章正文内容,下面案例可供参考

一、硬件连接

我所用到的材料如下: 四针IIC OLED,OpenMV4 7h(OV7725),STM32F103C8T6最小系统板,数据线N条,LED灯模块(OpenMV的数据线只能用官方自带的),杜邦线若干。

下图以Arduino为例把主控TXD与openmv的P5连接,RXD与openmv的P4连接,在STM32端:USART_TX—PA9 -----USART_RX—PA10。(用的是32的USART1)

四针IIC OLED连接:

SDA--PB9,SCL--PB8;GND,VCC(3.3V)正常接入就好;

 

二、数据包传输

这里采用的数据包传输方式是固定包长,含包头包尾方式(即为第一种),帧头帧尾不固定,可自定义,图示帧头帧尾分别为0xfe与0xef;代码呈现的分别是0xb3,0xb5,下图为图解原理:

三、openmv发送stm32端接收

1.openmv端(发送函数)

这里是根据当时做送药小车的模板匹配所用的主要发送程序,完整程序会在文末呈现。

代码如下(示例):

  1. #最初加载匹配
  2. def FirstFindTemplate(template):
  3. R = img.find_template(template, 0.8, step=1, roi=(40, 0, 70, 40), search=SEARCH_EX) #只检测中间的
  4. return R
  5. def FirstFindedNum(R, Finded_Num): #第一个参数是模板匹配的对象,第二个是它所代表的数字
  6. global Find_Task
  7. global find_flag
  8. img.draw_rectangle(R, color=(225, 0, 0))
  9. LoR = 0
  10. find_flag = 1
  11. Num = Finded_Num
  12. FH = bytearray([0xb3,0xb3,Num, LoR,Find_Task,0x5b])
  13. uart.write(FH)
  14. print("目标病房号:", Num)

 这里的FirstFindTemplate(template)函数是用于识别,识别成功后返回R值,主函数在调用FirstFindedNum(R, Finded_Num)时会将R值传输进去,识别到之后进行框选,然后bytearray([, , ,])组合uart.write()将打包好的参数进行发送。

2.stm32端(接收函数)

代码如下(示例):这里先省略初始化以及变量定义的程序,文末补全

串口中断服务函数:

  1. void USART1_IRQHandler(void) //串口1中断服务程序
  2. {
  3. u8 com_data;
  4. #if SYSTEM_SUPPORT_OS //如果SYSTEM_SUPPORT_OS为真,则需要支持OS.
  5. OSIntEnter();
  6. #endif
  7. if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断(接收到的数据必须是0x0d 0x0a结尾)
  8. {
  9. USART_ClearFlag(USART1,USART_FLAG_RXNE);
  10. com_data = USART1->DR;
  11. Openmv_Receive_Data(com_data); //openmv数据处理函数
  12. Openmv_Data(); //openmv数据处理函数
  13. }
  14. #if SYSTEM_SUPPORT_OS //如果SYSTEM_SUPPORT_OS为真,则需要支持OS.
  15. OSIntExit();
  16. #endif
  17. }

 常规串口中断程序,串口接收到数据后查询标志位,查询完成后清除并开始将数据读取处理。

数据读取函数:

  1. void Openmv_Receive_Data(int16_t data)//接收Openmv传过来的数据
  2. {
  3. static u8 state = 0;
  4. if(state==0&&data==0xb3)//第一个帧头
  5. {
  6. state=1;
  7. openmv[0]=data;
  8. }
  9. else if(state==1&&data==0xb3)//第二个帧头
  10. {
  11. state=2;
  12. openmv[1]=data;
  13. }
  14. else if(state==2)//第一个有效数据
  15. {
  16. state=3;
  17. openmv[2]=data;
  18. }
  19. else if(state==3)//第二个有效数据
  20. {
  21. state = 4;
  22. openmv[3]=data;
  23. }
  24. else if(state==4)//第三个有效数据
  25. {
  26. state = 5;
  27. openmv[4]=data;
  28. }
  29. else if(state==5) //检测是否接受到结束标志,检测接收帧尾
  30. {
  31. if(data == 0x5B)
  32. {
  33. state = 0;
  34. openmv[5]=data;
  35. Openmv_Data();
  36. }
  37. else if(data != 0x5B)
  38. {
  39. state = 0;
  40. for(i=0;i<6;i++)
  41. {
  42. openmv[i]=0x00;
  43. }
  44. }
  45. }
  46. else
  47. {
  48. state = 0;
  49. for(i=0;i<6;i++)
  50. {
  51. openmv[i]=0x00;
  52. }
  53. }
  54. }

 先解析帧头(我这里用了两个帧头,一个也可以,但是与openmv那边一定要相同),再解析有效数据,有效数据接受完传递到openmv[i]数据缓存区后调用Openmv_Data()对数据进行处理,最后解析帧尾。

数据处理函数:

  1. void Openmv_Data(void)//处理Openmv接收的数据
  2. {
  3. TargerNum=openmv[2];
  4. LoR=openmv[3];
  5. Find_Task =openmv[4];
  6. }

 这里被调用是为了将openmv端的数据传递给变量进行储存,方便在main函数对变量进行打印。

 main函数oled屏幕显示调用:

  1. extern int16_t TargerNum;
  2. extern int16_t Find_Task;
  3. extern int16_t LoR;
  4. int main(void)
  5. {
  6. Usart1_Init(115200);
  7. OLED_Init();
  8. OLED_ShowString(1,1,"TargerNum:");
  9. OLED_ShowString(2,1,"LoR:");
  10. OLED_ShowString(3,1,"Find_Task: ");
  11. while (1)
  12. {
  13. OLED_ShowNum(1,11,TargerNum,2);
  14. OLED_ShowNum(2,5,LoR,2);
  15. OLED_ShowNum(3,11,Find_Task,2);
  16. }
  17. }

3.实物调试

程序实现效果,当openmv识别到数字后,会通过调用串口发送函数将数据包发送到stm32主控,如图示:

 此时已识别数字1并框选

oled这边也接收到了数字TargetNum为1,Find_Task为1,LoR暂时未用不需要理会。

注:数据是以16进制发送过来,但是这里oled是以10进制的形式打印,因为只是1-8数字,所以不会太大影响。

 

四、stm32发送openmv端接收

1.stm32端(发送函数) 

这里数据位逐位发送,与openmv接收端解析函数相匹配。

代码如下(示例):

  1. void Usart1_Sendata(u8 * str)
  2. {
  3. u8 i = 0;
  4. USART_SendData(USART1,0x0d);
  5. while( USART_GetFlagStatus(USART1,USART_FLAG_TC)!= SET);
  6. for(i = 0;i < 2;i++)
  7. {
  8. USART_SendData(USART1,str[i]);
  9. while( USART_GetFlagStatus(USART1,USART_FLAG_TC)!= SET);
  10. }
  11. USART_SendData(USART1,0x5b);
  12. while( USART_GetFlagStatus(USART1,USART_FLAG_TC)!= SET);
  13. }
  14. int main(void)
  15. {
  16. NVIC_Config();
  17. Usart1_Init(115200);
  18. Find_Task=1;
  19. TargerNum=3;
  20. u8 send_buff[2] = {TargerNum,Find_Task};
  21. while (1)
  22. {
  23. Usart1_Sendata(send_buff);
  24. }
  25. }

 首先解析第一位数据是否为帧头,同时判断发送标志位是否已经检测到,完成后进行有效数据位发送,这里用for循环,使用库函数USART_SendData(USART1,str[i]),将数据发送出去,同时检测标志位,完成后发送帧尾。

而主函数部分是数组内数据进行反复发送。

2.openmv端(接收函数)

这里对stm32那边的数据包进行解析实际上同上方的原理大同小异,这里的数据长度可以自行更改,但是需要发送端和接收端的接收和发送函数相匹配,包括数据缓存区长度也要符合。

代码如下(示例):

  1. ########串口接收数据函数处理#########
  2. def Receive_Prepare(): #data
  3. global state
  4. global x
  5. global tx_flag
  6. global data
  7. global Find_Task
  8. global Target_Num
  9. if state==0:
  10. data[0]=uart.readchar()
  11. if data[0] == 0x0d:#帧头
  12. state = 1
  13. else:
  14. state = 0
  15. rx_buff.clear()
  16. elif state==1:
  17. data[1]=uart.readchar()
  18. #rx_buff.append(data)
  19. Target_Num=data[x+1]
  20. state = 2
  21. elif state==2:
  22. data[2]=uart.readchar()
  23. #rx_buff.append(data)
  24. Find_Task=data[x+2]
  25. state = 3
  26. elif state == 3:
  27. data[4]=uart.readchar()
  28. if data[4] == 0x5b:
  29. tx_flag = int(data[0])
  30. state = 4
  31. elif state == 4:
  32. state=0
  33. else:
  34. state = 0
  35. rx_buff.clear()

3.openmv主函数调用

  1. while (True):
  2. clock.tick()
  3. img = sensor.snapshot()# 镜头初始化
  4. if(uart.any()>0):
  5. Receive_Prepare()
  6. print(clock.fps(),Find_Task, Target_Num)

这里的if(uart.any()>0):是检测是否有数据传输进来,有的话就执行。

调用接收函数Receive_Prepare(),把Find_Task, Target_Num在串行终端中打印出来。

4.openmv调试展示

这里我们用ide自带的串行终端进行观察调试结果

可以看到,此时的openmv端接收到数字Find_Task为1,TargetNum为3,LoR暂时未用不需要理会。

注:这里接收/发送有点小问题,如果不断的接收打印,数据会变成13,1,通信时请注意;可能通信协议有点小毛病,希望大佬们可以指正。

 

五、完整代码1(openmv发送stm32端接收)

(如果上面看的一头雾水可以试试看完全部程序,也方便梳理)

1.openmv发送

  1. import time, image,sensor,math,pyb,ustruct
  2. from image import SEARCH_EX, SEARCH_DS
  3. from pyb import Pin, Timer,LED
  4. #从imgae模块引入SEARCH_EX和SEARCH_DS。使用from import仅仅引入SEARCH_EX,
  5. #SEARCH_DS两个需要的部分,而不把image模块全部引入。
  6. sensor.reset()
  7. # Set sensor settings
  8. sensor.set_contrast(1)
  9. sensor.set_gainceiling(16)
  10. # Max resolution for template matching with SEARCH_EX is QQVGA
  11. sensor.set_framesize(sensor.QQVGA)
  12. # You can set windowing to reduce the search image.
  13. sensor.set_pixformat(sensor.GRAYSCALE)
  14. sensor.set_windowing(0, 40, 160, 40) #观察窗口 后面ROI设置也会以这个为新的基准
  15. rx_buff=[]
  16. state = 0
  17. tx_flag = 0
  18. # Load template.
  19. # Template should be a small (eg. 32x32 pixels) grayscale image.
  20. #加载模板图片
  21. template01 = image.Image("/1.pgm")
  22. template02 = image.Image("/2.pgm")
  23. template03 = image.Image("/3.pgm")
  24. template04 = image.Image("/4.pgm")
  25. template05 = image.Image("/5.pgm")
  26. template06 = image.Image("/6.pgm")
  27. template07 = image.Image("/7.pgm")
  28. template08 = image.Image("/8.pgm")
  29. uart = pyb.UART(3, 115200, timeout_char = 1000) #定义串口1变量
  30. blue_led = LED(2)
  31. Find_Task =1 #1
  32. Target_Num =0
  33. ##### FindTask == 1 时使用
  34. #最初加载匹配
  35. def FirstFindTemplate(template):
  36. R = img.find_template(template, 0.8, step=1, roi=(40, 0, 70, 40), search=SEARCH_EX) #只检测中间的
  37. return R
  38. def FirstFindedNum(R, Finded_Num): #第一个参数是模板匹配的对象,第二个是它所代表的数字
  39. global Find_Task
  40. global find_flag
  41. img.draw_rectangle(R, color=(225, 0, 0))
  42. #本来中值是80的,但返回值是框边缘,所以减去15就好 小于65是在左边,大于65是在右边
  43. LoR = 0
  44. find_flag = 1
  45. Num = Finded_Num
  46. FH = bytearray([0xb3,0xb3,Num, LoR,Find_Task,0x5b])
  47. uart.write(FH)
  48. print("目标病房号:", Num)
  49. clock = time.clock()
  50. # Run template matching
  51. while (True):
  52. clock.tick()
  53. img = sensor.snapshot()# 镜头初始化
  54. # find_template(template, threshold, [roi, step, search])
  55. # ROI: The region of interest tuple (x, y, w, h).
  56. # Step: The loop step used (y+=step, x+=step) use a bigger step to make it faster.
  57. # Search is either image.SEARCH_EX for exhaustive search or image.SEARCH_DS for diamond search
  58. #
  59. # Note1: ROI has to be smaller than the image and bigger than the template.
  60. # Note2: In diamond search, step and ROI are both ignored.
  61. if Find_Task == 1:
  62. #进行模板匹配
  63. r01 = FirstFindTemplate(template01)
  64. r02 = FirstFindTemplate(template02)
  65. r03 = FirstFindTemplate(template03)
  66. r04 = FirstFindTemplate(template04)
  67. r05 = FirstFindTemplate(template05)
  68. r06 = FirstFindTemplate(template06)
  69. r07 = FirstFindTemplate(template07)
  70. r08 = FirstFindTemplate(template08)
  71. #判断哪个模板匹配成功,并将成功匹配的相应数据发送给主控
  72. if r01:
  73. FirstFindedNum(r01, 1)
  74. elif r02:
  75. FirstFindedNum(r02,2)
  76. elif r03:
  77. FirstFindedNum(r03,3)
  78. elif r04:
  79. FirstFindedNum(r04,4)
  80. elif r05:
  81. FirstFindedNum(r05,5)
  82. elif r06:
  83. FirstFindedNum(r06,6)
  84. elif r07:
  85. FirstFindedNum(r07,7)
  86. elif r08:
  87. FirstFindedNum(r08,8)
  88. else:
  89. FH = bytearray([0x2C,0x12,0x00, 0x00, 0x00, 0x00,0x5B])
  90. uart.write(FH)
  91. else: time.sleep_ms(100)
  92. print(clock.fps(),Find_Task, Target_Num)

多出来的数据位不需要那么多的话可以选择对代码进行删减,或者直接当成0x00发送,不会有太大影响。

2.stm32端接收

usart1.c

  1. #include "usart1.h"
  2. //char TargerNum='0';
  3. int openmv[6];//stm32接收数据数组
  4. int16_t TargerNum;
  5. int16_t Find_Task;
  6. int16_t LoR;
  7. int i;
  8. /**
  9. * 函数名:Usart1_Init
  10. * 描述:串口1初始化
  11. * 输入:Bound-波特率
  12. * 输出:无
  13. */
  14. void Usart1_Init(uint32_t Bound)
  15. {
  16. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  17. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
  18. GPIO_InitTypeDef GPIO_InitStructure;
  19. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  20. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  21. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  22. GPIO_Init(GPIOA,&GPIO_InitStructure);
  23. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  24. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  25. GPIO_Init(GPIOA,&GPIO_InitStructure);
  26. USART_InitTypeDef USART_InitStructure;
  27. USART_InitStructure.USART_BaudRate = Bound;
  28. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  29. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  30. USART_InitStructure.USART_Parity = USART_Parity_No;
  31. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  32. USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
  33. USART_Init(USART1,&USART_InitStructure);
  34. NVIC_InitTypeDef NVIC_InitStructure;
  35. NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  36. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  37. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  38. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  39. NVIC_Init(&NVIC_InitStructure);
  40. USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
  41. USART_Cmd(USART1,ENABLE);
  42. }
  43. void Openmv_Data(void)//处理Openmv接收的数据
  44. {
  45. TargerNum=openmv[2];
  46. LoR=openmv[3];
  47. Find_Task =openmv[4];
  48. }
  49. void Openmv_Receive_Data(int16_t data)//接收Openmv传过来的数据
  50. {
  51. static u8 state = 0;
  52. if(state==0&&data==0xb3)//第一个帧头
  53. {
  54. state=1;
  55. openmv[0]=data;
  56. }
  57. else if(state==1&&data==0xb3)//第二个帧头
  58. {
  59. state=2;
  60. openmv[1]=data;
  61. }
  62. else if(state==2)//第一个有效数据
  63. {
  64. state=3;
  65. openmv[2]=data;
  66. }
  67. else if(state==3)//第二个有效数据
  68. {
  69. state = 4;
  70. openmv[3]=data;
  71. }
  72. else if(state==4)//第三个有效数据
  73. {
  74. state = 5;
  75. openmv[4]=data;
  76. }
  77. else if(state==5) //检测是否接受到结束标志,检测接收帧尾
  78. {
  79. if(data == 0x5B)
  80. {
  81. state = 0;
  82. openmv[5]=data;
  83. Openmv_Data();
  84. }
  85. else if(data != 0x5B)
  86. {
  87. state = 0;
  88. for(i=0;i<6;i++)
  89. {
  90. openmv[i]=0x00;
  91. }
  92. }
  93. }
  94. else
  95. {
  96. state = 0;
  97. for(i=0;i<6;i++)
  98. {
  99. openmv[i]=0x00;
  100. }
  101. }
  102. }
  103. void USART_SendByte(USART_TypeDef* USARTx, char str)
  104. {
  105. USART_SendData(USARTx, str);//发送单个字符
  106. while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);//判断是否发送完成
  107. }
  108. void USART1_IRQHandler(void) //串口1中断服务程序
  109. {
  110. u8 com_data;
  111. #if SYSTEM_SUPPORT_OS //如果SYSTEM_SUPPORT_OS为真,则需要支持OS.
  112. OSIntEnter();
  113. #endif
  114. if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断(接收到的数据必须是0x0d 0x0a结尾)
  115. {
  116. USART_ClearFlag(USART1,USART_FLAG_RXNE);
  117. com_data = USART1->DR;
  118. Openmv_Receive_Data(com_data); //openmv数据处理函数
  119. Openmv_Data(); //openmv数据处理函数
  120. }
  121. #if SYSTEM_SUPPORT_OS //如果SYSTEM_SUPPORT_OS为真,则需要支持OS.
  122. OSIntExit();
  123. #endif
  124. }

 usart1.h

  1. #ifndef __USART1_H
  2. #define __USART1_H
  3. #include "stm32f10x.h" // Device header
  4. #include "sys.h"
  5. void Usart1_Init(uint32_t Bound);
  6. #endif

oled.c

  1. #include "stm32f10x.h"
  2. #include "OLED_Font.h"
  3. #include "stdio.h"
  4. /*引脚配置*/
  5. #define OLED_W_SCL(x) GPIO_WriteBit(GPIOB, GPIO_Pin_8, (BitAction)(x))
  6. #define OLED_W_SDA(x) GPIO_WriteBit(GPIOB, GPIO_Pin_9, (BitAction)(x))
  7. /*引脚初始化*/
  8. void OLED_I2C_Init(void)
  9. {
  10. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  11. GPIO_InitTypeDef GPIO_InitStructure;
  12. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
  13. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  14. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  15. GPIO_Init(GPIOB, &GPIO_InitStructure);
  16. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  17. GPIO_Init(GPIOB, &GPIO_InitStructure);
  18. OLED_W_SCL(1);
  19. OLED_W_SDA(1);
  20. }
  21. /**
  22. * @brief I2C开始
  23. * @param 无
  24. * @retval 无
  25. */
  26. void OLED_I2C_Start(void)
  27. {
  28. OLED_W_SDA(1);
  29. OLED_W_SCL(1);
  30. OLED_W_SDA(0);
  31. OLED_W_SCL(0);
  32. }
  33. /**
  34. * @brief I2C停止
  35. * @param 无
  36. * @retval 无
  37. */
  38. void OLED_I2C_Stop(void)
  39. {
  40. OLED_W_SDA(0);
  41. OLED_W_SCL(1);
  42. OLED_W_SDA(1);
  43. }
  44. /**
  45. * @brief I2C发送一个字节
  46. * @param Byte 要发送的一个字节
  47. * @retval 无
  48. */
  49. void OLED_I2C_SendByte(uint8_t Byte)
  50. {
  51. uint8_t i;
  52. for (i = 0; i < 8; i++)
  53. {
  54. OLED_W_SDA(Byte & (0x80 >> i));
  55. OLED_W_SCL(1);
  56. OLED_W_SCL(0);
  57. }
  58. OLED_W_SCL(1); //额外的一个时钟,不处理应答信号
  59. OLED_W_SCL(0);
  60. }
  61. /**
  62. * @brief OLED写命令
  63. * @param Command 要写入的命令
  64. * @retval 无
  65. */
  66. void OLED_WriteCommand(uint8_t Command)
  67. {
  68. OLED_I2C_Start();
  69. OLED_I2C_SendByte(0x78); //从机地址
  70. OLED_I2C_SendByte(0x00); //写命令
  71. OLED_I2C_SendByte(Command);
  72. OLED_I2C_Stop();
  73. }
  74. /**
  75. * @brief OLED写数据
  76. * @param Data 要写入的数据
  77. * @retval 无
  78. */
  79. void OLED_WriteData(uint8_t Data)
  80. {
  81. OLED_I2C_Start();
  82. OLED_I2C_SendByte(0x78); //从机地址
  83. OLED_I2C_SendByte(0x40); //写数据
  84. OLED_I2C_SendByte(Data);
  85. OLED_I2C_Stop();
  86. }
  87. /**
  88. * @brief OLED设置光标位置
  89. * @param Y 以左上角为原点,向下方向的坐标,范围:0~7
  90. * @param X 以左上角为原点,向右方向的坐标,范围:0~127
  91. * @retval 无
  92. */
  93. void OLED_SetCursor(uint8_t Y, uint8_t X)
  94. {
  95. OLED_WriteCommand(0xB0 | Y); //设置Y位置
  96. OLED_WriteCommand(0x10 | ((X & 0xF0) >> 4)); //设置X位置高4位
  97. OLED_WriteCommand(0x00 | (X & 0x0F)); //设置X位置低4位
  98. }
  99. /**
  100. * @brief OLED清屏
  101. * @param 无
  102. * @retval 无
  103. */
  104. void OLED_Clear(void)
  105. {
  106. uint8_t i, j;
  107. for (j = 0; j < 8; j++)
  108. {
  109. OLED_SetCursor(j, 0);
  110. for(i = 0; i < 128; i++)
  111. {
  112. OLED_WriteData(0x00);
  113. }
  114. }
  115. }
  116. /**
  117. * @brief OLED显示一个字符
  118. * @param Line 行位置,范围:1~4
  119. * @param Column 列位置,范围:1~16
  120. * @param Char 要显示的一个字符,范围:ASCII可见字符
  121. * @retval 无
  122. */
  123. void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char)
  124. {
  125. uint8_t i;
  126. OLED_SetCursor((Line - 1) * 2, (Column - 1) * 8); //设置光标位置在上半部分
  127. for (i = 0; i < 8; i++)
  128. {
  129. OLED_WriteData(OLED_F8x16[Char - ' '][i]); //显示上半部分内容
  130. }
  131. OLED_SetCursor((Line - 1) * 2 + 1, (Column - 1) * 8); //设置光标位置在下半部分
  132. for (i = 0; i < 8; i++)
  133. {
  134. OLED_WriteData(OLED_F8x16[Char - ' '][i + 8]); //显示下半部分内容
  135. }
  136. }
  137. /**
  138. * @brief OLED显示字符串
  139. * @param Line 起始行位置,范围:1~4
  140. * @param Column 起始列位置,范围:1~16
  141. * @param String 要显示的字符串,范围:ASCII可见字符
  142. * @retval 无
  143. */
  144. void OLED_ShowString(uint8_t Line, uint8_t Column, char *String)
  145. {
  146. uint8_t i;
  147. for (i = 0; String[i] != '\0'; i++)
  148. {
  149. OLED_ShowChar(Line, Column + i, String[i]);
  150. }
  151. }
  152. /**
  153. * @brief OLED次方函数
  154. * @retval 返回值等于X的Y次方
  155. */
  156. uint32_t OLED_Pow(uint32_t X, uint32_t Y)
  157. {
  158. uint32_t Result = 1;
  159. while (Y--)
  160. {
  161. Result *= X;
  162. }
  163. return Result;
  164. }
  165. /**
  166. * @brief OLED显示数字(十进制,正数)
  167. * @param Line 起始行位置,范围:1~4
  168. * @param Column 起始列位置,范围:1~16
  169. * @param Number 要显示的数字,范围:0~4294967295
  170. * @param Length 要显示数字的长度,范围:1~10
  171. * @retval 无
  172. */
  173. void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
  174. {
  175. uint8_t i;
  176. for (i = 0; i < Length; i++)
  177. {
  178. OLED_ShowChar(Line, Column + i, Number / OLED_Pow(10, Length - i - 1) % 10 + '0');
  179. }
  180. }
  181. /**
  182. * @brief OLED显示数字(十进制,带符号数)
  183. * @param Line 起始行位置,范围:1~4
  184. * @param Column 起始列位置,范围:1~16
  185. * @param Number 要显示的数字,范围:-2147483648~2147483647
  186. * @param Length 要显示数字的长度,范围:1~10
  187. * @retval 无
  188. */
  189. void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length)
  190. {
  191. uint8_t i;
  192. uint32_t Number1;
  193. if (Number >= 0)
  194. {
  195. OLED_ShowChar(Line, Column, '+');
  196. Number1 = Number;
  197. }
  198. else
  199. {
  200. OLED_ShowChar(Line, Column, '-');
  201. Number1 = -Number;
  202. }
  203. for (i = 0; i < Length; i++)
  204. {
  205. OLED_ShowChar(Line, Column + i + 1, Number1 / OLED_Pow(10, Length - i - 1) % 10 + '0');
  206. }
  207. }
  208. /**
  209. * @brief OLED显示数字(十六进制,正数)
  210. * @param Line 起始行位置,范围:1~4
  211. * @param Column 起始列位置,范围:1~16
  212. * @param Number 要显示的数字,范围:0~0xFFFFFFFF
  213. * @param Length 要显示数字的长度,范围:1~8
  214. * @retval 无
  215. */
  216. void OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
  217. {
  218. uint8_t i, SingleNumber;
  219. for (i = 0; i < Length; i++)
  220. {
  221. SingleNumber = Number / OLED_Pow(16, Length - i - 1) % 16;
  222. if (SingleNumber < 10)
  223. {
  224. OLED_ShowChar(Line, Column + i, SingleNumber + '0');
  225. }
  226. else
  227. {
  228. OLED_ShowChar(Line, Column + i, SingleNumber - 10 + 'A');
  229. }
  230. }
  231. }
  232. /**
  233. * @brief OLED显示数字(二进制,正数)
  234. * @param Line 起始行位置,范围:1~4
  235. * @param Column 起始列位置,范围:1~16
  236. * @param Number 要显示的数字,范围:0~1111 1111 1111 1111
  237. * @param Length 要显示数字的长度,范围:1~16
  238. * @retval 无
  239. */
  240. void OLED_ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
  241. {
  242. uint8_t i;
  243. for (i = 0; i < Length; i++)
  244. {
  245. OLED_ShowChar(Line, Column + i, Number / OLED_Pow(2, Length - i - 1) % 2 + '0');
  246. }
  247. }
  248. /**
  249. * @brief OLED初始化
  250. * @param 无
  251. * @retval 无
  252. */
  253. void OLED_Init(void)
  254. {
  255. uint32_t i, j;
  256. for (i = 0; i < 1000; i++) //上电延时
  257. {
  258. for (j = 0; j < 1000; j++);
  259. }
  260. OLED_I2C_Init(); //端口初始化
  261. OLED_WriteCommand(0xAE); //关闭显示
  262. OLED_WriteCommand(0xD5); //设置显示时钟分频比/振荡器频率
  263. OLED_WriteCommand(0x80);
  264. OLED_WriteCommand(0xA8); //设置多路复用率
  265. OLED_WriteCommand(0x3F);
  266. OLED_WriteCommand(0xD3); //设置显示偏移
  267. OLED_WriteCommand(0x00);
  268. OLED_WriteCommand(0x40); //设置显示开始行
  269. OLED_WriteCommand(0xA1); //设置左右方向,0xA1正常 0xA0左右反置
  270. OLED_WriteCommand(0xC8); //设置上下方向,0xC8正常 0xC0上下反置
  271. OLED_WriteCommand(0xDA); //设置COM引脚硬件配置
  272. OLED_WriteCommand(0x12);
  273. OLED_WriteCommand(0x81); //设置对比度控制
  274. OLED_WriteCommand(0xCF);
  275. OLED_WriteCommand(0xD9); //设置预充电周期
  276. OLED_WriteCommand(0xF1);
  277. OLED_WriteCommand(0xDB); //设置VCOMH取消选择级别
  278. OLED_WriteCommand(0x30);
  279. OLED_WriteCommand(0xA4); //设置整个显示打开/关闭
  280. OLED_WriteCommand(0xA6); //设置正常/倒转显示
  281. OLED_WriteCommand(0x8D); //设置充电泵
  282. OLED_WriteCommand(0x14);
  283. OLED_WriteCommand(0xAF); //开启显示
  284. OLED_Clear(); //OLED清屏
  285. }

oled.h 

  1. #ifndef __OLED_H
  2. #define __OLED_H
  3. void OLED_Init(void);
  4. void OLED_Clear(void);
  5. void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char);
  6. void OLED_ShowString(uint8_t Line, uint8_t Column, char *String);
  7. void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
  8. void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length);
  9. void OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
  10. void OLED_ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
  11. #endif

oled_Font.h

  1. #ifndef __OLED_FONT_H
  2. #define __OLED_FONT_H
  3. /*OLED字模库,宽8像素,高16像素*/
  4. const uint8_t OLED_F8x16[][16]=
  5. {
  6. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// 0
  8. 0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,
  9. 0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,//! 1
  10. 0x00,0x10,0x0C,0x06,0x10,0x0C,0x06,0x00,
  11. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//" 2
  12. 0x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00,
  13. 0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,//# 3
  14. 0x00,0x70,0x88,0xFC,0x08,0x30,0x00,0x00,
  15. 0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,//$ 4
  16. 0xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,
  17. 0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,//% 5
  18. 0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,
  19. 0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,//& 6
  20. 0x10,0x16,0x0E,0x00,0x00,0x00,0x00,0x00,
  21. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//' 7
  22. 0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,
  23. 0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,//( 8
  24. 0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,
  25. 0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,//) 9
  26. 0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,
  27. 0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,//* 10
  28. 0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,
  29. 0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,//+ 11
  30. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31. 0x80,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,//, 12
  32. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  33. 0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,//- 13
  34. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  35. 0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,//. 14
  36. 0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,
  37. 0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,/// 15
  38. 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,
  39. 0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,//0 16
  40. 0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,
  41. 0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//1 17
  42. 0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,
  43. 0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,//2 18
  44. 0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,
  45. 0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,//3 19
  46. 0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,
  47. 0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,//4 20
  48. 0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,
  49. 0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,//5 21
  50. 0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,
  51. 0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,//6 22
  52. 0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,
  53. 0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,//7 23
  54. 0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,
  55. 0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,//8 24
  56. 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,
  57. 0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,//9 25
  58. 0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,
  59. 0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//: 26
  60. 0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,
  61. 0x00,0x00,0x80,0x60,0x00,0x00,0x00,0x00,//; 27
  62. 0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,
  63. 0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,//< 28
  64. 0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,
  65. 0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,//= 29
  66. 0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,
  67. 0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,//> 30
  68. 0x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,
  69. 0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,//? 31
  70. 0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,
  71. 0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,//@ 32
  72. 0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,
  73. 0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,//A 33
  74. 0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,
  75. 0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,//B 34
  76. 0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,
  77. 0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,//C 35
  78. 0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,
  79. 0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,//D 36
  80. 0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,
  81. 0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,//E 37
  82. 0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,
  83. 0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,//F 38
  84. 0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,
  85. 0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,//G 39
  86. 0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,
  87. 0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,//H 40
  88. 0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,
  89. 0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//I 41
  90. 0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,
  91. 0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,//J 42
  92. 0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,
  93. 0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,//K 43
  94. 0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,
  95. 0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,//L 44
  96. 0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,
  97. 0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,//M 45
  98. 0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,
  99. 0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,//N 46
  100. 0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,
  101. 0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,//O 47
  102. 0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,
  103. 0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,//P 48
  104. 0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,
  105. 0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,//Q 49
  106. 0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,
  107. 0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,//R 50
  108. 0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,
  109. 0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,//S 51
  110. 0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,
  111. 0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//T 52
  112. 0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,
  113. 0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//U 53
  114. 0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,
  115. 0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,//V 54
  116. 0xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,
  117. 0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,//W 55
  118. 0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,
  119. 0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,//X 56
  120. 0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,
  121. 0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//Y 57
  122. 0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,
  123. 0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,//Z 58
  124. 0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,
  125. 0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,//[ 59
  126. 0x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,
  127. 0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,//\ 60
  128. 0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,
  129. 0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,//] 61
  130. 0x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,
  131. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//^ 62
  132. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  133. 0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,//_ 63
  134. 0x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00,
  135. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//` 64
  136. 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
  137. 0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,//a 65
  138. 0x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,
  139. 0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,//b 66
  140. 0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,
  141. 0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,//c 67
  142. 0x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,
  143. 0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,//d 68
  144. 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
  145. 0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,//e 69
  146. 0x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,
  147. 0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//f 70
  148. 0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,
  149. 0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,//g 71
  150. 0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,
  151. 0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//h 72
  152. 0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,
  153. 0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//i 73
  154. 0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,
  155. 0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,//j 74
  156. 0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,
  157. 0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,//k 75
  158. 0x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,
  159. 0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//l 76
  160. 0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,
  161. 0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,//m 77
  162. 0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,
  163. 0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//n 78
  164. 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
  165. 0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//o 79
  166. 0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,
  167. 0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,//p 80
  168. 0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,
  169. 0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,//q 81
  170. 0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,
  171. 0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,//r 82
  172. 0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,
  173. 0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,//s 83
  174. 0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,
  175. 0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,//t 84
  176. 0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,
  177. 0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,//u 85
  178. 0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,
  179. 0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,//v 86
  180. 0x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,
  181. 0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,//w 87
  182. 0x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,
  183. 0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,//x 88
  184. 0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,
  185. 0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,//y 89
  186. 0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,
  187. 0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,//z 90
  188. 0x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,
  189. 0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,//{ 91
  190. 0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,
  191. 0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,//| 92
  192. 0x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,
  193. 0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,//} 93
  194. 0x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04,
  195. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//~ 94
  196. };
  197. #endif

 

main.c

  1. #include "stm32f10x.h" // Device header
  2. #include "MyConfig.h"
  3. //#include "stdio.h"
  4. extern int16_t TargerNum;
  5. extern int16_t Find_Task;
  6. extern int16_t LoR;
  7. int main(void)
  8. {
  9. Usart1_Init(115200);
  10. OLED_Init();
  11. OLED_ShowString(1,1,"TargerNum:");
  12. OLED_ShowString(2,1,"LoR:");
  13. OLED_ShowString(3,1,"Find_Task: ");
  14. while (1)
  15. {
  16. OLED_ShowNum(1,11,TargerNum,2);
  17. OLED_ShowNum(2,5,LoR,2);
  18. OLED_ShowNum(3,11,Find_Task,2);
  19. }
  20. }

 六、完整代码2(stm32端发送openmv接收)

1.stm32端

  1. #include "stm32f10x.h" // Device header
  2. #include "MyConfig.h"
  3. //#include "stdio.h"
  4. uint8_t RoadLineCheck[2] = {0,0};
  5. uint8_t RoadLine;
  6. extern int16_t TargerNum;
  7. extern int16_t Find_Task;
  8. extern int16_t LoR;
  9. void Usart1_Sendata(u8 * str)
  10. {
  11. u8 i = 0;
  12. USART_SendData(USART1,0x0d);
  13. while( USART_GetFlagStatus(USART1,USART_FLAG_TC)!= SET);
  14. for(i = 0;i < 2;i++)
  15. {
  16. USART_SendData(USART1,str[i]);
  17. while( USART_GetFlagStatus(USART1,USART_FLAG_TC)!= SET);
  18. }
  19. USART_SendData(USART1,0x5b);
  20. while( USART_GetFlagStatus(USART1,USART_FLAG_TC)!= SET);
  21. }
  22. int main(void)
  23. {
  24. NVIC_Config();
  25. Usart1_Init(115200);
  26. Find_Task=2;
  27. u8 send_buff[2] = {TargerNum,Find_Task};
  28. while (1)
  29. {
  30. Usart1_Sendata(send_buff);
  31. }
  32. }

2.openmv端

  1. import time, image,sensor,math,pyb,ustruct
  2. from image import SEARCH_EX, SEARCH_DS
  3. from pyb import Pin, Timer,LED
  4. #从imgae模块引入SEARCH_EX和SEARCH_DS。使用from import仅仅引入SEARCH_EX,
  5. #SEARCH_DS两个需要的部分,而不把image模块全部引入。
  6. sensor.reset()
  7. # Set sensor settings
  8. sensor.set_contrast(1)
  9. sensor.set_gainceiling(16)
  10. # Max resolution for template matching with SEARCH_EX is QQVGA
  11. sensor.set_framesize(sensor.QQVGA)
  12. # You can set windowing to reduce the search image.
  13. sensor.set_pixformat(sensor.GRAYSCALE)
  14. sensor.set_windowing(0, 40, 160, 40) #观察窗口 后面ROI设置也会以这个为新的基准
  15. rx_buff=[]
  16. state = 0
  17. tx_flag = 0
  18. x = 0
  19. Find_Task =1 #1
  20. Target_Num =0
  21. data = [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00]
  22. uart = pyb.UART(3, 115200, timeout_char = 1000) #定义串口1变量
  23. blue_led = LED(2)
  24. ########串口接收数据函数处理#########
  25. def Receive_Prepare(): #data
  26. global state
  27. global x
  28. global tx_flag
  29. global data
  30. global Find_Task
  31. global Target_Num
  32. if state==0:
  33. data[0]=uart.readchar()
  34. if data[0] == 0x0d:#帧头
  35. state = 1
  36. else:
  37. state = 0
  38. rx_buff.clear()
  39. elif state==1:
  40. data[1]=uart.readchar()
  41. Target_Num=data[x+1]
  42. state = 2
  43. elif state==2:
  44. data[2]=uart.readchar()
  45. Find_Task=data[x+2]
  46. state = 3
  47. elif state == 3:
  48. data[4]=uart.readchar()
  49. if data[4] == 0x5b:
  50. state = 4
  51. elif state == 4:
  52. state=0
  53. else:
  54. state = 0
  55. rx_buff.clear()
  56. clock = time.clock()
  57. # Run template matching
  58. while (True):
  59. clock.tick()
  60. img = sensor.snapshot()# 镜头初始化
  61. if(uart.any()>0):
  62. Receive_Prepare()
  63. print(clock.fps(),Find_Task, Target_Num)

总结

这里对文章进行总结:

个人认为这套代码比较通俗易懂,稍微读懂一点直接移植就好(可能需要根据实际略微修改)。

要移植的话最好选择完整代码这边

stm32端用的是串口1,openmv端用的是串口3;
固定包长并不是只能三个或者两个数据位,是代码编写设置好的不超过缓存区的数据位是自定义的,只是运用的时候会根据设置好的数据位发送/接收。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/707630
推荐阅读
相关标签
  

闽ICP备14008679号