STM32F103 USB CDC Tx Error - Page 1
Forum section: electronics / beginners

author: noname4me
author_rank: Regular Contributor
timestamp: July 14, 2016, 09:16:57 pm
content: Hi,I am having some issues with trying to send information from an STM32F103C8T6 minimal development board using the USB in CDC mode.I have used STMCubeMX to generate the startup code, and I am able to get the ADC working, and the USB working to a degree. I am able to send hard coded "test" out of the board to a PC terminal.I am trying to read the ADC value, and then transfer this to the PC terminal but I am getting only rubbish ascii characters.I consider myself a beginner in terms of microcontrollers and C programming.The code I am using is below.I am trying to read the ADC, turn this into a char * (I think) which can be transmitted to the PC using the CDC_Transmit_FS() function (which is a standard library function).It complains that I can't return the array from the conversion function at the bottom (I thought that the problem with the program was that the USB transmission was being sent in the wrong format, and I'm trying to convert it to something more readable).Any help please, or something that I can read?ThanksCode://unsigned char chunk []= {0x46,0x6F,0x6F,0x64,0x20,0x0D,0x0A};unsigned char chunk ;uint8_t sender[sizeof(chunk)];HAL_Delay(1000);uint16_t tres;while (1){/* USER CODE END WHILE *//* USER CODE BEGIN 3 */tres = Getval();chunk = tres;for (int i=0; i < sizeof(chunk); i++){sender = chunk;uint16_t p;p = sizeof(sender);CDC_Transmit_FS(&sender,4);HAL_Delay(100);}HAL_Delay(1000);Code:int bin2char(int binter){int charback;//int dib = binter/4;int i = 4;for (int j = 0; j == i; j++){charback = binter & 0x0F;binter = binter >> 0x0F;}return charback;}

author: MosherIV
author_rank: Super Contributor
timestamp: July 15, 2016, 12:01:54 am
content: HiFirstly, the St USB CDC driver code is flawed when you send lots of characters. From the sound if it that is not your problem but be aware of it.You need to create an array of chars as a variableChar fooThen to turn the ADC readings into a string representation, use sprintfSprintf( &foo, "%i", adc_value );Then send the char variable to the usb transmit function.

author: noname4me
author_rank: Regular Contributor
timestamp: July 15, 2016, 07:25:35 pm
content: Hi MosherIV,Thanks for your reply.I tried using the Sprintf function but the compiler complains that the function is being declared implicitly.I take this to mean that there is no code match for the function Sprintf().The include files that I am using are:Code:/* Includes ------------------------------------------------------------------*/#include "stm32f1xx_hal.h"#include "usb_device.h"/* USER CODE BEGIN Includes */#include "usbd_cdc_if.h"#include "stdio.h"#include "stdlib.h"#include "stdint.h"#include "string.h"#include "usbd_cdc.h"Is there something that I'm missing here?Thanks

author: Signal32
author_rank: Frequent Contributor
timestamp: July 15, 2016, 07:28:01 pm
content: Quote from: noname4me on July 15, 2016, 07:25:35 pmAre you calling "Sprintf" or "sprintf", notice that it has to be all lower case.

author: noname4me
author_rank: Regular Contributor
timestamp: July 15, 2016, 07:35:56 pm
content: I was using the capitalised one - smaller case seems not to complain.Apologies - my bad...

author: noname4me
author_rank: Regular Contributor
timestamp: July 15, 2016, 07:51:12 pm
content: Hi,Thanks all, it's working now.