Programação do PIC 16F877

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
/* Já testado para funções analógicas e pwm*/
#include <16F877.h>
#include <math.h>
#use delay(clock=20000000)
#fuses HS,NOWDT,NOBROWNOUT,NOLVP
#use
#use
#use
#use

FAST_IO(B)
FAST_IO(C)
FAST_IO(D)
FAST_IO(E)

#byte port_b = 0x06
#byte port_c = 0x07
#byte port_d = 0x08
/************************************************************************************************/
void func_receber(int num_port) {
long int data;
int num_port1;
if (num_port == 2)
{num_port1 = 3;}
else
{ num_port1 = num_port; }
set_adc_channel(num_port1);
delay_ms(20);
data = read_adc();
output_bit(PIN_D3, data & 1);
output_bit(PIN_D4, (data & 2)/2);
output_bit(PIN_D5, (data & 4)/4);
output_bit(PIN_D7, (data & 8)/8);
output_bit(PIN_D6, 1); //sinaliza OK 1
while ( (port_d & 7) != 2 )
{ }
output_bit(PIN_D3,
output_bit(PIN_D4,
output_bit(PIN_D5,
output_bit(PIN_D7,
output_bit(PIN_D6,

//espera segundo sinal

(data & 16)/16);
(data & 32)/32);
(data & 64)/64);
(data & 128)/128);
0); // sinaliza OK 2

while ( (port_d & 7) != 3 )
{ }

//espera terceiro sinal

output_bit(PIN_D3, (data & 256)/256);
output_bit(PIN_D4, (data & 512)/512);
output_bit(PIN_D6, 1); // sinaliza OK 3
} // fim func_receber
/************************************************************************************************/
void func_enviar(int num_port) {
int i,data_low,data_high;
int bit_serial = 16;
int valorDA = 0;
output_high(PIN_D6); // sinaliza OK 1
while ( (port_d & 7) != 2 )
{ }

//espera segundo sinal

data_low = port_b & 31;
output_low(PIN_D6); // sinaliza OK 2

1

} valorDA = (data_low & bit_serial). output_low(PIN_C5).valorDA).} valorDA = (data_low & bit_serial). //baixar CS d/a 1 for(i=12. //baixar clock do a/d 1 output_bit(PIN_E2. //fim do pulso } output_low(PIN_C3).} valorDA = (data_low & bit_serial).valorDA). } bit_serial = bit_serial / 2. i--) { if (i >= 8) { valorDA = (data_high & bit_serial). //fim do pulso } output_low(PIN_C5). i>0 . output_high(PIN_C5). i--) { if (i >= 8) { valorDA = (data_high & bit_serial). //baixar clock do A/D 1 output_low(PIN_C6). } else { 2 . } else { valorDA = 0. } else if (i >= 3) { if (i == 7) { bit_serial = 16. i--) { if (i >= 8) { valorDA = (data_high & bit_serial). output_high(PIN_C0). //baixar clock do A/D 1 output_low(PIN_C4). //ativar CS D/A 1 } else if (num_port == 1) { output_low(PIN_C5). if (num_port == 2) { output_low(PIN_C3). output_high(PIN_C4). } bit_serial = bit_serial / 2. //baixar CS d/a 1 for(i=12. //baixar clock do a/d 1 output_bit(PIN_E2. } else if (i >= 3) { if (i == 7) { bit_serial = 16. } //ativar CS D/A 1 else if (num_port == 0) { output_low(PIN_C7). i>0 . } else { valorDA = 0. i>0 . output_low(PIN_C3).Programação do PIC 16F877 while ( (port_d & 7) != 3 ) { } //espera terceiro sinal data_high = port_b & 31. //baixar clock do A/D 1 output_low(PIN_C0). } else if (i >= 3) { if (i == 7) { bit_serial = 16. output_high(PIN_C3). //baixar CS d/a 1 for(i=12.

case 3: exit_dig = input(PIN_A4). } bit_serial = bit_serial / 2. // sinaliza OK 1 }//fim func_receber_digital /************************************************************************************************/ void func_pwm_duty (int num_port) { long int duty. // sinaliza OK 1 }//fim func_enviar_digital /************************************************************************************************/ void func_receber_digital(int num_port) { int exit_dig. //indica valor(0/1) da saida digital desejada output_high(PIN_D6). long int data_low. // end switch output_high(PIN_D6). output_low(PIN_C7).valor). // sinaliza OK 1 while ( (port_d & 7) != 2 ) { } //espera segundo sinal data_low = port_b & 31. duty_cycle = (data_low + 32*data_high). } // define duty-cycle 1 // define duty-cycle 2 . output_high(PIN_D6). if (port_b & 16) {valor = 1. break. case 4: exit_dig = input(PIN_A2). } //ativar CS D/A 1 output_high(PIN_D6). output_high(PIN_C7). break.valor). // sinaliza OK 3 } // fim fun_enviar /***********************************************************************/ void func_enviar_digital(int num_port) { int valor.} else {valor = 0. //fim do pulso } output_low(PIN_C7). default: } // end switch output_bit(PIN_D3. default: }. case 1: output_bit(PIN_E0. switch (num_port) { case 2: exit_dig = input(PIN_A5). if (num_port == 0) { set_pwm1_duty(duty_cycle). // sinaliza OK 2 while ( (port_d & 7) != 3 ) { } //espera terceiro sinal data_high = port_b & 31.valorDA).max. break.Programação do PIC 16F877 3 valorDA = 0. long int duty_cycle. output_high(PIN_C6). } else if (num_port == 1) { set_pwm2_duty(duty_cycle). int resolution. //baixar clock do a/d 1 output_bit(PIN_E2. break. output_low(PIN_D6). } switch (num_port) { case 0: output_bit(PIN_E1.exit_dig).data_high. break.

// sinaliza OK 2 } // fim func_pwm_freq /************************************************************************************************/ main() { int num_port. } output_low(PIN_D6). //CS do D/A numero 2 //CS do D/A numero 1 //CS do D/A numero 0 // manter ACK baixo while (TRUE) { if ( (port_d & 7) == 1 ) { // sinaliza recebimento de comando num_port = (port_b & 224)/32. output_high(PIN_D6). //pwm desligado set_pwm2_duty(0). RD3-RD7 saida digital //PSPMODE = 0. output_high(PIN_C6). setup_timer_2(T2_DIV_BY_4. if (tipo == 0) { setup_timer_2(T2_DIV_BY_1. // sinaliza OK 3 } // fim func_pwm_duty /************************************************************************************************/ void func_pwm_freq(int tipo) { int div_fr. SET_TRIS_B(0xFF). case 1: func_receber(num_port). // 20Mhz / (4*1*(249+1)) = 20Khz setup_ccp2(CCP_PWM). setup_adc(ADC_CLOCK_INTERNAL). // numero da porta switch (port_b & 15) { // pode-se ter até 16 funções diferentes case 0: func_enviar(num_port). output_low(PIN_E2).div_fr.1).1).1).Programação do PIC 16F877 output_high(PIN_D6). SET_TRIS_D(0x07). //Usa conversão do pino RA0 set_pwm1_duty(0). SET_ADC_CHANNEL(0). SET_TRIS_A(0xFF). //port_a como entrada //RB0-RB7 entrada digital //RC0-RC7 saida digital //RD0-RD2 entrada digital. output_low(PIN_D6). case 2: func_pwm_duty(num_port). RE1 saidas digitais // RE2 saida digitais setup_ccp1(CCP_PWM). } else if (tipo == 2) { setup_timer_2(T2_DIV_BY_16. SET_TRIS_E(0x00). break. RE0. } else if (tipo == 1) { setup_timer_2(T2_DIV_BY_4. // sinaliza OK 1 while ( (port_d & 7) != 2 ) { } //espera segundo sinal div_fr = port_b.1). output_high(PIN_C4). case 3: func_pwm_freq(num_port). SET_TRIS_C(0x00). postscaler e PR2 setup_adc_ports(RA0_RA1_RA3_ANALOG). break.div_fr. break. // define prescaler. //pwm desligado output_high(PIN_C0). 4 .div_fr. break. break. case 4: func_enviar_digital(num_port).249.

0xD1}. //// //// Derivative programs created using this software in object code //// //// form are not restricted in any way.41421356 #define TWOBYPI 0. float const costable[5] = {2. 1. res = res*y + ps[3].1997 Custom Computer Services //// //// This source code may only be used by licensed users of the CCS C //// //// compiler.4934718}. /* coefficients in series expansion for sine and cosine in reverse order */ float sqrt(float x) { float y.00000000E-1. r. 4. 21. int const pibytwo[6] = {0x92.57079633 float const pas[3] = {0.00000000}. -1. 0x44.5*y. res. r = qs[0]*y + qs[1]. No other use. res = res*y + ps[2]. 0x42.125224.0000000. 5 . reproduction //// //// or distribution is permitted without written permission. This source code may only be distributed to other //// //// licensed users of the CCS C compiler.00000000}.764134. break.9994) res = 0.29730279}. -5. 8. if (y >= 0. 0. float const qs[4] = {1. else { res = ps[0]*y + ps[1].16666667E-2. 0xB5.035723.0000000.98412698E-4. float const sintable[5] = {2. -5.33333333E-3. -1. -4.Programação do PIC 16F877 case 5: func_receber_digital(num_port). int n. 1.6036290}. //// //////////////////////////////////////////////////////////////////////////// #define SQRT2 1. 0x1F. *(&y) = 0x7E. 8.5484666. float const qas[3] = {1. default: break.9403076.6145309.6366197724 float const ps[4] = {5. 2.49559947.5 + 0. 5.48015873E-5. 5. // sinaliza OK 7 (FIM) } // end if 1 } // end while } // D:\UPF\Placa_Aq_Paralela\Placa Mônica\Programação PIC\Math.h //////////////////////////////////////////////////////////////////////////// //// (C) Copyright 1996. r = r*y + qs[2]. y = x. 17. 15.75573192E-6.38888889E-3. } while ( (port_d & 7) != 7 ) // espera sétimo sinal = FIM {} output_low(PIN_D6).66666667E-1.6036290}. -1. #define PIDIVBYTWO 1.9304945.

short neg = 0. f = f/256. n = (long)(f > 0 ? f+0. j++) { rs = costable[j].Programação do PIC 16F877 r = r*y + qs[3].0. r. } res = res/r.0. i = 0. r -= f*p. long n. p = (float)pibytwo[0].5). i = 4. n = *(&x). p = (float)pibytwo[1]. int i.0. f = f/256. rs. if (!shift) neg = 1. if (x < 0. } f = x*TWOBYPI. /***********************************************************/ float SIN_COS(float x. r -= f*p.0. } 6 . p = (float)pibytwo[4]. r = x . f = f/256. f = f/256. i = 1. i = 3. p = (float)pibytwo[3]. rs = rs*r*r + costable[j+1]. r -= f*p. if (shift & 0x1) { for (j = 0. j < 4.0. unsigned int shift) { float f. p = (float)pibytwo[2]. f = (float)n. *(&res) = (n + 1)/2 + 63.0) { x = -x. i = 2. *a. p.5 : f-0. r -= f*p. } return(res). f = f/256. j.f. if (n & 1) res = res/SQRT2. shift += (unsigned int)(n & 0x3). r -= f*p.

5) { y = sqrt((1. } 7 . } float tan(float x) { float c. else return f.0 .Programação do PIC 16F877 } else { for (j = 0. } if (y > 0. res. } float sin(float x) { return(SIN_COS(x. 1). if (c != 0. s. return(1. s = SIN_COS(x. int s.0e+36). c = SIN_COS(x. 1)). rs = rs*r*r + sintable[j+1]. 0)). y = x. } float ASIN_COS(float x. r. // r = cos(r) f = ((shift & 0x2) ? -r : r). } } if (!(shift & 0x1)) r = rs*r.0) return(s/c). n += 2. if (x < 0) { s = 1. if (neg) return -f.y)/2. // r = sin(r) else r = rs. 0). s = 0. y = -y. j++) { rs = sintable[j]. } float cos(float x) { return(SIN_COS(x. int n) { float y. j < 4.0).

y = x. flag = 1. res = y*res/r. return(res). flag.818457}.368190.2. if (n & 1) // take arccos res = PIDIVBYTWO . r = qas[0]*y*y + qas[1]. } /************************************************************/ float const pat[4] = {0. y = -y. 22. 19. if (s) res = -res. 11. } float asin(float x) { float r. 19. float acos(float x) { float r. if (x < 0) { s = 1.0) { y = 1.0/y. 8 . 28. 0). int s.6710795. return(r). } if (y > 1. 1).17630401. r = qat[0]*y*y + qat[1].res. s = 0. float atan(float x) { float y. res.5 res = PIDIVBYTWO . res = res*y*y + pat[3].0000000.Programação do PIC 16F877 res = pas[0]*y*y + pas[1]. res = res*y*y + pas[2]. } res = pat[0]*y*y + pat[1]. r = r*y*y + qas[2]. r = ASIN_COS(x. } r = ASIN_COS(x. float const qat[4] = {1.982246. if (n & 2) // |x| > 0.0*res. res = res*y*y + pat[2]. return(r). r. flag = 0. 5.376096.818457}.

(float)(long)y. return(res). r = r*y*y + qat[3]. s = 0. if (s) res = -res.0*(y/32768. res.res. res = y*res/r. return (res). res += (float)(long)y. } if (y <= 32768. 0). long l. int s. if (y != 0) { if (s == 1 && n == 0) res -= 1. if (x < 0) { s = 1. int n) { float y. 9 .0).0. y = -y. { else if (y < 10000000.Programação do PIC 16F877 r = r*y*y + qat[2]. } /***********************************************************/ float CEIL_FLOOR(float x. if (s) res = -res. if (flag) // for |x| > 1 res = PIDIVBYTWO . } if (x == 0) res = 0. r = CEIL_FLOOR(x.0 . } else res = y. y = x.0*(float)l.0.0) res = (float)(long)y. y = y . res = 32768.(float)l).0) l = (long)(y/32768. y = 32768. } float floor(float x) { float r. if (s == 0 && n == 1) res += 1.

-9. if (s) res = 1.780517. 0. = r*y + pe[4]. 26.0 + y*r).693147172}. if (x < 0) { s = 1. 1). 0. = r*y + pe[3]. = r*y + pe[2]. -9. int s. if (y != 1. y = x. 0. n = -n.0)/(y + 1. float exp(float x) { float y. } res = 0. r.0). = r*y + pe[5].45145214. -19. float log(float x) { float y.940971. signed n.(float)n. } /************************************************************/ float const pl[4] = {0. 0. r = CEIL_FLOOR(x. y = y/LN2 .Programação do PIC 16F877 } float ceil(float x) { float r. } /***********************************************************/ #define LN2 0.0554965651.0558803. 10 .1. *(&res) = n + 0x7F. float const ql[4] = {1.860189}. r r r r r = pe[0]*y + pe[1]. -8.0000000.00965065093.0.0) { *(&y) = 0x7E.000207455774. res = res*(1.1354259. r.00127100575.0/res. y = x. res.9300943}. y = (y . return(res). 16.6931471806 float const pe[6] = {0. n = (signed int)(x/LN2). signed int n. res. s = 0. y = -y.240227138. 0.

30258509 float log10(float x) { float r. } #define LN10 2. res += r*LN2. res = res*y*y + pl[3]. return(r).0. // D:\UPF\Placa_Aq_Paralela\Placa Mônica\Programação PIC\16f877. return(res).0x7E. } r = log(x). res = res*y*y + pl[2]. r = r*y*y + ql[3]. else r = (float)n. r = r*y*y + ql[2]. } else res = 0.Programação do PIC 16F877 res = pl[0]*y*y + pl[1]. n = *(&x) .h //////// Standard Header file for the PIC16F877 device //////// #device PIC16F877 #nolist /////////////////////////////// I/O definitions for INPUT() and OUTPUT_xxx() #define PIN_A0 40 #define PIN_A1 41 #define PIN_A2 42 #define PIN_A3 43 #define PIN_A4 44 #define PIN_A5 45 #define #define #define #define #define #define #define #define PIN_B0 PIN_B1 PIN_B2 PIN_B3 PIN_B4 PIN_B5 PIN_B6 PIN_B7 48 49 50 51 52 53 54 55 #define #define #define #define #define #define #define #define PIN_C0 PIN_C1 PIN_C2 PIN_C3 PIN_C4 PIN_C5 PIN_C6 PIN_C7 56 57 58 59 60 61 62 63 11 . r = r/LN10. r = ql[0]*y*y + ql[1]. if (n<0) r = -(float)-n. res = y*res/r.

putchar(13).} putc putchar /////////////////////////////// Constants used for RESTART_CAUSE() #define WDT_FROM_SLEEP 0 #define WDT_TIMEOUT 8 #define MCLR_FROM_SLEEP 16 #define NORMAL_POWER_UP 24 /////////////////////////////// Constants used for SETUP_COUNTERS() #define RTCC_INTERNAL 0 #define RTCC_EXT_L_TO_H 32 #define RTCC_EXT_H_TO_L 48 #define RTCC_DIV_2 0 #define RTCC_DIV_4 1 #define RTCC_DIV_8 2 #define RTCC_DIV_16 3 #define RTCC_DIV_32 4 #define RTCC_DIV_64 5 #define RTCC_DIV_128 6 #define RTCC_DIV_256 7 #define WDT_18MS 8 #define WDT_36MS 9 #define WDT_72MS 10 #define WDT_144MS 11 #define WDT_288MS 12 #define WDT_576MS 13 #define WDT_1152MS 14 #define WDT_2304MS 15 #define L_TO_H 0x40 #define H_TO_L 0 #define #define #define #define #define #define RTCC_ZERO INT_RTCC RB_CHANGE INT_RB EXT_INT INT_EXT 0x0B20 // Used for ENABLE/DISABLE INTERRUPTS 0x0B20 // Used for ENABLE/DISABLE INTERRUPTS 0x0B08 // Used for ENABLE/DISABLE INTERRUPTS 0x0B08 // Used for ENABLE/DISABLE INTERRUPTS 0x0B10 // Used for ENABLE/DISABLE INTERRUPTS 0x0B10 // Used for ENABLE/DISABLE INTERRUPTS #define GLOBAL 0x0BC0 // Used for ENABLE/DISABLE INTERRUPTS ///////////////////////////////////// Constants used for Timer1 and Timer2 #define T1_DISABLED 0 #define T1_INTERNAL 5 #define T1_EXTERNAL 7 #define T1_EXTERNAL_SYNC 3 #define T1_CLK_OUT 8 #define T1_DIV_BY_1 0 #define T1_DIV_BY_2 0x10 12 .Programação do PIC 16F877 #define #define #define #define #define #define #define #define PIN_D0 PIN_D1 PIN_D2 PIN_D3 PIN_D4 PIN_D5 PIN_D6 PIN_D7 64 65 66 67 68 69 70 71 #define PIN_E0 72 #define PIN_E1 73 #define PIN_E2 74 /////////////////////////////// Useful defines #define FALSE 0 #define TRUE 1 #define BYTE int #define BOOLEAN short int #define #define #define #define getc getch getchar getch puts(s) {printf(s). putchar(10).

Programação do PIC 16F877 13 #define T1_DIV_BY_4 0x20 #define T1_DIV_BY_8 0x30 #byte TIMER_1_LOW= 0x0e #byte TIMER_1_HIGH= 0x0f #define T2_DISABLED 0 #define T2_DIV_BY_1 4 #define T2_DIV_BY_4 5 #define T2_DIV_BY_16 6 #byte TIMER_2= 0x11 #define INT_TIMER1 #define INT_TIMER2 0x8C01 0x8C02 // Used for ENABLE/DISABLE INTERRUPTS // Used for ENABLE/DISABLE INTERRUPTS //////////////////////////////////// Constants used for SETUP_CCP1() #define CCP_OFF 0 #define CCP_CAPTURE_FE 4 #define CCP_CAPTURE_RE 5 #define CCP_CAPTURE_DIV_4 6 #define CCP_CAPTURE_DIV_16 7 #define CCP_COMPARE_SET_ON_MATCH 8 #define CCP_COMPARE_CLR_ON_MATCH 9 #define CCP_COMPARE_INT 0xA #define CCP_COMPARE_RESET_TIMER 0xB #define CCP_PWM 0xC #define CCP_PWM_PLUS_1 0x1c #define CCP_PWM_PLUS_2 0x2c #define CCP_PWM_PLUS_3 0x3c long CCP_1. #byte CCP_1 = 0x15 #byte CCP_1_LOW= 0x15 #byte CCP_1_HIGH= 0x16 #define INT_CCP1 0x8C04 // Used for ENABLE/DISABLE INTERRUPTS //////////////////////////////////// Constants used for SETUP_CCP2() long CCP_2. #byte CCP_2 = 0x1B #byte CCP_2_LOW= 0x1B #byte CCP_2_HIGH= 0x1C #define INT_CCP2 0x8D01 // Used for ENABLE/DISABLE INTERRUPTS //////////////////////////////////// Constants used for SETUP_PSP() #define PSP_ENABLED 0x10 #define PSP_DISABLED 0 #byte PSP_DATA= 8 #define INT_PSP 0x8C80 // Used for ENABLE/DISABLE INTERRUPTS //////////////////////////////////// Constants used in SETUP_SSP() #define SPI_MASTER 0x20 #define SPI_SLAVE 0x24 #define SPI_L_TO_H 0 #define SPI_H_TO_L 0x10 #define SPI_CLK_DIV_4 0 #define SPI_CLK_DIV_16 1 #define SPI_CLK_DIV_64 2 #define SPI_CLK_T2 3 #define SPI_SS_DISABLED 1 #define INT_SSP 0x8C08 // Used for ENABLE/DISABLE INTERRUPTS #define SPI_SAMPLE_AT_END 0x8000 #define SPI_XMIT_L_TO_H 0x4000 #define INT_RDA #define INT_TBE 0x8C20 0x8C10 // Used for ENABLE/DISABLE INTERRUPTS // Used for ENABLE/DISABLE INTERRUPTS ///////////////////////////////////// Constants used for SETUP_ADC_PORTS() .

Programação do PIC 16F877 #define #define #define #define #define #define #define NO_ANALOGS 0x86 // None ALL_ANALOG 0x80 // RA0 RA1 RA2 RA3 RA5 RE0 RE1 RE2 Ref=Vdd ANALOG_RA3_REF 0x81 // RA0 RA1 RA2 RA5 RE0 RE1 RE2 Ref=RA3 A_ANALOG 0x82 // RA0 RA1 RA2 RA3 RA5 Ref=Vdd A_ANALOG_RA3_REF 0x83 // RA0 RA1 RA2 RA5 Ref=RA3 RA0_RA1_RA3_ANALOG 0x84 // RA0 RA1 RA3 Ref=Vdd RA0_RA1_ANALOG_RA3_REF 0x85 // RA0 RA1 Ref=RA3 #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 ///////////////////////////////////// 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 #define ADC_DONE #define INT_ADC #define INT_EEPROM #list 0x8C40 // Used for ENABLE/DISABLE INTERRUPTS 0x8C40 // Used for ENABLE/DISABLE INTERRUPTS 0x0B10 // Used for ENABLE/DISABLE INTERRUPTS 14 .RA3 #define A_ANALOG_RA3_RA2_REF 0x8C // RA0 RA1 RA5 Ref=RA2.RA3 #define RA0_ANALOG 0x8E // RA0 #define RA0_ANALOG_RA3_RA2_REF 0x8F // RA0 Ref=RA2.RA3 #define RA0_RA1_ANALOG_RA3_RA2_REF 0x8D // RA0 RA1 Ref=RA2.

Sign up to vote on this title
UsefulNot useful