Archive

Archive for February, 2010

STM32 Oscilloscope

February 18th, 2010 33 comments

This week I got some inspiration from the ST DSP library, so I made my own oscilloscope with the STM32 and my 320×240 pixels QTFT screen. I made the code from scratch, both the sampling and screen updating code – but I used the ST DSP library for the FFT calculations of course

You can see the video of the project here…
httphd://www.youtube.com/watch?v=llDmjxUtoRA

Due to the huge amount of requests I’ve decided to publish the code used in this project.
The code is a bit messy as it contains bits of code that is never reached – fx in the main() function.
As said above, the code uses the ST DSP Library to do the FFT calculations, and it just uses one of the ADC inputs for the sampling.

The code package can be downloaded here: STM32 Oscilloscope

Categories: ARM Tags: , ,

Magnetic Card lock with the Arduino

February 17th, 2010 9 comments

I have posted alot of projects recently, and here is another project with the Arduino!
This projects is about making a magnetic card lock using the Arduino, a servo and a $4 cheap magnetic card reader from AllElectronics.com

Magnetic Card Lock project


Magnetic Card Reader Connections

The card reader, which you can see in the first picture, has 7 pins. The picture below shows theese pins functionality!

I’ve made a video about the project which explains it all!
httphd://www.youtube.com/watch?v=S1i1DAMpy5E

If you want to try it yourself, you can grab the code underneath and change the checkCode character array to match the string on your card!

#include <string.h>

#include <Servo.h>
Servo servo1;

 /* Magnetic Card lock with the Arduino and servo's
 * by Thomas Jespersen http://elec.tkjweb.dk
 *
 * Reads a magnetic stripe and opens lock (turns servo) if card is the same as programmed
 *
 */

// Connections: DATA = Pin 2, CLOCK = Pin 3, CARD IN = Pin 5
// See PDF "Magnetic Stripe Card Reader! << HACKMIAMI.pdf" for more connection information


int cld1Pin = 5;            // Card status pin
int rdtPin = 2;             // Data pin
int reading = 0;            // Reading status
volatile int buffer[400];   // Buffer for data
volatile int i = 0;         // Buffer counter
volatile int bit = 0;       // global bit
char cardData[40];          // holds card info
int charCount = 0;          // counter for info
int DEBUG = 0;
const byte checkCodeLen = 23;
char checkCode[checkCodeLen+1] = ";0123456789=0123456789?";
boolean unlock = false;

void setup() {
  Serial.begin(9600);

  servo1.attach(9);
  servo1.write(10);

  // The interrupts are key to reliable
  // reading of the clock and data feed
  attachInterrupt(0, changeBit, CHANGE);
  attachInterrupt(1, writeBit, FALLING);
}

void loop(){

  // Active when card present
  while(digitalRead(cld1Pin) == LOW){
    reading = 1;
  }

  // Active when read is complete
  // Reset the buffer
  if(reading == 1) {

    if (DEBUG == 1) {
      printBuffer();
    }

    decode();
    reading = 0;
    i = 0;

    int l;
    for (l = 0; l < 40; l = l + 1) {
     cardData[l] = '\n';
    }

    // Code added by mindthomas for buffer clearing (If not, you can just insert card slightly, and then last card data will be written)
    for (l = 0; l < 200; l = l + 1) {
      buffer[l] = 0;
    }
    // End of added code

    charCount = 0;
  }
}

// Flips the global bit
void changeBit(){
  if (bit == 0) {
    bit = 1;
  } else {
    bit = 0;
  }
}

// Writes the bit to the buffer
void writeBit(){
  buffer[i] = bit;
  i++;
}

// prints the buffer
void printBuffer(){
  int j;
  for (j = 0; j < 200; j = j + 1) {
    Serial.println(buffer[j]);
  }
}

int getStartSentinal(){
  int j;
  int queue[5];
  int sentinal = 0;

  for (j = 0; j < 400; j = j + 1) {
    queue[4] = queue[3];
    queue[3] = queue[2];
    queue[2] = queue[1];
    queue[1] = queue[0];
    queue[0] = buffer[j];

    if (DEBUG == 1) {
      Serial.print(queue[0]);
      Serial.print(queue[1]);
      Serial.print(queue[2]);
      Serial.print(queue[3]);
      Serial.println(queue[4]);
    }

    if (queue[0] == 0 & queue[1] == 1 & queue[2] == 0 & queue[3] == 1 & queue[4] == 1) {
      sentinal = j - 4;
      break;
    }
  }

  if (DEBUG == 1) {
    Serial.print("sentinal:");
    Serial.println(sentinal);
    Serial.println("");
  }

  return sentinal;
}

void decode() {
  int sentinal = getStartSentinal();
  int j;
  int i = 0;
  int k = 0;
  int thisByte[5];

  for (j = sentinal; j < 400 - sentinal; j = j + 1) {
    thisByte[i] = buffer[j];
    i++;
    if (i % 5 == 0) {
      i = 0;
      if (thisByte[0] == 0 & thisByte[1] == 0 & thisByte[2] == 0 & thisByte[3] == 0 & thisByte[4] == 0) {
        break;
      }
      printMyByte(thisByte);
    }
  }

  cardData[charCount] = '\0';

  Serial.print("Stripe_Data:");
  for (k = 0; k < charCount; k = k + 1) {
    Serial.print(cardData[k]);
  }
  Serial.println("");


 // Check if the card data is the same as the data in checkCode - if yes, unlock (turn servo)
 unlock = true;
 for (k = 0; k < checkCodeLen; k++) {
   if (cardData[k] == checkCode[k]) {
     if (DEBUG == 1) {
       Serial.print(cardData[k]);
       Serial.print("=");
       Serial.println(checkCode[k]);
     }
   } else {
     if (DEBUG == 1) {
       Serial.print(cardData[k]);
       Serial.print("!=");
       Serial.println(checkCode[k]);
     }
     unlock = false;
     break;
   }
 }

 if (unlock) {
  servo1.write(200);
  delay(2000);
  servo1.write(10);
  unlock = false;
 }


}

void printMyByte(int thisByte[]) {
  int i;
  for (i = 0; i < 5; i = i + 1) {
    if (DEBUG == 1) {
      Serial.print(thisByte[i]);
    }
}
    if (DEBUG == 1) {
      Serial.print("\t");
      Serial.print(decodeByte(thisByte));
      Serial.println("");
    }

    cardData[charCount] = decodeByte(thisByte);
    charCount ++;
}

char decodeByte(int thisByte[]) {
    if (thisByte[0] == 0 & thisByte[1] == 0 & thisByte[2] == 0 & thisByte[3] == 0 & thisByte[4] == 1){
      return '0';
    }
    if (thisByte[0] == 1 & thisByte[1] == 0 & thisByte[2] == 0 & thisByte[3] == 0 & thisByte[4] == 0){
      return '1';
    }

    if (thisByte[0] == 0 & thisByte[1] == 1 & thisByte[2] == 0 & thisByte[3] == 0 & thisByte[4] == 0){
      return '2';
    }

    if (thisByte[0] == 1 & thisByte[1] == 1 & thisByte[2] == 0 & thisByte[3] == 0 & thisByte[4] == 1){
      return '3';
    }

    if (thisByte[0] == 0 & thisByte[1] == 0 & thisByte[2] == 1 & thisByte[3] == 0 & thisByte[4] == 0){
      return '4';
    }

    if (thisByte[0] == 1 & thisByte[1] == 0 & thisByte[2] == 1 & thisByte[3] == 0 & thisByte[4] == 1){
      return '5';
    }

    if (thisByte[0] == 0 & thisByte[1] == 1 & thisByte[2] == 1 & thisByte[3] == 0 & thisByte[4] == 1){
      return '6';
    }

    if (thisByte[0] == 1 & thisByte[1] == 1 & thisByte[2] == 1 & thisByte[3] == 0 & thisByte[4] == 0){
      return '7';
    }

    if (thisByte[0] == 0 & thisByte[1] == 0 & thisByte[2] == 0 & thisByte[3] == 1 & thisByte[4] == 0){
      return '8';
    }

    if (thisByte[0] == 1 & thisByte[1] == 0 & thisByte[2] == 0 & thisByte[3] == 1 & thisByte[4] == 1){
      return '9';
    }

    if (thisByte[0] == 0 & thisByte[1] == 1 & thisByte[2] == 0 & thisByte[3] == 1 & thisByte[4] == 1){
      return ':';
    }

    if (thisByte[0] == 1 & thisByte[1] == 1 & thisByte[2] == 0 & thisByte[3] == 1 & thisByte[4] == 0){
      return ';';
    }

    if (thisByte[0] == 0 & thisByte[1] == 0 & thisByte[2] == 1 & thisByte[3] == 1 & thisByte[4] == 1){
      return '<';
    }

    if (thisByte[0] == 1 & thisByte[1] == 0 & thisByte[2] == 1 & thisByte[3] == 1 & thisByte[4] == 0){
      return '=';
    }

    if (thisByte[0] == 0 & thisByte[1] == 1 & thisByte[2] == 1 & thisByte[3] == 1 & thisByte[4] == 0){
      return '>';
    }

    if (thisByte[0] == 1 & thisByte[1] == 1 & thisByte[2] == 1 & thisByte[3] == 1 & thisByte[4] == 1){
      return '?';
    }
}

Categories: Arduino Tags:

STM32 – Overclocking

February 14th, 2010 12 comments

Have you ever wondered if it was possible to overclock the STM32? It is, with a simple change in code line!
We only have to change the PLL setting, which is able to go up to 16 – so that means that we can overclock the STM32 up to 8MHz x 16 = 128 MHz
Here is the RCC Initialization code for 128MHz – remember, you also have to comment the “SYSCLK_FREQ_72MHz”, uncomment the “SYSCLK_FREQ_HSE” and set it to 128MHz in the system_stm32f10x.c

/*******************************************************************************
* Function Name  : RCC_Configuration
* Description    : Configures the different system clocks.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void RCC_Configuration(void)
   {
   /* RCC system reset(for debug purpose) */
   RCC_DeInit();

   /* Enable HSE */
   RCC_HSEConfig(RCC_HSE_ON);

   /* Wait till HSE is ready */
   HSEStartUpStatus = RCC_WaitForHSEStartUp();

   if(HSEStartUpStatus == SUCCESS)
      {
      /* Enable Prefetch Buffer */
      FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

      /* Flash 2 wait state */
      FLASH_SetLatency(FLASH_Latency_2);

      /* HCLK = SYSCLK */
      RCC_HCLKConfig(RCC_SYSCLK_Div1);

      /* PCLK2 = HCLK */
      RCC_PCLK2Config(RCC_HCLK_Div1);

      /* PCLK1 = HCLK/2 */
      RCC_PCLK1Config(RCC_HCLK_Div2);

      /* PLLCLK = 8MHz * 9 = 72 MHz */
      //RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
      /* PLLCLK = 8MHz * 16 = 128 MHz */
      RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_16);
      // The frequency has also been changed in system_stm32f10x

      /* Enable PLL */
      RCC_PLLCmd(ENABLE);

      /* Wait till PLL is ready */
      while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
         {;}

      /* Select PLL as system clock source */
      RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

      /* Wait till PLL is used as system clock source */
      while(RCC_GetSYSCLKSource() != 0x08)
         {;}
      }

   /* Enable peripheral clocks --------------------------------------------------*/
   /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG and AFIO clocks */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC
         | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOG
         | RCC_APB2Periph_AFIO, ENABLE);
   }
Categories: ARM Tags: , ,

STM32 – Internal 8MHz Clock Setup Routine

February 10th, 2010 6 comments

Here is the setup code to use the internal 8MHz clock – but with the internal clock, we are only able to get a max frequency of 36MHz.

void clock_init(){
  /*Configure all clocks to max for best performance.
   * If there are EMI, power, or noise problems, try slowing the clocks*/


  /* First set the flash latency to work with our clock*/
  /*000 Zero wait state, if 0  MHz < SYSCLK <= 24 MHz
    001 One wait state, if  24 MHz < SYSCLK <= 48 MHz
    010 Two wait states, if 48 MHz < SYSCLK <= 72 MHz */

  FLASH_SetLatency(FLASH_Latency_1);

  /* Start with HSI clock (internal 8mhz), divide by 2 and multiply by 9 to
   * get maximum allowed frequency: 36Mhz
   * Enable PLL, wait till it's stable, then select it as system clock*/

  RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_9);
  RCC_PLLCmd(ENABLE);
  while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) {}
  RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

  /* Set HCLK, PCLK1, and PCLK2 to SCLK (these are default */
  RCC_HCLKConfig(RCC_SYSCLK_Div1);
  RCC_PCLK1Config(RCC_HCLK_Div1);
  RCC_PCLK2Config(RCC_HCLK_Div1);

  /* Set ADC clk to 9MHz (14MHz max, 18MHz default)*/
  RCC_ADCCLKConfig(RCC_PCLK2_Div4);

  /*To save power, use below functions to stop the clock to ceratin
   * peripherals
   * RCC_AHBPeriphClockCmd
   */

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ALL, ENABLE);
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_ALL, ENABLE);
}
Categories: ARM Tags: