• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Help with keypad + pic18f46k20

Status
Not open for further replies.

bla

New Member
Hi,

I'm new here and in C18... So, i was trying to do a clock, and here's my code:


Code:
#include "stdlib.h"
#include "p18f46k20.h"
#include "delays.h"
#include"stdio.h"

int x,z;
int unidadesegundo, dezenasegundo, unidademinuto, dezenaminuto, unidadehora, dezenahora;
Code:
unsigned int convert_7s(int num){

	int vector[11] = {0b01000000, 0b11111001, 0b00100100, 0b00110000, 0b00011001, 0b00010010, 0b00000010, 0b11011000, 0b00000000, 0b00011000};
	return vector[num];

}
Code:
unsigned int habilit_port(int display){

	int vector[6] = {0b00100000, 0b00010000, 0b00001000, 0b00000100, 0b00000010, 0b00000001};
	return vector[display];

}
Code:
void habilit_port_number(unsigned int port, int num){

	switch(port){

		case 1:{
			PORTA = convert_7s(num);
			break;
		}

		case 2:{
			PORTB = convert_7s(num);
			break;
		}

		case 3:{
			PORTC = convert_7s(num);
			break;
		}

		case 4:{
			PORTD = convert_7s(num);	
			break;
		}

	}

}

void habilit_port_habilit(unsigned int port, int display_habilit){

	switch(port){

		case 1:{
			PORTA = habilit_port(display_habilit);
			break;
		}

		case 2:{
			PORTB = habilit_port(display_habilit);
			break;
		}

		case 3:{
			PORTC = habilita_port(display_habilit);
			break;
		}

		case 4:{
			PORTD = habilita_port(display_habilit);	
			break;
		}

	}

}
Code:
void desabilit_port(unsigned int port){

	switch(port){

		case 1:{
			PORTA = 0x00;
			break;
		}

		case 2:{
			PORTB = 0x00;
			break;
		}

		case 3:{
			PORTC = 0x00;
			break;
		}

		case 4:{
			PORTD = 0x00;
			break;
		}

	}

}
Code:
void reset_numbers(int *num1, int *num2, int *num3, int *num4, int *num5, int *num6){

	*num1=0;
	*num2=0;
	*num3=0;
	*num4=0;
	*num5=0;
	*num6=0;

}
Code:
void write_display(int *num1, int *num2, int *num3, int *num4, int *num5, int *num6, int port1, int port2){

//Segundos
	//Unidade
	habilit_port_number(port1, *num1);
	habilit_port_habilit(port2, 0);
	Delay1KTCYx(1);
	desabilit_port(4);

	//Dezena
	habilit_port_number(port1, *num2);
	habilit_port_habilit(port2, 1);
	if(*num1==10){
		(*num2)++;
	}
	Delay1KTCYx(1);
	desabilit_port(4);
	if(*num1==10){
		(*num1)=0;
	}

//Minutos
	//Unidade
	habilit_port_number(port1, *num3);
	habilit_port_habilit(port2, 2);
	if(*num2==6){
		(*num3)++;
	}
	Delay1KTCYx(1);
	desabilit_port(4);
	if(*num2==6){
		(*num2)=0;
	}

	//Dezena
	habilit_port_number(port1, *num4);
	habilit_port_habilit(port2, 3);
	if(*num3==10){
		(*num4)++;
	}
	Delay1KTCYx(1);
	desabilit_port(4);
	if(*num3==10){
		(*num3)=0;
	}

//Horas
	//Unidade
	habilit_port_number(port1, *num5);
	habilit_port_habilit(port2, 4);
	if(*num4==6){
		(*num5)++;
	}
	Delay1KTCYx(1);
	desabilit_port(4);
	if(*num4==6){
		(*num4)=0;
	}

	//Dezena
	if((*num5)==5 && (*num6)==2){
			*num1=0;
			*num2=0;
			*num3=0;
			*num4=0;
			*num5=0;
			*num6=0;
	}else{
		habilit_port_number(port1, *num6);
		habilit_port_habilit(port2, 5);
		if((*num5)==10){
				(*num6)++;
		}
		Delay1KTCYx(1);
		desabilit_port(4);
	}
}
Code:
void main(void){
	
	TRISC= 0x00;
	TRISD= 0b00000000;
	PORTC=0xFF;
	PORTD= 0b00000000;
	
	reset_numbers(&unidadesegundo, &dezenasegundo, &unidademinuto, &dezenaminuto, &unidadehora, &dezenahora);
	
	while(1){
	
		int i;
		for(i=0;i<40;i++){
			write_display(&unidadesegundo, &dezenasegundo, &unidademinuto, &dezenaminuto, &unidadehora, &dezenahora, 3, 4);
			desabilit_port(4);
			if(i==39){
				unidadesegundo++;
			}
		}
	
	}

}

How can I connect a keypad-phone to my code and print at the display the numbers that the user choose on the keypad? Assuming that my keypad is conected on PORTB. :|

Thanks for any help.
 
Status
Not open for further replies.

EE World Online Articles

Loading
Top