You are on page 1of 5

/*

* File: main2.c
* Author: red02
*
* Created on 18 de octubre de 2015, 12:40 PM
*/
#include <xc.h>

// Libreria del compilador xc8

// Zona de definiciones
#define PI

3.1416

#define Mensaje "Hola Mundo"


#define MSG1

"Frio"

#define MSG2

"Normal"

#define MSG3

"Caliente"

// Zona de variables (Memoria RAM)


unsigned char a,b,c; // 3 variables de 8 bits sin signo
signed int sal; // variable de 16 bits con signo
signed long int sal2; // variable de 24 bits con signo
float d; // variable de punto flotante

unsigned char temperatura;


unsigned char vent, calef;
unsigned char msg[] = MSG1;

unsigned char valores[10];

unsigned char i;

// Declaracion de la funcion
void ControlTemperatura(unsigned char);

// Funcion principal
void main() {
// Configuraciones iniciales
a = 5; // asignacion en formato decimal
a = 0b00000101; // asignacion en formato binario
a = 0x05; // asignacion en formato hexadecimal

a = 0b10101010; // a = 170
b = 0b11110000; // b = 240

// Operaciones logicas
// AND
c = a & b; // c = 0b10100000
// OR
c = a | b; // c = 0b11111010
// XOR (OR exclusivo)
c = a ^ b; // c = 0b01011010
// NOT
c = ~a;

// c = 0b01010101

// Desplazamiento Izquierda

c = a << 4; // c = 0b10101010 << 4 = 0b10100000

// Desplazamiento Derecha
c = b >> 3; // c = 0b11110000 >> 3 = 0b00011110

// Operaciones aritmeticas
sal = a + b;
sal = a - b;

// sal = 170 + 240 = 410


// sal = 170 - 240 = -70

sal2 = (signed long int)(a) * (signed long int)(b); // sal2 = 170 * 240 =
40800
sal = b / a;

// sal = 240 / 170 = 1

d = (float)(b * 1.0 / a); // d = 240.0 / 170 = 1.4117

// Sentencia IF-ELSE
if(temperatura < 20){
calef = 50;
vent = 0;
} else if((temperatura >= 20) && (temperatura < 23)){
calef = 10;
vent = 0;
} else if((temperatura >= 23) && (temperatura < 25)){
calef = 0;
vent = 10;
} else{ // temp >= 25
calef = 0;
vent = 50;
}

// Estructura WHILE
i = 0;
while(i<10){
valores[i] = i+3;
i++;
}

// Estructura FOR
for(i = 0;i < 10;i++){
valores[i] = i+3;
}

// Llamado de la funcion
temperatura = 22;
ControlTemperatura(temperatura);

// Bucle principal
while(1){

}
}

// Creacion de una funcion


void ControlTemperatura(unsigned char temp){
if(temp < 20){

calef = 50;
vent = 0;
} else if((temp >= 20) && (temp < 23)){
calef = 10;
vent = 0;
} else if((temp >= 23) && (temp < 25)){
calef = 0;
vent = 10;
} else{ // temp >= 25
calef = 0;
vent = 50;
}
}

You might also like