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

Showing posts with label TUTORIALS. Show all posts
Showing posts with label TUTORIALS. Show all posts

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.