SlideShare una empresa de Scribd logo
1 de 7
1. Code đo nhiệt độ dùng LM35+PIC16F877A+hiển thị tiếng Việt trên LCD
#define<16f877a.h>
#include"16f877A.h"
#device adc=8
#use delay(clock=12000000)
#FUSES NOWDT,HS,NOPUT,NOPROTECT,NODEBUG,NOBROWNOUT,NOLVP,NOCPD,NOWRT
#include<lcd.c>
int8 read;
int8 ketqua[10];
int8 dem[10];
byte const CGRAM[24]={ 0X0E,0X00,0X0E,0X11,0X1F,0X10,0X0E,0X04,
0X01,0X03,0X01,0X07,0X09,0X09,0X07,0X00,
0X0E,0X00,0X0E,0X11,0X11,0X11,0X0E,0x04,};
byte const LED[10]={ 0b00000011,
0b10011111,
0b00100101,
0b00001101,
0b10011001,
0b01001001,
0b01000001,
0b00011111,
0b00000001,
0b00001001,
};
void main()
{
int8 i,j,max,tam;
int8 solan;
set_tris_a(0xFF);
set_tris_d(0x00);
set_tris_b(0x00);
set_tris_c(0x00);
lcd_init();
lcd_send_byte(0,0x2D);// viet cau lenh lcd 4 bit , 2 hang , 5*10dots
lcd_send_byte(0,0x40); // viet cau lenh , ra lenh cho lcd chi toi cgram
//lcd_send_byte(0,0x07);// Dich sang trai khi co hanh dong ghi vao CGRAM
for(i=0;i<24;i++)
lcd_send_byte(1,CGRAM[I]);// viet du lieu vao cgram co dia chi la
0x00 ++
while(TRUE)
{
setup_adc_ports(RA0_RA1_ANALOG_RA3_REF);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
for(i=0;i<10;i++)
{
if(adc_done())
{
ketqua[i]=read_adc();
delay_us(20);
}
}
setup_adc( ADC_OFF ); //Sau khi lay 10 gia tri xong thi tat ADC
for(i=0;i<10;i++)
{
dem[i]=0;
for(j=0;j<9;j++)
{
if(ketqua[i]==ketqua[j])
dem[i]++;
}
}
tam=dem[0];
max=ketqua[0];
for(i=0;i<9;i++)
{
if(tam<=dem[i+1])
{
tam=dem[i+1];
max=ketqua[i+1];
}
}
lcd_gotoxy(1,1);
max=max/0.51;
printf(lcd_putc,"Nhiet do max: %u",max);
delay_us(20);
//ting trung binh
tam=0;
for(i=0;i<10;i++)
{
tam=tam+ketqua[i];
}
max=tam/10/0.51;
LCD_GOTOXY(3,0);
LCD_SEND_BYTE(1,'N');
LCD_SEND_BYTE(1,'h');
LCD_SEND_BYTE(1,'i');
LCD_SEND_BYTE(1,0x00);
LCD_SEND_BYTE(1,'t');
LCD_SEND_BYTE(1,' ');
LCD_SEND_BYTE(1,0x01);
LCD_SEND_BYTE(1,0x02);
LCD_SEND_BYTE(1,' ');
LCD_SEND_BYTE(1,'T');
LCD_SEND_BYTE(1,'B');
LCD_SEND_BYTE(1,':');
printf(lcd_putc,"%u",max);
lcd_send_byte(0,0x1c);
delay_ms(1000);
}
}
2. Code đo nhiệt độ dùng LM35+PIC16F877A+Hiển thị LED 7 đoạn
#define<16f877a.h>
#include"16f877A.h"
#device adc=8
#use delay(clock=12000000)
#FUSES NOWDT,HS,NOPUT,NOPROTECT,NODEBUG,NOBROWNOUT,NOLVP,NOCPD,NOWRT
int8 read;
byte const LED[10]={ 0b00001001,//0
0b00111111,//1
0b10010001,//2
0b00010011,//3
0b00100111,//4
0b01000011,//5
0b01000001,//6
0b00011111,//7
0b00000000,//8
0b00000010,//9
};
void main()
{
int8 chuc,donvi;
int1 done;
int8 i;
set_tris_a(0xFF);
set_tris_d(0x00);
set_tris_b(0x00);
set_tris_c(0x00);
setup_adc_ports(RA0_RA1_ANALOG_RA3_REF);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
while(TRUE)
{
setup_adc(ADC_START_ONLY);
done=adc_done();
while(!done)
{
done=adc_done();
}
delay_ms(100);
READ=READ_ADC();
for(i=0;i=200;i++)
{
chuc=read/10;
donvi=read%10;
output_low(pin_c0);
output_b(led[chuc]);
delay_ms(10);
output_high(pin_c0);
output_low(pin_c1);
output_b(led[donvi]);
delay_ms(10);
output_high(pin_c1);
}
}
}
3. Mạch đo nhiệt độ Điều khiển động cơ, hiển thị LCD
Code:
#include <16F877A.h>
#include <def_877a.h>
#device *=16 ADC=10
#include <lcd.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH)
(>10mhz for PCD)
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or
B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected
#FUSES RESERVED //Used to set the reserved FUSE bits
#use delay(clock=20000000)
int8 mode,tmin,tmax; // Khai bao cac bien dung trong ct
int16 ct;
int1 bat,anphim,docnhietdo;
int8 cnt=0;
void readkb() // Ham phat hien phim bam
{
anphim=0;
if(INPUT(PIN_C0)==0) // Neu phim 1 duoc an (phim chon mode)
{
delay_ms(150); // Ham delay chong rung phim
while(INPUT(PIN_C0)==0); // Cho den khi nut duoc tha ra
mode++; // Tang mode len 1 de chuyen sang mode ke tiep
if(mode==3) mode=0; // Lap lai mode 0 khi da chuyen qua het cac
mode
anphim=1;
}
else
{
if(INPUT(PIN_C1)==0) // Neu phim 2 duoc an (phim giam)
{
delay_ms(150); // Chong rung phim (cho phim duoc an trong 1 luc)
if(INPUT(PIN_C1)==0)
{
if(mode==1)
{ if(tmin>0) tmin--; anphim=1; } // Giam nguong tuy theo
mode, "tmin" >0 thì giảm 1
if(mode==2)
{ if(tmax>tmin+1) tmax--; anphim=1; } // de "tmax" không nhỏ hơn
"tmin" +1
}
}
else
{
if(INPUT(PIN_C2)==0) // Neu phim 3 duoc an (phim tang)
{
delay_ms(150); // Cho phim duoc an trong 1 luc
if(INPUT(PIN_C2)==0)
{
if(mode==1)
{
if(tmin<tmax-1) tmin++; anphim=1; // Tang
nguong tuy theo mode. Tmin khong the >tmax-1
}
if(mode==2)
{
if(tmax<90) tmax++; anphim=1; // tmax khong lon
hon 90do.
}
}
}
else
if(INPUT(PIN_C3)==0) // Neu phim 4 duoc an (phim bat/tat)
{
delay_ms(150);
while(INPUT(PIN_C3)==0); // Cho den khi nut duoc tha ra
if (ct>=tmin&&ct<=tmax) // Neu nhiet do nam trong
khoang giua 2 nguong
{ // thi nut 4 co chuc
nang bat/tat quat
bat=~bat;
if(bat==1) OUTPUT_BIT(PIN_D0,1);
if(bat==0) OUTPUT_BIT(PIN_D0,0);
anphim=1;
}
}
}
}
}
void check() // Ham kiem tra nguong (tat quat neu nhiet do duoi
tmin
{ // va bat quat neu nhiet do tren tmax)
if (ct<tmin) { OUTPUT_BIT(PIN_D0,0); bat=0; }
if (ct>tmax) { OUTPUT_BIT(PIN_D0,1); bat=1; }
}
void hiennhietdo() // Ham hien thi nhiet do do duoc tu LM35
{
int8 t,chuc,donvi;
setcursor(0,0);
wrtchar("- DO NHIET DO -"); // Hien thi dong tren
t = ct;
chuc = t/10 + 48; // Tach so hang chuc va hang don vi
donvi = t%10 + 48;
setcursor(0,1);
wrtchar("NHIET DO: "); // Hien thi so do nhiet do len LCD
wrtchar(chuc);
wrtchar(donvi);
wrtchar("*C");
delay_ms(50);
}
void settmin() // Ham cai dat nguong nhiet do min
{
int8 t,chuc,donvi;
setcursor(0,0);
wrtchar("- HEN TAT QUAT -");
t = tmin; // Tach hang chuc va don vi cua tmin va hien thi ra
LCD
chuc = t/10 + 48;
donvi = t%10 + 48;
setcursor(0,1);
wrtchar("NEU DUOI: ");
wrtchar(chuc);
wrtchar(donvi);
wrtchar("*C");
delay_ms(50);
}
void settmax() // Ham cai dat nguong nhiet do max (tuong tu ham setmin)
{
int8 t,chuc,donvi;
setcursor(0,0);
wrtchar("- HEN BAT QUAT -");
t = tmax;
chuc = t/10 + 48;
donvi = t%10 + 48;
setcursor(0,1);
wrtchar("NEU TREN: ");
wrtchar(chuc);
wrtchar(donvi);
wrtchar("*C");
delay_ms(50);
}
void selectmode() // Ham lua chon mode hien thi
{
switch (mode)
{
case 0: hiennhietdo(); break;
case 1: settmin(); break;
case 2: settmax(); break;
}
}
#INT_TIMER1 // Khai bao su dung ngat timer1
void ct_phuc_vu_ngat()
{
CLEAR_INTERRUPT(INT_TIMER1); // Xoa co ngat timer1
DISABLE_INTERRUPTS(GLOBAL); // Khong cho phep ngat khac
cnt++; // Sau moi ngat ta tang bien dem
if(cnt==10) // khi cnt=10 thi da dem duoc 10 lan tuong ung voi
1s
{
docnhietdo=1;
ct = READ_ADC();
ct = ct*0.49;
cnt=0;
}
else docnhietdo=0;
SET_TIMER1(3035);
ENABLE_INTERRUPTS(GLOBAL);
}
void main() // Ham ct chinh
{
Setup_ADC(ADC_CLOCK_INTERNAL); // Cai dat bo chuyen doi ADC
setup_adc_ports(AN0); // Ngo vao Analog la chan AN0
SET_ADC_CHANNEL(0); // Chon kenh chuyen doi ADC la kenh 0
delay_us(20);
mode = 0; // Khoi tao ban dau voi tmin=20*C va tmax=27*C
tmin = 20; // va hien thi mode do nhiet do dau tien
tmax = 27;
lcd_init(); // Khoi tao LCD
wrtcmd(0x01);
wrtcmd(0x28);
wrtcmd(0x0c);
SETUP_TIMER_1(T1_INTERNAL|T1_DIV_BY_8); // Cai dat timer1 voi bo chia
tan 1:8
SET_TIMER1(3035); // Gia tri khoi tao cho timer1 ( dem den 65535
// voi clock 20Mhz chia 8 => mot ngat tuong ung voi
0.1s )
ENABLE_INTERRUPTS(INT_TIMER1); // Cho phep ngat timer1
ENABLE_INTERRUPTS(GLOBAL); // Cho phep ngat toan cuc
ct = READ_ADC(); // Doc gia tri nhiet do dau tien
ct = ct*0.49;
while(1) // Vong lap vo han
{
selectmode();
do
{
if(docnhietdo==1) // Neu nhiet do duoc doc thi hien thi
ra LCD neu dang
{ // o mode hien thi va tien hanh kiem
tra nguong
if(mode==0) { hiennhietdo(); }
check(); // Kiem tra nguong
}
readkb();
}
while(anphim==0); // Lien tuc lap lai viec kiem tra xem nhiet
do
} // co duoc doc hay ko va kiem tra phim bam
}

Más contenido relacionado

Destacado

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Destacado (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Code pic

  • 1. 1. Code đo nhiệt độ dùng LM35+PIC16F877A+hiển thị tiếng Việt trên LCD #define<16f877a.h> #include"16f877A.h" #device adc=8 #use delay(clock=12000000) #FUSES NOWDT,HS,NOPUT,NOPROTECT,NODEBUG,NOBROWNOUT,NOLVP,NOCPD,NOWRT #include<lcd.c> int8 read; int8 ketqua[10]; int8 dem[10]; byte const CGRAM[24]={ 0X0E,0X00,0X0E,0X11,0X1F,0X10,0X0E,0X04, 0X01,0X03,0X01,0X07,0X09,0X09,0X07,0X00, 0X0E,0X00,0X0E,0X11,0X11,0X11,0X0E,0x04,}; byte const LED[10]={ 0b00000011, 0b10011111, 0b00100101, 0b00001101, 0b10011001, 0b01001001, 0b01000001, 0b00011111, 0b00000001, 0b00001001, }; void main() { int8 i,j,max,tam; int8 solan; set_tris_a(0xFF); set_tris_d(0x00); set_tris_b(0x00); set_tris_c(0x00); lcd_init(); lcd_send_byte(0,0x2D);// viet cau lenh lcd 4 bit , 2 hang , 5*10dots lcd_send_byte(0,0x40); // viet cau lenh , ra lenh cho lcd chi toi cgram //lcd_send_byte(0,0x07);// Dich sang trai khi co hanh dong ghi vao CGRAM for(i=0;i<24;i++) lcd_send_byte(1,CGRAM[I]);// viet du lieu vao cgram co dia chi la 0x00 ++ while(TRUE) { setup_adc_ports(RA0_RA1_ANALOG_RA3_REF); setup_adc(ADC_CLOCK_INTERNAL); set_adc_channel(0); for(i=0;i<10;i++) { if(adc_done()) { ketqua[i]=read_adc(); delay_us(20); } } setup_adc( ADC_OFF ); //Sau khi lay 10 gia tri xong thi tat ADC for(i=0;i<10;i++) { dem[i]=0; for(j=0;j<9;j++)
  • 2. { if(ketqua[i]==ketqua[j]) dem[i]++; } } tam=dem[0]; max=ketqua[0]; for(i=0;i<9;i++) { if(tam<=dem[i+1]) { tam=dem[i+1]; max=ketqua[i+1]; } } lcd_gotoxy(1,1); max=max/0.51; printf(lcd_putc,"Nhiet do max: %u",max); delay_us(20); //ting trung binh tam=0; for(i=0;i<10;i++) { tam=tam+ketqua[i]; } max=tam/10/0.51; LCD_GOTOXY(3,0); LCD_SEND_BYTE(1,'N'); LCD_SEND_BYTE(1,'h'); LCD_SEND_BYTE(1,'i'); LCD_SEND_BYTE(1,0x00); LCD_SEND_BYTE(1,'t'); LCD_SEND_BYTE(1,' '); LCD_SEND_BYTE(1,0x01); LCD_SEND_BYTE(1,0x02); LCD_SEND_BYTE(1,' '); LCD_SEND_BYTE(1,'T'); LCD_SEND_BYTE(1,'B'); LCD_SEND_BYTE(1,':'); printf(lcd_putc,"%u",max); lcd_send_byte(0,0x1c); delay_ms(1000); } } 2. Code đo nhiệt độ dùng LM35+PIC16F877A+Hiển thị LED 7 đoạn #define<16f877a.h> #include"16f877A.h" #device adc=8 #use delay(clock=12000000) #FUSES NOWDT,HS,NOPUT,NOPROTECT,NODEBUG,NOBROWNOUT,NOLVP,NOCPD,NOWRT int8 read; byte const LED[10]={ 0b00001001,//0 0b00111111,//1 0b10010001,//2 0b00010011,//3 0b00100111,//4 0b01000011,//5 0b01000001,//6 0b00011111,//7 0b00000000,//8
  • 3. 0b00000010,//9 }; void main() { int8 chuc,donvi; int1 done; int8 i; set_tris_a(0xFF); set_tris_d(0x00); set_tris_b(0x00); set_tris_c(0x00); setup_adc_ports(RA0_RA1_ANALOG_RA3_REF); setup_adc(ADC_CLOCK_INTERNAL); set_adc_channel(0); while(TRUE) { setup_adc(ADC_START_ONLY); done=adc_done(); while(!done) { done=adc_done(); } delay_ms(100); READ=READ_ADC(); for(i=0;i=200;i++) { chuc=read/10; donvi=read%10; output_low(pin_c0); output_b(led[chuc]); delay_ms(10); output_high(pin_c0); output_low(pin_c1); output_b(led[donvi]); delay_ms(10); output_high(pin_c1); } } } 3. Mạch đo nhiệt độ Điều khiển động cơ, hiển thị LCD Code: #include <16F877A.h> #include <def_877a.h> #device *=16 ADC=10 #include <lcd.h> #FUSES NOWDT //No Watch Dog Timer #FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD) #FUSES NOPUT //No Power Up Timer #FUSES NOPROTECT //Code not protected from reading #FUSES NODEBUG //No Debug mode for ICD #FUSES NOBROWNOUT //No brownout reset #FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O #FUSES NOCPD //No EE protection #FUSES NOWRT //Program memory not write protected #FUSES RESERVED //Used to set the reserved FUSE bits
  • 4. #use delay(clock=20000000) int8 mode,tmin,tmax; // Khai bao cac bien dung trong ct int16 ct; int1 bat,anphim,docnhietdo; int8 cnt=0; void readkb() // Ham phat hien phim bam { anphim=0; if(INPUT(PIN_C0)==0) // Neu phim 1 duoc an (phim chon mode) { delay_ms(150); // Ham delay chong rung phim while(INPUT(PIN_C0)==0); // Cho den khi nut duoc tha ra mode++; // Tang mode len 1 de chuyen sang mode ke tiep if(mode==3) mode=0; // Lap lai mode 0 khi da chuyen qua het cac mode anphim=1; } else { if(INPUT(PIN_C1)==0) // Neu phim 2 duoc an (phim giam) { delay_ms(150); // Chong rung phim (cho phim duoc an trong 1 luc) if(INPUT(PIN_C1)==0) { if(mode==1) { if(tmin>0) tmin--; anphim=1; } // Giam nguong tuy theo mode, "tmin" >0 thì giảm 1 if(mode==2) { if(tmax>tmin+1) tmax--; anphim=1; } // de "tmax" không nhỏ hơn "tmin" +1 } } else { if(INPUT(PIN_C2)==0) // Neu phim 3 duoc an (phim tang) { delay_ms(150); // Cho phim duoc an trong 1 luc if(INPUT(PIN_C2)==0) { if(mode==1) { if(tmin<tmax-1) tmin++; anphim=1; // Tang nguong tuy theo mode. Tmin khong the >tmax-1 } if(mode==2) { if(tmax<90) tmax++; anphim=1; // tmax khong lon hon 90do. } } } else if(INPUT(PIN_C3)==0) // Neu phim 4 duoc an (phim bat/tat) { delay_ms(150); while(INPUT(PIN_C3)==0); // Cho den khi nut duoc tha ra
  • 5. if (ct>=tmin&&ct<=tmax) // Neu nhiet do nam trong khoang giua 2 nguong { // thi nut 4 co chuc nang bat/tat quat bat=~bat; if(bat==1) OUTPUT_BIT(PIN_D0,1); if(bat==0) OUTPUT_BIT(PIN_D0,0); anphim=1; } } } } } void check() // Ham kiem tra nguong (tat quat neu nhiet do duoi tmin { // va bat quat neu nhiet do tren tmax) if (ct<tmin) { OUTPUT_BIT(PIN_D0,0); bat=0; } if (ct>tmax) { OUTPUT_BIT(PIN_D0,1); bat=1; } } void hiennhietdo() // Ham hien thi nhiet do do duoc tu LM35 { int8 t,chuc,donvi; setcursor(0,0); wrtchar("- DO NHIET DO -"); // Hien thi dong tren t = ct; chuc = t/10 + 48; // Tach so hang chuc va hang don vi donvi = t%10 + 48; setcursor(0,1); wrtchar("NHIET DO: "); // Hien thi so do nhiet do len LCD wrtchar(chuc); wrtchar(donvi); wrtchar("*C"); delay_ms(50); } void settmin() // Ham cai dat nguong nhiet do min { int8 t,chuc,donvi; setcursor(0,0); wrtchar("- HEN TAT QUAT -"); t = tmin; // Tach hang chuc va don vi cua tmin va hien thi ra LCD chuc = t/10 + 48; donvi = t%10 + 48; setcursor(0,1); wrtchar("NEU DUOI: "); wrtchar(chuc); wrtchar(donvi); wrtchar("*C"); delay_ms(50); } void settmax() // Ham cai dat nguong nhiet do max (tuong tu ham setmin) { int8 t,chuc,donvi; setcursor(0,0); wrtchar("- HEN BAT QUAT -"); t = tmax;
  • 6. chuc = t/10 + 48; donvi = t%10 + 48; setcursor(0,1); wrtchar("NEU TREN: "); wrtchar(chuc); wrtchar(donvi); wrtchar("*C"); delay_ms(50); } void selectmode() // Ham lua chon mode hien thi { switch (mode) { case 0: hiennhietdo(); break; case 1: settmin(); break; case 2: settmax(); break; } } #INT_TIMER1 // Khai bao su dung ngat timer1 void ct_phuc_vu_ngat() { CLEAR_INTERRUPT(INT_TIMER1); // Xoa co ngat timer1 DISABLE_INTERRUPTS(GLOBAL); // Khong cho phep ngat khac cnt++; // Sau moi ngat ta tang bien dem if(cnt==10) // khi cnt=10 thi da dem duoc 10 lan tuong ung voi 1s { docnhietdo=1; ct = READ_ADC(); ct = ct*0.49; cnt=0; } else docnhietdo=0; SET_TIMER1(3035); ENABLE_INTERRUPTS(GLOBAL); } void main() // Ham ct chinh { Setup_ADC(ADC_CLOCK_INTERNAL); // Cai dat bo chuyen doi ADC setup_adc_ports(AN0); // Ngo vao Analog la chan AN0 SET_ADC_CHANNEL(0); // Chon kenh chuyen doi ADC la kenh 0 delay_us(20); mode = 0; // Khoi tao ban dau voi tmin=20*C va tmax=27*C tmin = 20; // va hien thi mode do nhiet do dau tien tmax = 27; lcd_init(); // Khoi tao LCD wrtcmd(0x01); wrtcmd(0x28); wrtcmd(0x0c); SETUP_TIMER_1(T1_INTERNAL|T1_DIV_BY_8); // Cai dat timer1 voi bo chia tan 1:8 SET_TIMER1(3035); // Gia tri khoi tao cho timer1 ( dem den 65535 // voi clock 20Mhz chia 8 => mot ngat tuong ung voi 0.1s ) ENABLE_INTERRUPTS(INT_TIMER1); // Cho phep ngat timer1
  • 7. ENABLE_INTERRUPTS(GLOBAL); // Cho phep ngat toan cuc ct = READ_ADC(); // Doc gia tri nhiet do dau tien ct = ct*0.49; while(1) // Vong lap vo han { selectmode(); do { if(docnhietdo==1) // Neu nhiet do duoc doc thi hien thi ra LCD neu dang { // o mode hien thi va tien hanh kiem tra nguong if(mode==0) { hiennhietdo(); } check(); // Kiem tra nguong } readkb(); } while(anphim==0); // Lien tuc lap lai viec kiem tra xem nhiet do } // co duoc doc hay ko va kiem tra phim bam }