You are on page 1of 33

TI LIU HNG DN LP TRNH PIC16F877A

CCS CHO NGI MI BT U

- y l ti liu v nhng phn c bn ca PIC16F877A trn CCS dnh cho nhng


ngi mi bt u.

- Lu ai mun tm hiu su hn v PIC1F877A th vo trn ch ca BLOG l


https://xcvn.blogspot.com cn di y ch l 1 phn nh trong cc bi hc c trnh
ra t cc bi hc trn trang ch to thnh ti liu ny. Tuy ti liu s xi nhng l
nhng phn c bn nht ca PIC16F877A ri, t nhng ci c bn ny cc bn s t
mnh tm hiu thm !

- Nu cc bn mun hiu r hn v l thuyt ca cc bi tp mu di y hy v link


cui mi bi vo xem s hiu r hn !

NGI SON : HUNH NGC LM.


BI 1 : CHP TT LED PIC1F877A CCS.

- HNH M PHNG PROTUES.

- CODE CHNG TRNH.


#include <16f877a.h>

#use delay(clock=800000)

#fuses HS,NOWDT

void main(void)

set_tris_b(0x00);

output_b(0);

while(true)

{
delay_ms(1000);

output_b(0xff);

delay_ms(1000);

output_b(0x00);

- Link download code v file m phng :


http://xcvn.blogspot.com/2015/12/bai-1-chop-tat-led-pic16f877a-ccs.html

BI 2 : HIU NG LED PIC1F877A CCS.


- HNH M PHNG PROTUES.

- CODE CHNG TRNH.


#include <16f877a.h>
#include <def_877a.h>

#fuses HS,NOLVP,NOWDT,PUT

#use delay(clock=8000000)

INT a,I,J,CD,D,Y,X;

void main (void)

TRISB = 0X00;

PORTB = 0X00;

while(true)

// left

PORTB = 0X01;

for(a=0;a<8;a++)

delay_ms(200);

PORTB = PORTB<<1;

// right

PORTB = 0X80;

for(a=0;a<8;a++)

delay_ms(200);

PORTB = PORTB>>1;
}

// left 2

PORTB = 0X03;

for(a=0;a<4;a++)

delay_ms(200);

PORTB = PORTB<<2;

// right 2

PORTB = 0XC0;

for(a=0;a<4;a++)

delay_ms(200);

PORTB = PORTB>>2;

// chase left

delay_ms(200);

PORTB = 0X00;

for(a=0;a<9;a++)

delay_ms(200);

PORTB = PORTB<<1;
PORTB = PORTB | 0X01;

// chase right

delay_ms(200);

PORTB = 0X00;

for(a=0;a<9;a++)

delay_ms(200);

PORTB = PORTB>>1;

PORTB = PORTB | 0X80;

// sang dan and tat dan

delay_ms(200);

PORTB = 0X00;

for(a=0;a<9;a++)

delay_ms(200);

PORTB = PORTB<<1 | 0x01;

for(i=0;i<9;i++)

delay_ms(100);

PORTB = PORTB<<1;
}

/////////

X=0X00000000;

for(I=0;I<8;I++)

X=(X<<1)+0X01;

PORTB = X;

delay_ms(100);

for(I=0;I<8;I++)

X=(X>>1);

PORTB = X;

delay_ms(100);

///////////////

delay_ms(200);

CD=0X00000000;

for ( J=8;J>0;J--)

D=0X00000001;

for(I=0;I<J;I++)

{
Y=CD+D;

PORTB = (Y);

delay_ms(100);

D=(D<<1);

CD=Y;

//////////

delay_ms(200);

CD=0X00000000;

for ( J=4;J>0;J--)

D=0X03;

for(I=0;I<J;I++)

Y=CD+D;

PORTB = (Y);
delay_ms(100);

D=(D<<2);

CD=Y;

- Link download code v file m phng :


http://xcvn.blogspot.com/2015/12/bai-2-hieu-ung-led-pic16f877a-ccs.html
BI 3 : GIAO TIP VI NT NHN PIC1F877A CCS.

- HNH M PHNG PROTUES.

- CODE CHNG TRNH.


#include <16f877a.h>

#include <def_877a.h>

#use delay(clock=800000)

#fuses HS,NOWDT

void main (void)

set_tris_a(0xff);

set_tris_b(0x00);
output_b(0x00);

while(true)

if(input(PIN_A0)==0)

delay_ms(10);

output_b(0xff);

if(input(PIN_A1)==0)

delay_ms(10);

output_b(0xaa);

if(input(PIN_A2)==1)

delay_ms(10);

output_b(0x00);

if(input(PIN_A3)==1)

delay_ms(10);

output_b(0xf0);
}

- Link download code v file m phng :


http://xcvn.blogspot.com/2015/12/bai-3-giao-tiep-voi-nut-nhan-pic16f877a.html

BI 4 : HIN TH S 2015 LN 4 LED 7 ON PIC1F877A CCS.

- HNH M PHNG PROTUES.


- CODE CHNG TRNH.
#include <16f877a.h>

#use delay(clock=800000)

#fuses HS,NOWDT

#use fast_io (b)

#use fast_io (c)

#byte portb=0x06

#bit rc0=0x07.0

#bit rc1=0x07.1

#bit rc2=0x07.2

#bit rc3=0x07.3

const unsigned char maled[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F,
0x6F};

void main(void)

set_tris_b(0x00);

set_tris_c(0x00);

output_b(0);

output_c(0);

while(true)

// hien thi so 2

portb=0x5b;

rc0=0;

delay_ms(10);

rc0=1;
// hien thi so 0

portb=0x3f;

rc1=0;

delay_ms(10);

rc1=1;

// hien thi so 1

portb=0x06;

rc2=0;

delay_ms(10);

rc2=1;

// hien thi so 5

portb=0x6d;

rc3=0;

delay_ms(10);

rc3=1;

- Link download code v file m phng :


http://xcvn.blogspot.com/2015/12/bai-5-hien-thi-so-2015-len-led-7-doan-ccs.html
BI 6 : M T 0 9 PIC16F877A CCS.

- HNH M PHNG PROTUES.

- CODE CHNG TRNH.


#include <16f877a.h>

#fuses NOLVP,NOWDT,PUT,XT

#use delay(clock=8000000)

const unsigned char maled[10]={0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82,0xF8, 0x80,
0x90};

int dem;

void main (void)


{

set_tris_b(0x00);

output_b(0);

while(true)

output_b(maled[dem]);

delay_ms(300);

dem++;

if(dem==10)

dem=0;

- Link download code v file m phng :


http://xcvn.blogspot.com/2015/12/bai-6-dem-tu-0-den-9-pic16f877a-ccs.html
BI 7 : M T 0 99 PIC16F877A CCS.

- HNH M PHNG PROTUES.

- CODE CHNG TRNH.


#include <16f877a.h>

#fuses NOLVP,NOWDT,PUT,XT

#use delay(clock=8000000)

#bit ra0=0x05.0

#bit ra1=0x05.1

const unsigned char maled[10]={0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82,0xF8, 0x80,
0x90};

int dem,chuc,donvi;

void main (void)

{
set_tris_c(0x00);

set_tris_a(0x00);

output_b(0);

while(true)

chuc=dem/10;

donvi=dem%10;

output_c(maled[chuc]);

ra0=0;

delay_ms(40);

ra0=1;

output_c(maled[donvi]);

ra1=0;

delay_ms(40);

ra1=1;

dem++;

delay_ms(10);

if(dem==100)

dem=0;

}
}

- Link download code v file m phng :


http://xcvn.blogspot.com/2015/12/bai-7-dem-tu-0-den-99-pic16f877a-ccs.html

BI 8 : N T 99 0 PIC1F877A CCS.
- HNH M PHNG PROTUES.

- CODE CHNG TRNH.


#include <16f877a.h>

#fuses NOLVP,NOWDT,PUT,XT

#use delay(clock=8000000)

#bit ra0=0x05.0

#bit ra1=0x05.1
const unsigned char maled[10]={0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82,0xF8, 0x80,
0x90};

int dem,chuc,donvi;

void main (void)

set_tris_c(0x00);

set_tris_a(0x00);

output_b(0);

dem = 99;

while(true)

chuc=dem/10;

donvi=dem%10;

output_c(maled[chuc]);

ra0=0;

delay_ms(40);

ra0=1;

output_c(maled[donvi]);

ra1=0;

delay_ms(40);

ra1=1;
dem--;

delay_ms(10);

if(dem==0)

dem=99;

- File m phng s dng bi trn.

BI 8 : TIMER 0 PIC16F877A CCS.


- HNH M PHNG PTOURES.
- CODE CHNG TRNH.
#include <16F877a.h>

#include <def_877a.h>

#FUSES NOWDT, XT, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT

#use Delay(Clock=4000000)

#define led RC0

#int_TIMER0

void TIMER0_isr(void)

set_timer0(206);

led=!led;

void main()

set_tris_c(0x00);

setup_timer_0(RTCC_INTERNAL|RTCC_DIV_4);

set_timer0(206);

enable_interrupts(INT_TIMER0);

enable_interrupts(GlOBAL);

while(true)

- Link download code v file m phng :


http://xcvn.blogspot.com/2015/12/bai-10-timer-0-pic16f877a-ccs.html
BI 9 : TIMER 1 PIC1F877A CCS.

- HNH M PHNG PROTUES.

- CODE CHNG TRNH.


#include <16F877a.h>

#include <def_877a.h>

#FUSES NOWDT, XT, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT

#use Delay(Clock=4000000)

#define led RC0

#int_TIMER1

void TIMER1_isr(void)

set_timer1(59286);
led^=1;

void main()

set_tris_c(0x00);

setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);

set_timer1(59286);

enable_interrupts(INT_TIMER1);

enable_interrupts(GlOBAL);

while(true)

- Link download code v file m phng :


http://xcvn.blogspot.com/2015/12/bai-11-timer-1-pic16f877a-ccs.html
BI 10 : PWM PIC1F877A CCS.

- HNH M PHNG PROTUES.

- CODE CHNG TRNH.


#include <16F877A.h>

#include <def_877a.h>

#device *=16 adc=10

#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT,NOLVP, NOCPD, NOWRT

#use delay(clock=20000000)

#include <lcd_lib_4bit.c>

void main()

LCD_Init();

LCD_PutCmd(0x80);
LCD_PutChar(" DEMO PWM");

LCD_PutCmd(0xC0);

LCD_PutChar(" BY LAM 3 NGON");

setup_timer_2(T2_DIV_BY_1,59,1);

setup_ccp1(CCP_PWM);

set_pwm1_duty(72);

while(TRUE)

- Link download code v file m phng :


http://xcvn.blogspot.com/2016/01/bai-21-pwm-pic16f877a-ccs.html
BI 11 : C ADC HIN TH LCD

- HNH M PHNG PROTUES.

- CODE CHNG TRNH.


#include <16f877a.h>

#device 16f877a*16ADC=10

#fuses nowdt,xt

#use delay(clock=4000000)

#include <lcd_lib_4bit.c>

int16 x;

void docADC();

void main ()

LCD_Init();

set_tris_a(0x01);

while(true)
{

docADC();

x = read_ADC();

LCD_PutCmd(0x80);

printf(LCD_PutChar,"Value ADC :%4Lu",x);

void docADC()

setup_ADC(ADC_clock_internal);

setup_ADC_ports(AN0);

set_ADC_channel(0);

delay_us(10);

}
BI 12 : M T 0 9 S DNG NGT RB0 PIC1F877A CCS.

- HNH M PHNG PROTUES.

- CODE CHNG TRNH.


#include <16F877A.h>

#fuses NOWDT,PUT,XT,NOPROTECT

#use delay(clock=4000000)

#use fast_io(b)

#use fast_io(d)

#byte portb=0x06

#byte portd=0x08

const unsigned char


digital[]={0x3F,0x06,0x5B,0x4F,0X66,0X6D,0X7D,0X07,0X7F,0X6F};
#int_ext

void ngat_RB0()

int i;

portd=digital[i];

++i;

if(i==10)

i=0;

void main(void)

set_tris_b(0b00000001);

set_tris_d(0);

enable_interrupts(global);

enable_interrupts(int_ext);

ext_int_edge(H_to_L);

portd=0b00111111;

while(true)

}
- Link download code v file m phng :
http://xcvn.blogspot.com/2016/01/bai-22-dem-tu-0-den-9-su-dung-ngat-rb0-pic16f877a-
ccs.html

BI 12 : C ADC HIN TH LED 7 ON.

- HNH M PHNG PROTUES.

- CODE CHNG TRNH.


#include <16f877a.h>

#device 16f877a*16ADC=10

#use delay(clock=4000000)

#bit led1 =0x07.0

#bit led2 =0x07.1

#bit led3 =0x07.2

#bit led4 =0x07.3

const unsigned char maled[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};


int16 x;

int16 nghin,tram,chuc,donvi;

void docADC();

void main()

set_tris_a(0x01);

set_tris_c(0x00);

output_b(0x00);

output_c(0x00);

while(true)

docADC();

x=read_ADC();

nghin = (x/1000);

tram = (x - nghin*1000)/100;

chuc = (x - nghin*1000 - tram*100)/10;

donvi = x - nghin*1000 - tram*100 - chuc*10;

output_b(maled[nghin]);

led1=1;

delay_ms(1);

led1=0;

output_b(maled[tram]);

led2=1;
delay_ms(1);

led2=0;

output_b(maled[chuc]);

led3=1;

delay_ms(1);

led3=0;

output_b(maled[donvi]);

led4=1;

delay_ms(1);

led4=0;

void docADC()

setup_ADC(ADC_clock_internal);

setup_ADC_ports(AN0);

set_ADC_channel(0);

delay_us(100);

You might also like