วันจันทร์ที่ 14 มกราคม พ.ศ. 2562

microcontroller 2

DC motor and Fan speed control using Pic16f877 Microcontroller using PWM(Pulse width modulation) Technique




Pic microcontroller speed control project requirements

  • Pic16f877 microcontroller
  • L293d h-bridge Motor driver Ic
  • DC Motor or small fan
  • Crystal 20MHz
  • Push Buttons
  • connecting wires
  • Bread board or PCB
  • Power Supply battery


Project Code



#include< htc.h>
#include< pic.h>
#define _XTAL_FREQ   20000000 
void rundutycycle(unsigned int x );
#define s1           RA0
#define s2           RA1
#define s3           RA2
#define s4           RA3
#define s5           RA4
#define s6           RA5
void main()
{
ADCON1=0x06;             //All pins as digital
        TRISA=0b111111;          //PortA as Input
        TRISC2  = 0;             //Make CCP1 pin as output
CCP1CON = 0x0C;          //Configure CCP1 module in PWM mode
PR2   = 0xFF;            //Configure the Timer2 period
rundutycycle(512);
}

void rundutycycle(unsigned int dutycyc){
T2CON = 0x01;           // Set Prescaler to be 4, hence PWM frequency is set to 4.88KHz.
T2CON |= 0x04;          // Enable the Timer2, hence enable the PWM.

while(1){
CCPR1L   = dutycyc>>2;                // Put MSB 8 bits in CCPR1L
CCP1CON &= 0xCF;                 // Make bit4 and 5 zero
CCP1CON |= (0x30&(dutycyc<<4));         // Assign Last 2 LSBs to CCP1CON
if(s1==1){dutycyc=172; }
if(s2==1){dutycyc=342; }
if(s3==1){dutycyc=512; }
if(s4==1){dutycyc=686; }
if(s5==1){dutycyc=858; }
if(s6==1){dutycyc=1020; }
dutycyc=dutycyc;
}
}

microcontroller 1

 Calculator using Pic16f877 microcontroller






Pic16f877 microcontroller calculator - Project requirements

  • Pic16f877 Microcontroller
  • 16x2 lcd 
  • 4x4 keypad
  • Crystal 20 MHz
  • Potentiometer/variable resistor (For setting Lcd Contrast)
  • Bread board or PCB for Circuit Designing
  • Power Supply




Project Code




#include< htc.h>
#define _XTAL_FREQ 20e6
#define rs RD6
#define en RD7
#define r0 RC0
#define r1 RC1
#define r2 RC2
#define r3 RC3
#define c0 RC4
#define c1 RC5
#define c2 RC6
#define c3 RC7

void lcdcmd  (unsigned char);
void lcddata (unsigned char);
void disp_num(float num);
int get_num  (char ch);
void lcdinit ();
char scan_key(void);

unsigned char  s[]={"ENTER 1 NO= "};
unsigned char s1[]={"ENTER 2 NO= "};
unsigned char s2[]={"OPERATOR = "};
unsigned char s3[]={"***RESULT***"};

void lcdinit(){
 __delay_ms(400); lcdcmd(0x30); __delay_ms(400); lcdcmd(0x30);
__delay_ms(400); lcdcmd(0x30); __delay_ms(400); lcdcmd(0x38);
    lcdcmd(0x0F); lcdcmd(0x01); lcdcmd(0x06); lcdcmd(0x80);
}

void main (void)
  {
    TRISC=0xF0;        //Rows Output, Coulombs Input
    TRISB=0x00;        //Port-B as Output
    TRISD6=0;          //Port-D PIN-6 as Output
    TRISD7=0;          //Port-D PIN-7 as Output
    __delay_ms(400);
  
   unsigned int count=0;
   int k2,k1; 
   char ke,key,key1;
   lcdinit();        //Initializing Lcd

 while(1){   
      while(s[count]!='\0')     //Displaying String s on LCD
      {
        lcddata(s[count]);
        count++;
      }

ke=scan_key();   //Scan the First Digit
k2=get_num(ke);  //Converting Char into number
lcdcmd(0x01);    //Clear Lcd
count=0;

while(s2[count]!='\0')    //Displaying String s2 on LCD
{
lcddata(s2[count]);
count++;
}

key=scan_key();  //Scaning operator
lcdcmd(0x01);    //Cleat Lcd
count=0;

while(s1[count]!='\0')  //Displaying String s1 on LCD
{
lcddata(s1[count]);
count++;
}

key1=scan_key();  //Scan Second digit
k1=get_num(key1); //Converting Char into number
lcdcmd(0x01);     //Clear Lcd
lcdcmd(0x82);     //Start displying data on lcd at position Row=1 Coulomb=3
count=0;

while(s3[count]!='\0')  //Displaying String s3 on LCD
{
lcddata(s3[count]);
count++;
}
count=0;
lcdcmd(0xC0);   //Jump to second Line of Lcd
lcddata(ke);
lcddata(key);
lcddata(key1);
lcddata(' ');
lcddata('=');

switch(key)
{
case '+':
disp_num(k1+k2);
break;
case '-':
disp_num(k2-k1);
break;
case '*':
disp_num(k2*k1);
break;
case '/':
disp_num((float)k2/k1);
break;
}
}
}

void lcdcmd(unsigned char value)  //Sending Commands to Lcd
  {
    PORTB = value;      
    rs = 0;
    en = 1;            
    __delay_ms(100);
    en = 0;
    __delay_ms(100);    
  }
void lcddata(unsigned char value) //Sending Data to Lcd
  {
    PORTB = value;  
    rs = 1;
    en = 1;          
    __delay_ms(100);
    en = 0;
    __delay_ms(100);
  }
  


char scan_key()      //Scan the Pressed Key by user 
{
unsigned char c='s';
while(c!='a')
{
r0=0;r1=1;r2=1;r3=1;
if(c0==1 && r0==0){ lcddata('7');__delay_ms(500);return '7';c='a';}
if(c1==1 && r0==0){ lcddata('8');__delay_ms(500);return '8';c='a';}
if(c2==1 && r0==0){ lcddata('9');__delay_ms(500);return '9';c='a';}
if(c3==1 && r0==0){ lcddata('/');__delay_ms(500);return '/';c='a';}
r0=1;r1=0;r2=1;r3=1;
if(c0==1 && r1==0){ lcddata('4');__delay_ms(500);return '4';c='a';}
if(c1==1 && r1==0){ lcddata('5');__delay_ms(500);return '5';c='a';}
if(c2==1 && r1==0){ lcddata('6');__delay_ms(500);return '6';c='a';}
if(c3==1 && r1==0){ lcddata('*');__delay_ms(500);return '*';c='a';}
r0=1;r1=1;r2=0;r3=1;
if(c0==1 && r2==0){ lcddata('1');__delay_ms(500);return '1';c='a';}
if(c1==1 && r2==0){ lcddata('2');__delay_ms(500);return '2';c='a';}
if(c2==1 && r2==0){ lcddata('3');__delay_ms(500);return '3';c='a';}
if(c3==1 && r2==0){ lcddata('-');__delay_ms(500);return '-';c='a';}
r0=1;r1=1;r2=1;r3=0;
if(c1==1 && r3==0){ lcddata('0');__delay_ms(500);return '0';c='a';}
if(c3==1 && r3==0){ lcddata('+');__delay_ms(500);return '+';c='a';}
}
return 0;
}

int get_num(char ch)         //converting character into integer
{
switch(ch)
{
case '0': return 0; break;
case '1': return 1; break;
case '2': return 2; break;
case '3': return 3; break;
case '4': return 4; break;
case '5': return 5; break;
case '6': return 6; break;
case '7': return 7; break;
case '8': return 8; break;
case '9': return 9; break;
}
return 0;
}

void disp_num(float num)            //Displays calculated value on LCD
{
unsigned char UnitDigit  = 0;  //Contains unit digit of calculated value
unsigned char TenthDigit = 0;  //contains 10th digit of calculated value
unsigned char decimal = 0;
int j,numb;

j=(int)(num*10);
numb=(int)num;

if(numb<0)
{
numb = -1*numb;  // Make number positive
lcddata('-'); // Display a negative sign on LCD
}

TenthDigit = (numb/10);         // Findout Tenth Digit

if( TenthDigit != 0)         // If it is zero, then don't display
lcddata(TenthDigit+0x30); // Make Char of TenthDigit and then display it on LCD

UnitDigit = numb - (TenthDigit*10);

lcddata(UnitDigit+0x30); // Make Char of UnitDigit and then display it on LCD
lcddata('.');
decimal=(j%10)+0x30;             //Display If any value after Decimal Point
lcddata(decimal);
__delay_ms(3000);
lcdcmd(0x01);
}

วันเสาร์ที่ 5 มกราคม พ.ศ. 2562

T18







void main()
{
     unsigned char segment[]=                        {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
     unsigned char step[]={0x01,0x02,0x04,0x08};
     unsigned char i,j;
     TRISB=0;
     TRISC=0;
     PORTB=segment[0];
     while(1)
     {
           for(i=0;i<4;i++)
           {
                 PORTC=step[i];
                 delay_ms(500);
           }
           j++;
           PORTB=segment[j];
           if(j>8)
           {
                  while(1)PORTC=0x00;
           }
     }
}

T17







void main()
{
     unsigned char rcw,rccw;
     unsigned char step_cw[]={0x01,0x02,0x04,0x08};
     unsigned char step_ccw[]={0x04,0x02,0x01,0x08};
     unsigned char keypad[]={0,1,2,3,0,
                               4,5,6,0,
                               7,8,9,0,
                               0,0,0,0};
     unsigned char i,m,kp;
     TRISB=0;
     TRISC=0;
     TRISD=0xFF;
     Lcd_Init(&PORTB);
     Keypad_Init(&PORTD);
     Lcd_Cmd(LCD_CURSOR_OFF);
     Return:Lcd_Cmd(LCD_CLEAR);
     Lcd_Out(1,1,"Rotate CW?");
     while(!Keypad_Read());
     kp=Keypad_Read();
     rcw=keypad[kp];
     Lcd_Chr(2,1,keypad[kp]+48);
     while(Keypad_Read());
     while(Keypad_Read()!=15);
     while(Keypad_Read());
     Lcd_Cmd(LCD_CLEAR);
     Lcd_Out(1,1,"Rotate CCW?");
     while(!Keypad_Read());
     kp=Keypad_Read();
     rccw=keypad[kp];
     Lcd_Chr(2,1,keypad[kp]+48);
     while(Keypad_Read());
     while(Keypad_Read()!=15);
     while(Keypad_Read());
     Lcd_Cmd(LCD_CLEAR);
     Lcd_Out(1,1,"Rotating...");
     for(i=0;i<rcw;i++)
     {
            while(m<4)
            {
                   PORTC=step_cw[m];
                   delay_ms(500);
                   m++;
            }
            m=0;
     }
     for(i=0;i<rccw;i++)
     {
            while(m<4)
            {
                   PORTC=step_ccw[m];
                   delay_ms(500);
                   m++;
            }
            m=0;
     }
     goto Return;
}


T16







void main()
{
     unsigned int analog;
     TRISA=0xFF;
     TRISC=0;
     T2CON=0x02;
     CCP1CON=0x0C;
     PR2=0xFF;
     T2CON.TMR2ON=1;
     while(1)
     {
            analog=Adc_Read(3);
            analog=analog/4;
            CCPR1L=analog;
     }
}

T15







void main()
{
     TRISC=0;
     T2CON=0x00;
     PR2=0x18;
     CCPR1L=0x0C;
     CCP1CON=0x2C;
     T2CON.TMR2ON=1;
}

T14







#define r1 PORTB.F0
#define r2 PORTB.F1
#define r3 PORTB.F2
#define r4 PORTB.F3
#define r5 PORTB.F4
#define r6 PORTB.F5
#define r7 PORTB.F6
#define r8 PORTB.F7
#define c1 PORTC.F0
#define c2 PORTC.F1
#define c3 PORTC.F2
#define c4 PORTC.F3
#define c5 PORTC.F4
#define c6 PORTC.F5
#define c7 PORTC.F6
#define c8 PORTC.F7

void delay_reset()
{
      delay_ms(5);
      PORTC=0;PORTB=0xFF;
}

void main()
{
     TRISB=0;
     TRISC=0;
     PORTC=0;PORTB=0xFF;
     while(1)
     {
            c4=1;r4=0;r5=0;delay_reset();
            c5=1;r4=0;r5=0;delay_reset();

            delay_ms(100);

            c3=1;PORTB=0xC3;delay_reset();
            c4=1;r3=0;r6=0;delay_reset();
            c5=1;r3=0;r6=0;delay_reset();
            c6=1;PORTB=0xC3;delay_reset();

            delay_ms(100);

            c2=1;PORTB=0x81;delay_reset();
            c3=1;r2=0;r7=0;delay_reset();
            c4=1;r2=0;r7=0;delay_reset();
            c5=1;r2=0;r7=0;delay_reset();
            c6=1;r2=0;r7=0;delay_reset();
            c7=1;PORTB=0x81;delay_reset();

            delay_ms(100);

            c1=1;PORTB=0;delay_reset();
            c2=1;r1=0;r8=0;delay_reset();
            c3=1;r1=0;r8=0;delay_reset();
            c4=1;r1=0;r8=0;delay_reset();
            c5=1;r1=0;r8=0;delay_reset();
            c6=1;r1=0;r8=0;delay_reset();
            c7=1;r1=0;r8=0;delay_reset();
            c8=1;PORTB=0;delay_reset();

            delay_ms(100);
     }
}

T13








void main()
{
     unsigned char num[4];
     unsigned char input[4];
     unsigned char a,b;
     unsigned char keypad[]={0,1,2,3,0,
                               4,5,6,0,
                               7,8,9,0,
                               0,0,0,0};
     unsigned int kp,i,j;
     TRISC=0;
     TRISD=0xFF;
     Lcd_Init(&PORTC);
     Keypad_Init(&PORTD);
     Lcd_Cmd(LCD_CURSOR_OFF);
     Lcd_Cmd(LCD_CLEAR);
     Lcd_Out(1,1,"Enter 4 numbers");
     for(i=0;i<4;i++)
     {
           while(!Keypad_Read());
           kp=Keypad_Read();
           num[i]=keypad[kp];
           Lcd_Chr(2,i+1,keypad[kp]+48);
           while(Keypad_Read());
     }
     while(Keypad_Read()!=15);
     Return:Lcd_Cmd(LCD_CLEAR);
     Lcd_Out(1,1,"What are 4 numbers?");
     while(Keypad_Read());
     for(i=0;i<4;i++)
     {
           while(!Keypad_Read());
           kp=Keypad_Read();
           input[i]=keypad[kp];
           Lcd_Chr(2,i+1,keypad[kp]+48);
           while(Keypad_Read());
     }
     while(Keypad_Read()!=15);
     Lcd_Cmd(LCD_CLEAR);
     while(Keypad_Read());
     for(i=0;i<4;i++)
     {
           if(input[i]==num[i])
           {
                  a++;
                  continue;
           }
           for(j=0;j<4;j++)
           {
                  if(input[i]==num[j])b++;
           }
     }
     Lcd_Chr(1,1,a+48);
     Lcd_Chr(1,2,'A');
     Lcd_Chr(1,3,b+48);
     Lcd_Chr(1,4,'B');
     if(a==4)Lcd_Out(2,1,"Correct");
     else
     {
           Lcd_Out(2,1,"Wrong!");
           a=0;b=0;
           while(Keypad_Read()!=15);
           goto Return;
     }
}





T12







void main()
{
     unsigned char num;
     unsigned char input;
     unsigned char keypad[]={0,1,2,3,0,
                               4,5,6,0,
                               7,8,9,0,
                               0,0,0,0};
     unsigned int kp,i;
     TRISC=0;
     TRISD=0xFF;
     Lcd_Init(&PORTC);
     Keypad_Init(&PORTD);
     Lcd_Cmd(LCD_CURSOR_OFF);
     Lcd_Cmd(LCD_CLEAR);
     Lcd_Out(1,1,"Enter number");
     for(i=0;i<2;i++)
     {
            while(!Keypad_Read());
            kp=Keypad_Read();
            num=num*10*i+keypad[kp];
            Lcd_Chr(2,i+1,keypad[kp]+48);
            while(Keypad_Read());
     }
     while(Keypad_Read()!=15);
     Return:Lcd_Cmd(LCD_CLEAR);
     Lcd_Out(1,1,"What is number?");
     while(Keypad_Read());
     for(i=0;i<2;i++)
     {
            while(!Keypad_Read());
            kp=Keypad_Read();
            input=input*10*i+keypad[kp];
            Lcd_Chr(2,i+1,keypad[kp]+48);
            while(Keypad_Read());
     }
     while(Keypad_Read()!=15);
     Lcd_Cmd(LCD_CLEAR);
     while(Keypad_Read());
     if(num>input)
     {
            Lcd_Out(1,1,"More than!");
            while(Keypad_Read()!=15);
            goto Return;
     }
     else if(num<input)
     {
            Lcd_Out(1,1,"Less than!");
            while(Keypad_Read()!=15);
            goto Return;
     }
     else
     {
            Lcd_Out(1,1,"Correct");
     }
}





Internet of Things (IoT)

Internet of Things หรือ IoT Internet of Things (IoT)  คือ การที่อุปกรณ์อิเล็กทรอนิกส์ต่างๆ สามารถเชื่อมโยงหรือส่งข้อมูลถึงกันได้ด้วยอิน...