Académique Documents
Professionnel Documents
Culture Documents
Programa principal:
// D:\UPF\Placa_Aq_Paralela\Placa Mônica\Programação PIC\mnca_pic.c
//control bits(entrada):
// PIN_D0 = indica recebimento de comando
// PIN_D1 = receber(1) ou enviar(0)
// PIN_D2 = mandar segundo bit
//data out bits: PIN_D3 -> PIN_D7
#include <16F877.h>
#include <math.h>
#use delay(clock=20000000)
#fuses HS,NOWDT,NOBROWNOUT,NOLVP
#use FAST_IO(B)
#use FAST_IO(C)
#use FAST_IO(D)
#use FAST_IO(E)
/************************************************************************************************/
} // fim func_receber
/************************************************************************************************/
output_high(PIN_D6); // sinaliza OK 1
if (num_port == 2) {
output_low(PIN_C3); //baixar clock do A/D 1
output_low(PIN_C0); //baixar CS d/a 1
for(i=12; i>0 ; i--)
{
Programação do PIC 16F877 2
if (i >= 8)
{
valorDA = (data_high & bit_serial);
}
else if (i >= 3)
{
if (i == 7) { bit_serial = 16;}
valorDA = (data_low & bit_serial);
}
else
{
valorDA = 0;
}
bit_serial = bit_serial / 2;
output_low(PIN_C3); //baixar clock do a/d 1
output_bit(PIN_E2,valorDA);
output_high(PIN_C3); //fim do pulso
}
output_low(PIN_C3);
output_high(PIN_C0); //ativar CS D/A 1
}
else if (num_port == 1) {
output_low(PIN_C5); //baixar clock do A/D 1
output_low(PIN_C4); //baixar CS d/a 1
for(i=12; i>0 ; i--)
{
if (i >= 8)
{
valorDA = (data_high & bit_serial);
}
else if (i >= 3)
{
if (i == 7) { bit_serial = 16;}
valorDA = (data_low & bit_serial);
}
else
{
valorDA = 0;
}
bit_serial = bit_serial / 2;
else if (num_port == 0) {
output_low(PIN_C7); //baixar clock do A/D 1
output_low(PIN_C6); //baixar CS d/a 1
for(i=12; i>0 ; i--)
{
if (i >= 8)
{
valorDA = (data_high & bit_serial);
}
else if (i >= 3)
{
if (i == 7) { bit_serial = 16;}
valorDA = (data_low & bit_serial);
}
else
{
valorDA = 0;
}
bit_serial = bit_serial / 2;
output_low(PIN_C7); //baixar clock do a/d 1
output_bit(PIN_E2,valorDA);
output_high(PIN_C7); //fim do pulso
}
output_low(PIN_C7);
output_high(PIN_C6); //ativar CS D/A 1
}
output_high(PIN_D6); // sinaliza OK 3
} // fim fun_enviar
/***********************************************************************/
void func_enviar_digital(int num_port) {
int valor;
if (port_b & 16) {valor = 1;}
else {valor = 0; }
switch (num_port) {
case 0: output_bit(PIN_E1,valor);
break;
case 1: output_bit(PIN_E0,valor);
Programação do PIC 16F877 3
break;
default:
}; // end switch
output_high(PIN_D6); // sinaliza OK 1
}//fim func_enviar_digital
/************************************************************************************************/
output_high(PIN_D6); // sinaliza OK 1
}//fim func_receber_digital
/************************************************************************************************/
output_high(PIN_D6); // sinaliza OK 1
output_low(PIN_D6); // sinaliza OK 2
if (num_port == 0) {
set_pwm1_duty(duty_cycle); // define duty-cycle 1
}
else if (num_port == 1) {
set_pwm2_duty(duty_cycle); // define duty-cycle 2
}
output_high(PIN_D6); // sinaliza OK 3
} // fim func_pwm_duty
/************************************************************************************************/
div_fr = port_b;
if (tipo == 0) {
setup_timer_2(T2_DIV_BY_1,div_fr,1);
}
else if (tipo == 1) {
setup_timer_2(T2_DIV_BY_4,div_fr,1);
}
else if (tipo == 2) {
setup_timer_2(T2_DIV_BY_16,div_fr,1);
}
output_low(PIN_D6); // sinaliza OK 2
} // fim func_pwm_freq
/************************************************************************************************/
main() {
int num_port;
setup_adc_ports(RA0_RA1_RA3_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
SET_ADC_CHANNEL(0); //Usa conversão do pino RA0
while (TRUE) {
case 0: func_enviar(num_port);
break;
case 1: func_receber(num_port);
break;
case 2: func_pwm_duty(num_port);
break;
case 3: func_pwm_freq(num_port);
break;
case 4: func_enviar_digital(num_port);
break;
case 5: func_receber_digital(num_port);
break;
default: break;
}
} // end if 1
} // end while
float sqrt(float x)
{
float y, res, r;
int n;
y = x;
*(&y) = 0x7E;
if (y >= 0.9994)
res = 0.5 + 0.5*y;
else
{
res = ps[0]*y + ps[1];
res = res*y + ps[2];
res = res*y + ps[3];
r = qs[0]*y + qs[1];
r = r*y + qs[2];
r = r*y + qs[3];
res = res/r;
}
n = *(&x);
*(&res) = (n + 1)/2 + 63;
if (n & 1)
res = res/SQRT2;
return(res);
}
/***********************************************************/
float SIN_COS(float x, unsigned int shift)
{
float f, p, r, rs, *a;
long n;
int i, j;
short neg = 0;
if (x < 0.0)
{
x = -x;
if (!shift)
neg = 1;
}
f = x*TWOBYPI;
n = (long)(f > 0 ? f+0.5 : f-0.5);
f = (float)n;
r = x - f;
i = 0;
p = (float)pibytwo[0];
f = f/256.0;
r -= f*p;
i = 1;
Programação do PIC 16F877 6
p = (float)pibytwo[1];
f = f/256.0;
r -= f*p;
i = 2;
p = (float)pibytwo[2];
f = f/256.0;
r -= f*p;
i = 3;
p = (float)pibytwo[3];
f = f/256.0;
r -= f*p;
i = 4;
p = (float)pibytwo[4];
f = f/256.0;
r -= f*p;
if (neg)
return -f;
else
return f;
}
float sin(float x)
{
return(SIN_COS(x, 0));
}
float cos(float x)
{
return(SIN_COS(x, 1));
}
float tan(float x)
{
float c, s;
Programação do PIC 16F877 7
c = SIN_COS(x, 1);
s = SIN_COS(x, 0);
if (c != 0.0)
return(s/c);
return(1.0e+36);
}
s = 0;
y = x;
if (x < 0)
{
s = 1;
y = -y;
}
if (y > 0.5)
{
y = sqrt((1.0 - y)/2.0);
n += 2;
}
r = qas[0]*y*y + qas[1];
r = r*y*y + qas[2];
res = y*res/r;
return(res);
}
float asin(float x)
{
float r;
r = ASIN_COS(x, 0);
return(r);
}
float acos(float x)
{
float r;
r = ASIN_COS(x, 1);
return(r);
}
/************************************************************/
float atan(float x)
{
float y, res, r;
int s, flag;
s = 0;
flag = 0;
y = x;
if (x < 0)
{
s = 1;
y = -y;
}
if (y > 1.0)
{
y = 1.0/y;
flag = 1;
}
r = qat[0]*y*y + qat[1];
r = r*y*y + qat[2];
r = r*y*y + qat[3];
res = y*res/r;
return(res);
}
/***********************************************************/
s = 0;
y = x;
if (x < 0)
{
s = 1;
y = -y;
}
if (y <= 32768.0)
res = (float)(long)y;
else
res = y;
y = y - (float)(long)y;
if (s)
res = -res;
if (y != 0)
{
if (s == 1 && n == 0)
res -= 1.0;
if (s == 0 && n == 1)
res += 1.0;
}
if (x == 0)
res = 0;
return (res);
}
float floor(float x)
{
float r;
r = CEIL_FLOOR(x, 0);
}
float ceil(float x)
{
float r;
r = CEIL_FLOOR(x, 1);
}
/***********************************************************/
float exp(float x)
{
float y, res, r;
signed int n;
int s;
n = (signed int)(x/LN2);
s = 0;
y = x;
if (x < 0)
{
s = 1;
n = -n;
y = -y;
}
res = 0.0;
*(&res) = n + 0x7F;
y = y/LN2 - (float)n;
r = pe[0]*y + pe[1];
r = r*y + pe[2];
Programação do PIC 16F877 10
r = r*y + pe[3];
r = r*y + pe[4];
r = r*y + pe[5];
if (s)
res = 1.0/res;
return(res);
}
/************************************************************/
float log(float x)
{
float y, res, r;
signed n;
y = x;
if (y != 1.0)
{
*(&y) = 0x7E;
y = (y - 1.0)/(y + 1.0);
r = ql[0]*y*y + ql[1];
r = r*y*y + ql[2];
r = r*y*y + ql[3];
res = y*res/r;
n = *(&x) - 0x7E;
if (n<0)
r = -(float)-n;
else
r = (float)n;
res += r*LN2;
}
else
res = 0.0;
return(res);
float log10(float x)
{
float r;
r = log(x);
r = r/LN10;
return(r);
}
#define PIN_B0 48
#define PIN_B1 49
#define PIN_B2 50
#define PIN_B3 51
#define PIN_B4 52
#define PIN_B5 53
#define PIN_B6 54
#define PIN_B7 55
#define PIN_C0 56
#define PIN_C1 57
#define PIN_C2 58
#define PIN_C3 59
#define PIN_C4 60
#define PIN_C5 61
#define PIN_C6 62
#define PIN_C7 63
#define PIN_D0 64
#define PIN_D1 65
#define PIN_D2 66
#define PIN_D3 67
#define PIN_D4 68
#define PIN_D5 69
#define PIN_D6 70
#define PIN_D7 71
#define PIN_E0 72
#define PIN_E1 73
#define PIN_E2 74
#define ANALOG_RA3_RA2_REF 0x88 // RA0 RA1 RA5 RE0 RE1 RE2 Ref=RA2,RA3
#define ANALOG_NOT_RE1_RE2 0x89 // RA0 RA1 RA2 RA3 RA5 RE0 Ref=Vdd
#define ANALOG_NOT_RE1_RE2_REF_RA3 0x8A // RA0 RA1 RA2 RA5 RE0 Ref=RA3
#define ANALOG_NOT_RE1_RE2_REF_RA3_RA2 0x8B // RA0 RA1 RA5 RE0 Ref=RA2,RA3
#define A_ANALOG_RA3_RA2_REF 0x8C // RA0 RA1 RA5 Ref=RA2,RA3
#define RA0_RA1_ANALOG_RA3_RA2_REF 0x8D // RA0 RA1 Ref=RA2,RA3
#define RA0_ANALOG 0x8E // RA0
#define RA0_ANALOG_RA3_RA2_REF 0x8F // RA0 Ref=RA2,RA3
///////////////////////////////////// Constants used for SETUP_ADC()
#define ADC_OFF 0
#define ADC_CLOCK_DIV_2 1
#define ADC_CLOCK_DIV_8 0x41
#define ADC_CLOCK_DIV_32 0x81
#define ADC_CLOCK_INTERNAL 0xc1
#list