Programació en C del PIC 16F690

Referència Trucs Perifèrics   Recursos CITCEA
Tutorial Exemples Projectes   Inici

Bàscula electrònica

Programa del grup 2

En aquest cas van considerar que la bàscula tenia un límit màxim de 3 kg. L'usuari pot indicar el número de persones i la bàscula li indicarà el pes mesurat i també el pes per persona.

El polsador BTN1 serveix per guardar el pes actual com a tara i el BTN2 per eliminar la tara i mostrar directament els pesos. El polsador BTN3 permet passar de grams a kilograms i al contrari. Els polsadors BTN4 i BTN5 permeten augmentar i disminuir el nombre de persones per al càlcul de racions.

El programa és el següent:

#pragma config FOSC = INTRCIO, WDTE = OFF, PWRTE = OFF, MCLRE = OFF, CP = OFF
#pragma config CPD = OFF, BOREN = OFF, IESO = OFF, FCMEN = OFF
#include "pic16f690.h"				// Carrega el fitxer d'adreces i paràmetres del PIC 16F690
#include <xc.h>					// Carrega el fitxer de funcions necessari per al compilador XC8
#define pols RA3
#define _XTAL_FREQ  8000000
unsigned short long Lectura;
char Polsador(void);
char Polsad;
char Port;					
unsigned short long LlegirHX(void);
void Cursor(char Filera, char Columna);		
void Escriu1(unsigned short long Valor);
void Nombre(unsigned short long Valor);
void Escriu(unsigned short long Valor);		
void EnviaL(char Caracter);                 
void Esborra(void);                 
void main (void) {
	ANSEL = 0b00000101;			
	ANSELH = 0;				
	TRISC = 0b00010000;			
	TRISB = 0;				
	TRISA = 0b11111111;			
	TXSTAbits.BRGH = 1;			
	BAUDCTLbits.BRG16 = 0;			
	SPBRG = 25;				
	TXSTAbits.SYNC = 0;			
	TXSTAbits.TX9 = 0;			
	RCSTAbits.SPEN = 1;			
	TXSTAbits.TXEN = 1;			
	PORTB = 0;				
	PORTC = 0;
	int Q;
	int L;
	int J;
	float Tara;
	float G;
	float F;
	float A;
	float C;
	float Tara2;
	ADCON1 = 0b00010000;			
	ADCON0 = 0b00001001;			
	A=0.0046189;
	Q=1;
	F=0;
	J=0;
	L=1;
	Tara2=0;
	Tara = LlegirHX()*A;
	while(1){
		if ((C)>3000){
			Cursor(2,1);
			EnviaL('M');
			EnviaL('A');
			EnviaL('X');
			RC0 = 1;
			RC1 = 1;
			RC2 = 1;
			RC3 = 1;
			__delay_ms(1000);
                   
		}
		Polsad =Polsador();
		C=Lectura*A-Tara;
		Lectura = LlegirHX();		
		Esborra(); 
		if (Polsad==4){
			Q=Q+1;
			J=1;
		}
		if (Polsad==5){
			Q=Q-1;
			J=1;
		}
		if (Q<1){
			Q=1;
		}
		if ((Polsad==0)&&((C)<3000)){
			if ((C-Tara2)>0){
				Escriu(C-Tara2);
				if (G==1){
					RC0 = 1;
				}
			}
			if ((C-Tara2)<0){
				Escriu(F);
				if (G==1){
					RC0 = 1;
				}
			}
		}               
		if (Polsad==1){
			J=0;
			Tara2=C;
			G=1;
		}
		if (Polsad==2){
			J=0;
			G=0;
			Tara2=0;          
		}
		if (Polsad==3){
			L=L+1;
			J=0;
			__delay_ms(1000);
			while (L%2==0){
				if ((C)>3000){
					Cursor(2,1);
					EnviaL('M');
					EnviaL('A');
					EnviaL('X');
					RC0 = 1;
					RC1 = 1;
					RC2 = 1;
					RC3 = 1;
					__delay_ms(1000);
				}
				if (Polsad==4){
					Q=Q+1;
					J=1;
				}
				if (Polsad==5){
					Q=Q-1;
					J=1;
				}
				if (Q<1){
					Q=1;
				}
				Polsad =Polsador();
				C=Lectura*A-Tara;
				Lectura = LlegirHX();		
				Esborra(); 
				if ((Polsad==0)&&(C<3000)){
					if ((C-Tara2)>0){
						Escriu1(C-Tara2);
						if (G==1){
							RC0 = 1;
						}
					}
					if ((C-Tara2)<0){
						Escriu1(F);
						if (G==1){
							RC0 = 1;
						}
					}
				}               
				if (Polsad==1){
					Tara2=C;
					G=1;
					J=0;
				}
				if (Polsad==2){
					G=0;
					Tara2=0;
					J=0;
				}
				if (C<3000){
					Cursor(1,7);
					EnviaL('k');
					EnviaL('g');
				}
				if (J==1 ){
					if ((C-Tara2)<3000){
						Cursor(2,1);
						Nombre(Q);
						Cursor(2,4);
						Escriu1((C-Tara2)/Q);
						Cursor(2,10);
						EnviaL('k');
						EnviaL('g');
						EnviaL('/');
						EnviaL('p');
						EnviaL('e');
						EnviaL('r');
						EnviaL('s');
					}
				}
				if (Polsad==3){
					L=L+1;
					J=0;
				}
				__delay_ms(1000);		
			}
		}
		if (C<3000){
			Cursor(1, 7);
			EnviaL('g');
		}
		if (J==1){
			if ((C-Tara2)<3000){
				Cursor(2, 1);
				Nombre(Q);
				Cursor(2, 5);
				Escriu((C-Tara2)/Q);
				Cursor(2,11);
				EnviaL('g');
				EnviaL('/');
				EnviaL('p');
				EnviaL('e');
				EnviaL('r');
				EnviaL('s');
			}
		}
		__delay_ms(1000);
	}
}
unsigned short long LlegirHX(void) {
	unsigned short long Buffer;		
	Port = Port & 0b10111111;		
	PORTC = Port;
	for (int j = 1; j <= 24; j++){		
		Port = Port | 0b01000000;	
		PORTC = Port;
		Buffer = Buffer << 1;		
		if (RC4 == 1) {	
			Buffer = Buffer | 0b00000001;
		}
		Port = Port & 0b10111111;	
		PORTC = Port;
		__delay_us(3);	   	 	
	}
	Port = Port | 0b01000000;		
	PORTC = Port;
	__delay_us(6);	   	 		
	Port = Port & 0b10111111;		
	PORTC = Port;
	return Buffer;				
}
void Escriu(unsigned short long Valor) {
	char Digits[4];				
	unsigned short long Result;		
	Result = Valor % 10;
	Digits[0] = (char) Result;
	Valor = Valor / 10;
	Result = Valor % 10;
	Digits[1] = (char) Result;
	Valor = Valor / 10;
	Result = Valor % 10;
	Digits[2] = (char) Result;		
	Valor = Valor / 10;
	Result = Valor % 10;
	Digits[3] = (char) Result;		
	for (int j = 3; j >= 0; j--){		
		if ((j==3) && (Digits[j]==0)){
			Digits[j]=' ';   
		} else if ((j==2)&&(Digits[j]==0)&&(Digits[3]==' ')){
			Digits[j]=' ';
			EnviaL(' ');
		} else if ((j==1)&&(Digits[j]==0)&&(Digits[3]==' ')&&(Digits[2]==' ')){
			Digits[j]=' ';
		} else{
			Digits[j]=Digits[j]+'0';
			if ((j ==2)&& (Digits[3]!=' ')){
				EnviaL('.');
			} else if ((j==2)&& (Digits[3]==' ')){
				EnviaL(' ');
			}
		}
		EnviaL(Digits[j]);		
	}
}
void EnviaL(char Caracter) {
	TXREG = Caracter;			
	_delay(5);				
	while (PIR1bits.TXIF == 0);		
		;				
}
void Esborra(void) {
	EnviaL(254);				
	EnviaL(1);				
}
void Nombre(unsigned short long Valor) {
	char Nombre[2];				
	unsigned short long Result;		
	Result = Valor % 10;
	Nombre[0] = (char) Result;		
	Valor = Valor / 10;
	Result = Valor % 10;
	Nombre[1] = (char) Result;		
	for (int j = 1; j >= 0; j--){
		if ((j==1)&& (Nombre[j]==0)){
			Nombre[j]=' ';
		} else{
			Nombre[j] = Nombre[j] + '0';
		}
		EnviaL(Nombre[j]);
	}		
}
char Polsador(void) {
	char Pols=0;
	ADCON0bits.GO = 1;		
	while (ADCON0bits.GO == 1)	
		;
	if (ADRESH < 220 && ADRESH > 200) {
		Pols = 1;		
	}
	if (ADRESH < 194 && ADRESH > 174) {
		Pols = 2;		
	}
	if (ADRESH < 163 && ADRESH > 143) {
		Pols = 3;		
	}
	if (ADRESH < 90 && ADRESH > 70) {
		Pols = 4;		
	}
	if (ADRESH < 55 && ADRESH > 35) {
		Pols = 5;		
	}
	return Pols;
}
void Cursor(char Filera, char Columna) {
	char Posicio = 0;			
	if (Filera == 2) {
		Posicio = 64;			
	}
	if (Columna > 0 && Columna < 33) {	
		Posicio = Posicio + Columna;	
		Posicio = Posicio - 1;		
	}
	Posicio = Posicio + 128;		
	EnviaL(254);				
	EnviaL(Posicio);			
}
void Escriu1(unsigned short long Valor) {
	char Digit[4];				
	unsigned short long Result;		
	Result = Valor % 10;
	Digit[0] = (char) Result;
	Valor = Valor / 10;
	Result = Valor % 10;
	Digit[1] = (char) Result;
	Valor = Valor / 10;
	Result = Valor % 10;
	Digit[2] = (char) Result;		
	Valor = Valor / 10;
	Result = Valor % 10;
	Digit[3] = (char) Result;		
	for (int j = 3; j >= 0; j--){		
		Digit[j] = Digit[j] + '0';	
		if (j == 2) {		
			EnviaL(',');		
		}
		EnviaL(Digit[j]);		
	}
}

 

 

Llicència de Creative Commons
Aquesta obra d'Oriol Boix està llicenciada sota una llicència no importada Reconeixement-NoComercial-SenseObraDerivada 3.0.