Funções e rotinas com mikroc

Estou pretendendo fazer o controle de velocidade de uma ventoinha através da leitura de um sensor de temperatura (LM35). Alem do controle pretendo acionar alguns ledes conforme o valor da temperatura e também mostrar em um display o valor da temperatura e do duty cicle - PWM. Achei na internet um programa destes que pretendo adaptar e fazer o meu, porém fazendo alguns testes utilizando o mikroc e proteus ainda não deu certo. Embora o programa compile sem algum erro quando simulo no proteus não funciona nada. O programa é mais ou menos assim:
- Código: Selecionar todos
void pre_main();
void test_led_fan();
void read_lm35();
void led_control();
void pwm();
// Conexões LCD 2x16
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// Final Conexões LCD 2x16
//Declaração de variáveis globais
unsigned int temp;
void main()
{
//função iniciação
pre_main();
//função teste led e fan
test_led_fan();
while(1)
{
//leitura LM35
read_lm35();
//controla led's perante temp
led_control();
//Velocidade fan
pwm();
}
}
void pre_main()
{
//Declaração de info das portas
//ADCON0 = 0x81;
//ADCON1 = 0xCE;
adcon1 = 0b00000100;
trisa = 0XFF; // portA entrada (trisa = 1)
trisb = 0; // portB saída (trisb = 0x00)
trisc = 0; // portC saída
trisd = 0; // portD saída
PORTD = 0; // portC nível lógico 0
//Iniciar PWM
PWM1_Init(1500);
//PWM Start
pwm1_start();
PWM1_set_Duty(0);
//Iniciar adc
adc_init();
//Iniciar Lcd
Lcd_Init();
//Iniciar Uart1
UART1_Init(9600);
Lcd_Cmd(_Lcd_CLEAR);
Lcd_Cmd(_Lcd_CURSOR_OFF);
}
void test_led_fan()
{
int j=0;
Lcd_Out(1, 1, "Aguarde: Teste");
Lcd_Out(2, 2, "Led's e Fan");
UART1_Write_text("Aguarde: TESTE\r");
UART1_Write_text("Led's e Fan\r");
do{
portd.f0=1; //verde
portd.f1=1; //amarelo
portd.f2=1; //vermelho
delay_ms(200);
portd.f0=0;
portd.f1=0;
portd.f2=0;
delay_ms(200);
j++;
pwm1_set_duty(255);
delay_ms(200);
pwm1_set_duty(0);
}
while(j!=2);
Lcd_Cmd(_Lcd_CLEAR);
Lcd_Out(1, 1, "Aguarde");
Lcd_Out(2, 2, "Led's e Fan OK");
UART1_Write_text("Led's e Fan OK\r");
delay_ms(200);
Lcd_Cmd(_Lcd_CLEAR);
}
void read_lm35()
{
char txt[15];
const unsigned short VREF = 5;
unsigned int adc_rd = 0;
adc_rd = ADC_Get_Sample(0);
temp = (adc_rd * VREF)/(1000*240);
//temp = ((unsigned long)adc_rd * VREF * 100)/1024;
floatToStr(temp, txt);
txt[4] =0;
//
UART1_Write_text(txt);
UART1_Write_text("\r");
Lcd_Out(1, 1, "Temp || Potencia");
Lcd_Chr(2, 5, 223);
Lcd_Chr(2,6,'C');
Lcd_Out(2, 1, txt);
delay_ms(10);
}
void led_control()
{
//indicação dos led's perante temperatura
if (temp <50)
{
portd.f0=1;
portd.f1=0;
}
else if (temp >51 && temp <69)
{
portd.f0=1;
portd.f1=1;
portd.f2=0;
}
else if (temp >70)
{
portd.f0=1;
portd.f1=1;
portd.f2=1;
}
}
void pwm()
{
int i=0;
if(temp > 70)
i=255;
else
{
i=temp*3;
}
PWM1_set_Duty(i);
}