Autor Tema: Problemas con pic nuevo  (Leído 1741 veces)

0 Usuarios y 2 Visitantes están viendo este tema.

Desconectado pablo

  • Colaborador
  • PIC18
  • *****
  • Mensajes: 253
Problemas con pic nuevo
« en: 11 de Septiembre de 2007, 22:05:13 »
Hola gente, tenia un progama hecho con un pic 18f452 y ahora quiero usar un 4620 el tema es que estoy tratando de simularlo con el proteus y con el pic anterior funciona perfecto pero cuando lo pruebo con el 18f4620 funciona pero me dice como que no tengo termometros conectados y por ejemplo algo mas basico no prende un led que fuerzo solo como para ver si funcioana.
Lo raro es que el display LCD funciona.
Cuelgo el codigo por si a alguien se le ocurre algo, yo supongo que es alguna configuracion propia de este micro.
Ya lo tenia funcionando pero se me rompio el rigido, trato de hacer lo mismo pero ahora no funciona. Si alguien me puede dar una mano se lo voy a agradecer.
Ah tengo el archivo para simularlo con el proteus si alguien lo necesita avisenme como ponerlo en la WEB, gracias.

Un abrazo
Pablo

Codigo programa
Código: [Seleccionar]
#include <18F4620.h>

#use delay(internal=8000000)
//#fuses XT, NOPROTECT, PUT, NOWDT, BROWNOUT, NOLVP, NOCPD, NOWRT
#FUSES INTRC_IO, NOPROTECT, PUT, NOWDT, NOBROWNOUT,NOLVP, NOCPD, NOWRT


//-------------------------data pin definition--------------------
#define DQ PIN_C0


#include "lcd420.c"
//#include <ds1307.h>
#include <stdlib.h>
#include "1wire.h"

float final_sonda[10] = {0,0,0,0,0,0,0,0,0,0},sonda[10];
byte  numero_total_de_ROMs;

//-----------------------
float leer_temperatura(byte n)
{
   byte temperatura_l=0, temperatura_h=0;
   int read_ok=0;
   float t=0;

      if(ow_reset()==0)
      {
         //write_byte(0xcc);                 //skip ROM
         Send_MatchRom();
         write_byte(0x44);                //convert T
      }
      delay_ms(1);     // retardo para desahogar al sensor y evitar posibles colisiones que el sensor

      if(ow_reset()==0)
      {
         //write_byte(0xcc);               //skip ROM
          numROMs=n;                    // Metemos en la variable 'numROMs' el número del sensor del cual queremos medir la temperatura
         Send_MatchRom();
         write_byte(0xbe);               //read scratchpad
 

         temperatura_l=read_byte();
         temperatura_h=read_byte();


         read_ok=1;
      }

      if(read_ok)
      {       
         t=make16(temperatura_h,temperatura_l);        //calcula la temperatura
         //t=temperatura_l;        //calcula la temperatura
         //t-=6;
         t=(t*1000*628)/10000000;
         //t*=0.1;
         return t;
      } else {   
         numero_total_de_ROMs = 0;         
      }
}


#separate
void Ver_Principal(void)
{
   int n;
   
   for(n=1;n<numero_total_de_ROMs+1;n++) {
      final_sonda[n-1] = leer_temperatura(n);
      delay_ms(100);
   }
   lcd_gotoxy(6, 1);
   printf(lcd_putc,"PRINCIPAL");
   lcd_gotoxy(1, 2);
   if(numero_total_de_ROMs == 0) {
      printf(lcd_putc,"Sin Termometro      ");
   } else {
      if(numero_total_de_ROMs > 1) {
         printf(lcd_putc,"T1: %2.1f - T2: %2.1f ", final_sonda[0], final_sonda[1]);
      } else {
         printf(lcd_putc,"Temp. Acuario: %2.1f ", final_sonda[0]);
      }
   }
   lcd_gotoxy(1, 3);
   printf(lcd_putc,"Sondas: %d ", numero_total_de_ROMs);

   if(final_sonda[0] >= 27.0)
      output_bit(PIN_D4,0);
   else
      output_bit(PIN_D4,1);
}



#separate
void proceso(void) {
   int n;
   while(true) {
     
      Ver_Principal();
   }

}

#separate
void main (void) {
//   int n;
   //set_timer0(0);
   //setup_oscillator(OSC_8MHZ,OSC_INTRC);
   //setup_counters(RTCC_INTERNAL, RTCC_DIV_2);
   //enable_interrupts(INT_RTCC);
   //enable_interrupts(GLOBAL);

   port_b_pullups(TRUE);

   set_tris_d(0xFF);   
   output_bit(DQ,0);

   FindDevices();
   numero_total_de_ROMs = numRoms;
   delay_ms(300);     
   
   lcd_init();
   proceso();
}

Codigo Libreria generocidad de Azicuetano. Para leer DS18B20
Código: [Seleccionar]
//----------------------------------------------------------------
//
//                                    1wire.h
//
//
//
//
//
//
//----------------------------------------------------------------

////////////////////////////////////////////////////////////////////////////
// GLOBAL VARIABLES
//

//unsigned char ROM_NO[8];
unsigned char ROMs[8]; // ROM Bit
unsigned char lastDiscrep = 0; // last discrepancy
unsigned char doneFlag = 0; // Done flag
unsigned char FoundROM[5][8]; // table of found ROM codes
unsigned char numROMs;
unsigned char dowcrc;

unsigned char dscrc_table[] = {
0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65,
157,195, 33,127,252,162, 64, 30, 95, 1,227,189, 62, 96,130,220,
35,125,159,193, 66, 28,254,160,225,191, 93, 3,128,222, 60, 98,
190,224, 2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255,
70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89, 7,
219,133,103, 57,186,228, 6, 88, 25, 71,165,251,120, 38,196,154,
101, 59,217,135, 4, 90,184,230,167,249, 27, 69,198,152,122, 36,
248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91, 5,231,185,
140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205,
17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80,
175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238,
50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115,
202,148,118, 40,171,245, 23, 73, 8, 86,180,234,105, 55,213,139,
87, 9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22,
233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168,
116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53};
//unsigned char page_data[32];

//////////////////////////////////////////////////////////////////////////////
// OW_RESET - performs a reset on the one-wire bus and
// returns the presence detect. Reset is 480us, so delay
// value is (480-24)/16 = 28.5 - we use 29. Presence checked
// another 70us later, so delay is (70-24)/16 = 2.875 - we use 3.
//

ow_reset(void)
{
   int presence;
   output_low(DQ); //pull DQ line low
   delay_us(480);// leave it low for 480us
   output_bit(DQ,1); // allow line to return high
   delay_us(72); // wait for presence
   presence = input(DQ); // get presence signal
   delay_us(424);
   return(presence); // presence signal returned
} // 0=presence, 1 = no part

//////////////////////////////////////////////////////////////////////////////
// READ_BIT - reads a bit from the one-wire bus. The delay
// required for a read is 15us, so the DELAY routine won't work.
// We put our own delay function in this routine in the form of a
// for() loop.
//

byte read_bit(void)
{
   //byte i;
   
   output_bit(DQ,0); // pull DQ low to start timeslot
   output_bit(DQ,1); // then return high
   delay_us(15); // delay 15us from start of timeslot
   return(input(DQ)); // return value of DQ line
}

//////////////////////////////////////////////////////////////////////////////
// WRITE_BIT - writes a bit to the one-wire bus, passed in bitval.
//

void write_bit(byte bitval)
{
   output_bit(DQ,0); // pull DQ low to start timeslot
   if(bitval==1) output_bit(DQ,1); // return DQ high if write 1
   delay_us(104); // hold value for remainder of timeslot
   output_bit(DQ,1);
}// Delay provides 16us per loop, plus 24us. Therefore delay(5) = 104us

//////////////////////////////////////////////////////////////////////////////
// READ_BYTE - reads a byte from the one-wire bus.
//

byte read_byte(void)
{
   byte i;
   byte value=0;
   for (i=0;i<8;i++)
      {
      if(read_bit()) value|=0x01<<i; // reads byte in, one byte at a time and then
      // shifts it left
      delay_us(120); // wait for rest of timeslot
      }
   return(value);
}

//////////////////////////////////////////////////////////////////////////////
// WRITE_BYTE - writes a byte to the one-wire bus.
//

void write_byte(byte val)
{
   byte i;
   byte temp;
   for (i=0; i<8; i++) // writes byte, one bit at a time
      {
      temp = val>>i; // shifts val right 'i' spaces
      temp &= 0x01; // copy that bit to temp
      write_bit(temp); // write bit in temp into
      }
   delay_us(104);
}

//////////////////////////////////////////////////////////////////////////////
// ONE WIRE CRC
//

unsigned char ow_crc( unsigned char x)
{

   dowcrc = dscrc_table[dowcrc^x];
   return dowcrc;
}

/////////////////////////////////////////////////////////////////////////////////
// NEXT
// The Next function searches for the next device on the 1-wire bus. If
// there are no more devices on the 1-wire then false is returned.
//

unsigned char Next(void)
{
   unsigned char m = 1; // ROM Bit index
   unsigned char n = 0; // ROM Byte index
   unsigned char k = 1; // bit mask
   unsigned char x = 0;
   unsigned char discrepMarker = 0; // discrepancy marker
   unsigned char g; // Output bit
   unsigned char nxt; // return value
   int flag;
   nxt = FALSE; // set the next flag to false
   dowcrc = 0; // reset the dowcrc
   flag = ow_reset(); // reset the 1-wire
   if(flag||doneFlag) // no parts -> return false
      {
      lastDiscrep = 0; // reset the search
      return FALSE;
      }
   write_byte(0xF0); // send SearchROM command
   do
   // for all eight bytes
      {
      x = 0;
      if(read_bit()==1) x = 2;
      delay_us(120);
      if(read_bit()==1) x |= 1; // and its complement
      if(x ==3) // there are no devices on the 1-wire
      break;
      else
         {
         if(x>0) // all devices coupled have 0 or 1
         g = x>>1; // bit write value for search
         else
            {
            // if this discrepancy is before the last
            // discrepancy on a previous Next then pick
            // the same as last time
            if(m<lastDiscrep)
            g = ((ROMs[n]&k)>0);
            else // if equal to last pick 1
            g = (m==lastDiscrep); // if not then pick 0
            // if 0 was picked then record
            // position with mask k
            if (g==0) discrepMarker = m;
            }
         if(g==1) // isolate bit in ROM[n] with mask k
         ROMs[n] |= k;
         else
         ROMs[n] &= ~k;
         write_bit(g); // ROM search write
         m++; // increment bit counter m
         k = k<<1; // and shift the bit mask k
         if(k==0) // if the mask is 0 then go to new ROM
            { // byte n and reset mask
            ow_crc(ROMs[n]); // accumulate the CRC
            n++; k++;
            }
         }
      }while(n<8); //loop until through all ROM bytes 0-7
   if(m<65||dowcrc) // if search was unsuccessful then
   lastDiscrep=0; // reset the last discrepancy to 0
   else
      {
      // search was successful, so set lastDiscrep,
      // lastOne, nxt
      lastDiscrep = discrepMarker;
      doneFlag = (lastDiscrep==0);
      nxt = TRUE; // indicates search is not complete yet, more
      // parts remain
      }
   return nxt;
}

////////////////////////////////////////////////////////////////////////////
// FIRST
// The First function resets the current state of a ROM search and calls
// Next to find the first device on the 1-wire bus.
//

unsigned char First(void)
{
   lastDiscrep = 0; // reset the rom search last discrepancy global
   doneFlag = FALSE;
   return Next(); // call Next and return its return value
}

////////////////////////////////////////////////////////////////////////////////
// FIND DEVICES

void FindDevices(void)
{
   unsigned char m;
   
   if(!ow_reset()) //Begins when a presence is detected
   {
      if(First()) //Begins when at least one part is found
      {
         numROMs=0;
         do
         {
            numROMs++;
            for(m=0;m<8;m++)
            {
               FoundROM[numROMs][m]=ROMs[m];  //Identifies ROM
                                             //number on found device
            }

//          printf("\nROM CODE =%02X%02X%02X%02X\n",FoundROM[4][7],FoundROM[4][6],FoundROM[4][5],FoundROM[4][4],FoundROM[4][3],FoundROM[4][2],FoundROM[4][1],FoundROM[4][0]);
         }while (Next()&&(numROMs<10)); //Continues until no additional devices are found
      }
   }
   else
   {
      numROMs=55;
   }
}

////////////////////////////////////////////////////////////////////////////////
// Perform Match ROM
//
unsigned char Send_MatchRom(void)
{
   unsigned char i;
   
   if(ow_reset()) return false;
   
   write_byte(0x55); // match ROM
   
   for(i=0;i<8;i++)
   {
      write_byte(FoundROM[numROMs][i]); //send ROM code
   }
   return true;
}


Codigo LCD 20x4
Código: [Seleccionar]
// flex_lcd.c

#define LCD_LINEA1 0x80 // Direccion de memoria para la 1 linea
#define LCD_LINEA2 0xc0 // Direccion de memoria para la 2 linea
#define LCD_LINEA3 0x94 // Direccion de memoria para la 3 linea
#define LCD_LINEA4 0xd4 // Direccion de memoria para la 4 linea

// Change these pins to fit your own board.

#define LCD_RS PIN_B7
#define LCD_RW PIN_B6
#define LCD_E  PIN_B5

#define LCD_DB4 PIN_B4
#define LCD_DB5 PIN_B3
#define LCD_DB6 PIN_B2
#define LCD_DB7 PIN_B1


// If you only want a 6-pin interface to your LCD, then
// connect the R/W pin on the LCD to ground, and comment
// out the following line.

#define USE_LCD_RW 1

//========================================

#define lcd_type 2 // 0=5x7, 1=5x10, 2=2 lines
#define lcd_line_two 0x40 // LCD RAM address for the 2nd line


int8 const LCD_INIT_STRING[4] =
{
  0x20 | (lcd_type << 2), // Func set: 4-bit, 2 lines, 5x8 dots
  0xc, // Display on
  1, // Clear display
  6 // Increment cursor
};


//-------------------------------------
void lcd_send_nibble(int8 nibble)
{
  // Note: !! converts an integer expression
  // to a boolean (1 or 0).
  output_bit(LCD_DB4, !!(nibble & 1));
  output_bit(LCD_DB5, !!(nibble & 2));
  output_bit(LCD_DB6, !!(nibble & 4));
  output_bit(LCD_DB7, !!(nibble & 8));
 
  delay_cycles(1);
  output_high(LCD_E);
  delay_us(2);
  output_low(LCD_E);
}

//-----------------------------------
// This sub-routine is only called by lcd_read_byte().
// It's not a stand-alone routine. For example, the
// R/W signal is set high by lcd_read_byte() before
// this routine is called.

#ifdef USE_LCD_RW
int8 lcd_read_nibble(void)
{
  int8 retval;
  // Create bit variables so that we can easily set
  // individual bits in the retval variable.
  #bit retval_0 = retval.0
  #bit retval_1 = retval.1
  #bit retval_2 = retval.2
  #bit retval_3 = retval.3

  retval = 0;

  output_high(LCD_E);
  delay_cycles(1);

  retval_0 = input(LCD_DB4);
  retval_1 = input(LCD_DB5);
  retval_2 = input(LCD_DB6);
  retval_3 = input(LCD_DB7);

  output_low(LCD_E);

  return(retval);
}
#endif

//---------------------------------------
// Read a byte from the LCD and return it.

#ifdef USE_LCD_RW
int8 lcd_read_byte(void)
{
  int8 low;
  int8 high;

  output_high(LCD_RW);
  delay_cycles(1);

  high = lcd_read_nibble();

  low = lcd_read_nibble();

  return( (high<<4) | low);
}
#endif

//----------------------------------------
// Send a byte to the LCD.
void lcd_send_byte(int8 address, int8 n)
{
  output_low(LCD_RS);

#ifdef USE_LCD_RW
while(bit_test(lcd_read_byte(),7)) ;
#else
delay_us(60);
#endif

  if(address)
  output_high(LCD_RS);
  else
  output_low(LCD_RS);

  delay_cycles(1);

#ifdef USE_LCD_RW
output_low(LCD_RW);
delay_cycles(1);
#endif

  output_low(LCD_E);

  lcd_send_nibble(n >> 4);
  lcd_send_nibble(n & 0xf);
}

//----------------------------
void lcd_init(void)
{
  int8 i;

  output_low(LCD_RS);

#ifdef USE_LCD_RW
output_low(LCD_RW);
#endif

  output_low(LCD_E);

  delay_ms(15);

  for(i=0 ;i < 3; i++)
  {
    lcd_send_nibble(0x03);
    delay_ms(5);
  }

  lcd_send_nibble(0x02);

  for(i=0; i < sizeof(LCD_INIT_STRING); i++)
  {
    lcd_send_byte(0, LCD_INIT_STRING[i]);

    // If the R/W signal is not used, then
    // the busy bit can't be polled. One of
    // the init commands takes longer than
    // the hard-coded delay of 60 us, so in
    // that case, lets just do a 5 ms delay
    // after all four of them.
#ifndef USE_LCD_RW
delay_ms(5);
#endif
}

}

//----------------------------
void lcd_gotoxy( byte x, byte y) {
byte dir;
if(y == 1)
   dir=LCD_LINEA1;
else if(y == 2)
   dir=LCD_LINEA2;
else if(y == 3)
   dir=LCD_LINEA3;
else if(y == 4)
   dir=LCD_LINEA4;

//if(y!=1)
//dir=LCD_LINEA2;
//else
//dir=0;

dir+=x-1;
lcd_send_byte(0, 0x80 | dir);
}
//-----------------------------
void lcd_putc(char c)
{
  switch(c)
  {
    case '\f':
      lcd_send_byte(0,1);
      delay_ms(2);
      break;

    case '\n':
      lcd_gotoxy(1,2);
      break;

    case '\b':
      lcd_send_byte(0,0x10);
      break;

    default:
      lcd_send_byte(1,c);
      break;
  }
}

//------------------------------
#ifdef USE_LCD_RW
char lcd_getc(int8 x, int8 y)
{
  char value;

  lcd_gotoxy(x,y);

  // Wait until busy flag is low.
  while(bit_test(lcd_read_byte(),7));

  output_high(LCD_RS);
  value = lcd_read_byte();
  output_low(lcd_RS);

  return(value);
}
#endif

void lcd_setcursor_vb(short visible, short blink) {
  lcd_send_byte(0, 0xC|(visible<<1)|blink);
}

Desconectado pablo

  • Colaborador
  • PIC18
  • *****
  • Mensajes: 253
Re: Problemas con pic nuevo
« Respuesta #1 en: 12 de Septiembre de 2007, 10:35:18 »
Puede que este pic necesite alguna configuracion para las entradas y salidas?

Desconectado pablo

  • Colaborador
  • PIC18
  • *****
  • Mensajes: 253
Re: Problemas con pic nuevo
« Respuesta #2 en: 12 de Septiembre de 2007, 14:26:48 »
Muchachos, aca les dejo los archivos por si alguien quiere o puede probarlo.
Seguramente es mas facil que mostrar el codigo.

Termostato

Muchas gracias, saludos.
Pablo


 

anything