0% found this document useful (0 votes)
19 views5 pages

PROGRAMAÇÃO

Uploaded by

Wesley Ferrarezi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views5 pages

PROGRAMAÇÃO

Uploaded by

Wesley Ferrarezi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <18f4550.

h>
#include "lcd.c"
#device adc=10
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=20000000)
#use rs232(baud=9600,parity=O,xmit=PIN_C6,rcv=PIN_C7,bits=8) // Paridade Impar
#ignore_warnings 208,203
#use FAST_IO(a)
#use FAST_IO(b)
#use FAST_IO(c)
#use FAST_IO(d)
#use FAST_IO(e)

#define setbit(y, bit) (y |= (1 << bit)) //coloca em 1 o bit x da variável Y


#define clrbit(y, bit) (y &= ~(1 << bit)) //coloca em 0 o bit x da variável Y
#define cplbit(y, bit) (y ^= (1 << bit)) //troca o estado lógico do bit x da
variável Y
#define tstbit(y, bit) (y & (1 << bit)) //retorna 0 ou 1 conforme leitura do bit

#ifndef lcd_enable
#define lcd_rs pin_d2 // pino rs do LCD
#define lcd_enable pin_d3 // pino enable do LCD
#define lcd_d4 pin_d4 // pino de da/int16 P_Carregar_Magazine
=1000;
#define lcd_d5 pin_d5 // pino de dados d5 do LCD
#define lcd_d6 pin_d6 // pino de dados d6 do LCD
#define lcd_d7 pin_d7 // pino de dados d7 do LCD
#endif
#define lcd_type 2
#define lcd_seg_lin 0x40
byte CONST INI_LCD[4] = {0x20 | (lcd_type << 2), 0xf, 1, 6};

boolean btn[8];
boolean _btn[8];
boolean msg2=0;
byte i=0;
int carga =0;
int _carga=0;
float vbat = 0;
float scala=0;

//------------------------------------------------------------------------------
void lcd_envia_nibble( byte dado )
{
output_bit(lcd_d4,bit_test(dado,0));
output_bit(lcd_d5,bit_test(dado,1));
output_bit(lcd_d6,bit_test(dado,2));
output_bit(lcd_d7,bit_test(dado,3));
output_high(lcd_enable);
output_low(lcd_enable);
}
//------------------------------------------------------------------------------
void lcd_send_byte( boolean endereco, byte dado )
{
output_low(lcd_rs);
output_bit(lcd_rs,endereco);
delay_us(100);
output_low(lcd_enable);
lcd_envia_nibble(dado >> 4);
lcd_envia_nibble(dado & 0x0f);
}
//------------------------------------------------------------------------------
void lcd_init()
{
byte conta;
output_low(lcd_d4);
output_low(lcd_d5);
output_low(lcd_d6);
output_low(lcd_d7);
output_low(lcd_rs);
for(conta=1;conta<=3;++conta)
{
lcd_envia_nibble(3);
delay_ms(5);
}
lcd_envia_nibble(2);
for(conta=0;conta<=3;++conta) lcd_send_byte(0,INI_LCD[conta]);
}
//------------------------------------------------------------------------------
void lcd_pos_xy( byte x, byte y)
{
byte endereco;
if(y!=1) endereco = lcd_seg_lin;
else endereco = 0;
endereco += x-1;
lcd_send_byte(0,0x80|endereco);
}
//------------------------------------------------------------------------------
void lcd_putc( char c)
{
switch (c)
{
case '\f':lcd_send_byte(0,1); delay_ms(2);break;
case '\n':case '\r':lcd_pos_xy(1,2); break;
case '\b':lcd_send_byte(0,0x10);break;
default:lcd_send_byte(1,c);break;
}
}

//------------------------------------------------------------------------------
void ShowLCD( )
{
if (carga!=_carga)
{
_carga=carga;
printf(lcd_putc,"\fCARGA = ");
printf(lcd_putc,"%d ",carga); //
printf(lcd_putc," %%\n");
}
}

//------------------------------------------------------------------------------
void Teclado()
{
btn[3]=input(pin_E0); //ESQUERDA - BTN1
btn[0]=input(pin_A1); //FRENTE - BTN2
btn[5]=input(pin_A2); //DESCE - BTN3
btn[4]=input(pin_A3); //SOBE - BTN4
btn[2]=input(pin_A4); //DIREITA - BTN5
btn[1]=input(pin_A5); //RE - BTN6
btn[6]=input(pin_B6); //BUZINA - BTN7
if (btn[0]==0&& btn[1]==0&& btn[2]==0&& btn[3]==0&& btn[4]==0&& btn[5]==0&&
btn[6]==0&& msg2==0)
{
msg2=1;
ShowLCD();
printf(lcd_putc,"SISTEMA OK\n");
}
if (btn[0]==1 && _btn[0]==0)
{
msg2=0;
_btn[0]=1;
output_high(pin_b0);
output_high(pin_b2);
output_high(pin_c0);
ShowLCD();
printf(lcd_putc,"AVANCA \n");
}
if (btn[0]==0 && _btn[0]==1)
{
_btn[0]=0;
output_low(pin_b0);
output_low(pin_b2);
output_low(pin_c0);
}
//------------------------------------
if (btn[1]==1 && _btn[1]==0)
{
msg2=0;
_btn[1]=1;
output_high(pin_b1);
output_high(pin_b3);
output_high(pin_c0);
ShowLCD();
printf(lcd_putc,"RECUA \n");
}
if (btn[1]==0 && _btn[1]==1)
{
_btn[1]=0;
output_low(pin_b1);
output_low(pin_b3);
output_low(pin_c0);
}
//------------------------------------
if (btn[2]==1 && _btn[2]==0)
{
msg2=0;
_btn[2]=1;
output_high(pin_b0);
output_high(pin_b3);
output_high(pin_c0);
ShowLCD();
printf(lcd_putc,"DIREITA \n");
}
if (btn[2]==0 && _btn[2]==1)
{
_btn[2]=0;
output_low(pin_b0);
output_low(pin_b3);
output_low(pin_c0);
}
//------------------------------------
if (btn[3]==1 && _btn[3]==0)
{
msg2=0;
_btn[3]=1;
output_high(pin_b1);
output_high(pin_b2);
output_high(pin_c0);
ShowLCD();
printf(lcd_putc,"ESQUERDA \n");
}
if (btn[3]==0 && _btn[3]==1)
{
_btn[3]=0;
output_low(pin_b1);
output_low(pin_b2);
output_low(pin_c0);
}
//------------------------------------
if (btn[4]==1 && _btn[4]==0)
{
msg2=0;
_btn[4]=1;
output_high(pin_b4);
output_high(pin_c0);
ShowLCD();
printf(lcd_putc,"SOBE \n");
}
if (btn[4]==0 && _btn[4]==1)
{
_btn[4]=0;
output_low(pin_b4);
output_low(pin_c0);
}
//------------------------------------
if (btn[5]==1 && _btn[5]==0)
{
msg2=0;
_btn[5]=1;
output_high(pin_b5);
output_high(pin_c0);
ShowLCD();
printf(lcd_putc,"DESCE \n");
}
if (btn[5]==0 && _btn[5]==1)
{
_btn[5]=0;
output_low(pin_b5);
output_low(pin_c0);
}
//------------------------------------
if (btn[6]==1 && _btn[6]==0)
{
msg2=0;
_btn[6]=1;
output_high(pin_c1);
ShowLCD();
printf(lcd_putc,"BUZINA \n");
}
if (btn[6]==0 && _btn[6]==1)
{
_btn[6]=0;
output_low(pin_c1);
}

}
//------------------------------------------------------------------------------
void Sbat()
{
set_adc_channel(0);
delay_ms(1);
vbat= Read_ADC();
vbat=vbat-740;
if(vbat<0)vbat=0;
scala=((vbat)/270)*100;
carga=scala;
if(carga<=0)carga=0;
}
//------------------------------------------------------------------------------
void main()
{
setup_adc_ports(AN0|VSS_VDD);
setup_adc( ADC_CLOCK_INTERNAL );

set_tris_a ( 0b11111111);
set_tris_b ( 0b00000000);
set_tris_c ( 0b11111100);
set_tris_d ( 0b00000011);
set_tris_e ( 0b11111111);

setup_wdt(WDT_ON);
setup_timer_0(RTCC_INTERNAL);
lcd_init();
delay_ms(10);
lcd_send_byte(0,0x0C); // Comando para desativar cursor
delay_ms(10);
while (true)
{
Teclado();
Sbat();
}
}
//-----------------------------------------------------------

You might also like