Projeto 2: Semáforo Microcontrolado

30/11/2016

PIC18F4431

#include<p18f4431.h>
#include<delays.h>
#include<xlcd.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<timers.h>
unsigned int timer=0;
unsigned int real_timer=0;
unsigned char string[16];
//INICIO DEFINIÇÕES DO LCD//
#define limpar_LCD WriteCmdXLCD(0x01)
void gotoxy(int linha_LCD , int coluna_LCD) // Situa cursor en x,y
{
if(linha_LCD == 1) {
while(BusyXLCD());
SetDDRamAddr(0x80 ' coluna_LCD); // Cursor en linea 1
}
else {
while(BusyXLCD());
SetDDRamAddr(0xC0 ' coluna_LCD); // Cursor en linea 2
}
}
void escreve_LCD(char out[16])
{
char msg[16];
sprintf(msg,out);
putsXLCD(msg);
}
void DelayFor18TCY(void) //200us*FOSC/(10*4)= [ (200/1000000)*48000000)/40]
{
Delay10TCYx(40);
return;
}
void DelayPORXLCD(void) //15ms*FOSC/(10*4)=
{
Delay1KTCYx(30);
return;
}
void DelayXLCD(void) //5us*FOSC/(10*4)=
{
Delay10TCYx(1);
return;
}
void iniciar_LCD()
{
OpenXLCD(FOUR_BIT & LINES_5X7);
WriteCmdXLCD(0x0C);
WriteCmdXLCD(0x06);
}
//FIM DEFINIÇÕES DO LCD//
void config_io()
{
TRISC=0X00;
PORTC=0X00;
TRISD=0X00;
PORTD=0X00;
}
void lcd_tempo()
{
gotoxy(1,0);
escreve_LCD("SEMAFORO");
if(real_timer==1)
{
gotoxy(2,0);
escreve_LCD("SINAL VERDE ");
}
else if(real_timer==10)
{
gotoxy(2,0);
escreve_LCD("SINAL AMARELO ");
}
else if(real_timer==15)
{
gotoxy(2,0);
escreve_LCD("SINAL VERMELHO ");
}
}
void logica_semaforo()
{
if(real_timer==1)
{
PORTC=0X04;
}
else if(real_timer==10)
{
PORTC=0X02;
}
else if(real_timer==15)
{
PORTC=0x01;
}
else if(real_timer==32)
{
WriteTimer0(0);
real_timer=0;
}
}
void cont_tempo()
{
timer=ReadTimer0();
if(timer==15625)
{
real_timer++;
WriteTimer0(0);
lcd_tempo();
}
}
void main()
{
OSCCON=0X72;
config_io();
iniciar_LCD();
OpenTimer0(TIMER_INT_OFF &T0_16BIT &T0_SOURCE_INT
&T0_PS_1_128);
while(1)
{
cont_tempo();
logica_semaforo();
}
}

© 2016 Dnl Silva. São Paulo - SP
Desenvolvido por Webnode
Crie seu site grátis! Este site foi criado com Webnode. Crie um grátis para você também! Comece agora