Buenas :-) Aquí me he dejado caer, aficionado y profesional de la electrónica desde hace años... algunos me conoceréis de una página muy conocida de modding.
Weno, al grano... echando un ojo por estos foros encontré este post... y me llamó muchísimo la atención el código que ha posteado Piries para el control de velocidad y temperaturas con el 16F876... weno... me ha llamado tanto la atención porque ese código lo conozco... se trata de código modificado de un proyecto realizado hace un año de un sistema de control de temperaturas para servidores que hemos desarrollado un grupo de personas y que tiene como nombre HM-Baybus.
Al margen de que me parece MUY MAL que Piries (que ya hablaré contigo en la web de modding, ejem) se te olvide citar la procedencia original de ese código e insinúes que forma parte de un proyecto tuyo, sólo entro para comentar que el proyecto completo para el sistema de control de temperaturas con pic llamado HM-Baybus puede ser descargado gratuítamente utilizando las redes P2P o enviándome un correo electrónico (o un MP) pidiéndome la memoria completa del proyecto.
Por otro lado, aprovecho para postear el código completo del módulo esclavo.
Código:
/***********************************************
Slave module HM-Baybus system
Manuel Díaz García
Computer Systems
European University of Madrid
September 2004
More informaciónrmation than comments in:
http://www.microchip.com (for 16F876 información)
http://www.ccsinformación.com (for CCS compiler información)
***********************************************/
/*Define the I2C operation mode: SLAVE=I2C slave mode,
SCL=specifies de SCL pin, SDA=specifies de SDA pin,
FAST=fast I2C especification,
FORCE_HW=Use hardware I2C functions (only if micro support)*/
/*Define the RS232 operation mode:
BAUD=Set baud rate, XMIT=Set transmit pin, RCV=Set receive pin*/
/*The fast method of doing I/O will cause the compiler to perform I/O
without programming of the direction register*/
/*Directive to reserve memory in compilation from 0x1F00 to 0x1FFF to
the bootloader code (more informaciónrmation in http://www.microchipc.com)*/
//Include the library with specific functions to project and 16F876
typedef enum {NOTHING,CONTROL_READ,ADDRESS_READ,READ_COMMAND_READ} I2C_STATE;
byte slaveinformacións[0x8];
short update = 1;
unsigned int16 tach1=0, tach2=0;
void ssp_interupt (){
static i2c_state fstate=NOTHING;
static byte address=0x00;
static byte incoming=0;
if (i2c_poll() == FALSE) {
if (fState == ADDRESS_READ) {
i2c_write (slaveinformacións[address]);
fState = NOTHING;
}
}
else {
incoming = i2c_read();
if (fState == NOTHING){
fState = CONTROL_READ;
}else if (fState == CONTROL_READ) {
address = incoming;
fState = ADDRESS_READ;
}else if (fState == ADDRESS_READ) {
slaveinformacións[address] = incoming;
fState = NOTHING;
}
}
}
void detect_rb_change() {
short enc1;
short enc2;
static short last_enc1=1;
static short last_enc2=1;
enc1 = input(PIN_B5);
enc2 = input(PIN_B6);
if (last_enc1 && !enc1) {tach1++;}
if (last_enc2 && !enc2) {tach2++;}
last_enc1=enc1;
last_enc2=enc2;
}
void initmodule(){
int i;
for (i=0;i<0x08;i++)
slaveinformacións[i] = 0; //Init values array
setup_timer_2(T2_DIV_BY_1,9,1); //4Mhz, 100 Khz PWM module
output_high(PIN_C1); //Init Pin C1 as output
output_high(PIN_C2); //Init Pin C2 as output
setup_ccp1(CCP_PWM); //Mode PWM CPP1
setup_ccp2(CCP_PWM); //Mode PWM CPP2
slaveinformacións[0]=10; //Init PWM Channel 1 value
slaveinformacións[1]=10; //Init PWM Channel 2 value
set_tris_A(0x2F);
setup_adc_ports(A_ANALOG_RA3_REF);
setup_adc(ADC_CLOCK_DIV_32);
setup_timer_0(RTCC_INTERNAL | RTCC_DIV_256); //Setup Timer0 to interrupt (1/(4 Mhz/256))=0,064 sec.
set_timer0 (8);
enable_interrupts(int_timer0);
set_tris_b(0xF0);
enable_interrupts(INT_RB);
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
}
void timer_0_isr (void) {
static byte timer_0_count = 0;
if (timer_0_count < 31) {
timer_0_count++;
} else {
update = 1;
timer_0_count = 0;
slaveinformacións[2]=tach1;
slaveinformacións[3]=tach2;
tach1=0;
tach2=0;
}
set_timer0 (8);
}
void updateinformacións(){
int temp;
static byte duty1=10,duty2=10,channel=0; //Static variables
if (duty1!=slaveinformacións[0]){ //Verify if the duty1 has been changed
duty1=slaveinformacións[0]; //Save the new value
set_pwm1_duty(duty1); //Init PWM1 (10=100%..0=0%)
}
if (duty2!=slaveinformacións[1]){ //Verify if the duty1 has been changed
duty2=slaveinformacións[1]; //Save the new value
set_pwm2_duty(duty2); //Init PWM2 (10=100%..0=0%)
}
if (channel==0){ //Verify the channel that has to update
slaveinformacións[4]=(int8) ((getadc(1)*500)/1024); //Read Temperature channel 0
temp = getadc(0);
printf("Canal 6 I1=%i PWM1=%i\r",temp,slaveinformacións[0]);
if (slaveinformacións[0]>1 && slaveinformacións[2]==0){
if (slaveinformacións[6]==0){
slaveinformacións[6]=1;
} else {
if (slaveinformacións[6]==2){
slaveinformacións[6]=3;
}
}
}
channel=1;
} else {
slaveinformacións[5]=(int8) ((getadc(4)*500)/1024); //Read Temperature channel 1
temp = getadc(2);
printf("Canal 6 I2=%i PWM2=%i\r",temp,slaveinformacións[1]);
if (slaveinformacións[1]>1 && slaveinformacións[3]==0){
if (slaveinformacións[6]==0){
slaveinformacións[6]=2;
} else {
if (slaveinformacións[6]==1){
slaveinformacións[6]=3;
}
}
}
channel=0;
}
if (slaveinformacións[7]==1){
delay_ms(500);
resetcpu();
}
}
void main (){
loader16F876();
initmodule();
delay_ms(2000);
while (TRUE) {
if (update) {
updateinformacións();
update = 0;
}
}
} El programa que ha de instalarse en el chip maestro, que es el elemento más importante, ya que incorpora el algoritmo PID de control de velocidad no lo posteo porque es enorme, muy enorme (culpa de los menús del LCD, jejeje) pero lo mismo, disponible de forma gratuíta para quien quiera realizar este montaje o derivados, pero SIEMPRE CITANDO LA FUENTE, PROCEDENCIA ORIGINAL DEL CÓDIGO Y EL PROYECTO.
Siento hacer estas cosas, pero concretamente en este HM-Baybus hemos trabajado un grupo de personas durante casi DOS AÑOS para tenerlo listo... y no nos gustaría que alguien se apropiara de este esfuerzo.
Mi email información(at)d-zins(dot)com para quien necesite la memoria y los ficheros.
Un saludo