Konuşan Termometre

Başlatan feyzi, 12 Aralık 2011, 18:05:23

feyzi

Arkadaşlar! 18F452 ,SmartMP3 kartı ve DS1820 ile konuşan termometre yapmaya çalışıyorum.Termometre programındaki sıcaklık değerinin tutulduğu değişkeni tesbit edemiyorum.if...then veya select case ile karşılaştırmak istiyorum.Termometre programını aşağıya veriyorum:
program termoMikroB
dim i, j1, j2 as byte
    text      as string[6]
    tmp_sign  as byte

main:

   text   = "Temp:"
   adcon1 = 255                     ' configure RA5 pin as digital I/O
   PORTA  = 255                     ' initialize porte to 255
   PORTB  =   0                     ' initialize portb to 255
   TRISA  = 255                     ' designate porte as input
   TRISB  =   0                     ' designate portb as output
   Lcd_Init(PORTB)
   lcd_cmd(LCD_CURSOR_OFF)
   lcd_out(1, 1, text)
   lcd_chr(2, 12, 223)              ' 'degree' character
   lcd_chr(2, 13, "C")

   while TRUE
     ow_reset(PORTA, 5)             ' onewire reset signal
     ow_write(PORTA, 5, $CC)        ' issue command to DS1820
     ow_write(PORTA, 5, $44)        ' issue command to DS1820
     delay_us(120)
     i = ow_reset(PORTA, 5)
     ow_write(PORTA, 5, $CC)        ' issue command to DS1820
     ow_write(PORTA, 5, $BE)        ' issue command to DS1820

     j1 = ow_read(PORTA, 5)         ' get result
     j2 = ow_read(PORTA, 5)         ' get result (assuming the temperature is positive)

     if j2 = $FF then
       tmp_sign = "-"               ' temperature sign
       j1= j1 or $FF                ' complement of two
       j1= j1 + $01
     else
       tmp_sign = "+"
     end if
     j2 = (j1 and $01) * 5          ' Get decimal value
     j1 = j1 >> 1                   ' Get temp value

     ByteToStr(j1, text)            ' whole number
     lcd_chr(2, 7, tmp_sign)
     lcd_chr(2, 8, text[1])
     lcd_chr(2, 9, text[2])
     lcd_chr(2, 10, 46)             ''.'

     ByteToStr(j2, text)            ' decimal
     lcd_chr(2, 11, text[2])

     Delay_ms(500)
   wend                             'endless loop
end.
"insanların hayırlısı ,onlara faydalı olandır."  (H.Ş.)

mcan

J1 ve j2 degiskenlerinde 2 byte olarak tutuyor veriyi.

feyzi

Alıntı yapılan: mcan - 12 Aralık 2011, 18:54:48
J1 ve j2 degiskenlerinde 2 byte olarak tutuyor veriyi.
Karşılaştırmada,hem j1 hemde j2'yi denedim.Olmadı.Hatta,text[1] ve text[2]'yi dahi denedim.
"insanların hayırlısı ,onlara faydalı olandır."  (H.Ş.)

feyzi

Bilgisayar öğretmeni bir arkadaşın yardımıyla konu çözüldü.Konuşan termometre kodlarını veriyorum:



program ayh

' Declarations section
dim BtnLeftDown            as sbit at RB7_bit
dim BtnLeftUp              as sbit at RB6_bit
dim BtnRightDown           as sbit at RB5_bit
dim BtnRightUp             as sbit at RB4_bit
dim BtnLeftDown_Direction  as sbit at TRISB7_bit
dim BtnLeftUp_Direction    as sbit at TRISB6_bit
dim BtnRightDown_Direction as sbit at TRISB5_bit
dim BtnRightUp_Direction   as sbit at TRISB4_bit


' Smart MP3 board connections
dim Mmc_Chip_Select           as sbit at RC0_bit
dim MP3_CS                    as sbit at RC1_bit
dim MP3_RST                   as sbit at RC2_bit
dim DREQ                      as sbit at RD0_bit
dim BSYNC                     as sbit at RD1_bit
dim DCLK                      as sbit at RD2_bit
dim SDATA                     as sbit at RD3_bit
dim Mmc_Chip_Select_Direction as sbit at TRISC0_bit
dim MP3_CS_Direction          as sbit at TRISC1_bit
dim MP3_RST_Direction         as sbit at TRISC2_bit
dim DREQ_Direction            as sbit at TRISD0_bit
dim BSYNC_Direction           as sbit at TRISD1_bit
dim DCLK_Direction            as sbit at TRISD2_bit
dim SDATA_Direction           as sbit at TRISD3_bit

' VS1011E constants
const WRITE_CODE  as byte = 0x02
const READ_CODE   as byte = 0x03
const MODE_ADDR   as byte = 0x00
const CLOCKF_ADDR as byte = 0x03
const VOL_ADDR    as byte = 0x0B

' global variables
const BUFFER_SIZE = 512
dim filename as string[13]
   j1,j2, i, file_size as longword
   dim j as byte
    data_buffer_32 as byte[32]
    BufferLarge as byte[BUFFER_SIZE]
    volume_left, volume_right as byte
    const TEMP_RESOLUTION as byte = 9
dim sc as integer
dim text as char[6]
    temp as word

sub procedure Display_Temperature( dim temp2write as word )
const RES_SHIFT = TEMP_RESOLUTION - 8

dim temp_whole as byte
    temp_fraction as word

    text = "000"
    ' Check if temperature is negative
    if (temp2write and 0x8000) then
        text[0] = "-"
        temp2write = not temp2write + 1
    end if

    ' Extract temp_whole
    temp_whole = word(temp2write >> RES_SHIFT)

    ' Convert temp_whole to characters
    if ( temp_whole div 100 ) then
      text[0] = temp_whole div 100  + 48
    else
      text[0] = "0"
    end if

    text[1] = (temp_whole div 10)mod 10 + 48               ' Extract tens digit
    text[2] =  temp_whole mod 10        + 48               ' Extract ones digit

    ' Extract temp_fraction and convert it to unsigned int
    temp_fraction  =  word(temp2write << (4-RES_SHIFT))
    temp_fraction  = temp_fraction and 0x000F
    temp_fraction  = temp_fraction * 625

    ' Convert temp_fraction to characters
    text[4] = word(temp_fraction div 1000)    + 48         ' Extract thousands digit
    text[5] = word((temp_fraction div 100)mod 10 + 48)     ' Extract hundreds digit
    text[6] = word((temp_fraction div 10)mod 10  + 48)     ' Extract tens digit

end sub

' Write text and goto new line CR and LF
sub procedure UART1_Write_Line(dim byref text as string[50])
  UART1_Write_Text(text)
  UART1_Write(13)
  UART1_Write(10)
end sub

' Writes one byte to MP3 SDI
sub procedure SW_SPI_Write(dim data_ as byte)

  BSYNC = 1                              ' Set BSYNC before sending the first bit

  DCLK = 0
  SDATA = data_.0                        ' bitorder is LSB first
  DCLK = 1

  DCLK = 0
  SDATA = data_.1
  DCLK = 1

  BSYNC = 0                              ' Clear BSYNC after sending the second bit

  DCLK = 0
  SDATA = data_.2
  DCLK = 1

  DCLK = 0
  SDATA = data_.3
  DCLK = 1

  DCLK = 0
  SDATA = data_.4
  DCLK = 1

  DCLK = 0
  SDATA = data_.5
  DCLK = 1

  DCLK = 0
  SDATA = data_.6
  DCLK = 1

  DCLK = 0
  SDATA = data_.7
  DCLK = 1

  DCLK = 0
end sub

' Writes one word to MP3 SCI
sub procedure MP3_SCI_Write(dim address as byte, dim data_in as word)
  MP3_CS = 0                              ' select MP3 SCI
  SPI1_Write(WRITE_CODE)
  SPI1_Write(address)
  SPI1_Write(Hi(data_in))                  ' high byte
  SPI1_Write(Lo(data_in))                  ' low byte
  MP3_CS = 1                              ' deselect MP3 SCI
  while (DREQ = 0) nop wend               ' wait until DREQ becomes 1, see MP3 codec datasheet, Serial Protocol for SCI
end sub

' Reads words_count words from MP3 SCI
sub procedure MP3_SCI_Read(dim start_address, words_count as byte, dim data_buffer as ^byte)
dim i as byte
  MP3_CS = 0                              ' select MP3 SCI
  SPI1_Write(READ_CODE)                    ' Read command
  SPI1_Write(start_address)

  for i = 1 to (2*words_count)            ' read words_count words byte per byte
    data_buffer^ = SPI1_Read(0)            ' read and store a byte
    Inc(data_buffer)                      ' point to next byte
  next i
  MP3_CS = 1                              ' deselect MP3 SCI
  while (DREQ = 0) nop wend               ' wait until DREQ becomes 1, see MP3 codec datasheet, Serial Protocol for SCI
end sub

' Write one byte to MP3 SDI
sub procedure MP3_SDI_Write(dim data_ as byte)
  while (DREQ = 0) nop wend               ' wait until DREQ becomes 1, see MP3 codec datasheet, Serial Protocol for SCI

  SW_SPI_Write(data_)
end sub

' Write 32 bytes to MP3 SDI
sub procedure MP3_SDI_Write_32(dim data_ as ^byte)
  dim i as byte

  while (DREQ = 0) nop wend               ' wait until DREQ becomes 1, see MP3 codec datasheet, Serial Protocol for SCI

  for i = 1 to 32
    SW_SPI_Write(data_^)                  ' Write byte pointed by data
    Inc(data_)
  next i
end sub

' Set clock
sub procedure Set_Clock(dim clock_khz_ as word, dim doubler as byte)
  clock_khz_ = clock_khz_ / 2             ' calculate value
  if (doubler > 0) then
    clock_khz_ = clock_khz_ or 0x40000
  end if

  MP3_SCI_Write(CLOCKF_ADDR, clock_khz_)  ' Write value to CLOCKF register
end sub

' Set volume
sub procedure Set_Volume(dim left_, right_ as byte)
dim volume as word
  volume = (left_ << 8) + right_          ' calculate value
  MP3_SCI_Write(VOL_ADDR, volume)         ' Write value to VOL register
end sub

' If volume Buttons are pressed update volume
sub procedure Check_Volume()
  dim old_volume_left, old_volume_right as byte

  old_volume_left  = volume_left          ' save volume values
  old_volume_right = volume_right

  if (BtnLeftDown) then                   ' RB7 pressed = LEFT VOLUME DOWN command
    if (volume_left < 255) then
      Inc(volume_left)
    end if
  end if

  if (BtnLeftUp) then                     ' RB6 pressed = LEFT VOLUME UP command
    if (volume_left > 0) then
      Dec(volume_left)
    end if
  end if

  if (BtnRightDown) then                  ' RB5 pressed = RIGHT VOLUME DOWN command
    if (volume_right < 255) then
      Inc(volume_right)
    end if
  end if

  if (BtnRightUp) then                    ' RB4 pressed = RIGHT VOLUME UP command
    if (volume_right > 0) then
      Dec(volume_right)
    end if
  end if

  ' if volume values are changed, set volume to new values
  if  (volume_left <> old_volume_left) or (volume_right <> old_volume_right) then
    Set_Volume(volume_left,volume_right)
  end if
end sub

' Software Reset
sub procedure Soft_Reset(dim fill_zeros as byte)
  dim i as byte

  MP3_SCI_Write(MODE_ADDR,0x0204)         ' Write to MODE register
                                          '   set SM_RESET bit and SM_BITORD bit(bitorder is LSB first)
  Delay_us(2)                             ' Required, see MP3 codec datasheet -> Software Reset
  while (DREQ = 0) nop wend               ' wait until DREQ becomes 1, see MP3 codec datasheet, Serial Protocol for SCI

  if (fill_zeros > 0) then                ' feed 2048 zeros to the MP3 SDI bus:
    for i = 0 to 31                       ' clear 32byte buffer
      data_buffer_32 = 0
    next i

    for i = 0 to 2048/32 - 1              ' use 32byte buffer to clear MP3 buffer
      MP3_SDI_Write_32(@data_buffer_32)
    next i
  else
    MP3_SDI_Write(0)                      ' write zero once
  end if

end sub


' Initialize VS1011E
sub procedure MP3_init()
  Soft_Reset(0)                           ' SW Reset, do not feed zeros

  Set_Clock(25000,0)                      ' Set clock to 25MHz, do not use clock doubler

  volume_left  = 25                       ' Set volume to initial value
  volume_right = 25
  Set_Volume(volume_left,volume_right)
end sub

sub procedure Init()
  ADCON1 = ADCON1 or 0x0F                 ' Configure AN pins as digital
  'CMCON  = CMCON  or 7                    ' Disable comparators

       j=0
  trisa=255

  BtnLeftDown_Direction  = 1              ' set pin direction as input
  BtnLeftUp_Direction    = 1
  BtnRightDown_Direction = 1
  BtnRightUp_Direction   = 1
  BtnLeftDown  = 0                        ' set pin value to zero
  BtnLeftUp    = 0
  BtnRightDown = 0
  BtnRightUp   = 0

  DCLK_Direction  = 0                     ' Set SW SPI pin directions to output
  SDATA_Direction = 0
  DCLK  = 0                               ' Clear SW SPI SCK
  SDATA = 0                               ' Clear SW SPI SDO

  MP3_CS_Direction  = 0                   ' Configure MP3_CS as output
  MP3_CS            = 1                   ' Deselect MP3_CS
  MP3_RST_Direction = 0                   ' Configure MP3_RST as output
  MP3_RST           = 1                   ' Set MP3_RST pin

  DREQ_Direction  = 1                     ' Configure DREQ as input
  BSYNC_Direction = 0                     ' Configure BSYNC as output
  BSYNC           = 0                     ' Clear BSYNC
end sub

main:
  while TRUE
sc=strtoInt(text)
select case sc
case 18
filename="18.MP3"
case 19
filename="19.MP3"
case 20
filename="20.MP3"
case 21
filename="21.MP3"
case 22
filename="22.MP3"
  case 23
filename="23.MP3"
  case 24
filename="24.MP3"
  case 25
filename="25.MP3"
    case 26
filename="26.MP3"
  case 27
filename="27.MP3"
   case 28
filename="28.MP3"
  case 29
filename="29.MP3"
   case 30
filename="30.MP3"
end select
Init()

   ' Initialize USART for signaling
     UART1_Init(9600)
     Delay_100ms()

     UART1_Write_Line("Initializing SPI")
     SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH)

     UART1_Write_Line("Initializing VS1011E decoder interface")
     MP3_init()

     UART1_Write_Line("Initializing MMC_FAT")

      text = "000.0000"

       ow_reset(PORTA,5)              ' onewire hattýna reset signali gönder
       ow_write(PORTA,5,$CC)          ' DS1820  komutu  (pdf dosyaya bakýn )
       ow_write(PORTA,5,$44)          ' DS1820  komutu  (pdf dosyaya bakýn )
       delay_us(120)
       i = ow_reset(PORTA,5)
       ow_write(PORTA,5,$CC)          ' DS1820  komutu  (pdf dosyaya bakýn )
       ow_write(PORTA,5,$BE)          ' DS1820  komutu  (pdf dosyaya bakýn )

       j1 = ow_read(PORTA,5)          ' sonucu al
       j2 = ow_read(PORTA,5)          ' sonucu al (varsayýlan sýcaklýk +)

       if j2 = $FF then
          'isaret = "-"                ' sýcaklýk iþareti
          j1 = j1 or $FF              ' 2 nin komplementi
          j1 = j1 + $01
       else
       'isaret = "+"
       end if

       j2 = (j1 and $01) * 5          ' onluk deðerin elde edilmesi
       j1 = j1 >> 1                   ' sýcaklýk deðerinin elde edilmesi
       ByteToStr(j1, text)            ' tüm sayý


     if (Mmc_Fat_Init() = 0) then
      if (Mmc_Fat_Assign(filename, 0) <> 0) then
       UART1_Write_Line("File Assigned")
       Mmc_Fat_Reset(file_size)            ' Call Reset before file reading,
                                          '   procedure returns size of the file
      ' send file blocks to MP3 SDI
       while (file_size > BUFFER_SIZE)

        for i = 0 to BUFFER_SIZE - 1
          Mmc_Fat_Read(BufferLarge)
        next i

        for i = 0 to BUFFER_SIZE/32 - 1
          MP3_SDI_Write_32(@BufferLarge + i*32)
        next i

        file_size = file_size - BUFFER_SIZE

        Check_Volume()
       wend

      ' send the rest of the file to MP3 SDI
       for i = 0 to file_size - 1
        Mmc_Fat_Read(BufferLarge)
       next i

       for i = 0 to file_size - 1
        MP3_SDI_Write(BufferLarge)
       next i
      else
       UART1_Write_Line("File not assigned")
      end if
     else
      UART1_Write_Line("MMC FAT not initialized")
     end if
   wend

end.
"insanların hayırlısı ,onlara faydalı olandır."  (H.Ş.)