CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Problems with the UART of the PIC24F

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
frankcr



Joined: 29 Apr 2010
Posts: 11

View user's profile Send private message MSN Messenger

Problems with the UART of the PIC24F
PostPosted: Wed Jul 07, 2010 11:23 pm     Reply with quote

Hi, I have problems with the uart of the PIC24FJ128GA006. I need to connect the uart with a serial/usb converter to save information. But I don't know if my code is the problem or the voltage levels is the problem (the converter use 5V and the PIC use 3.3V ). My code is:
Code:

#include <24FJ128GA006.h> //MICROCONTROLADOR UTILIZADO
#device adc=10 //ADC 10 Bits de presicion
//************************ FUSES **********************

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES NOJTAG                   //JTAG disabled
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOWRT                    //Program memory not write protected
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES ICSP1                    //ICD uses PGC1/PGD1 pins
#FUSES NOWINDIS                 //Watch Dog Timer in Window mode
#FUSES WPRES128                 //Watch Dog Timer PreScalar 1:128
#FUSES WPOSTS16                 //Watch Dog Timer PostScalar 1:32768
#FUSES IESO                     //Internal External Switch Over mode enabled
//#FUSES FRC                      //Internal Fast RC Oscillator
#FUSES XT                       //Crystal osc <= 4mhz for PCM/PCH , 3mhz to 10 mhz for PCD
#FUSES NOCKSFSM                 //Clock Switching is disabled, fail Safe clock monitor is disabled
#FUSES NOOSCIO                  //OSC2 is clock output
#FUSES NOPR                     //Pimary oscillaotr disabled

// ********************** CLOCK ************************

#use delay(clock=8000000)
#use rs232(UART1,baud=9600,parity=N,bits=8) //

#define RTS PIN_B12  //Clear to send
#define CTS PIN_B13 //Request to send

//Variables del RTC, a mandar o leer
int seconds=0;
int minutes=0;
int hours=0;
int day=0;
int date=0;
int month=0;
int year=0;
int control=0;
int time=0;

//Variables de los datos de los sensores
float adchin=0; //TEMPERATURA Y HUMEDAD
float adctin=0;
float humedadin=0;
float temperaturain=0;
float adchout=0;
float adctout=0;
float humedadout=0;
float temperaturaout=0;
float promediotin=0;
float promediotout=0;

//Variables utilizadas en el comunicacion con el UART
static char texto_rs232[50]; //cadena de caracteres para el rs232                 //70
static char PRINT[36]; //SE ESCRIBE EN EL ARCHIVO DE TEXTO 'MEDICION.TXT'
int1 dato_recibido=0,wait_datos=0;
int cont_rs232=0;
int cont_comand_rs232=0;      //variable del explorador

//Metodos de VDRIVE Y UART
void inic_host(void);

void CARACTER(void);
void recepcion_serial(void);
void wait(void);
void serial_sendbyte(unsigned int8 TXbyte);
int1 buscar_dispositivo(void);
void retire_dispositivo(void);
void OPW_FILE(void);
void WRF_FILE(void);
void CLF_FILE(void);

void main(void);

// **************** INTERRUPCION UART1 *****************

#int_RDA
void  RDA_isr(void)
{
   disable_interrupts(INTR_GLOBAL);
   recepcion_serial();
   enable_interrupts(INTR_GLOBAL);
}

//******** ENVIA UN CARACTER AL PUERTO SERIE ************

void serial_sendbyte(unsigned int8 TXbyte){ //METODO PARA ENVIAR UN CARACTER
   
      while(input(CTS));
      putc(TXbyte);
}

//*********** **METODO QUE BUSCA MEMORIA ***************

int1 buscar_dispositivo() //BUSCA LA MEMORIA
{
   int1 dispositivo_encontrado=0;

   serial_sendbyte(0x0D);
         
         OUTPUT_LOW(PIN_E0);
         OUTPUT_LOW(PIN_E1);
         OUTPUT_LOW(PIN_E2);

         delay_ms(950);
   delay_ms(50);
   if((texto_rs232[0]==0x3E)){
      dispositivo_encontrado=1;
   }

   return dispositivo_encontrado; 
}

//******** METODO GUARDA CARACTERES RECIBIDOS **********

void recepcion_serial()
{
   texto_rs232[cont_rs232]=getc();      //Cada vez que llega un caracter
   ++cont_rs232;                //este se introduce en esta variable.
   dato_recibido=0;

         OUTPUT_HIGH(PIN_E0);
         OUTPUT_LOW(PIN_E1);
         OUTPUT_LOW(PIN_E2);

         delay_ms(950);

   if((texto_rs232[cont_rs232-1]==0x0D))   //Pregunta si ya se envio el ultimo caracter
   {
      if(wait_datos){   //Espera que lleguen los 1024 bytes
         cont_rs232=0;
         wait_datos=0;
      }
      if(!wait_datos) {    //Para que el contador no se recetee si se esta esperando leer los
         cont_rs232=0;   //bytes de un archivo
      }
      dato_recibido=1;   //Bandera para el metodo wait()
      ++cont_comand_rs232;
   }
}

//***** METODO ESPERA PARA RECIBIR TODOS LOS DATOS******

void wait() {  //ESPERA A RECIBIR EL PROM DEL MODULO
 
   delay_ms(25);
   while(!dato_recibido);

}

//********************************************************

void medicion(){
             
   OPW_FILE(); //Abre el archivo de texto para escribir en el
   delay_ms(1);
 
   CARACTER(); //caracter correcto para escribir en la llave
               
   delay_ms(1);
   
   WRF_FILE();  //Escribir caracter en el archivo creado
 
   delay_ms(8000); //tiempo que garantiza una correcta medicion de ls sensores de humedad
 
   delay_ms(8000);
     
   CLF_FILE(); //Cerrar archivo de texto
}

//******************************************************

void OPW_FILE() { //ABRE EL ARCHIVO DE TEXTO PARA ESCRIBIR EN EL
   serial_sendbyte(0x09);
   serial_sendbyte(0x20);
   serial_sendbyte("T");
   serial_sendbyte(".");
   serial_sendbyte("T");
   serial_sendbyte("X");
   serial_sendbyte("T");
   serial_sendbyte(0x0D);
}

//*****************************************************
void CARACTER(){ //36 Caracteres
   sprintf(PRINT,"%2x-%2x-%2x%2x:%2x:%2x%g%g%g%g",date,month,year,hours,minutes,seconds,humedadin,humedadout,temperaturain,temperaturaout);
   delay_ms(100);
}

//*****************************************************
void WRF_FILE() { //ESCRIBIR EN EL ARCHIVO DE TEXTO

   int x=0;
   serial_sendbyte(0x08);    //WRF
   serial_sendbyte(0x20);
   serial_sendbyte(0x00);
   serial_sendbyte(0x00);
   serial_sendbyte(0x00); //
   serial_sendbyte(0x26); //38 caracteres    //escribe 38d = 0x26 "bytes"
   serial_sendbyte(0x0D);
   while(x<36) { //36 caracteres de la funcion CARACTER
      serial_sendbyte(PRINT[x]);
      ++x;
   }
   serial_sendbyte(0x0D); //2 caracteres que permiten hacer cambio de linea
   serial_sendbyte(0x0A); //36 + 2 = 38
   serial_sendbyte(0x0D);
  // wait();
}
//*******************************************************

void CLF_FILE() { //CERRAR EL ARCHIVO DE TEXTO
   serial_sendbyte(0x0A);
   serial_sendbyte(0x20);
   serial_sendbyte("T");
   serial_sendbyte(".");
   serial_sendbyte("T");
   serial_sendbyte("X");
   serial_sendbyte("T");
   serial_sendbyte(0x0D);
   delay_ms(200);
}

//******************************************************

void retire_dispositivo() {
 
   while (buscar_dispositivo());
   
}

// ***************** INICIALIZA HOST USB *****************

void inic_host(void)  {

   output_high(RTS); //RTS
   while(input(CTS)){}     //espero que el VDRIVE2 busque el pic CTS
   output_low(RTS);       //le envio una respuesta RTS

}

void main()
{
   
// ******** Configuraciones de ADC, INT, WDT, TMRS ********* 
   setup_adc_ports(sAN3 | sAN4 | sAN5 );//Configuracion ADC: AN3-TEMP, AN4-HUM, AN5-CO2
   setup_adc(ADC_CLOCK_INTERNAL);
   //setup_psp(PSP_DISABLED);
   setup_wdt(WDT_OFF);
   //setup_timer_0(RTCC_INTERNAL);
   setup_timer1(TMR_DISABLED);
   setup_timer2(TMR_DISABLED);
   setup_timer3(TMR_INTERNAL|TMR_DIV_BY_8);
   setup_comparator(NC_NC_NC_NC);   
   setup_vref(FALSE); //setup_vref(VREF_DISABLED);
   setup_oscillator(OSC_INTERNAL);

   set_timer3(2450);

   enable_interrupts(INT_RDA); //Habilita interrupciones de la USART
   enable_interrupts(INT_RDA2);
   //enable_interrupts(INT_EXT0); //Habilita interrupcion INT0
   enable_interrupts(INTR_GLOBAL); //Habilito Interrupciones
   //ext_int_edge(L_TO_H);

   OUTPUT_HIGH(PIN_E0);
   OUTPUT_HIGH(PIN_E1);
   OUTPUT_HIGH(PIN_E2);

    delay_ms(5000);

     inic_host();//Inicializa el Host VDRIVE2

   int estado=0;
   
   while(TRUE) {
   
      switch (estado)
      {

         case 0:
                       
         OUTPUT_LOW(PIN_E0);
         OUTPUT_LOW(PIN_E1);
         OUTPUT_LOW(PIN_E2);

         delay_ms(5000);
         
            estado=1;
         break;

         case 1:
         OUTPUT_HIGH(PIN_E0);
         OUTPUT_LOW(PIN_E1);
         OUTPUT_LOW(PIN_E2);

         delay_ms(5000);

         estado=2;
         break;

         case 2:

         OUTPUT_LOW(PIN_E0);
         OUTPUT_HIGH(PIN_E1);
         OUTPUT_LOW(PIN_E2);

         delay_ms(5000);

            estado=3;
         break;

         case 3:
         OUTPUT_HIGH(PIN_E0);
         OUTPUT_HIGH(PIN_E1);
         OUTPUT_LOW(PIN_E2);

         delay_ms(5000);

            estado=4;//estado=4;
         break;

         case 4:
           
         OUTPUT_LOW(PIN_E0);
         OUTPUT_LOW(PIN_E1);
         OUTPUT_HIGH(PIN_E2);

         while(!buscar_dispositivo());
            delay_ms(3000);
   
            estado=5;
            break;
     
         case 5:

         OUTPUT_HIGH(PIN_E0);
         OUTPUT_LOW(PIN_E1);
         OUTPUT_HIGH(PIN_E2);         

         medicion();

         delay_ms(5000);
   
            estado=6;
            break;
     
         case 6:
         
            retire_dispositivo();

         OUTPUT_LOW(PIN_E0);
         OUTPUT_HIGH(PIN_E1);
         OUTPUT_HIGH(PIN_E2);

            delay_ms(5000);
           estado=7;
            break;

       case 7:
           
         OUTPUT_HIGH(PIN_E0);
         OUTPUT_HIGH(PIN_E1);
         OUTPUT_HIGH(PIN_E2);

            delay_ms(5000);

            estado=7;
            break;
      }

   }
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group