yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
Self-balancing Robot Car📖
00.Get started quickly
  • 01.Balance car manual
  • 02.Balancing car assembly
  • 03.Get started quickly
  • 04.Balance car interface description
  • 05.Common Problem Analysis
  • 06.Balance Car V2 Library Function Development Manual
01.Environment development
  • 01.MDK-ARM installation
  • 02.STM32CubeIDE installation
  • 03.Common driver installation
  • 04.Download the program
  • 05.MDK-ARM project usage
  • 06.Program simulation
  • 07. VSCode Installation
02.STM32 getting started course
  • 01.Project creation
  • 02.LED Control(GPIO)
  • 03.Buzzer control(GPIO)
  • 04.Button control(EXTI)
  • 05.Serial communication(USART)
  • 06.Voltage detection(ADC)
  • 07.Basic timer(TIM)
  • 08.Universal timer(TIM)
  • 09.Advanced Timer(TIM)
  • 10.I2C Communication(I2C)-OLED
  • 11.SPI communication(SPI)-2.4G handle
  • 12.Independent Watchdog (IWDG)
  • 13.Window Watchdog (WWDG)
  • 14.DMA-USART
  • 15.DMA-I2C
03.STM32 expansion course
  • 01.Battery voltage detection (ADC)
  • 02.Ultrasonic module-measuring distance (TIM)
  • 03.Motor drive+encoder (TIM)
  • 04.OLED Data-Display(I2C)
  • 05.MPU6050-Data Read (I2C)
  • 06.Bluetooth module-Data reading (USART)
  • 07.2.4G handle-Data reading (SPI)
  • 08.Tracking module-Read status (GPIO)
  • 09.CCD module-Reading data (ADC)
  • 10.Electromagnetic module-Reading data (ADC)
  • 11.K210 module-Serial communication (USART)
  • 12.Tmini-Plus lidar-Reading data (USART)
04.Robot car PID control course
  • 01.PID basic concept
  • 02.PID example analysis
  • 03.P, PI, PD controller theory
  • 04.Position PID
  • 05.Incremental PID
  • 06.Cascade PID
  • 07.Balance principle of car
  • 08.Car upright control (PD)
  • 09.Car speed ​​control (PI)
  • 10.Car steering control (PD)
  • 11.Got angle and angular velocity (DMP algorithm)
  • 12.Got angle and angular velocity (Kalman filter algorithm)
  • 13.Got angle and angular velocity(Complementary filter algorithm)
05.Robot car basic course
  • 01.Car parameter adjustment
  • 02.Ultrasonic obstacle avoidance
  • 03.Ultrasonic follow
  • 04.Bluetooth remote control
  • 05.Load balance
06.Robot car advanced course
  • 01.4-channel tracking
  • 02.4-channel tracking avoid
  • 03.2.4G handle control
  • 04.CCD tracking
  • 05.CCD tracking avoid
  • 06.Electromagnetic tracking
  • 07.K210-QR code recognition
  • 08.K210-Color line tracking
  • 09.K210-Color follow
  • 10.K210-Self learning
  • 11.K210-Number recognition
  • 12.Lidar avoid
  • 13.Lidar guard
  • 14.Lidar follow
  • 15.Lidar patrol
  • 16.Lidar wall tracking-straight line
  • 17.Lidar wall tracking-multiple walls
  • 18. DIY Automatic driving car
07.Robot car LQR control
  • Robot car LQR control
08.Circuit design
  • 01.Overall circuit design
  • 02.STM32 minimum system
  • 03.Serial port circuit design
  • 04.Power circuit design
  • 05.MPU6050 Circuit Design
  • 06.Motor drive circuit design
09.Mathematical model
  • Mathematical model
10.Expand course
  • Autonomous Driving
Download
  • APP(Android)
  • Codes
  • Hardware-Info
  • SCH
  • Software Tools
  • STM32_Chip_DataSheet
  • 3D_Model_File
  • Video_PPT
Download ZIP

Welcome to Self-balancing Robot Car repository

09.CCD module-Reading data (ADC)

CCD module-Reading data (ADC)

CCD module-Reading data (ADC)Hardware connectionControl principleSoftware configurationPin definitionSoftware codeControl functionExperimental phenomenon

The tutorial combines OLED and serial port to display the data output by CCD module.

The tutorial only introduces the standard library project code

Hardware connection

image-20240825213503957

Since we have configured a special connection line, we only need to install it to the corresponding interface:

PeripheralsDevelopment board
CCD: VCC3.3V
CCD: AOPA4
CCD: SLKPB4
CCD: SIPB5
CCD: GNDGND

Control principle

  • CCD module

image-20240808115819550

PeripheralsDescription
CCD: VCCPower supply pin: 3.3V-5V
CCD: AOGray value output pin: analog output
CCD: CLKClock pin, determines the exposure time and controls the output of pixel gray value.
CCD: SIControls the acquisition and output of pixel gray value.
CCD: GNDPower supply pin: GND

Working principle

Acquisition timing diagram:

image-20240808114920107

  • Every 129 clock cycles of TSL1401 is an output acquisition cycle, and the clock cycle is provided by the CLK pin.

  • When the SI pin receives a high-level signal, the TSL1401 gray value acquisition task and gray value output task start at the same time.

  • CCD grayscale value acquisition task: perform reset work in the 1st to 18th clock cycles (corresponding to the Internal Reset timing in the figure), perform ambient light sensing in the 19th to 129th clock cycles (corresponding to the Integration timing in the figure), and convert it into an electrical signal. The brighter the environment, the stronger the electrical signal. At the same time, if the clock cycle is longer, the longer the light sensing time, the stronger the electrical signal. The signal collected in the current acquisition cycle is allowed to be read in the next cycle.

  • CCD grayscale value output task: After each falling edge between the 2nd and 129th cycles, the AO pin outputs a grayscale value once, representing the grayscale value of the 1st to 128th pixels from the left. The value can be read through the microcontroller ADC. Similarly, the signal output in the current output cycle belongs to the signal collected in the previous cycle.

Black line detected

The corresponding grayscale value is low;

White line detected

The corresponding grayscale value is high.

Software configuration

Pin definition

Main control chipPinMain function (after reset)Default multiplexing functionRedefine function
STM32F103RCT6PA4PA4SPI1_NSS/USART2_CK/DAC_OUT1/ADC12_IN4 
STM32F103RCT6PB4NJTRSTSPI3_MISOPB4/TIM3_CH1/SPI1_MISO
STM32F103RCT6PB5PB5I2C1_SMBA/SPI3_MOSI/I2S3_SDTIM3_CH2/SPI1_MOSI

Software code

We need to configure the pins differently according to the function of the CCD pins.

xxxxxxxxxx
Product supporting materials source code path: Attachment → Source code summary → 2.Extended_Course → 9.CCD

Control function

The tutorial only briefly introduces the code, you can open the project source code to read the details.

ccd_Init

​x
void  ccd_Init(void)
{    
    ADC_InitTypeDef ADC_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;

    RCC_APB2PeriphClockCmd( CCD_AO_CLK | CCD_ADC_CLK , ENABLE ); //Enable ADC3 channel clock
    RCC_ADCCLKConfig(RCC_PCLK2_Div6); //Set ADC division factor 6 72M/6=12, ADC maximum time cannot exceed 14M

    //Set analog channel input pin
    GPIO_InitStructure.GPIO_Pin = CCD_AO_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //Analog input pin
    GPIO_Init(CCD_AO_PORT, &GPIO_InitStructure);

    //Initialize SI interface
    RCC_APB2PeriphClockCmd( CCD_SI_CLK , ENABLE ); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //Push-pull output GPIO_InitStructure.GPIO_Pin = CCD_SI_PIN; GPIO_InitStructure.GPIO_Speed ••= GPIO_Speed_10MHz; GPIO_Init(CCD_SI_PORT,&GPIO_InitStructure); //Initialize CLK interface RCC_APB2Per iphClockCmd( CCD_CLK_CLK , ENABLE ); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //Push-pull output GPIO_InitStructure.GPIO_Pin = CCD_CLK_PIN; GPIO_InitStructure.GPIO_Speed ••= GPIO_Speed_10MHz; GPIO_Init(CCD_CLK_PORT,&GPIO_InitStructure);

    ADC_DeInit(CCD_ADC); //Reset ADC2 and reset all registers of peripheral ADC2 to default values
    ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC working mode: ADC1 and ADC2 work in independent mode
    ADC_InitStructure.ADC_ScanConvMode = DISABLE; //Analog-to-digital conversion works in single-channel mode
    ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //Analog-to-digital conversion works in single-conversion mode
    ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //Conversion is started by software instead of external trigger
    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //ADC data right-aligned
    ADC_InitStructure.ADC_NbrOfChannel = 1; //Number of ADC channels for sequential regular conversion
    ADC_Init(CCD_ADC, &ADC_InitStructure); //Initialize the registers of the peripheral ADCx according to the parameters specified in ADC_InitStruct
    ADC_Cmd(CCD_ADC, ENABLE); //Enable the specified ADC1

    ADC_ResetCalibration(CCD_ADC); //Enable reset calibration
    while(ADC_GetResetCalibrationStatus(CCD_ADC)); //Wait for reset calibration to end
    ADC_StartCalibration(CCD_ADC); //Start AD calibration
    while(ADC_GetCalibrationStatus(CCD_ADC)); //Wait for calibration to end
}   

Get_Adc_CCD

xxxxxxxxxx
u16 Get_Adc_CCD(u8 ch)   
{
    //Sets the specified ADC rule group channel, one sequence, and sampling time
    //Sets the specified ADC rule group channel, one sequence, and sampling time
    //ADC1, ADC channel, sampling time is 480 cycles
    ADC_RegularChannelConfig(CCD_ADC, ch, 1, ADC_SampleTime_239Cycles5 ); //ADC1, ADC channel, sampling time is 239.5 cycles
    //Enable the specified ADC1 software transformation startup function
    //Enable the specified ADC1 software conversion startup function
    ADC_SoftwareStartConvCmd(CCD_ADC, ENABLE);
    //Wait for the conversion to finish
    //Wait for the conversion to finish
    while(!ADC_GetFlagStatus(CCD_ADC, ADC_FLAG_EOC ));
    //Returns the result of the last ADC1 rule group conversion
    //Returns the most recent conversion result of the ADC1 rule group
    return ADC_GetConversionValue(CCD_ADC);
}

RD_TSL

xxxxxxxxxx
 void RD_TSL(void) 
{
  u8 i=0,tslp=0;
  TSL_CLK=1;
  TSL_SI=0; 
  Dly_us();
      
  TSL_SI=1; 
  TSL_CLK=0;
  Dly_us();
  
  TSL_CLK=1;
  TSL_SI=0;
  Dly_us(); 
  for(i=0;i<128;i++)
  { 
    TSL_CLK=0; 
    Dly_us();
    Dly_us();
        
    ADV[tslp]=(Get_Adc_CCD(CCD_ADC_CH))>>4; 
    ++tslp;
    TSL_CLK=1;
    Dly_us();   
  }  
}

Find_CCD_Zhongzhi

xxxxxxxxxx
void  Find_CCD_Zhongzhi(void)
{ 
        static u16 i,j,Left,Right;
    static u16 value1_max,value1_min;

    value1_max=ADV[0]; //Dynamic threshold algorithm, read the maximum and minimum values
    for(i=5;i<123;i++) //Remove 5 points on each side
    {
    if(value1_max<=ADV[i])
    value1_max=ADV[i];
    }
    value1_min=ADV[0]; //Minimum value
    for(i=5;i<123;i++)
    {
    if(value1_min>=ADV[i])
    value1_min=ADV[i];
    }
    CCD_Yuzhi=(value1_max+value1_min)/2; //Calculate the threshold for this midline extraction
    for(i = 5;i<118; i++) //Find the left jump edge
    {
    if(ADV[i]>CCD_Yuzhi&&ADV[i+1]>CCD_Yuzhi&&ADV[i+2]>CCD_Yuzhi&&ADV[i+3]<CCD_Yuzhi&&ADV[i+4]<CCD_Yuzhi&&ADV[i+5]<CCD_Yuzhi)
    {   
        Left=i;
        break;  
    }
    }
    for(j = 118;j>5; j--)
    {
    if(ADV[j]<CCD_Yuzhi&&ADV[j+1]<CCD_Yuzhi&&ADV[j+2]<CCD_Yuzhi&&ADV[j+3]>CCD_Yuzhi&&ADV[j+4]>CCD_Yuzhi&&ADV[j+5]>CCD_Yuzhi)
    {   
      Right=j;
      break;    
    }
    }
    CCD_Zhongzhi=(Right+Left)/2;
}

CCD_Get_ADC_128X32

xxxxxxxxxx
uint8_t* CCD_Get_ADC_128X32(void)
{
    RD_TSL();
    for (int i = 0; i < 128; i++)
    {
        ADC_128X32[i] = ADV[i] >> 3;
    }
    return ADC_128X32;
}

OLED_Show_CCD_Image

xxxxxxxxxx
void OLED_Show_CCD_Image(uint8_t* p_img)
{
    OLED_Clear();
    for (int i = 0; i < 128; i++)
    {
        if (p_img[i] < 32)
        {
            SSD1306_DrawPixel(i, p_img[i], SSD1306_COLOR_WHITE);
        }
    }
    OLED_Refresh();
}

Experimental phenomenon

The CCD.hex file generated by the project compilation is located in the OBJ folder of the CCD project. Find the CCD.hex file corresponding to the project and use the FlyMcu software to download the program into the development board.

After the program is successfully downloaded: OLED and serial port display the data output by the CCD module, and OLED displays the CCD module data in the form of a curve.

xxxxxxxxxx
When using the serial port debugging assistant, you need to pay attention to the serial port settings. If the settings are wrong, the phenomenon may be inconsistent.

image-20240827181344627

Tracking module-Read status (GPIO)
Electromagnetic module-Reading data (ADC)

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文