Professional Documents
Culture Documents
Code Cho Pic Tham Khao
Code Cho Pic Tham Khao
#include <16F877A.h>
#include <def_877a.h>
#FUSES
XT,NOWDT,NOPROTECT,NOLVP
#USE DELAY(CLOCK=4000000)
#BYTE PORTB=0X06
#BYTE PORTC=0X07
#BYTE PORTD=0X08
#define lcd portB
NOTE
Cm
1 void lenh(void)
1 {
1
1
1
1
1
OUTPUT_LOW(PIN_c0);
OUTPUT_LOW(PIN_c1);
RS=0
RW=0
OUTPUT_HIGH(PIN_c2);
OUTPUT_LOW(PIN_c2);
DELAY_MS(5);
E=1
E=0
1 }
2 VOID DU_LIEU(VOID)
2
{
2
OUTPUT_HIGH(PIN_c0); //RS
2
OUTPUT_LOW(PIN_c1); //RW
OUTPUT_HIGH(PIN_c2); // E
OUTPUT_LOW(PIN_c2);
DELAY_MS(5);
2
3
3
3
3
3
3
3
3
3
3
3
3
3
3
void InKey()
{
while(1)
{
OUTPUT_D(0B11111110);
DELAY_US(50);
IF(INPUT(PIN_D4)==0)
{Key=7;break;}
else IF(INPUT(PIN_D5)==0)
{Key=8; break;}
else IF(INPUT(PIN_D6)==0)
{Key=9; break;}
else IF(INPUT(pin_D7)==0)
{Key=10;break;}
3 OUTPUT_D(0B11111101);
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
Tat RS TRUYEN
ch ghi
m d liu
tt d liu
sn sng ghi d liu
Qut hng 1
CHO KHI D4 XUONG 0
CHO KHI D5 XUONG 0
CHO KHI D6 XUONG 0
2
2
2
2
2
2
2
Qut hng 2
DELAY_US(50);
IF(INPUT(pin_D4)==0)
{Key=4; break;}
else if(input(pin_d5)==0)
{Key=5; break;}
else if(input(pin_d6)==0)
{Key=6; break;}
else if(input(pin_d7)==0)
{Key=11; break;}
OUTPUT_D(0B11111011);
DELAY_US(50);
IF(INPUT(PIN_D4)==0)
{Key=1; break;}
else if(input(pin_d5)==0)
{Key=2; break;}
else if(input(pin_d6)==0)
{Key=3; break;}
else if(input(pin_d7)==0)
{Key=12; break;}
OUTPUT_D(0B11110111);
DELAY_US(50);
1
1
1
1
1
1
2
2
2
2
Qut hng 3
Qut hng 4
2
3
3
3
4
4
4
4
4
4
4
3
3
3
3
3
3
3
3
3
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
IF(INPUT(PIN_D4)==0)
{Key=13; break;}
else if(input(pin_d5)==0)
{Key=0; break;}
else if(input(pin_d6)==0)
{Key=14; break;}
else if(input(pin_d7)==0)
{Key=15; break;}
}}
void Scan()
{
switch(Key)
{
case 7:
OUTPUT_B(0X37);
DU_LIEU();
break;
case 8:
OUTPUT_B(0X38);
DU_LIEU();
break;
case 9:
OUTPUT_B(0X39);
DU_LIEU();
break;
case 10:
OUTPUT_B('/');
DU_LIEU();
break;
case 4:
OUTPUT_B(0X34);
DU_LIEU();
break;
case 5:
OUTPUT_B(0X35);
DU_LIEU();
break;
case 6:
OUTPUT_B(0X36);
DU_LIEU();
break;
case 11:
OUTPUT_B('*');
DU_LIEU();
break;
case 1:
OUTPUT_B(0X31);
DU_LIEU();
break;
case 2:
OUTPUT_B(0X32);
DU_LIEU();
break;
case 3:
OUTPUT_B(0X33);
DU_LIEU();
break;
4
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
case 12:
OUTPUT_B('-');
DU_LIEU();
break;
case 13:
OUTPUT_B('O');
DU_LIEU();
break;
case 0:
OUTPUT_B(0X30);
DU_LIEU();
break;
case 14:
OUTPUT_B(0X01);
DU_LIEU();
break;
case 15:
portB='-';
DU_LIEU();
break;
}}
NOTE
Cho hin th dng 1
Cho hin th dng 2
ci t s ban u bng phm nhn
m t ng,nhn phm khng tc dng
hm kim tra c nhn phm?
hm hin th s phm ln LCD
m ci t 3 s thc hin on 3
2 L KHI LNH KIM TRA
V HIN TH NHN PHM
key la dia chi phim can quet 0-15
ham quet phim
qut 9 s theo th t gim dn
th t gim i 1 s
ci t cho hin th s ban u
hin th s tng ng
delay cho hin th
}
dem=0;
While(1)
{
if(input(pin_c3)==0)
{
dem=dem+1;
DELAY_MS(50);
}
OUTPUT_B(0XC8);
LENH();
B[2]=dem/100;
B[1]=(dem%100)/10;
B[0]=dem%10;
OUTPUT_B(0X30+B[2]);
DU_LIEU();
OUTPUT_B(0X30+B[1]);
DU_LIEU();
OUTPUT_B(0X30+B[0]);
DU_LIEU();
if(dem>=Val) dem==0;
}
V tr hin th s m
cm
#define LCD_RS
PIN_D1
#define LCD_RW
PIN_D2
#define LCD_EN
PIN_D3
#define LCD_D4
PIN_D4
#define LCD_D5
PIN_D5
#define LCD_D6
PIN_D6
#define LCD_D7
PIN_D7
1 setup_adc(ADC_CLOCK_INTERNAL);
1 setup_adc_ports(AN0);
1 set_adc_channel(0);
1 delay_us(10);
1 disable_interrupts(INT_AD);
void main()
{
unsigned char str[20];
1 float Value;
unsigned char td;
unsigned char tocdo=0,tocdo1;
set_tris_b(0xFF);
port_b_pullups(1);
output_drive(PIN_C1);
output_drive(PIN_C2);
output_drive(PIN_C6);
output_drive(PIN_C7);
output_low(PIN_E2);
mode : tn s = tn s
chip/16
255: gi tr timer reset
v
1: s ln xy ra ngt
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_16,255,1)
set_pwm1_duty(tocdo);
LCD_Init();
adc_config();
sprintf(str,"DO AN TOT NGHIEP");
delay_ms(10);
LCD_Puts(str);
delay_ms(2000);
LCD_Clear();
sprintf(str,"NhietDo");
LCD_Gotoxy(0,0);
LCD_Puts(str);
sprintf(str,"oC");
LCD_Gotoxy(5,1);
LCD_Puts(str);
sprintf(str,"ST");
LCD_Gotoxy(14,0);
LCD_Puts(str);
#define
#define
#define
#define
#define
STOP
FOW
REV
GIAMTOC
TANGTOC
NOTE
C6 - IN1/ IN4
C7- IN2/ IN3
PIN_B0
PIN_B1
PIN_B2
PIN_B3
PIN_B4
cm
LCD
a
a
a
a
a
a
while(TRUE)
{
set_adc_channel(0);
Value=read_adc();
Value/=2.046;
LCD_Gotoxy(0,1);
sprintf(str,"%05.1f\xdfC",Value);
LCD_Puts(str);
b
b
b
LCD_Gotoxy(8,0);
sprintf(str,"TocDo");
LCD_Puts(str);
c
c
c
LCD_Gotoxy(8,1);
sprintf(str,"%03u%%",td);
LCD_Puts(str);
if(!input(PIN_B0))
{
output_low(PIN_C6);
output_low(PIN_C7);
tocdo1=0;
d
e
e
e
f
f
f
set_pwm1_duty(tocdo1);
sprintf(str," ");
LCD_Gotoxy(14,1);
LCD_Puts(str);
sprintf(str,"ST");
LCD_Gotoxy(14,0);
LCD_Puts(str); }
a
a
a
if(!input(PIN_B1))
{
while(!input(PIN_B1));
su dung ADC voi tan so = tan so dao dong ben trong chip
Xa mn hnh
hin th ch NHIT
Xc nh v tr vit
Vit k t / chui ln LCD
hin th ch NHIT
Xc nh v tr vit
Vit k t / chui ln LCD
hin th ch ST
Xc nh v tr vit
Vit k t / chui ln LCD
b
b
c
c
d
d
d
e
e
output_low(PIN_C6);
output_low(PIN_C7);
tocdo1=0;
set_pwm1_duty(tocdo1);
delay_ms(1000);
output_high(PIN_C6);
output_low(PIN_C7);
sprintf(str,"RU");
LCD_Gotoxy(14,0);
LCD_Puts(str);
sprintf(str,"FO");
LCD_Gotoxy(14,1);
LCD_Puts(str);
delay_ms(1000); }
a
a
a
if(!input(PIN_B2))
{
while(!input(PIN_B2));
output_low(PIN_C6);
output_low(PIN_C7);
tocdo1=0;
set_pwm1_duty(tocdo1);
delay_ms(1000);
output_low(PIN_C6);
output_high(PIN_C7);
sprintf(str,"RU");
LCD_Gotoxy(14,0);
LCD_Puts(str);
sprintf(str,"RE");
LCD_Gotoxy(14,1);
LCD_Puts(str); }
b
b
c
c
d
d
d
e
e
e
A
A
B
if(input(PIN_B4)==0)
{
delay_ms(500);
if(tocdo==255)
{tocdo=250;}
tocdo=tocdo+5;
}
if(input(PIN_B3)==0)
{
delay_ms(500);
A if(tocdo==0)
A {tocdo=5;}
B tocdo=tocdo-5;
}
td=tocdo/2.55;
tocdo1=tocdo;
set_pwm1_duty(tocdo1);
}
}
NOTE
o=0,tocdo1;
2 bin c lp nhau
hin th tc o c theo %
Nhn STOP ; o tt PIN_B0,khng i nh phm
QUAY NGHCH
1 TN TRNG H
2 DH THOI GIAN THUC
3 THU/NGAY/THANG/NAM show_12
GI-PHT-GIY
menu=0
MODE=
0/1
display_12/24
1 minnute INC/DECOK
2 hour
INC/DECOK
3 day
INC/DECOK
4 date
INC/DECOK
5 month
INC/DECOK
6 year
INC/DECOK
tittit(3);
Lcd_init();
hello();
while(1)
{
if (menu!=0) {set_time();}
update_time();
if(show_12==0){display_24();}
else {display_12();}
;
}
}
void update_time()
{
i2c_start();
i2c_write(0xD0);
i2c_write(0x00);
i2c_stop();
I2C_start();
I2C_write(0xD1);
sec = i2c_read(1);
min = i2c_read(1);
hour = i2c_read(1);
dow = i2c_read(1);
date = i2c_read(1);
month = i2c_read(1);
year = i2c_read(0);
i2c_stop();
delay_us(100);
sec1=sec & 0x0F;
sec2=(sec & 0x70)>>4;
min1=min & 0x0F;
min2=(min & 0x70)>>4;
hour1=hour & 0x0F;
hour2=(hour & 0x30)>>4;
day=(dow & 0x07);
date1=date & 0x0F;
date2=(date & 0x30)>>4;
month1=month & 0x0F;
month2=(month & 0x10)>>4;
year1=year & 0x0F;
year2=(year & 0xF0)>>4;
delay_us(10);
}
void update_ds1307(void) {
int8 data=0;
i2c_start();
i2c_write(0xd0);
i2c_write(0x00);
data=sec1+(sec2<<4);
data=data & 0b01111111;
i2c_write(data);
data=min1+(min2<<4);
i2c_write(data);
data=hour1+(hour2<<4);
i2c_write(data);
data=day;
i2c_write(data);
data=date1+(date2<<4);
i2c_write(data);
data=month1+(month2<<4);
i2c_write(data);
data=year1+(year2<<4);
i2c_write(data);
data=0x00;
i2c_write(data);
i2c_stop();
LCD_init();
menu=0;
}
void set_time(void) {
while (menu!=0)
{
if (menu==1) set_min();
if (menu==2) set_hour();
if (menu==3) set_day();
if (menu==4) set_date();
if (menu==5) set_month();
if (menu==6) set_year();
if (menu==7) update_ds1307();
}
}
void set_year(void) {
LCD_init();
while(menu==6) {
if (increase ==0) {
while (increase ==0);
year1++;
if ((year2==9)&&(year1==10)) {year1=year2=0;}
if (year1==10) {year1=0;year2++;}
}
if (decrease==0) {
while (decrease ==0);
year1--;
if ((year2==0)&&(year1==255)) {year1=year2=9;}
if (year1==255) {year1=9;year2--;}
}
Lcd_Setposition(Line_1);
printf(Lcd_putchar," DATE SETING ");
Lcd_Setposition(Line_2);
printf(Lcd_putchar,"Year : 20%u%u",year2,year1);
}}
void set_month(void) {
LCD_init();
while(menu==5){
if (increase==0) {
while (increase==0);
month1++;
if ((month2==1) && (month1==3)) { month2=0;month1=1;}
if (month1==10) { month2++;month1=0;}
}
if (decrease==0) {
while (decrease==0);
month1--;
if ((month2==0) && (month1==0)) {month2=1;month1=2; }
if (month1==0) {month2--;month1=9;}
}
Lcd_Setposition(Line_1);
printf(Lcd_putchar," DATE SETING ");
Lcd_Setposition(Line_2);
printf(Lcd_putchar,"Month : %u%u
",month2,month1);
}
}
/////////////////////////////////////////
void set_date(void) {
LCD_init();
while(menu==4) {
if (increase==0) {
while (increase==0);
date1++;
if ((date2==3) && (date1==2)) { date2=0;date1=1;}
if (date1==10) { date2++;date1=0;}
}
if (decrease==0) {
while (decrease==0);
date1--;
if ((date2==0) && (date1==0)) {date2=3;date1=1; }
if (date1==255) {date2--;date1=9;}
}
Lcd_Setposition(Line_1);
if (hour1==255) {hour2--;hour1=9;}
}
Lcd_Setposition(Line_1);
printf(Lcd_putchar," TIME SETTING ");
Lcd_Setposition(Line_2);
printf(Lcd_putchar,"Hour : %u%u ",hour2,hour1);
}
}
/////////////////////////////////////////
void set_min(void) {
LCD_init();
while (menu==1){
if (increase==0) {
while (increase==0);
min1++;
if ((min2==5) && (min1==10)) { min2=0;min1=0;}
if (min1==10) { min2++;min1=0;}
}
if (decrease==0) {
while (decrease==0);
min1--;
if ((min2==0) & (min1==255)) {min2=5;min1=9; }
if (min1==255) {min2--;min1=9;}
}
Lcd_Setposition(Line_1);
printf(Lcd_putchar," TIME SETTING ");
Lcd_Setposition(Line_2);
printf(Lcd_putchar,"Minute : %u%u ",min2,min1);
}
}
// CHUONG TRINH HIEN THI //
void display_12()
{
int8 h;h = hour2*10+hour1;
if(day==1)
{
Lcd_Setposition(Line_1);
printf(Lcd_putchar,"Cnhat,%u%u/%u%u/20%u%u",date2,date1,month2,month1,year2,year1);
}
else
{
Lcd_Setposition(Line_1);
printf(Lcd_putchar,"THU %u,%u%u/%u%u/20%u%u",day,date2,date1,month2,month1,year2,year1);
}
;
if(h>12)
{
Lcd_Setposition(Line_2);
#BIT mode
= 0x06.0//B0
#BIT increase = 0x06.2//B2
#BIT decrease = 0x06.3//B3
#BIT ok
= 0x06.4//B4
#BIT TIT
= 0x07.2//C2
NOTE
void set_year(void);
void update_time();
void display_12();
void display_24();
void hello();
c nhn mode
DS1307
phat bit start I2C
gui dia chi cua slave
thiet lap lai con tro - set register pointer
phat bit stop I2C
phat bit start I2C
gui lenh doc du lieu
doc du lieu tai dia chi 0x00
doc du lieu tai dia chi 0x01
doc du lieu tai dia chi 0x02
1,year2,year1);
1,year2,year1);
2,month1,month2,year1,year2;
1
2
3
Khai bo th vin
Khai bo th vin
Khai bo th vin
Khai bo th vin
Khai bo th vin
Khai bo th vin
5
6
7
8
9
Khai bo I/O 10
Khai bo I/O
Cc CT con s thc hin 11
Khai bo cc bin 12
signed, hay int
Khai bo cc bin
Khai bo cc bin
Khai bo cc bin
Khai bo Ngt 13
Thc hin chng trnh Ngt
Khai bo s dng ADC 14
18
Khi to LCD 19
Khi to ADC nu cha c 20
Hin th ban u cho LCD 21
c nhit 22
tnh gi tr nhit
23
Cc chng trnh ph 24
F1
F2
F3
F
F
#use delay(clock=20000000)
N1
#include "adc.c"
N2
#include <lcd_lib_4bit.c>
#use i2c(Master,Slow,sda=PIN_C4,scl=PIN_C3) N3
#BIT
#define
mode
STOP
N1
N2
= 0x06.0
PIN_B0
void InKey();
CONST UNSIGNED CHAR
N1
N1
DIG[]={"Set: #"};
N1
N2
N3
N4
N5
N6
#INT_theo y/c
void ngat()
N1
N2
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc_ports(AN0);
set_adc_channel(0);
N1
N2
N3
na F
set_tris_C(0x00);
N1
N2
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
set_timer1(3035);
enable_interrupts(INT_TIMER1);
setup_ccp1(CCP_PWM);//set up pwm 1
setup_timer_2(T2_DIV_BY_16,255,1); //1465hz
set_pwm1_duty(duty);
enable_interrupts(GLOBAL);
N1
N2
F
N1
GIE_bit=1;
INTE_bit=1;
N2
F
N3
N4
LCD_Init();
adc_config()
sprintf(str,"Hello Tomorrow");
delay_ms(10);
LCD_Puts(str);
delay_ms(2000);
LCD_Clear();
sprintf(str,"Welcome");
LCD_Gotoxy(0,0);
LCD_Puts(str);
OUTPUT_B(0X38);
LENH();
OUTPUT_B(0X0E);
LENH();
OUTPUT_B(0X80);
LENH();
value=(float)read_adc();
value = (value - 558.5)/2.048;
// For 5V supply
// value = (value - 754.8)/2.048; // For 3.7V Supply
// value = (value - 698.2)/2.048; // For 4V supply
{ _____();}
}
Void ____()
c th while( )
Tn s s dng
Chn
knh tng ng
mc 1: vo
mc 0: ra
Khi to LCD
Khi to ADC nu pha trn cha khai bo
Vit k t ln mn hnh
Xut lnh( xc nh v tr)
xa mn hnh
xc nh v tr
xut lnh
// 38 Hai dong v ma tran 5x7
// 0E Bat hien thi,nhap nhap con tro
// 80 Ep con tro dau dong thu nhat
void thoigian(void)
{
s2++;
if(s2==10)
{
s1++;
s2=0;
if(s1==10)
{
s2=0;
s1=0;
s3++;
}
}
if(s3==10)
{
s4++;
s3=0;
if(s4==10)
{
s1=0;
s2=0;
s3=0;
s4=0;
}
}
if(count==15)
{
d1++;
if(d1==10)
{
d2++;
d1=0;
}
if(d2==10)
{
d1=0;
d2=0;
}}}
//HAM CHONG DOI BAN PHIM
void debounce(void)
{
int db_cntrl1 , db_cntrl2; // Bien su dung cho vong lap chong doi.
for (db_cntrl1=0;db_cntrl1<=0x0f;db_cntrl1++)
{
for(db_cntrl2=0;db_cntrl2<=0x0f;db_cntrl2++)
{}
}}
//HAM NGAT
void interrupt isr(void)
{
debounce();
if(T0IF && T0IE) // Kiem tra cho phep ngat va co ngat duoc kich hoat (ba
{
TMR0 =255;
T0IF = 0; // Xoa co ngat ngoai.
thoigian(); // timer la sao de cho duoc 1s
count++;
if(count==16)
count=1;
}}
void init_Timer1(void) //HAM KHOI TAO CHO TIMER0
{
TMR0 =254; // CHO TIMER 0 HOAT DONG TU 0;
T0CS=1; // Chon xung clock la TOCKI
T0SE=0; // Tang tu timer0 den tran
PSA = 1; // KHONG SU DUNG BO CHI TI LE LA TIMER0
PS0 = 0;
PS1 = 0;
PS2 = 0;
}
OI BAN PHIM
NOTE
Cm
NOTE
#include <pic.h>
#include <htc.h>
#include <math.h>
#include "lcd4x20.h"
// Cau hinh su dung.
__CONFIG(HS & WDTDIS & PWRTEN & UNPROTECT & BORDIS & LV
#define _XTAL_FREQ 4000000
const unsigned char A[] = "DEM SLSP T.THUNG";
char B[16] = {' ',' ',' ',' ',0x00,0x00,':','1','5',':',0x00,0x00,' ',' ',' ',' '};
char so[10] = {0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
unsigned char count = 1; // Khai bao va dat gia tri ban dau cho bien dem
int s1,s2,s3,s4;
int p1,p2;
int h1,h2;
d1=0;
d2=0;
s1=0;
s2=0;
s3=0;
s4=0;
void main()
{
init_PORTD();
init_PORTC();
init_PORTB();
lcd_init();
debounce();
init_Timer1(); // Khoi tao Timer1.
ADCON1 = 1;
PCFG0 = 0;
PCFG1 = 0;
PCFG2 = 1;
PCFG3 = 0;
// ANSEL = 0; // Cau hinh tat ca chan Analog la chan Digital I/O.
// ANSELH = 0;
TRISA = 0X00; // Cau hinh PortE, ket noi LED tai RE1 va RE2
PORTA = 0xFF; //Tat cac LED RE1 va RE2.
T0IE = 1; // CHO PHEP NGAT TIMER0.
T0IF = 0; // XOA CO NGAT TIMER0.
GIE = 1; // Cho phep ngat toan cuc.
while(1)
{
//Xuat cac ky tu co dinh tren LCD
lcd_gotoxy(0,0);
for(int i=0;i<=15;i++)
b_cntrl1<=0x0f;db_cntrl1++)
lcd_putc(A[i]);
lcd_gotoxy(5,1);
lcd_putc(B[6]);
lcd_gotoxy(8,1);
lcd_putc(B[9]);
db_cntrl2<=0x0f;db_cntrl2++)
#include <stddef.h>
#define LCD_Light PIN_D0
#define LCD_RS PIN_D1
#define LCD_RW PIN_D2
#define LCD_EN PIN_D3
rs = 0: Command - rs = 1: Data.
RS=0: Bt ch truyn lnh
RW=0 : Bt ch d ghi
LCD_PulseEnable();
}
#separate void LCD_PutChar ( unsigned int cX )
{
/* this subroutine works specifically for 4-bit Port A */
output_high ( LCD_RS );
LCD_PutCmd( cX );
output_low ( LCD_RS );
}
#separate void LCD_PutCmd ( unsigned int cX )
{
/* this subroutine works specifically for 4-bit Port A */
LCD_SetData ( swap ( cX ) ); /* send high nibble */
LCD_PulseEnable();
LCD_SetData ( swap ( cX ) ); /* send low nibble */
LCD_PulseEnable();
}
#separate void LCD_PulseEnable ( void )
{
output_high ( LCD_EN );
delay_us ( 50); // was 10
output_low ( LCD_EN );
delay_ms ( 1); // was 5
}
#separate void LCD_SetData ( unsigned int cX )
{
output_bit ( LCD_D4, cX & 0x01 );
output_bit ( LCD_D5, cX & 0x02 );
output_bit ( LCD_D6, cX & 0x04 );
output_bit ( LCD_D7, cX & 0x08 );
}
NOTE
void quet1()
{
if(ra0==0) //dng 1 gim t 50 xung,dng 2 khng nhy
{
while(ra0==0)
{}
x=x-1;
}
if(ra1==0) //dng 1 khng nhy,dng 2 t 0 tng ln
{
while(ra1==0)
{}
y=y+1;
}
}
datawrt();
portd = 0x0C0;
comnwrt();
portd= 's';
datawrt();
portd = 'o';
datawrt();
portd = ' ';
datawrt();
portd = 's';
datawrt();
portd = 'p';
datawrt();
portd = ' ';
datawrt();
portd = 'c';
datawrt();
portd = 'a';
datawrt();
portd = 'o';
datawrt();
portd = ':';
datawrt();
while (1)
{
quet();
tram1=(x/100+48);
chuc1=(x/10%10+48);
dv1=(x%10+48);
portd=0x08B;
comnwrt();
portd= tram1;
datawrt();
portd= chuc1;
datawrt();
portd= dv1;
datawrt();
tram2=(y/100+48);
chuc2=(y/10%10+48);
dv2=(y%10+48);
portd=0x0CB;
comnwrt();
portd= tram2;
datawrt();
portd= chuc2;
datawrt();
portd= dv2;
datawrt();
}
}
NOTE
a ch port E
a ch port A
a ch port D
a ch chn
a ch chn
a ch chn
a ch chn
a ch chn
Khai bo bin m
ram2=0,chuc2=0,dv2=0;
X dng 1 THP
Y dng 2 CAO
MAIN.C
#include <main.h>
#include <var.h>
#include "lcd.c"
#include "adc.c"
#BYTE PORTB=0x06
#BIT B4=PORTB.4
ADC.C
#ifndef __ADC_
#define __ADC_
LCD.C
def_877a.h
#define W 0
#define F 1
void adc_config()
#endif
MAIN.H
#ifndef _MAIN_
#define _MAIN_
/***************KHAI BAO CAC THU VIEN*****************/
#include <16F877A.h>
/**************KHAI BAO CAU HINH FUSE BIT************/
///////////////////////////////////////////////////////
// Voi moi muc phai chon mot trong danh sach dua ra //
//_________________POINTER AND ADC____________________
#device *=16 adc=10
//#device *=16 adc=8
//___________________CLOCK____________________________
#use delay(clock=24000000)
// Su dung tan so 24Mhz, khong reset watch dog khi goi den ham delay.
//#use delay(clock=20000000,RESTART_WDT) // Su dung tan so 20Mhz, reset watch dog khi goi den ham delay.
//________________FAST_STANDAR I/O PORTA___________________
#use FAST_IO(A)
// Thiet lap che do fast I/O cho PORTA, yeu cau phai chi ro huong Vao/Ra
// cho cac chan I/O. cac ham Input,Output se chi su dung 1 chu ky lenh
//#use STANDARD_IO(A) // Thiet lap che do chuan I/O cho PORTA, khong yeu cau phai chi ro huong Vao/Ra
// cho cac chan I/O. cac ham Input,Output se su dung 3-4 chu ky lenh
//#use FIXED_IO(A)
//
//________________FAST_STANDAR I/O PORTB___________________
#use FAST_IO(B)
// Thiet lap che do fast I/O cho PORTB, yeu cau phai chi ro huong Vao/Ra
// cho cac chan I/O. cac ham Input,Output se chi su dung 1 chu ky lenh
//#use STANDARD_IO(B) // Thiet lap che do chuan I/O cho PORTB, khong yeu cau phai chi ro huong Vao/Ra
// cho cac chan I/O. cac ham Input,Output se su dung 3-4 chu ky lenh
//#use FIXED_IO(B)
//
//________________FAST_STANDAR I/O PORTC____________________
#use FAST_IO(C)
// Thiet lap che do fast I/O cho PORTC, yeu cau phai chi ro huong Vao/Ra
// cho cac chan I/O. cac ham Input,Output se chi su dung 1 chu ky lenh
//#use STANDARD_IO(C) // Thiet lap che do chuan I/O cho PORTC, khong yeu cau phai chi ro huong Vao/Ra
// cho cac chan I/O. cac ham Input,Output se su dung 3-4 chu ky lenh
//#use FIXED_IO(C)
//
//________________FAST_STANDAR I/O PORTD_____________________
#use FAST_IO(D)
// Thiet lap che do fast I/O cho PORTD, yeu cau phai chi ro huong Vao/Ra
// cho cac chan I/O. cac ham Input,Output se chi su dung 1 chu ky lenh
//#use STANDARD_IO(D) // Thiet lap che do chuan I/O cho PORTD, khong yeu cau phai chi ro huong Vao/Ra
// cho cac chan I/O. cac ham Input,Output se su dung 3-4 chu ky lenh
//#use FIXED_IO(D)
//
//_______________FAST_STANDAR I/O PORTE___________________
#use FAST_IO(E)
// Thiet lap che do fast I/O cho PORTE, yeu cau phai chi ro huong Vao/Ra
// cho cac chan I/O. cac ham Input,Output se chi su dung 1 chu ky lenh
//#use STANDARD_IO(E) // Thiet lap che do chuan I/O cho PORTE, khong yeu cau phai chi ro huong Vao/Ra
// cho cac chan I/O. cac ham Input,Output se su dung 3-4 chu ky lenh
//#use FIXED_IO(E)
//
bits------------------
//________________________CASE_____________________________
#CASE
// Phan biet chu hoa va chu thuong trong khi lap trinh code
/************** DINH NGHIA CAC CHAN I/O ************/
//______________________I/O LCD____________________________
#define LCD_RS PIN_D1
#define LCD_RW PIN_D2
#define LCD_EN PIN_D3
#define LCD_D4 PIN_D4
#define LCD_D5 PIN_D5
#define LCD_D6 PIN_D6
#define LCD_D7 PIN_D7
#endif
bits------------------
---------------------------------------------------------
---------------------------------------------------------
s ---------------------------------------------------------
s ---------------------------------------------------------
CON bits--------------------------
P1CON bits--------
STA bits-------------------
---CCP2CON bits----------
Bits --------------------------------------------------------
eg Bits --------------------------------------------------------
RISA bits---------------------------
RISB bits-----------------------------
ISC bits-------------------
---------------------------------------------------------
----------------------------------------------------
----------------------------------------------------------
ON2 bits---------------------------------------------
-SSPSTAT bits------------------------
STA bits-----------------
ON1 bits---------------
N1 bits----------
VAR.H
#ifndef _VAR_
#define _VAR_
//const unsigned char chu_so[10] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
//gia tri tren led 7 thanh
0 1 2
//const unsigned char OFF_LED=0xff;
//unsigned char buffer[3];
#endif
5 6
7 8
MAIN.C
#include <16f877a.h>
#include <defs_16f877a.h>
#fuses NOWDT, NOPROTECT, NOLVP, XT, PUT
#use delay(clock=4000000)
#define RS RE0
#define RW RE1
#define E RE2
th vin pic
Register Files
main.h
xc nh chn