Problema con frecuencia pic18f4550

Estoy tratando de generar PWM para un servomoto con un PIC18f4550. Con CCS

Los datos para variar el angulo le llegan desde la PC.

Estoy trabajando con un reloj de 4MHz pero hay un problema.
-Cuando especifico una tasa 9600baudios transmite a 2400baudios
-Cuando especifico una tase de 38400baudios transmite a 9600baudios
y eso por medio del comando "#use delay"
-Ademas los retardos duran 4 veces mas de lo que requieren por ejemplo un delay de 400us dura 1.6ms

Sospecho que es algun #fuse que me sobra o que me falta, ya que en el un 16f877a que lo probamos un tiempo funcionaba a la frecuencia correcta. pero no se cual de los #fuse puede ser ya que yo quito el que dice CPUDIV4 y no dice nada.

Adjunto el .c y el .h


Este es el main.c

Código:
#include "main.h"
#include <stdlib.h>
#bit RCIF= 0xf9e.5 //Bandera de Interrupcion RC
#byte PORTB= 0xf81//PuertoB
#byte LATB= 0xf8a//LatB

char dato_rs232[10];
int cont=0;
int dato;
float posicion=0;
int16 duty=375;

void myPWM(void) {
      output_high(PIN_A2);
      if(duty>550) duty=550;
      if(duty<241) duty=241;
      delay_us(5000-duty);
      output_low(PIN_A2);
      delay_us(duty);      
} 

#int_RDA
void RDA_isr(void) 
{   dato_rs232[cont]=getc();
   ++cont;
   if(dato_rs232[cont-1]==0x2F) {
      dato_rs232[cont-1]=0x00;   //Borro el caracter final "/"
      posicion=atof(dato_rs232);  
      duty=375+posicion;  //ecuacion para relacionar posicion con angulo
      cont=0;
      while(cont<10){
         dato_rs232[cont]=0x00;  //limpiando registro
         ++cont;
      }
     cont=0;
   } 
}



void main()
{

   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF|ADC_TAD_MUL_0);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);
//Setup_Oscillator parameter not selected from Intr Oscillator Config tab

   // TODO: USER CODE!
   set_tris_a(0x00);  //
   set_tris_b(0x00);  //
   set_tris_c(0xC0);  //C0;C1 push botton 

   output_low(PIN_A2);  //Condicion inicial
   output_high(PIN_A3);

   while(1) {
          myPWM();
   }

}

main.h
Código:
#include <18F4450.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES XT                       //Crystal osc <= 4mhz
#FUSES NOPROTECT                //Code not protected from reading
#FUSES BROWNOUT                 //Reset when brownout detected
#FUSES BORV20                   //Brownout reset at 2.0V
#FUSES NOPUT                    //No Power Up Timer
#FUSES VREGEN                   //USB voltage regulator enabled
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOLVP                      //Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES NOWRT                    //Program memory not write protected
#FUSES LPT1OSC                  //Timer1 configured for low-power operation
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES PBADEN                   //PORTB pins are configured as analog input channels on RESET
#FUSES BBSIZ2K                  //2K words Boot Block size
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOEBTRB                  //Boot block not protected from table reads
#FUSES NOCPB                    //No Boot Block code protection
#FUSES MCLR                     //Master Clear pin enabled
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES PLL12                    //Divide By 12(48MHz oscillator input)
#FUSES USBDIV                   //USB clock source comes from PLL divide by 2
//#FUSES CPUDIV4                  //System Clock by 4 //Este es el fuse que quite pero no esta afectando el codigo en nada.
#FUSES ICPRT                    //ICPRT enabled

#use delay(clock=4000000)
#use rs232(baud=38400,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)// Con 38400 transmite a 9600

Espero que puedan ayudarme, gracias
 
¿Qué soft usas para programar el PIC?
Allí es donde deberías chequear la configuración de los fuses.
 
Atrás
Arriba