0% found this document useful (0 votes)
21 views

Codea Them

The document defines constants and configures ports for a PIC microcontroller. It initializes a timer to toggle an LED every second and displays a number on 7-segment displays by pressing buttons to increment or decrement the value.

Uploaded by

Duy Vũ
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Codea Them

The document defines constants and configures ports for a PIC microcontroller. It initializes a timer to toggle an LED every second and displays a number on 7-segment displays by pressing buttons to increment or decrement the value.

Uploaded by

Duy Vũ
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <xc.

h>

#define _XTAL_FREQ 20000000

__CONFIG( FOSC_HS & WDTE_OFF & PWRTE_OFF & CP_OFF & BOREN_ON & LVP_OFF & CPD_OFF &
WRT_OFF & DEBUG_OFF);

#define SBIT_T2CKPS1 1

#define sw1 RC0

#define sw2 RC2

#define LED RC1


unsigned int count = 0, dem = 1;

unsigned int a,b;

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

void timer2_init(){

T2CON = (1<<SBIT_T2CKPS1);

TMR2=100; //Khoi tao gia tri bat dau de tao tre 500us

TMR2IE=1;

GIE=1;

PEIE=1;

TMR2ON = 1;

//tao ham ngat timer2 500us

void __interrupt() timer2(){

if(TMR2IF == 1){

TMR2 = 101;

TMR2IF = 0;

if(count>=2000) //500us * 2000=1000000us=1sec

count=0;

LED=~LED; //dao trang thai led

else

count++;
}

void main(void) {

TRISB = 0x00;

TRISD = 0x00;

TRISCbits.TRISC0 = 1;

TRISCbits.TRISC2 = 1;

TRISCbits.TRISC1 = 0;

LED = 0;

PORTD = 0x00;

PORTB = 0x00;

timer2_init();

while(1)

a=dem/10;

b=dem%10;

PORTDbits.RD0=0; PORTB = MALED[a];

__delay_ms(5); PORTDbits.RD0=1;

PORTDbits.RD1=0; PORTB = MALED[b];

__delay_ms(5); PORTDbits.RD1=1;

if(sw1 == 0 && dem < 99){

__delay_ms(50);

if(sw1 == 1){
dem++;

if(sw2 == 0 && dem > 0){

__delay_ms(50);

if(sw2 == 1){

dem--;

You might also like