Đếm từ 0 - 99 XC8

1:37:00 AM
Hôm nay, chúng ta sẽ cùng nhau làm một số ví dụ về LED 7 thanh để làm rõ hơn những lý thuyết đã tìm hiểu ở bài viết trước.



- Đây là code 

#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define _XTAL_FREQ 4000000
// CONFIG
#pragma config FOSC = XT        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF        // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

#define leddata PORTD

unsigned int Count;
const unsigned char maled7[] = {0XBF,0X86,0XDB,0XCF,0XE6,0XED,0XFD,0X87,0XFF,0XEF};
//0x40,0x79,0x24,0x30,0x19,0x12, 0x82,0x78,0x00,0x10
unsigned int chuc,donvi;
void Hienthi(unsigned int chuc,unsigned int donvi);
void dectoBCD(unsigned int Count);

void main (void)
{
    RA0 = 1,TRISD = 0,TRISB = 0,PORTB = 0X00,PORTD = 0X00;
while(1)
    {
        dectoBCD(Count);
        Hienthi(chuc,donvi);
        Count++;
        if(Count==33)
        Count=0;

}
}
void dectoBCD(unsigned int Count)
{
    chuc = Count/10;
    donvi = (Count - chuc*10);
}

void Hienthi(unsigned int chuc,unsigned int donvi)
{
    RB4=0;
    leddata=maled7[chuc];
    __delay_ms(10);
    RB4=1;

    RB5=0;
    leddata=maled7[donvi];
    __delay_ms(10);
    RB5=1;
}

Share this

Related Posts

Previous
Next Post »