วันศุกร์ที่ 8 กุมภาพันธ์ พ.ศ. 2562
โครงงานที24
code
void beep(void);
main()
{
int time1,time2;
TRISB = 0xF0;
while(1)
{
PORTB.F1=0;
PORTB.F2=0;
PORTB.F3=0;
while(~PORTB.F4&PORTB.F5&PORTB.F6)
{
PORTB.F1=1;
PORTB.F2=0;
PORTB.F3=0;
for(time1=0;time1<20000;time1++)
{
while(PORTB.F4&~PORTB.F5&PORTB.F6)
{
PORTB.F1=1;
PORTB.F2=1;
PORTB.F3=0;
for(time2=0;time2<10000;time2++)
{
while(PORTB.F4&PORTB.F5&~PORTB.F6)
{
PORTB.F1=1;
PORTB.F2=1;
PORTB.F3=1;
beep();
Delay_ms(1000);
}
}
}
}
}
}
}
void beep(void)
{
int i ;
for(i=0;i<200;i++)
{
PORTB.F0=1;
Delay_us(200);
PORTB.F0=0;
Delay_us(200);
}
}
โครงงานที่22
code
int conv7segment(int number);
main()
{
int minute,sec;
TRISB= 0x00;
TRISA= 0x00;
CMCON= 0x07;
PORTA.F0=0;
PORTA.F1=1;
for(minute=9;minute>0;minute--)
{
PORTB = conv7segment(minute);
for(sec=0;sec<=59;sec++)
{
PORTA.F7=1;
Delay_ms(500);
PORTA.F7=0;
Delay_ms(500);
}
}
PORTB = conv7segment(0);
PORTA.F0=0;
PORTA.F1=1;
}
int conv7segment(int number)
{
switch(number)
{
case 1: return 0x06;
case 2: return 0x5b;
case 3: return 0x4f;
case 4: return 0x66;
case 5: return 0x6d;
case 6: return 0x7d;
case 7: return 0x07;
case 8: return 0x7f;
case 9: return 0x6f;
case 0: return 0x3f;
}
}
โครงงานที่20
code
int Read_VR (void);
main()
{
int i,time;
TRISB = 0x00;
CMCON = 0x07;
while(1)
{
for(i=1;i<129;i=i*2)
{
PORTB = i;
time = Read_VR ()*2;
Vdelay_ms(time);
}
}
}
int Read_VR(void)
{
int i;
i = 0;
TRISA = 0xf0;
PORTA.F0 = 1;
Delay_ms(10);
TRISA = 0xF1;
while(PORTA.F0 == 1)
{
i++;
}
i = i/10;
return i ;
}
โครงการที่19
code
int Read_VR(void);
void alarm(void);
main()
{
TRISB = 0xF0;
CMCON = 0x07;
while(1)
{
while(Read_VR()<5)
{
alarm();
}
}
}
int Read_VR(void)
{
int i;
i =0;
TRISA = 0xF0;
PORTA.F0 =1;
Delay_ms(10);
TRISA = 0xF1;
while(PORTA.F0 == 1)
{
i++;
}
i = i/10;
return i ;
}
void alarm(void)
{
int i ;
PORTB.F1 = 1;
for(i=0;i<500;i++)
{
PORTB.F0=1;
Delay_us(500);
PORTB.F0=0;
Delay_us(500);
}
PORTB.F1=0;
for(i=0;i<250;i++)
{
PORTB.F0=0;
Delay_us(1000);
}
}
โครงงานที่19
code
int Read_VR(void);
main()
{
int i,time;
TRISB = 0x00;
CMCON = 0x07;
while(1)
{
for(i=1;i<129;i=i*2)
{
PORTB = i;
time = Read_VR()*2;
Vdelay_ms(time);
}
}
}
int Read_VR(void)
{
int i;
i =0;
TRISA = 0xF0;
PORTA.F0 = 1;
Delay_ms(10);
TRISA = 0xF1;
while(PORTA.F0 == 1)
{
i++;
}
i = i/10;
return i ;
}
โครงงานที่18 ไซเรน 2เสียง หน้า 364 เลขที่ 14
code
void sound1(void);
void sound2(void);
void Vdelay_us(int time);
main()
{
TRISB=0xFC;
PORTB.F1=0;
while(1)
{
while(PORTB.F2==0)
{
sound1();
}
while(PORTB.F3==0)
{
sound2();
}
}
}
void Vdelay_us(int time )
{
int i;
for(i=0;i<time;i++)
{
}
}
void sound1 (void)
{
int i ;
PORTB.F1=1;
for (i=80;i>20;i--)
{
PORTB.F0=1;
Vdelay_us(i);
PORTB.F0=0;
Vdelay_us(i);
}
PORTB.F1=0;
for(i=20;i<80;i++)
{
PORTB.F0=1;
Vdelay_us(i);
PORTB.F0=0;
Vdelay_us(i);
}
}
void sound2 (void)
{
int i;
PORTB.F1=1;
for (i=0;i<200;i++)
{
PORTB.F0=1;
Vdelay_us(20);
PORTB.F0=0;
Vdelay_us(20);
}
PORTB.F1=0;
for(i=0;i<200;i++)
{
PORTB.F0=1;
Vdelay_us(50);
PORTB.F0=0;
Vdelay_us(50);
}
}
โครงงานที่17 ลูกเต๋าอิเล็กทรอนิกส์
code
void beep(void);
int display_led(int in);
main()
{
int i,j,time_n;
TRISB = 0x00;
CMCON = 0x07;
TRISA = 0xfd;
while(1)
{
time_n =120;
while(PORTA.F0==1)
{
time_n++;
if(time_n>240)
{
time_n=120;
}
}
j=1;
for(i=1;i<time_n;i++)
{
PORTB = display_led(j);
beep();
Delay_ms(10);
j++;
if(j>6)
{
j=1;
}
}
}
}
int display_led(int in)
{
switch(in)
{
case 1:return 0b0001000;
case 2:return 0b0010100;
case 3:return 0b0101010;
case 4:return 0b1100011;
case 5:return 0b1101011;
case 6:return 0b1110111;
}
}
void beep(void)
{
int i_b;
for(i_b=0;i_b<20;i_b++)
{
PORTA.F1=1;
Delay_us(300);
PORTA.F1=0;
Delay_us(300);
}
}
โครงงานที่16
code
int Read_T(void);
main()
{
TRISB = 0xF0;
CMCON = 0x07;
PORTB.F0=0;
while(1)
{
if(Read_T()<20)
{
PORTB.F0=1;
}
if(Read_T()>30)
{
PORTB.F0;
}
}
}
int Read_T(void)
{
int i;
i =0;
TRISA = 0xF0;
PORTA.F0 = 1;
Delay_ms(10);
TRISA = 0xF1;
while(PORTA.F
{
i++;
}
i = i/10;
return i ;
}
โครงงานที่14วงจรเครื่องนับจำนวณ
code
int conv7segment(int number);
main()
{
int i;
TRISB = 0x00;
CMCON = 0x07;
TRISA = 0xFF;
i = 0;
while(1)
{
while(PORTA.F0==0)
{
i++;
if(i>9)
{
i = 0;
}
while(PORTA.F0==0)
{
Delay_ms(10);
}
}
while(PORTA.F1==0)
{
i--;
if(i<0)
{
i = 9;
}
while(PORTA.F1==0)
{
Delay_ms(10);
}
}
PORTB = conv7segment(i);
}
}
int conv7segment(int number)
{
switch(number)
{
case 1: return 0x06;
case 2: return 0x5b;
case 3: return 0x4f;
case 4: return 0x66;
case 5: return 0x6d;
case 6: return 0x7d;
case 7: return 0x07;
case 8: return 0x7f;
case 9: return 0x6f;
case 0: return 0x3f;
}
}
โครงงานที 13.5
code
void alarm(void);
main()
{
TRISB = 0xFC;
PORTB.F1 = 0;
while(1)
{
while(PORTB.F2==1)
{
while(PORTB.F3==1)
{
alarm();
}
}
}
}
void alarm(void)
{
int i ;
PORTB.F1 = 1;
for(i=0;i<500;i++)
{
PORTB.F0=1;
Delay_us(500);
PORTB.F0=0;
Delay_us(500);
}
PORTB.F1=0;
for(i=0;i<250;i++)
{
PORTB.F0=0;
Delay_us(1000);
}
}
โครงงานที่13 วงจรเครื่องตั้งเวลา
code
int conv7segment(int number);
main()
{
int minute,sec;
TRISB = 0x00;
TRISA = 0x00;
CMCON = 0x07;
PORTA.F0=0;
PORTA.F1=1;
for (minute=9;minute>0;minute--)
{
PORTB = conv7segment(minute);
for (sec=0;sec<=59;sec++)
{
PORTB.F7=1;
Delay_ms(100);
PORTB.F7=0;
Delay_ms(100);
}
}
PORTB = conv7segment(0);
PORTA.F0=1;
PORTA.F1=0;
}
int conv7segment(int number)
{
switch (number)
{
case 1: return 0x06;
case 2: return 0x5b;
case 3: return 0x4f;
case 4: return 0x66;
case 5: return 0x6d;
case 6: return 0x7d;
case 7: return 0x07;
case 8: return 0x7f;
case 9: return 0x6f;
case 0: return 0x3f;
}
}
โครงงานที่13
code
int Read_VR1(void);
int Read_VR2(void);
main()
{
TRISB = 0xF0;
CMCON = 0x07;
while (1)
{
PORTB.F0=1;
while (Read_VR1()<5)
{
while (Read_VR2()<5)
{
PORTB.F0 = 0;
}
}
}
}
int Read_VR1(void)
{
int i;
i =0;
TRISA = 0xF0;
PORTA.F0 = 1;
PORTA.F1 = 0;
Delay_ms(10);
TRISA = 0xF1;
while( PORTA.F0 == 1)
{
i++;
}
i = i/10;
return i ;
}
int Read_VR2(void)
{
int i;
i =0;
TRISA = 0xF0;
PORTA.F0 = 0;
PORTA.F1 = 1;
Delay_ms(10);
TRISA = 0xF2;
while( PORTA.F1 == 1)
{
i++;
}
i = i/10;
return i ;
}
โครงงานที่12 งานเครื่องทดสอบความไว
code
void alarm(void);
main()
{
TRISB = 0xF0;
while(1)
{
PORTB.F2=0;
PORTB.F3=0;
while(PORTB.F4==0)
{
while(PORTB.F6==1)
{
PORTB.F2=1;
alarm();
}
}
while(PORTB.F5==0)
{
while(PORTB.F6==1)
{
PORTB.F3=1;
alarm();
}
}
}
}
void alarm(void)
{
int i;
PORTB.F1=1;
for(i=0;i<500;i++)
{
PORTB.F0=1;
Delay_us(500);
PORTB.F0=0;
Delay_us(500);
}
PORTB.F1=0;
for(i=0;i<150;i++)
{
PORTB.F0=0;
Delay_us(1000);
}
}
โครงงานที่12
code
void alarm(void);
void Vdelay_us(int time);
main()
{
TRISB=0xFC;
PORTB.F1=0;
while(1)
{
while(PORTB.F2==0)
{
while(PORTB.F3==1)
{
alarm();
}
}
}
}
void alarm(void)
{
int i ;
PORTB.F1=1;
for(i=100;i>50;i--)
{
PORTB.F0=1;
Vdelay_us(i);
PORTB.F0=0;
Vdelay_us(i);
}
PORTB.F1=0;
for(i=50;i<100;i++)
{
PORTB.F0=1;
Vdelay_us(i);
PORTB.F0=0;
Vdelay_us(i);
}
}
void Vdelay_us(int time)
{
int i ;
for(i=0;i<time;i++)
{
}
}
โครงงานที่11 เครื่องทดสอบสมาธิ หน้า 329 เลขที่ 14
code
void alarm (void);
void Vdelay_us(int time);
main()
{
TRISB=0xFC;
PORTB.F1=0;
while(1)
{
while(PORTB.F2==0)
{
while(PORTB.F3==1)
{
alarm();
}
}
}
}
void alarm(void)
{
int i ;
PORTB.F1=1;
for (i=100;i>50;i--)
{
PORTB.F0=1;
Vdelay_us(i);
PORTB.F0=0;
Vdelay_us(i);
}
PORTB.F1=0;
for(i=50;i<100;i++)
{
PORTB.F0=1;
Vdelay_us(i);
PORTB.F0=0;
Vdelay_us(i);
}
}
void Vdelay_us(int time)
{
int i ;
for(i=0;i<time;i++)
{
}
}
โครงงานที่10
code
void alarm(void);
main()
{
TRISB = 0xF0;
while(1)
{
PORTB.F2=0;
PORTB.F3=0;
while(PORTB.F4==0)
{
while(PORTB.F6==1)
{
PORTB.F2=1;
alarm();
}
}
while(PORTB.F5==0)
{
while(PORTB.F6==1)
{
PORTB.F3=1;
alarm();
}
}
}
}
void alarm(void)
{
int i ;
PORTB.F1 = 1;
for(i=0;i<500;i++)
{
PORTB.F0=1;
Delay_us(500);
PORTB.F0=0;
Delay_us(500);
}
PORTB.F1=0;
for(i=0;i<150;i++)
{
PORTB.F0=0;
Delay_us(1000);
}
}
โครงงานที่10 กล่องดนตรี
code
void alarm(void);
main()
{
TRISB = 0xF0;
while(1)
{
PORTB.F2=0;
PORTB.F3=0;
while(PORTB.F4==0)
{
while(PORTB.F6==1)
{
PORTB.F2=1;
alarm();
}
}
while(PORTB.F5==0)
{
while(PORTB.F6==1)
{
PORTB.F3=1;
alarm();
}
}
}
}
void alarm (void)
{
int i ;
PORTB.F1 = i;
for(i=0;i<500;i++)
{
PORTB.F0=1;
Delay_us(500);
PORTB.F0=0;
Delay_us(500);
}
PORTB.F1=0;
for(i=0;i<150;i++)
{
PORTB.F0=0;
Delay_us(1000);
}
}
โครงงานที่8
code
void beep(void);
main()
{
int i;
TRISB= 0x00;
CMCON= 0x07;
TRISA= 0xfd;
while(1)
{
for(i=1;i<129;i=i*2)
{
PORTB = i;
while(PORTA.F0==0)
{
Delay_ms(3000);
}
beep();
Delay_ms(10);
}
}
}
void beep(void)
{
int i_b;
for(i_b=0;i_b<20;i_b++)
{
PORTA.F1=1;
Delay_ms(300);
PORTA.F1=0;
Delay_ms(300);
}
}
โครงงานที่7
code
int Read_T(void);
void alarm (void);
main()
{
TRISB = 0xF0;
CMCON = 0x07;
while(1)
{
while(Read_T()<10)
{
alarm();
}
}
}
int Read_T(void)
{
int i;
i =0;
TRISA = 0xF0;
PORTA.F0 = 1;
Delay_ms(10);
TRISA = 0xF1;
while(PORTA.F0 == 1)
{
i++;
}
i = i/10;
return i ;
}
void alarm(void)
{
int i ;
PORTB.F1 = 1;
for (i=0;i<200;i++)
{
PORTB.F0=1;
Delay_us(200);
PORTB.F0=0;
Delay_us(200);
}
PORTB.F1=0;
for (i=0;i<200;i++)
{
PORTB.F0;
Delay_us(200);
}
}
โครงงานที่6 ไฟวิ่งปรับความเร็ว หน้า304 เลขที่ 14
code
int Read_VR(void);
main()
{
int i,time;
TRISB =0x00;
CMCON =0x07;
while(1)
{
for(i=1;i<129;i=i*2)
{
PORTB = i;
time = Read_VR()*2;
Vdelay_ms(time);
}
}
}
int Read_VR(void)
{
int i;
i=0;
TRISA= 0xF0;
PORTA.F0 =1;
Delay_ms(10);
TRISA = 0xF1;
while(PORTA.F0 == 1)
{
i++;
}
i=i/10;
return i ;
}
โครงงานที่6
void beep(void);
int display_led(int in);
main()
{
int i,j,time_n;
TRISB = 0x00;
CMCON = 0x07;
TRISA = 0xfd;
while(1)
{
time_n =120;
while(PORTA.F0==1)
{
time_n++;
if(time_n>240)
{
time_n=120;
}
}
j=1;
for(i=1;i<time_n;i++)
{
PORTB = display_led(j);
beep();
Delay_ms(10);
j++;
if(j>6)
{
j = 1;
}
}
}
}
int display_led(int in)
{
switch (in)
{
case 1:return 0b0001000;
case 2:return 0b0010100;
case 3:return 0b0101010;
case 4:return 0b1100011;
case 5:return 0b1101011;
case 6:return 0b1110111;
}
}
void beep ( void )
{
int i_b;
for(i_b=0;i_b<20;i_b++)
{
PORTA.F1=1;
Delay_us(300);
PORTA.F1=0;
Delay_us(300);
}
}
โครงงานที่3
void beep(void);
main()
{
int timel,time2;
TRISB = 0xF0;
while(1)
{
PORTB.F1=0;
PORTB.F2=0;
PORTB.F3=0;
while (~PORTB.F4&PORTB.F5&PORTB.F6)
{
PORTB.F1=1;
PORTB.F2=0;
PORTB.F3=0;
for(timel=0;timel<20000;timel++)
{
while(PORTB.F4&~PORTB.F5&PORTB.F6)
{
PORTB.F1=1;
PORTB.F2=1;
PORTB.F3=0;
for(time2=0;time2<10000;time2++)
{
while(PORTB.F4&PORTB.F5&~PORTB.F6)
{
PORTB.F1=1;
PORTB.F2=1;
PORTB.F3=1;
beep();
Delay_ms(1000);
}
}
}
}
}
}
}
void beep(void)
{
int i ;
for(i=0;i<200;i++)
{
PORTB.F0=1;
Delay_us(200);
PORTB.F0=0;
Delay_us(200);
}
}
โครงงานที่ 2 เครื่องเตือนน้ำล้น
code
int Read_VR(void);
void alarm(void) ;
main()
{
TRISB = 0xF0;
CMCON = 0x07;
while(1)
{
while(Read_VR()<5)
{
alarm();
}
}
}
int Read_VR(void)
{
int i;
i =0;
TRISA = 0xF0;
PORTA.F0 = 1;
Delay_ms(10);
TRISA = 0xF1;
while(PORTA.F0 == 1)
{
i++;
}
i = i/10;
return i;
}
void alarm(void)
{
int i;
PORTB.F1 =1;
for( i=0;i<500;i++)
{
PORTB.F0 =1;
Delay_us(500);
PORTB.F0 =0;
Delay_us(500);
}
PORTB.F1 =0;
for( i=0;i<250;i++)
{
PORTB.F0 =0;
Delay_us(1000);
}
}
โครงงานที่1 วงจรเสียงเตือนกันขโมย
code
void alarm (void);
main()
{
TRISB = 0xFC ;
PORTB.F1 = 0;
while(1)
{
while (PORTB.F2==1)
{
while (PORTB.F3==1)
{
alarm();
}
}
}
}
void alarm (void)
{
int i ;
PORTB.F1 = 1;
for(i=0;i<500;i++)
{
PORTB.F0=1;
Delay_US(500);
PORTB.F0=0;
Delay_US(500);
}
PORTB.F1 = 0;
for(i=0;i<250;i++)
{
PORTB.F0=0;
Delay_us(1000);
}
}
วันพฤหัสบดีที่ 7 กุมภาพันธ์ พ.ศ. 2562
งานที่20 Obstacle Avoiding Robot using PIC Microcontroller
อุปกรณ์
- PIC16F877A
- IR Sensor (2Nos)
- Ultrasonic Sensor (1Nos)
- DC Gear Motor (2Nos)
- L293D Motor Driver
- Chaises (You can also build your own using cardboards)
- Power bank (Any available power source)
code
#include <xc.h>
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#define _XTAL_FREQ 20000000
#define Trigger RB1 //34 is Trigger
#define Echo RB2//35 is Echo
#define Trigger RB1 //34 is Trigger
#define Echo RB2//35 is Echo
int time_taken;
int distance;
int distance;
void back_off() //used to drive the robot backward
{
RC4=1; RC5=0; //Motor 1 reverse
RC6=0; RC7=1; //Motor 2 reverse
__delay_ms(1000);
}
void calculate_distance() //function to calculate distance of US
{
TMR1H =0; TMR1L =0; //clear the timer bits
Trigger = 1;
__delay_us(10);
Trigger = 0;
while (Echo==0);
TMR1ON = 1;
while (Echo==1);
TMR1ON = 0;
time_taken = (TMR1L | (TMR1H<<8));
distance= (0.0272*time_taken)/2;
}
void main()
{
TRISD = 0x00; //PORTD declared as output for interfacing LCD
TRISB1 = 0; //Trigger pin of US sensor is sent as output pin
TRISB2 = 1; //Echo pin of US sensor is set as input pin
TRISB3 = 0; //RB3 is output pin for LED
TRISD2 = 1; TRISD3 = 1; //Both the IR sensor pins are declared as input
TRISC4 = 0; TRISC5 = 0; //Motor 1 pins declared as output
TRISC6 = 0; TRISC7 = 0; //Motor 2 pins declared as output
T1CON=0x20;
while(1)
{
calculate_distance();
if (distance>5)
{
RC4=0; RC5=1; //Motor 1 forward
RC6=1; RC7=0; //Motor 2 forward
}
calculate_distance();
if (RD2==0 && RD3==1 && distance<=5) //Left sensor is blocked
{
back_off();
RC4=1; RC5=1; //Motor 1 stop
RC6=1; RC7=0; //Motor 2 forward
__delay_ms(500);
}
calculate_distance();
if (RD2==1 && RD3==0 && distance<=5) //Right sensor is blocked
{
back_off();
RC4=0; RC5=1; //Motor 1 forward
RC6=1; RC7=1; //Motor 2 stop
__delay_ms(500);
}
calculate_distance();
if (RD2==0 && RD3==0 && distance<=5)//Both sensor is open
{
back_off();
RC4=0; RC5=1; //Motor 1 forward
RC6=1; RC7=1; //Motor 2 stop
__delay_ms(500);
}
calculate_distance();
if (RD2==1 && RD3==1 && distance<=5)//Both sensor is blocked
{
back_off();
RC4=1; RC5=0; //Motor 1 reverse
RC6=1; RC7=1; //Motor 2 stop
__delay_ms(1000);
}
}
}
งานที่19 7 Segment Display Interfacing with PIC Microcontroller
code
#include <xc.h>
//***Define the signal pins of all four displays***//
#define s1 RC0
#define s2 RC1
#define s3 RC2
#define s4 RC3
//***End of definition**////
#define s1 RC0
#define s2 RC1
#define s3 RC2
#define s4 RC3
//***End of definition**////
void main()
{
unsigned int a,b,c,d,e,f,g,h; //just variables
int i = 0; //the 4-digit value that is to be displayed
int flag =0; //for creating delay
{
unsigned int a,b,c,d,e,f,g,h; //just variables
int i = 0; //the 4-digit value that is to be displayed
int flag =0; //for creating delay
unsigned int seg[]={0X3F, //Hex value to display the number 0
0X06, //Hex value to display the number 1
0X5B, //Hex value to display the number 2
0X4F, //Hex value to display the number 3
0X66, //Hex value to display the number 4
0X6D, //Hex value to display the number 5
0X7C, //Hex value to display the number 6
0X07, //Hex value to display the number 7
0X7F, //Hex value to display the number 8
0X6F //Hex value to display the number 9
}; //End of Array for displaying numbers from 0 to 9
0X06, //Hex value to display the number 1
0X5B, //Hex value to display the number 2
0X4F, //Hex value to display the number 3
0X66, //Hex value to display the number 4
0X6D, //Hex value to display the number 5
0X7C, //Hex value to display the number 6
0X07, //Hex value to display the number 7
0X7F, //Hex value to display the number 8
0X6F //Hex value to display the number 9
}; //End of Array for displaying numbers from 0 to 9
//*****I/O Configuration****//
TRISC=0X00;
PORTC=0X00;
TRISD=0x00;
PORTD=0X00;
//***End of I/O configuration**///
TRISC=0X00;
PORTC=0X00;
TRISD=0x00;
PORTD=0X00;
//***End of I/O configuration**///
#define _XTAL_FREQ 20000000
while(1)
{
//***Splitting "i" into four digits***//
a=i%10;//4th digit is saved here
b=i/10;
c=b%10;//3rd digit is saved here
d=b/10;
e=d%10; //2nd digit is saved here
f=d/10;
g=f%10; //1st digit is saved here
h=f/10;
//***End of splitting***//
{
//***Splitting "i" into four digits***//
a=i%10;//4th digit is saved here
b=i/10;
c=b%10;//3rd digit is saved here
d=b/10;
e=d%10; //2nd digit is saved here
f=d/10;
g=f%10; //1st digit is saved here
h=f/10;
//***End of splitting***//
PORTD=seg[g];s1=1; //Turn ON display 1 and print 4th digit
__delay_ms(5);s1=0; //Turn OFF display 1 after 5ms delay
PORTD=seg[e];s2=1; //Turn ON display 2 and print 3rd digit
__delay_ms(5);s2=0; //Turn OFF display 2 after 5ms delay
PORTD=seg[c];s3=1; //Turn ON display 3 and print 2nd digit
__delay_ms(5);s3=0; //Turn OFF display 3 after 5ms delay
PORTD=seg[a];s4=1; //Turn ON display 4 and print 1st digit
__delay_ms(5);s4=0; //Turn OFF display 4 after 5ms delay
__delay_ms(5);s1=0; //Turn OFF display 1 after 5ms delay
PORTD=seg[e];s2=1; //Turn ON display 2 and print 3rd digit
__delay_ms(5);s2=0; //Turn OFF display 2 after 5ms delay
PORTD=seg[c];s3=1; //Turn ON display 3 and print 2nd digit
__delay_ms(5);s3=0; //Turn OFF display 3 after 5ms delay
PORTD=seg[a];s4=1; //Turn ON display 4 and print 1st digit
__delay_ms(5);s4=0; //Turn OFF display 4 after 5ms delay
if(flag>=100) //wait till flag reaches 100
{
i++;flag=0; //only if flag is hundred "i" will be incremented
}
flag++; //increment flag for each flash
}
}
{
i++;flag=0; //only if flag is hundred "i" will be incremented
}
flag++; //increment flag for each flash
}
}
งานที่18 Interfacing Ultrasonic Sensor HC-SR04 with PIC Microcontroller
อุปกรณ์
- PIC16F877A MCU with programming set-up
- LCD 16*2 display
- Ultrasonic sensor (HC-SR04)
- Connecting wires
code
#define _XTAL_FREQ 20000000
#define RS RD2
#define EN RD3
#define D4 RD4
#define D5 RD5
#define D6 RD6
#define D7 RD7
#define Trigger RB1 //34 is Trigger
#define Echo RB2//35 is Echo
#include <xc.h>
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
//LCD Functions Developed by Circuit Digest.
void Lcd_SetBit(char data_bit) //Based on the Hex value Set the Bits of the Data Lines
{
if(data_bit& 1)
D4 = 1;
else
D4 = 0;
if(data_bit& 2)
D5 = 1;
else
D5 = 0;
if(data_bit& 4)
D6 = 1;
else
D6 = 0;
if(data_bit& 8)
D7 = 1;
else
D7 = 0;
}
void Lcd_Cmd(char a)
{
RS = 0;
Lcd_SetBit(a); //Incoming Hex value
EN = 1;
__delay_ms(4);
EN = 0;
}
void Lcd_Clear()
{
Lcd_Cmd(0); //Clear the LCD
Lcd_Cmd(1); //Move the curser to first position
}
void Lcd_Set_Cursor(char a, char b)
{
char temp,z,y;
if(a== 1)
{
temp = 0x80 + b - 1; //80H is used to move the curser
z = temp>>4; //Lower 8-bits
y = temp & 0x0F; //Upper 8-bits
Lcd_Cmd(z); //Set Row
Lcd_Cmd(y); //Set Column
}
else if(a== 2)
{
temp = 0xC0 + b - 1;
z = temp>>4; //Lower 8-bits
y = temp & 0x0F; //Upper 8-bits
Lcd_Cmd(z); //Set Row
Lcd_Cmd(y); //Set Column
}
}
void Lcd_Start()
{
Lcd_SetBit(0x00);
for(int i=1065244; i<=0; i--) NOP();
Lcd_Cmd(0x03);
__delay_ms(5);
Lcd_Cmd(0x03);
__delay_ms(11);
Lcd_Cmd(0x03);
Lcd_Cmd(0x02); //02H is used for Return home -> Clears the RAM and initializes the LCD
Lcd_Cmd(0x02); //02H is used for Return home -> Clears the RAM and initializes the LCD
Lcd_Cmd(0x08); //Select Row 1
Lcd_Cmd(0x00); //Clear Row 1 Display
Lcd_Cmd(0x0C); //Select Row 2
Lcd_Cmd(0x00); //Clear Row 2 Display
Lcd_Cmd(0x06);
}
void Lcd_Print_Char(char data) //Send 8-bits through 4-bit mode
{
char Lower_Nibble,Upper_Nibble;
Lower_Nibble = data&0x0F;
Upper_Nibble = data&0xF0;
RS = 1; // => RS = 1
Lcd_SetBit(Upper_Nibble>>4); //Send upper half by shifting by 4
EN = 1;
for(int i=2130483; i<=0; i--) NOP();
EN = 0;
Lcd_SetBit(Lower_Nibble); //Send Lower half
EN = 1;
for(int i=2130483; i<=0; i--) NOP();
EN = 0;
}
void Lcd_Print_String(char *a)
{
int i;
for(i=0;a[i]!='\0';i++)
Lcd_Print_Char(a[i]); //Split the string using pointers and call the Char function
}
/*****End of LCD Functions*****/
int time_taken;
int distance;
char t1,t2,t3,t4,t5;
char d1,d2,d3;
void Lcd_SetBit(char data_bit) //Based on the Hex value Set the Bits of the Data Lines
{
if(data_bit& 1)
D4 = 1;
else
D4 = 0;
if(data_bit& 2)
D5 = 1;
else
D5 = 0;
if(data_bit& 4)
D6 = 1;
else
D6 = 0;
if(data_bit& 8)
D7 = 1;
else
D7 = 0;
}
void Lcd_Cmd(char a)
{
RS = 0;
Lcd_SetBit(a); //Incoming Hex value
EN = 1;
__delay_ms(4);
EN = 0;
}
void Lcd_Clear()
{
Lcd_Cmd(0); //Clear the LCD
Lcd_Cmd(1); //Move the curser to first position
}
void Lcd_Set_Cursor(char a, char b)
{
char temp,z,y;
if(a== 1)
{
temp = 0x80 + b - 1; //80H is used to move the curser
z = temp>>4; //Lower 8-bits
y = temp & 0x0F; //Upper 8-bits
Lcd_Cmd(z); //Set Row
Lcd_Cmd(y); //Set Column
}
else if(a== 2)
{
temp = 0xC0 + b - 1;
z = temp>>4; //Lower 8-bits
y = temp & 0x0F; //Upper 8-bits
Lcd_Cmd(z); //Set Row
Lcd_Cmd(y); //Set Column
}
}
void Lcd_Start()
{
Lcd_SetBit(0x00);
for(int i=1065244; i<=0; i--) NOP();
Lcd_Cmd(0x03);
__delay_ms(5);
Lcd_Cmd(0x03);
__delay_ms(11);
Lcd_Cmd(0x03);
Lcd_Cmd(0x02); //02H is used for Return home -> Clears the RAM and initializes the LCD
Lcd_Cmd(0x02); //02H is used for Return home -> Clears the RAM and initializes the LCD
Lcd_Cmd(0x08); //Select Row 1
Lcd_Cmd(0x00); //Clear Row 1 Display
Lcd_Cmd(0x0C); //Select Row 2
Lcd_Cmd(0x00); //Clear Row 2 Display
Lcd_Cmd(0x06);
}
void Lcd_Print_Char(char data) //Send 8-bits through 4-bit mode
{
char Lower_Nibble,Upper_Nibble;
Lower_Nibble = data&0x0F;
Upper_Nibble = data&0xF0;
RS = 1; // => RS = 1
Lcd_SetBit(Upper_Nibble>>4); //Send upper half by shifting by 4
EN = 1;
for(int i=2130483; i<=0; i--) NOP();
EN = 0;
Lcd_SetBit(Lower_Nibble); //Send Lower half
EN = 1;
for(int i=2130483; i<=0; i--) NOP();
EN = 0;
}
void Lcd_Print_String(char *a)
{
int i;
for(i=0;a[i]!='\0';i++)
Lcd_Print_Char(a[i]); //Split the string using pointers and call the Char function
}
/*****End of LCD Functions*****/
int time_taken;
int distance;
char t1,t2,t3,t4,t5;
char d1,d2,d3;
int main()
{
TRISD = 0x00; //PORTD declared as output for interfacing LCD
TRISB0 = 1; //DEfine the RB0 pin as input to use as interrupt pin
TRISB1 = 0; //Trigger pin of US sensor is sent as output pin
TRISB2 = 1; //Echo pin of US sensor is set as input pin
TRISB3 = 0; //RB3 is output pin for LED
T1CON=0x20;
{
TRISD = 0x00; //PORTD declared as output for interfacing LCD
TRISB0 = 1; //DEfine the RB0 pin as input to use as interrupt pin
TRISB1 = 0; //Trigger pin of US sensor is sent as output pin
TRISB2 = 1; //Echo pin of US sensor is set as input pin
TRISB3 = 0; //RB3 is output pin for LED
T1CON=0x20;
Lcd_Start();
Lcd_Set_Cursor(1,1);
Lcd_Print_String("Ultrasonic sensor");
Lcd_Set_Cursor(2,1);
Lcd_Print_String("with PIC16F877A");
__delay_ms(2000);
Lcd_Clear();
while(1)
{
TMR1H =0; TMR1L =0; //clear the timer bits
Trigger = 1;
__delay_us(10);
Trigger = 0;
while (Echo==0);
TMR1ON = 1;
while (Echo==1);
TMR1ON = 0;
time_taken = (TMR1L | (TMR1H<<8));
distance= (0.0272*time_taken)/2;
time_taken = time_taken * 0.8;
t1 = (time_taken/1000)%10;
t2 = (time_taken/1000)%10;
t3 = (time_taken/100)%10;
t4 = (time_taken/10)%10;
t5 = (time_taken/1)%10;
d1 = (distance/100)%10;
d2 = (distance/10)%10;
d3 = (distance/1)%10;
Lcd_Set_Cursor(1,1);
Lcd_Print_String("Time_taken:");
Lcd_Print_Char(t1+'0');
Lcd_Print_Char(t2+'0');
Lcd_Print_Char(t3+'0');
Lcd_Print_Char(t4+'0');
Lcd_Print_Char(t5+'0');
Lcd_Set_Cursor(2,1);
Lcd_Print_String("distance:");
Lcd_Print_Char(d1+'0');
Lcd_Print_Char(d2+'0');
Lcd_Print_Char(d3+'0');
}
return 0;
}
Lcd_Set_Cursor(1,1);
Lcd_Print_String("Ultrasonic sensor");
Lcd_Set_Cursor(2,1);
Lcd_Print_String("with PIC16F877A");
__delay_ms(2000);
Lcd_Clear();
while(1)
{
TMR1H =0; TMR1L =0; //clear the timer bits
Trigger = 1;
__delay_us(10);
Trigger = 0;
while (Echo==0);
TMR1ON = 1;
while (Echo==1);
TMR1ON = 0;
time_taken = (TMR1L | (TMR1H<<8));
distance= (0.0272*time_taken)/2;
time_taken = time_taken * 0.8;
t1 = (time_taken/1000)%10;
t2 = (time_taken/1000)%10;
t3 = (time_taken/100)%10;
t4 = (time_taken/10)%10;
t5 = (time_taken/1)%10;
d1 = (distance/100)%10;
d2 = (distance/10)%10;
d3 = (distance/1)%10;
Lcd_Set_Cursor(1,1);
Lcd_Print_String("Time_taken:");
Lcd_Print_Char(t1+'0');
Lcd_Print_Char(t2+'0');
Lcd_Print_Char(t3+'0');
Lcd_Print_Char(t4+'0');
Lcd_Print_Char(t5+'0');
Lcd_Set_Cursor(2,1);
Lcd_Print_String("distance:");
Lcd_Print_Char(d1+'0');
Lcd_Print_Char(d2+'0');
Lcd_Print_Char(d3+'0');
}
return 0;
}
สมัครสมาชิก:
บทความ (Atom)