Programa

Programação do PIC16F876 (Linguagem JAL)	
-- ******************************************************************
--
-- Programa Controlo Robot ESEN
--
-- 
-- ******************************************************************
-- Bibliotecas utilizadas
include rotinas
-- Variáveis
var byte vel_m_dir,vel_m_esq
var byte vel_l, vel_h, vel_hh
var bit parede_e, parede_d
var byte ch_hi_E, ch_hi_D
var byte limiar = 45				-- distância à parede de 30 
cm
var  byte n = 1
-- Velocidades
vel_l = 50				-- 50 antes
vel_h = 120
vel_hh = 200
-- ******************************************************************
-- Inicio de Programa
-- ******************************************************************
-- procedure avancar 
-- procedure recuar 
-- procedure avancar_m_esq 
-- procedure recuar_m_esq 
-- procedure avancar_m_dir 	
-- procedure recuar_m_dir 
-- procedure parar 
-- pwm(motor direito, motor esquerdo)
--********************************************************************
-- *                          CONTROLO DOS MOTORES                                           
-- *******************************************************************
procedure detectar_paredes is
	ad8_noref
	ch0_on					-- inicia conversor A/D no 
ch0 (lado esquerdo)
	ch_write			      	-- leitura do resultado 
	
	ch_hi_E = ch_hi	
	if (ch_hi_E < 10)  then			-- distancia muito grande
		parede_e = low
	else
		parede_e = high
	end if
	
      	ch1_on					-- inicia conversor 
A/D no ch1(lado direito)
	ch_write		 	      	-- leitura do resultado 
	
	ch_hi_D = ch_hi	
	if (ch_hi_D  < 10) then			-- distancia muito grande
		parede_d = low
	else
		parede_d = high
	end if
end procedure
procedure segue_parede_dir is
	ad8_noref
	ch1_on					-- inicia conversor A/D no 
ch0
	ch_write			      	-- leitura do resultado 
	
	ch_hi_D = ch_hi	
	if (ch_hi_D > limiar + 5)  then		-- aproxima-se da 
parede (virar a esquerda)
		for n loop
			avancar_m_esq
			avancar_m_dir
			vel_m_dir = vel_h
			vel_m_esq = vel_l
		end loop	
	elsif (ch_hi_D < limiar - 5)  then	-- afasta-se da parede 
(virar a direita)
		for n loop
			avancar_m_esq
			avancar_m_dir
			vel_m_dir = vel_l 
			vel_m_esq = vel_h
		end loop
	else
		for n loop
			avancar
			vel_m_dir = vel_l 
			vel_m_esq = vel_l
		end loop
	end if
end procedure
procedure segue_parede_esq is
	ad8_noref
	ch0_on					-- inicia conversor A/D no 
ch1
	ch_write			      	-- leitura do resultado 
	
	ch_hi_E = ch_hi	
	if (ch_hi_E > limiar + 5)  then		-- aproxima-se da 
parede (virar a direita)
		for n loop
			avancar_m_esq
			avancar_m_dir
			vel_m_dir = vel_l 
			vel_m_esq = vel_h
		end loop
	
	elsif (ch_hi_E < limiar - 5)  then	-- afasta-se da parede 
(virar a esquerda)
		for n loop
			avancar_m_esq
			avancar_m_dir
			vel_m_dir = vel_h 
			vel_m_esq = vel_l
		end loop	
	else
		for n loop
			avancar
			vel_m_dir = vel_l 
			vel_m_esq = vel_l
		end loop
	end if
end procedure
-- *******************************************************************
forever loop

-- 00001 direita muito apertada (antes estava inactiva)
 if (pin_b4 == low) & (pin_b3 == low) & (pin_b2 == low) & (pin_b1 == 
low) & (pin_b0 == high) then
	for n loop
		avancar_m_esq
		recuar_m_dir
		vel_m_dir = vel_l 
		vel_m_esq = vel_h
	end loop
-- 10000 esquerda muito apertada						
		
 elsif (pin_b5 == high) & (pin_b4 == low) & (pin_b3 == low) & (pin_b2 
== low) & (pin_b1 == low) then
	for n loop
		avancar_m_dir
		recuar_m_esq
		vel_m_dir = vel_h 
		vel_m_esq = vel_l
	end loop
-- 0000	saida pista 		(parar)
elsif (pin_b4 == low) & (pin_b3 == low) & (pin_b2 == low) & (pin_b1 == 
low)  then
	detectar_paredes
	if (parede_e == low) & (parede_d == low) then 
		avancar			
		vel_m_dir = vel_h 	
		vel_m_esq = vel_h
		delay_10ms(15)
		parar
		vel_m_dir = 0
		vel_m_esq = 0
	end if
	if parede_e == high then 
		segue_parede_esq
	elsif parede_d == high then
		segue_parede_dir
	end if

	
elsif (pin_b4 == low) & (pin_b3 == low) & (pin_b2 == low) & (pin_b1 == 
low)   then
	for 5 loop
		avancar
		vel_m_dir = vel_h
		vel_m_esq = vel_h
	end loop
	
	parar
-- 0001 direita
elsif (pin_b4 == low) & (pin_b3 == low) & (pin_b2 == low) & (pin_b1 == 
high) then
	for n loop
		avancar_m_esq
		recuar_m_dir
		vel_m_dir = vel_l 
		vel_m_esq = vel_h
	end loop
-- 0010 direita
elsif (pin_b4 == low) & (pin_b3 == low) & (pin_b2 == high) & (pin_b1 
== low) then
	for n loop
		avancar
		vel_m_dir = vel_l 
		vel_m_esq = vel_h
	end loop
-- 0011 direita
elsif (pin_b4 == low) & (pin_b3 == low) & (pin_b2 == high) & (pin_b1 
== high) then
	for n loop
		avancar_m_esq
		recuar_m_dir
		vel_m_dir = vel_l 
		vel_m_esq = vel_h
	end loop
-- 0100 frente esq
elsif (pin_b4 == low) & (pin_b3 == high) & (pin_b2 == low) & (pin_b1 
== low)  then
	for n loop
		avancar_m_esq
		avancar_m_dir
		vel_m_dir = vel_hh
		vel_m_esq = vel_h
	end loop

-- 0101 direita
elsif (pin_b4 == low) & (pin_b3 == high) & (pin_b2 == low) & (pin_b1 
== high) then
	for n loop
		avancar_m_esq
		recuar_m_dir
		vel_m_dir = vel_l 
		vel_m_esq = vel_h
	end loop
-- 0110 frente
elsif (pin_b4 == low) & (pin_b3 == high) & (pin_b2 == high) & (pin_b1 
== low) then
	for n loop
		avancar
		vel_m_dir = vel_hh
		vel_m_esq = vel_hh
	end loop
-- 0111 bola a direita --> virar direita					
	
elsif (pin_b4 == low) & (pin_b3 == high) & (pin_b2 == high) & (pin_b1 
== high) then
	for n loop
		avancar_m_esq
		recuar_m_dir
		vel_m_dir = vel_l 
		vel_m_esq = vel_h
	end loop
-- 1000 esquerda								
elsif (pin_b4 == high) & (pin_b3 == low) & (pin_b2 == low) & (pin_b1 
== low) then
	for n loop
		avancar_m_dir
		recuar_m_esq
		vel_m_dir = vel_h 
		vel_m_esq = vel_l
	end loop
-- 1001 direita
elsif (pin_b4 == high) & (pin_b3 == low) & (pin_b2 == low) & (pin_b1 
== high) then
	for n loop
		avancar_m_esq
		recuar_m_dir
		vel_m_dir = vel_h 
		vel_m_esq = vel_hh
	end loop
-- 1010 direita
elsif (pin_b4 == high) & (pin_b3 == low) & (pin_b2 == high) & (pin_b1 
== low) then
	for n loop
		avancar_m_esq
		avancar_m_dir
		vel_m_dir = vel_l 
		vel_m_esq = vel_h
	end loop
-- 1011 
elsif (pin_b4 == high) & (pin_b3 == low) & (pin_b2 == high) & (pin_b1 
== high) then
	for n loop
		avancar
		vel_m_dir = vel_h 
		vel_m_esq = vel_h
	end loop
-- 1100 esquerda 
elsif (pin_b4 == high) & (pin_b3 == high) & (pin_b2 == low) & (pin_b1 
== low) then
	for n loop
		avancar_m_dir
		recuar_m_esq
		vel_m_dir = vel_h 
		vel_m_esq = vel_l
	end loop

-- 1101 direita
elsif (pin_b4 == high) & (pin_b3 == high) & (pin_b2 == low) & (pin_b1 
== high) then
	for n loop
		avancar_m_esq
		recuar_m_dir
		vel_m_dir = vel_h 
		vel_m_esq = vel_h
	end loop
-- 1110 esquerda
elsif (pin_b4 == high) & (pin_b3 == high) & (pin_b2 == high) & (pin_b1 
== low) then
	for n loop	
		avancar_m_dir
		recuar_m_esq
		vel_m_dir = vel_h 
		vel_m_esq = vel_l
	end loop
-- 1111 bola/tudo branco
elsif (pin_b4 == high) & (pin_b3 == high) & (pin_b2 == high) & (pin_b1 
== high)  then
	for n loop
		avancar
		vel_m_dir = vel_hh 
		vel_m_esq = vel_hh
	end loop
-- 10011 direita -->curva em situação extrema
elsif (pin_b5 == high) & (pin_b4 == low) & (pin_b3 == low) & (pin_b2 
== high) & (pin_b1 == high) then
	for n loop
		avancar_m_esq
		recuar_m_dir
		vel_m_dir = vel_l 
		vel_m_esq = vel_h
	end loop
-- 11011 esquerda--> atalho 2
elsif (pin_b5 == high) & (pin_b4 == high) & (pin_b3 == low) & (pin_b2 
== high) & (pin_b1 == high) then
	
	for n loop
		avancar_m_dir
		recuar_m_esq
		vel_m_dir = vel_h 
		vel_m_esq = vel_l
	end loop
 
-- 001101- Direita Atalho 1
elsif (pin_b5 == low) & (pin_b4 == low) & (pin_b3 == high) & (pin_b2 
== high) & (pin_b1 == low) & (pin_b0 == high) then
	for n loop 
		avancar_m_esq
		recuar_m_dir
		vel_m_dir = vel_l
		vel_m_esq = vel_h
	end loop
end if
pwm_ccp(1,vel_m_dir,vel_m_esq)
end loop