1.Chương trình đọc dữ liệu port D và xuất port C.
Sơ đồ mạch điện.
#include "16f887.h"
#include "def_16f887.h"
#fuses NOWDT,NOPROTECT,PUT,NOLVP,HS
#use delay(clock = 4000000) //Tan so thach anh 4MHz
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
//Setup_Oscillator parameter not selected from Intr Oscillotar Config tab
set_tris_c(0x00); //PORTC xuat du lieu.
set_tris_d(0xff); //PORTD nhap du lieu.
while(1)
{
PORTC = PORTD;
delay_ms(20);
}
}
2.Chương trình chớp tắt chu kỳ 1s các Led ở port C.
Sơ đồ mạch điện.
#include "16f887.h"
#include "def_16f887.h"
#fuses NOWDT,NOPROTECT,PUT,NOLVP,HS
#use delay(clock = 4000000) //Tan so thach anh 4MHz
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
//Setup_Oscillator parameter not selected from Intr Oscillotar Config tab
set_tris_c(0x00); //PORTC xuat du lieu.
PORTC = 0x00; //Cac led deu tat.
while(1)
{
PORTC = ~PORTC;
delay_ms(1000);
}
}
3.Chương trình chớp tắt LED chu kỳ 2s ở portC 5 lần.
Sơ đồ mạch điện.
#include "16f887.h"
#include "def_16f887.h"
#fuses NOWDT,NOPROTECT,PUT,NOLVP,HS
#use delay(clock = 4000000) //Tan so thach anh 4MHz
int8 k;
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
//Setup_Oscillator parameter not selected from Intr Oscillotar Config tab
set_tris_c(0x00); //PORTC xuat du lieu.
PORTC = 0x00; //Cac led deu tat.
for(k=0;k<10;k++) //Led sang va tat 5 lan.
{
PORTC = ~PORTC; //Dao trang thai cac led.
delay_ms(2000);
}
while(1); //VXL ko lam gi nua.
}
4.PortD=0 thì xuất PortC=0xff ngược lại xuất PortC=0x0f.
Sơ đồ mạch điện.
#include "16f887.h"
#include "def_16f887.h"
#fuses NOWDT,NOPROTECT,PUT,NOLVP,HS
#use delay(clock = 4000000) //Tan so thach anh 4MHz
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
//Setup_Oscillator parameter not selected from Intr Oscillotar Config tab
set_tris_c(0x00); //PORTC xuat du lieu.
set_tris_d(0xff); //PORTD nhap du lieu.
while(1)
{
if(PORTD == 0)
PORTC = 0xff;
else
PORTC = 0x0f;
delay_ms(20);
}
}
5.Chương trình dịch led từ bit thấp đến bit cao.
Sơ đồ mạch điện.
#include "16f887.h"
#include "def_16f887.h"
#fuses NOWDT,NOPROTECT,PUT,NOLVP,HS
#use delay(clock = 4000000) //Tan so thach anh 4MHz
int8 k;
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
//Setup_Oscillator parameter not selected from Intr Oscillotar Config tab
set_tris_c(0x00); //PORTC xuat du lieu.
PORTC = 0x00; //Cac led deu tat.
while(1)
{
if(PORTC == 0x00)
PORTC = 0x01;
delay_ms(1000); //Thoi gian dich 1s;
PORTC = PORTC<<1;
}
}
6.Led sáng dần từ Led thứ 0 đến Led thứ 7, thời gian sáng là 1s.
Sơ đồ mạch điện.
#include "16f887.h"
#include "def_16f887.h"
#fuses NOWDT,NOPROTECT,PUT,NOLVP,HS
#use delay(clock = 4000000) //Tan so thach anh 4MHz
int8 k;
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
//Setup_Oscillator parameter not selected from Intr Oscillotar Config tab
set_tris_c(0x00); //PORTC xuat du lieu.
PORTC = 0x00; //Cac led deu tat.
while(1)
{
delay_ms(1000); //Delay nhin thay cac led deu tat.
PORTC = (PORTC<<1)|0x01; //Led sang dan.
if(PORTC == 0xff)
{
delay_ms(1000); //Delay nhin thay cac led deu sang.
PORTC = 0x00;
}
}
}
7.Chương trình nhấn công tắt RA4 thì LED ở port C sẽ đếm lên.
Sơ đồ mạch điện.
#include "16f887.h"
#include "def_16f887.h"
#fuses NOWDT,NOPROTECT,PUT,NOLVP,HS
#use delay(clock = 4000000) //Tan so thach anh 4MHz
#define SW RA4
unsigned char temp;
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
//Setup_Oscillator parameter not selected from Intr Oscillotar Config tab
set_tris_a(0xff); //PORTA nhap du lieu;
set_tris_c(0x00); //PORTC xuat du lieu.
PORTC = 0x00; //Cac led deu tat.
temp = 0;
while(1)
{
if(!RA4) //Cho nhan phim.
{
delay_ms(20); //Chong doi phim.
while(!RA4); //Cho nha phim.
temp++; //Tang gia tri len 1 don vi.
}
PORTC = temp;
}
}
8.Chương trình nhấn RA4 thì 2 led trái và 2 led phải thay nhau sáng.
Sơ đồ mạch điện.
#include "16f887.h"
#include "def_16f887.h"
#fuses NOWDT,NOPROTECT,PUT,NOLVP,HS
#use delay(clock = 4000000) //Tan so thach anh 4MHz
#define SW RA4
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
//Setup_Oscillator parameter not selected from Intr Oscillotar Config tab
set_tris_a(0xff); //PORTA nhap du lieu;
set_tris_c(0x00); //PORTC xuat du lieu.
PORTC = 0x03; //2 led trai sang.
while(1)
{
if(!RA4) //Cho nhan phim.
{
delay_ms(20); //Chong doi phim.
while(!RA4); //Cho nha phim.
RC0 = !RC0;
RC1 = RC0;
RC6 = !RC0;
RC7 = RC6;
}
}
}
9.Đèn giao thông tại ngã 4, thời gian đèn xanh 30s, đèn vàng 5s.
Sơ đồ mạch điện.
#include "16f887.h"
#include "def_16f887.h"
#fuses NOWDT,NOPROTECT,PUT,NOLVP,HS
#use delay(clock = 4000000) //Tan so thach anh 4MHz
#define SW RA4
unsigned char const X1V2 = 0x11;
unsigned char const X1D2 = 0x21;
unsigned char const V1X2 = 0x0a;
unsigned char const D1X2 = 0x0c;
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
//Setup_Oscillator parameter not selected from Intr Oscillotar Config tab
set_tris_c(0x00); //PORTC xuat du lieu.
PORTC = 0x03; //2 led trai sang.
while(1)
{
PORTC = X1V2;
delay_ms(5000);
//------------
PORTC = X1D2;
delay_ms(25000);
//------------
PORTC = V1X2;
delay_ms(5000);
//------------
PORTC = D1X2;
delay_ms(25000);
//------------
}
}
10.Chương trình đếm số BCD từ 00 đến 99 xuất ra port C.
Sơ đồ mạch điện.
#include "16f887.h"
#include "def_16f887.h"
#fuses NOWDT,NOPROTECT,PUT,NOLVP,HS
#use delay(clock = 4000000) //Tan so thach anh 4MHz
#define SW RA4
int8 dv=0, chuc=0;
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
//Setup_Oscillator parameter not selected from Intr Oscillotar Config tab
set_tris_c(0x00); //PORTC xuat du lieu.
PORTC = 0x00; //cac led deu tat.
while(1)
{
PORTC = (chuc<<4)|dv;
delay_ms(500);
dv++;
if(dv > 9)
{
dv = 0;
chuc++;
if(chuc > 9)
chuc = 0;
}
}
}
11.Đọc dữ liệu analog từ chân AN0 xuất ra port C, với VREF- = 0V và VREF+ = 5V.
Sơ đồ mạch điện.
#include "16f887.h"
#include "def_16f887.h"
#device *=16 ADC = 10
#fuses HS,PUT,NOWDT,NOPROTECT,NOLVP
#use delay(Clock = 4000000) //Thach anh 4MHz
int16 temp;
void main()
{
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_timer_1(T1_INTERNAL);
setup_adc_ports(sAN0|VREF_VREF); //kenh A0 nhan tin hieu, A2 = 0V; A3 = 5V;
setup_adc(ADC_CLOCK_INTERNAL); //Thoi gian lay mau bang clock he thong.
delay_ms(5); //Cho thiet lap xong ADC.
// TODO: USER CODE!!
trisa = 0xff; //PORTA nhan du lieu.
trisc = 0x00; //PORTC xuat du lieu.
set_adc_channel(0); //Chon kenh 0.
delay_us(10); //cho chon kenh xong.
while(1)
{
temp = read_adc(); //doc gia tri analog da chuyen doi sang so.
temp = temp/10.24; //stepsize = 50mV. Neu giao tiep LM35 thi chia 2.048 (stepsize = 10mv)
PORTC = temp; //Value = Vol/0,05
delay_ms(500);
}
}
12.Đọc dữ liệu analog từ chân AN0 xuất ra port C và chân AN1xuất ra portD, với VREF- = 0V và VREF+ = 5V
Sơ đồ mạch điện.
#include "16f887.h"
#include "def_16f887.h"
#device *=16 ADC = 10
#fuses HS,PUT,NOWDT,NOPROTECT,NOLVP
#use delay(Clock = 4000000) //Thach anh 4MHz
int16 temp;
void main()
{
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_timer_1(T1_INTERNAL);
setup_adc_ports(sAN0|sAN1|VREF_VREF); //kenh A0,A1 nhan tin hieu, A2 = 0V; A3 = 5V;
setup_adc(ADC_CLOCK_INTERNAL); //Thoi gian lay mau bang clock he thong.
delay_ms(5); //Cho thiet lap xong ADC.
// TODO: USER CODE!!
trisa = 0xff; //PORTA nhan du lieu.
trisc = 0x00; //PORTC xuat du lieu.
trisd = 0x00; //PORTD xuat du lieu.
while(1)
{
//Doc kenh 0
set_adc_channel(0); //Chon kenh 0.
delay_us(10); //cho chon kenh xong.
temp = read_adc(); //doc gia tri analog da chuyen doi sang so.
temp = temp/10.24; //stepsize = 50mV. Neu giao tiep LM35 thi chia 2.048 (stepsize = 10mv)
PORTC = temp; //Value = Vol/0,05
delay_ms(1000); //Thoi gian hien thi 1s.
//Doc kenh 1
set_adc_channel(1); //Chon kenh 1.
delay_us(10); //cho chon kenh xong.
temp = read_adc(); //doc gia tri analog da chuyen doi sang so.
temp = temp/10.24; //stepsize = 50mV. Neu giao tiep LM35 thi chia 2.048 (stepsize = 10mv)
PORTD = temp; //Value = Vol/0,05
delay_ms(1000); //Thoi gian hien thi 1s.
}
}
13.Đọc dữ liệu analog từ chân AN0 xuất ra port C, với VREF- = 0V và VREF+ = 2V.
Sơ đồ mạch điện.
#include "16f887.h"
#include "def_16f887.h"
#device *=16 ADC = 10
#fuses HS,PUT,NOWDT,NOPROTECT,NOLVP
#use delay(Clock = 4000000) //Thach anh 4MHz
int16 temp;
void main()
{
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_timer_1(T1_INTERNAL);
setup_adc_ports(sAN0|VREF_VREF); //kenh A0 nhan tin hieu, A2 = 0V; A3 = 2V;
setup_adc(ADC_CLOCK_INTERNAL); //Thoi gian lay mau bang clock he thong.
delay_ms(5); //Cho thiet lap xong ADC.
// TODO: USER CODE!!
trisa = 0xff; //PORTA nhan du lieu.
trisc = 0x00; //PORTC xuat du lieu.
set_adc_channel(0); //Chon kenh 0.
delay_us(10); //cho chon kenh xong.
while(1)
{
temp = read_adc(); //doc gia tri analog da chuyen doi sang so.
temp = temp/25.6; //stepsize = 50mV. Neu giao tiep LM35 thi chia 5.12 (stepsize = 10mv)
PORTC = temp; //Value = Vol/0,05
delay_ms(500);
}
}
14.Chương trình gửi ký tự ‘A’ qua cổng nối tiếp có tốc độ Baud là 9600.
Sơ đồ mạch điện.
#include "16f887.h"
#include "def_16f887.h"
#device *=16 ADC = 10
#fuses HS,PUT,NOWDT,NOPROTECT,NOLVP
#use delay(Clock = 4000000) //Thach anh 4MHz
#use rs232(baud = 9600, parity = N , Xmit = Pin_c6,rcv = pin_c7) //khoi tao usart
void main()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
// TODO: USER CODE!!
trisc = 0x10; //PORTC xuat du lieu.
while(1)
{
putc('A'); //Gui ky tu A
delay_ms(500);
}
}
Nguồn: sangthai.com.vn