Đè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

  • Đồng hồ số PIC16F877A with XC8 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 đồng hồ số hiển thị led LCD 16x02 sử dụng PIC16F877A và
  • LM335 and 7seg with XC8 Đo nhiệt độ môi trường tại một điểm thông qua sensor nhiệt LM335(Chi tiết về LM335 xem trong (datasheet). LM335 là se
  • LM35 and LCD with XC8 LM35 là một cảm biến nhiệt độ analog Nhiệt độ được xác định bằng cách đo hiệu điện thế ngõ ra của LM35. → Đơn vị nh
  • Cảm biến khoảng cách HC - SR04 with XC8 Đặc điểm nỏi bật của sản phẩm Module cảm biến siêu âm (hc-sr04 ) Module HC-SR04 siêu âm khác nhau, mô-đun, hướng dẫn
Previous
Next Post »

EmoticonEmoticon

:)
:(
=(
^_^
:D
=D
=)D
|o|
@@,
;)
:-bd
:-d
:p
:ng