#include "control_pwm.h"
void main()
{
unsigned short pot1,pot2;
init();
pwm_init();
while(1)
{
pot1=adc_measure(0);
pot2=adc_measure(3);
set_pwm(1,pot1); //muestra la modulacion de channel Ra0
delay_ms(10);
}
}
//////////////////////////////////pwm 16f88///////////////////////////////////
void pwm_init(void)
{
t2con=0000100b;
pr2=11111111b;
adc_init();
}
void set_pwm(char channel,unsigned short pwm_val)
{
char lastbits;
union
{
unsigned short sht;
char ch[2];
}temp;
temp.sht =pwm_val;
lastbits =(temp.ch[0]&0x03)<<4;
temp.sht =temp.sht>>2;
if(channel==1)
{
ccpr1l =temp.ch[0];
ccp1con &=00001100b;
ccp1con |=lastbits;
}
}
void init(void)
{
adcon0=00000000b;
adcon1=10000000b;
ansel=11111111b;
trisa=11111111b;
trisb=00000000b;
porta=0;
portb=0;
adc_init();
}
//*****configuracion de adc para el 16F88*****//
void adc_init(void)
{
adcon0.ADCS1=1; // Select Tad = 32 * Tosc
adcon0.ADCS0=0; // (esto depende del X-tal aquí 10 MHz, deben trabajar hasta 20 MHz)
adcon0.ADON =1; // Activa el modulo ADC
}
short adc_measure(char channel)
{
// Clear channel selection in adcon0
adcon0 &= 11000111b;
// Shift channel value to appropriate position
channel = (channel & 0x07) << 3;
// Move it to appropriate position in adcon0
adcon0 |= channel;
// Wait required acquisition time (see datasheet)
delay_10us(4);
// Startconversion
adcon0.GO = 1;
// Wait until done
#ifndef DEBUG
while (adc_go) ;
#endif
#ifndef DEBUG
delay_10us(4);
#endif
adcon0.GO = 0;
short retval;
retval = (short)adresh;
retval = retval << 8;
retval |= adresl;
return retval;
}