Đèn giao thông PIC16F877A with XC8

7:55:00 AM
Chào các bạn trong bài đăng này mình sẽ hướng dẫn các bạn làm mạch đèn giao thông đơn giản sử dụng PIC16F877A.

Ảnh mô phỏng protues.


traffic lights ppic16f877a xc8

Đây là code chương trình.

#include <stdio.h>
#include <stdlib.h>
#define _XTAL_FREQ 4000000
#include <xc.h>
// CONFIG
#pragma config FOSC = XT        // Oscillator Selection bits (XT 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)

void GPIO_init(void);
signed char digit =0 ;
const unsigned char seg[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
void main(void)
{
    GPIO_init();
    ADCON1 = 0x07;
    while(1)
    {
            PORTAbits.RA0 = 1;
            PORTAbits.RA1 = 0;
            PORTAbits.RA2 = 0;
            for(digit=30;digit>=0;digit--) //thay đổi thời gian tại đây
            {
                PORTB = seg[digit/10];
                PORTD = seg[digit%10];
                __delay_ms(1000);
            }
            PORTAbits.RA0 = 0;
            PORTAbits.RA1 = 1;
            PORTAbits.RA2 = 0;
            for(digit=5;digit>=0;digit--) //thay đổi thời gian tại đây
            {
                PORTB = seg[digit/10];
                PORTD = seg[digit%10];
                __delay_ms(1000);
            }
            PORTAbits.RA0 = 0;
            PORTAbits.RA1 = 0;
            PORTAbits.RA2 = 1;
            for(digit=20;digit >= 0;digit--) //thay đổi thời gian tại đây
            {
                PORTB = seg[digit/10];
                PORTD = seg[digit%10];
                __delay_ms(1000);
            }
    }
}

void GPIO_init (void)
{
    TRISA = 0X00;
    PORTA = 0X00;
    TRISB = 0X00;
    PORTB = 0X00;
    TRISC = 0X00;
    PORTC = 0X00;
    TRISD = 0X00;
    PORTD = 0X00;
}

Link download project.

Share this

Related Posts

Previous
Next Post »