K40: Line Scan Camera Tutorial

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

K40: Line Scan Camera Tutorial

K40: Line Scan Camera Tutorial

This tutorial covers the details of obtaining data from the line scan camera on the Kinetis K40 using the TWR-K40X256-KIT evaluation board. General details of the line scan camera not related to the Kinetis can be found in the general Line Scan Camera Theory article.

This tutorial will help students familiarize themselves with how to interface the camera with the microcontroller, how to configure GPIO pins to create clock signals and also how to utilize the microcontroller's ADC to read the data from the camera. Outside of control algorithms, configuring the camera is one of the more complex tasks necessary to create a Freescale Cup Vehicle.

1.  Overview

In this exercise students will access the line scan camera, create a clock signal and create the initialization pulse which tells the camera to begin the exposure period. This tutorial will not describe line recognition or line following algorithms - these concepts are beyond its scope.

Students will:

  • Create the code using Codewarrior
  • Build a project
  • Download the code to the board
  • connect the microcontroller to the camera.
  • Run the program
  • view the camera data, clock and Si pulse on an oscilloscope
  • view the data from the camera in the CodeWarrior Debugger for verification purposes

  To successfully complete this exercise, the following software and hardware are required:

  • The K40 Tower card, TWR-K40x256
  • CodeWarrior for Microcontrollers
  • Freescale Line Scan Camera
  • Motor Board
  • Tower Prototyping board
  • Soldering Iron
  • Solder
  • Tower Elevators
  • USB Cord

2. Hardware

  Read the Line Scan Camera Overview article for general information on the camera, ADC, and GPIO microcontroller configuration settings.

3. Set up the Hardware: Line Scan Camera/Microcontroller Hardware Setup

It is crucial for an engineer to have the proper test equipment and tools for the job. In this case, without an oscilloscope, students will not be able to verify whether the proper signals are being sent to the camera.

4. Build the Code

If there is more than one project in your project view, make sure the proper project is the focus. The most reliable way to do this is to right click the project and choose Build Project as shown below. You can also go to the Project menu and choose the same command.

If errors are encountered, look in the Problems view and resolve them. For now ignore any warnings.

5. Download/Debug/Run

Download the code to your board, once this process is complete resume the project so that the code runs. If you want more information on how to complete this step see the download debug run section of the Kinetis Blinking Led tutorial.

6.Example Code

What will happen:

the function ImageCapture() is called in main.c with a pointer to the first element in the array. This function completes the processes necessary to capture images using the camera.

To become more familiar with Pointers and Arrays - navigate to the C Programming Tutorial. Knowledge of Pointers and Arrays is a pre-requisite for understanding the Camera Code.

Functions

The camera code utilizes the following function:

within main.c

ImageCapture(&Line.RawCameraData[0]);


is called to initiate the step of capturing an image into the first position of the array.

ImageCapture();

from Camerainterface.c

This function captures the images by creating the SI Pulse, and clock signals, capturing the data into an Array using the ADC features of the K40. It calls the function ReadADCChannel() at the proper time which then inputs data from the camera AO line.

void ImageCapture(int * ImageData){//this is a pointer to a single character in memory

  unsigned char i;
  TAOS_SI_HIGH;
  TAOS_EXPOSURE_DELAY;
  TAOS_CLK_HIGH;
  TAOS_EXPOSURE_DELAY;
  TAOS_SI_LOW;
  TAOS_EXPOSURE_DELAY;
  ImageData[0] = (int)ReadADCChannel(19);// inputs data from camera (first pixel)
  TAOS_CLK_LOW;

  for(i=1;i<128;i++)
  {
  TAOS_EXPOSURE_DELAY;
  TAOS_EXPOSURE_DELAY;
  TAOS_CLK_HIGH;
  TAOS_EXPOSURE_DELAY;
  TAOS_EXPOSURE_DELAY;
  ImageData[i] = (int)ReadADCChannel(19); // inputs data from camera (one pixel each time through loop)
  TAOS_CLK_LOW;
  }

  TAOS_EXPOSURE_DELAY;
  TAOS_EXPOSURE_DELAY;
  TAOS_CLK_HIGH;
  TAOS_EXPOSURE_DELAY;
  TAOS_EXPOSURE_DELAY;
  TAOS_CLK_LOW;  
}

ReadADCChannel()

from readADC.c

this function does the analog to digital conversion and returns a value between 0 and 255. It takes the proper ADC channel, in this case 19.

Channel 19 corresponds to analog input pin ADC1_DM0 (see page 117 in the K40 Sub-Family Reference Manual).

unsigned char ReadADCChannel(unsigned char Channel)
  {
  ADC1_SC1A = Channel;
  while((ADC1_SC1A & ADC_SC1_COCO_MASK) == 0)
  {
  }
  return ADC1_RA;  
  }

Initialize the ADC

Before utilizing the ADC it must be calibrated and initialized. The details for how to do this can be found in Chapter 19
Using Peripheral Delay Block (PDB) to Schedule Analog to Digital Converter (ADC) Conversions
of the

Kinetis Peripheral Module Quick Reference manual.

The PDB portions of the code have been removed for the Cup Car Demo Code.

Header File Definitions

from k40_TOWER_BOARD_SUPPORT.h

The following code enables the GPIO on the two pins:

#define TAOS_CLK_LOC (1<<28)

#define TAOS_SI_LOC (1<<18)

Initialize the GPIO

From K40_TOWER_BOARD_SUPPORT.c

The InitK40GPIO Function sets up the ports for use:

Relevant code

SIM_SCGC5 = SIM_SCGC5_PORTA_MASK | SIM_SCGC5_PORTB_MASK | SIM_SCGC5_PORTC_MASK | SIM_SCGC5_PORTD_MASK | SIM_SCGC5_PORTE_MASK; //CLK and SI signal

PORTC_PCR18 = PORT_PCR_MUX(1) | PORT_PCR_DSE_MASK;

PORTE_PCR28 = PORT_PCR_MUX(1) | PORT_PCR_DSE_MASK; //Make Sure the GPIO is setup to be an output

GPIOC_PDDR |= LED_E1_LOC | LED_E2_LOC | LED_E3_LOC | TAOS_SI_LOC;

GPIOE_PDDR |= TAOS_CLK_LOC;

GPIOB_PDDR |= LED_E4_LOC;

Other Tutorials:

  1. K40:Blinking LED Tutorial
  2. K40:Drive DC Motor Tutorial
  3. K40:Turning a Servo Tutorial

  

Labels (1)
Comments

Hello, where can I find the source of the code you explained?

Thanks in advance

The various examples, in project form, can be found here

Thanks!

Hello,

I'd like to point out the fact that approximately all the links in this tutorial are unaccurate. Please re-upload the content that was on wikidiot! The images, tutorials and pdf files were a very important source of information for the contestants.

Regards

The content migration from the wiki was painstakingly done and as true to the original as possible.  I thought we had captured everything accurately so your feedback is important.  This particular document is explicit to the K40 board.  You might find what you are looking for in the general line scan overview page which might be where the PDF and images and such you are looking for are located.  If you can give me details on anything specific you are looking for I can find it for you.

Thank you very much! This was the exact page I was looking for.

Hello, where can I get the source of the code you explained, such as Camerainterface.c?

Thanks in advance!!

No ratings
Version history
Last update:
‎09-10-2020 01:54 AM
Updated by: