a Cam-Microcontrollers

kh

ghi
ghi

Microcontroller Basics

Learn the basics of microcontrollers!

Build robots

Do you love robots? This is a place for you.

Microcontrollers

Get microcontrollers and other electronics components for your projects contact on +237674369026

Tuesday, December 1, 2015

Introduction into the ARM microcontroller family: using the STM32 discovery board



source code for project:

   void main(){
 GPIO_Digital_Output(&GPIOC_BASE, _GPIO_PINMASK_9);   ///for the red led
 GPIO_Digital_Output(&GPIOC_BASE, _GPIO_PINMASK_8);
 GPIOC_ODR.B9 = 0;
 GPIOC_ODR.B8 = 0;

 while(1){
  GPIOC_ODR.B9 = 1;
  GPIOC_ODR.B8 = 0;
  Delay_ms(500);
  GPIOC_ODR.B8 = 1;
  GPIOC_ODR.B9 = 0;
  Delay_ms(500);
  }
}


Thursday, November 26, 2015

HOW TO DRIVE MULTIPLE SEVEN SEGMENTS WITH PIC






7-segments could come in different forms and different pin configurations, but of all these varied forms are some simple schematics which will always be available for every one form. These are the a to g LED and a common. This tutorial will not bother about the specific pin configuration of every different type of 7-segment, but will concentrate on how to concatenate several 7-segments of a type, based on the a to g configuration.

The above image shows a typical pin configuration of most 7-segments. Simulation of this project will be done on Proteus and proteus has its own way of representing the above 7-segment.


The first 7 pins from left are the pins to the LEDs 'a' through 'g' and the eighth is 'dot'. The last pin is the common( could be common CATHODE or common ANODE depending on the type of 7-segment).


Displaying a number on a 7-segment is basically combining the corresponding LEDs in the set a to g that when lit will show that number. This is very easy as we will only need to manipulate the pins of the microcontroller that are connected to the the LEDs. But when we have to connect more than one seven segment-- on the same microcontroller port, things become a little more difficult. To be able to perform a task like this, we will have to use the method of scanning. Where we will scan through the 7-segments and display on each one its own digit.   The code below shows how to do this in C using MikroC pro for PIC as compiler.


/*
 * Project name:
     Displaying count from 0 to 999 on three 7-segments

 * Description:
     This code dislays count from 0 to 999 on three 7-segments
 * Test configuration:
     MCU:             PIC16F877a
     Oscillator:      HS, 04.0000 MHz
 * NOTES:
     - Displays count from 0 to 999
*/




/// 7-segment select pins
sbit q1 at RD0_bit;
sbit q2 at RD1_bit;
sbit q3 at RD2_bit;

/// database contains a set of combined LEDs that form the digits 0 to 9.
const char database[] = {0b00111111, 0b000000110, 0b01011011, 0b01001111, 0b01100110, 0b01101101, 0b01111101, 0b00000111, 0b01111111, 0b01101111};
char datum[3] = {' ', ' ', ' '};    //temporary storage for current number to be displayed on screen

void displayNum(unsigned int i, int times){                   //function to display a number a couple of frames.
 unsigned short j;
 unsigned short index;
 int repeat;
 char buf[7];
 if((i>999))
  i = 999;


  inttostr(i, buf);

 for(j = 5; j >= 3; j--){
  if(buf[j] == 32)
   buf[j] = '0';
  index = buf[j] - 48;
  datum[j-3] = database[index];
  //datum[j-3] = ~database[index];
  }

  for(repeat = 0; repeat < times; repeat++){
  //scan num
  //NOTE*** for real life connection, change logic of selectors, because definitely you will be using a current source transistor(if working
  //with common ANODE ), or a current sink transistor (if working with common CATHODE)
  q1 = 0;
  q2 = 1;
  q3 = 1;
  PORTB = datum[0];
  delay_ms(10);
  q1 = 1;
  q2 = 0;
  q3 = 1;
  PORTB = datum[1];
  delay_ms(10);
  q1 = 1;
  q2 = 1;
  q3 = 0;
  PORTB = datum[2];
  delay_ms(10);
  }
 }
void main() {
 unsigned int i;
 TRISD = 0;
 TRISB = 0;
 PORTD = PORTB = 0;
 while(1){
 for(i=0; i<999; i++){
  displayNum(i, 20);
  }
  }
}

Click here to download source code and proteus file

Monday, November 23, 2015

USING ACS712 to measure AC current



Features and Benefits
  • Low-noise analog signal path
  • Device bandwidth is set via the new FILTER pin
  • 5 us output rise time in response to step input current
  • 80kHz bandwidth 
  • Total output error 1.5% at Ta = 25C
  • Small footprint, low-profile SOIC8 package
  • 1.3mOhm internal conductor resistance
  • 2.1 kVRMS minimum isolation voltage from pins 1-4 to pins 5-8
  • 5.0 V, single supply operation
  • 66 to 185 mV/A output sensitivity
  • Output voltage proportional to AC or DC currents
  • Factory-trimmed for accuracy
  • Extremely stable outpput offset voltage
  • Nearly zero magnetic hysteresis
  • Ratiometric output from supply voltage
download full datasheet here 





When measuring AC current, there usually is no need for taking readings of the negative part of the cycle, so we can stick to the positive half by tying our Vref to 2.5 volts as the negative voltage reference. This will enable you have more resolution for your ADC conversion. But for the sake of simplicity, the code bellow will span the whole 0 -5V by having the negative voltage reference set to 0V.

////////////////////////////---------------------------------------------------------------------------------------------
// You can include this code to your microC project and make appropraite calls to the specific functions
// Usually, a single call to getpeakCurrent() does all the job.
//
int  getMax(int a, int b){
    return a>=b? a:b;
}

unsigned int getPeakCurrentVal(){
 int a, b;
 int i;
 int buffer[400];

 for(i=0; i< 400; i++){
  buffer[i] = ADC_Read(0) ;                 //Change the ADC channel to the specific channel you are using
  delay_us(100);
  }
 a = buffer[0];
 for(i=0; i<400; i++){
  b = buffer[i];
  a = getMax(a,b);
  }
 return a;
}

unsigned int Average10(){
unsigned int times;
unsigned long int ret = 0;
   for(times = 0; times < 10; times++) {
   ret += getPeakCurrentVal();
   }
 ret /= 10;
 return (unsigned int)ret;
}

float getPeakCurrent(){
 float p_current;
 p_current = (float)((Average10()- 512)/20.0);                      //scale peak current to actual current in Amps
 if(p_current < (float)0.0){
  p_current = 0.0;
 }
 return p_current;
}

         

Friday, November 20, 2015

LED RUNNER

LED Runner
This is project that varies the speed of a running led on a PORT of a PIC16F877A microcontroller. The current running speed is determined by a potentiometer. Varying the output of the potentiometer, varies the speed of the running LED.

The source code that performs this action is bellow:
The code was written on MikroC PRO for PIC.


void my_delay(unsigned short mul){
 unsigned int i;
 for (i = 0; i< mul; i++){
  delay_ms(50);                  //baseline delay
  }
}


unsigned short mapADC(unsigned int adc_val){
  return (unsigned short)(adc_val/ 50);
}

void runner(){
 //runner code
 portb = 1;
 my_delay(mapADC(ADC_Read(0)));
 portb = 2;
 my_delay(mapADC(ADC_Read(0)));
 portb = 4;
 my_delay(mapADC(ADC_Read(0)));
 portb = 8;
 my_delay(mapADC(ADC_Read(0)));
 portb = 16;
 my_delay(mapADC(ADC_Read(0)));
 portb = 32;
 my_delay(mapADC(ADC_Read(0)));
 portb = 64;
 my_delay(mapADC(ADC_Read(0)));
 portb = 128;
 my_delay(mapADC(ADC_Read(0)));
}

void main() {
 trisb = 0;
 portb = 0;
 while(1){
  runner();
  }
}

copy the code and paste in the code editor and compile.

Saturday, November 7, 2015

SNAKE GAME

// Glcd module connections
char GLCD_DataPort at PORTD;

sbit GLCD_CS1 at RC0_bit;
sbit GLCD_CS2 at RC1_bit;
sbit GLCD_RS  at RC2_bit;
sbit GLCD_RW  at RC3_bit;
sbit GLCD_EN  at RC4_bit;
sbit GLCD_RST at RC5_bit;

sbit GLCD_CS1_Direction at TRISC0_bit;
sbit GLCD_CS2_Direction at TRISC1_bit;
sbit GLCD_RS_Direction  at TRISC2_bit;
sbit GLCD_RW_Direction  at TRISC3_bit;
sbit GLCD_EN_Direction  at TRISC4_bit;
sbit GLCD_RST_Direction at TRISC5_bit;
// End Glcd module connections

//--------------------------BUTTON CONNECTIONS
sbit keyUp at RB4_bit;
sbit keyLeft at RB5_bit;
sbit keyDown at RB6_bit;
sbit keyRight at RB7_bit;
sbit keyB at RB3_bit;
sbit keyA at RB2_bit;

#define CODE_EMPTY   '.'
#define CODE_FOOD 3
#define CODE_SNAKE 219



#define GLCD_LEFT 0
#define GLCD_RIGHT 64


#define OFFSET_FOOD 0

#define OFFSET_SNVE 8
#define OFFSET_SNHO 16

#define OFFSET_SNDL 24
#define OFFSET_SNUL 32
#define OFFSET_SNUR 40
#define OFFSET_SNDR 48

#define OFFSET_SNHU 56
#define OFFSET_SNHR 64
#define OFFSET_SNHD 72
#define OFFSET_SNHL 80

#define OFFSET_SNTU 88
#define OFFSET_SNTR 96
#define OFFSET_SNTD 104
#define OFFSET_SNTL 112


void glcdFillScreen(unsigned short color) {
 glcd_fill(color);
}


unsigned long _Randseed;

unsigned int random(void) {
   _Randseed = _Randseed * 1103515245 + 12345;
   return ((unsigned int)(_Randseed >> 16) );
}

void srandom(unsigned long seed) {
   _Randseed = seed;
}
const unsigned short BITMAP[] = {0b00011110,  // food
                                0b00100001,
                                0b01000001,
                                0b10000010,
                                0b10000010,
                                0b01000001,
                                0b00100001,
                                0b00011110,

                                0b00000000,  // snake_vertical
                                0b00000000,
                                0b11101110,
                                0b11101110,
                                0b01110111,
                                0b01110111,
                                0b00000000,
                                0b00000000,

                                0b00001100,  // snake_horizontal
                                0b00111100,
                                0b00111100,
                                0b00110000,
                                0b00001100,
                                0b00111100,
                                0b00111100,
                                0b00110000,

                                0b00001100,  // snake_down_left
                                0b00111100,
                                0b11111100,
                                0b11101000,
                                0b01111000,
                                0b01100000,
                                0b00000000,
                                0b00000000,

                                0b00001100,  // snake_up_left
                                0b00111100,
                                0b00111110,
                                0b00010110,
                                0b00011111,
                                0b00000111,
                                0b00000000,
                                0b00000000,

                                0b00000000,  // snake_up_right
                                0b00000000,
                                0b00000110,
                                0b00011110,
                                0b00010111,
                                0b00111111,
                                0b00111100,
                                0b00110000,

                                0b00000000,  // snake_down_right
                                0b00000000,
                                0b11100000,
                                0b11111000,
                                0b01101000,
                                0b01111100,
                                0b00111100,
                                0b00110000,

                                0b00000000,  // snake_head_up   // link up, head looks down
                                0b01110000,
                                0b10011110,
                                0b11111110,
                                0b11111111,
                                0b10011111,
                                0b01110000,
                                0b00000000,

                                0b00111100,  // snake_head_right  // link right
                                0b01011010,
                                0b01011010,
                                0b01111110,
                                0b00111100,
                                0b00111100,
                                0b00111100,
                                0b00110000,

                                0b00000000,  // snake_head_down  // link down
                                0b00001110,
                                0b11111001,
                                0b11111111,
                                0b01111111,
                                0b01111001,
                                0b00001110,
                                0b00000000,

                                0b00001100,  // snake_head_left  // link left
                                0b00111100,
                                0b00111100,
                                0b00111100,
                                0b01111110,
                                0b01011010,
                                0b01011010,
                                0b00111100,




                                0b00000000,  // snake_tail_up   // link up
                                0b00000000,
                                0b00101110,
                                0b11101110,
                                0b11110111,
                                0b00110111,
                                0b00000000,
                                0b00000000,

                                0b00011000,  // snake_tail_right  // link right
                                0b00011000,
                                0b00111100,
                                0b00110000,
                                0b00001100,
                                0b00111100,
                                0b00111100,
                                0b00110000,

                                0b00000000,  // snake_tail_down  // link down
                                0b00000000,
                                0b11101100,
                                0b11101111,
                                0b01110111,
                                0b01110100,
                                0b00000000,
                                0b00000000,

                                0b00001100,  // snake_tail_left  // link left
                                0b00111100,
                                0b00111100,
                                0b00110000,
                                0b00001100,
                                0b00111100,
                                0b00011000,
                                0b00011000};